U
    ­Ê¹jT<  ã                	   @   s  d dl mZ d dlmZmZmZmZ d dlZd dlm	Z	 d dlm
Z
 ddlmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZmZ dddddddddg	Ze
ee
dœdd„ZG dd„ de	jƒZG dd„ de	jƒZee eeeedœdd„Zdedd œZ G d!d„ deƒZ!G d"d„ deƒZ"G d#d„ deƒZ#G d$d„ deƒZ$eƒ ed%e!j%fd&�dd'd(œee! eeed)œd*d„ƒƒZ&eƒ ed%e"j%fd&�dd'd(œee" eeed)œd+d„ƒƒZ'eƒ ed%e#j%fd&�dd'd(œee# eeed)œd,d„ƒƒZ(eƒ ed%e$j%fd&�dd'd(œee$ eeed)œd-d„ƒƒZ)dS ).é    )Úpartial)ÚAnyÚCallableÚListÚOptionalN)ÚTensoré   )ÚImageClassification)Ú_log_api_usage_onceé   )Úregister_modelÚWeightsÚWeightsEnum)Ú_IMAGENET_CATEGORIES)Ú_ovewrite_named_paramÚhandle_legacy_interfaceÚShuffleNetV2ÚShuffleNet_V2_X0_5_WeightsÚShuffleNet_V2_X1_0_WeightsÚShuffleNet_V2_X1_5_WeightsÚShuffleNet_V2_X2_0_WeightsÚshufflenet_v2_x0_5Úshufflenet_v2_x1_0Úshufflenet_v2_x1_5Úshufflenet_v2_x2_0)ÚxÚgroupsÚreturnc                 C   sP   |   ¡ \}}}}|| }|  |||||¡} t | dd¡ ¡ } |  ||||¡} | S )Nr   r   )ÚsizeÚviewÚtorchZ	transposeÚ
contiguous)r   r   Z	batchsizeZnum_channelsÚheightÚwidthZchannels_per_group© r$   ú^/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/models/shufflenetv2.pyÚchannel_shuffle   s    r&   c                
       sZ   e Zd Zeeeddœ‡ fdd„Zedeeeeeeejdœd	d
„ƒZ	e
e
dœdd„Z‡  ZS )ÚInvertedResidualN)ÚinpÚoupÚstrider   c                    sN  t ƒ  ¡  d|  krdks(n tdƒ‚|| _|d }| jdkrh||d> krhtd|› d|› d|› d�ƒ‚| jdkrÀt | j||d| jdd	�t |¡tj||ddd
dd�t |¡tj	dd�¡| _
n
t ¡ | _
t tj| jdkrà|n||ddd
dd�t |¡tj	dd�| j||d| jdd	�t |¡tj||ddd
dd�t |¡tj	dd�¡| _d S )Nr   é   zillegal stride valuer   zInvalid combination of stride z, inp z	 and oup zB values. If stride == 1 then inp should be equal to oup // 2 << 1.©Úkernel_sizer*   Úpaddingr   F)r-   r*   r.   ÚbiasT©Zinplace)ÚsuperÚ__init__Ú
ValueErrorr*   ÚnnÚ
SequentialÚdepthwise_convÚBatchNorm2dÚConv2dÚReLUÚbranch1Úbranch2)Úselfr(   r)   r*   Zbranch_features©Ú	__class__r$   r%   r2   ,   sF    
ÿ

û
ú

ñzInvertedResidual.__init__r   r   F)ÚiÚor-   r*   r.   r/   r   c              	   C   s   t j| |||||| d�S )N)r/   r   )r4   r8   )r?   r@   r-   r*   r.   r/   r$   r$   r%   r6   V   s    zInvertedResidual.depthwise_conv©r   r   c                 C   sb   | j dkr6|jddd�\}}tj||  |¡fdd�}ntj|  |¡|  |¡fdd�}t|dƒ}|S )Nr   r   )Údim)r*   Úchunkr    Úcatr;   r:   r&   )r<   r   Úx1Zx2Úoutr$   r$   r%   Úforward\   s    

zInvertedResidual.forward)r   r   F)Ú__name__Ú
__module__Ú__qualname__Úintr2   ÚstaticmethodÚboolr4   r8   r6   r   rG   Ú__classcell__r$   r$   r=   r%   r'   +   s   *     ÿ     þr'   c                       sb   e Zd Zdefee ee eedejf ddœ‡ fdd„Z	e
e
dœdd	„Ze
e
dœd
d„Z‡  ZS )r   iè  .N)Ústages_repeatsÚstages_out_channelsÚnum_classesÚinverted_residualr   c              
      sd  t ƒ  ¡  t| ƒ t|ƒdkr&tdƒ‚t|ƒdkr:tdƒ‚|| _d}| jd }t tj||ddddd	�t 	|¡tj
d
d�¡| _|}tjdddd�| _|  |  |  dd„ dD ƒ}t||| jdd … ƒD ]R\}}	}|||dƒg}
t|	d ƒD ]}|
 |||dƒ¡ qèt| |tj|
Ž ƒ |}qÄ| jd }t tj||ddddd	�t 	|¡tj
d
d�¡| _t ||¡| _d S )Nr+   z2expected stages_repeats as list of 3 positive intsé   z7expected stages_out_channels as list of 5 positive intsr   r   r   F)r/   Tr0   r,   c                 S   s   g | ]}d |› �‘qS )Zstager$   )Ú.0r?   r$   r$   r%   Ú
<listcomp>ˆ   s     z)ShuffleNetV2.__init__.<locals>.<listcomp>)r   r+   é   éÿÿÿÿ)r1   r2   r
   Úlenr3   Z_stage_out_channelsr4   r5   r8   r7   r9   Úconv1Z	MaxPool2dÚmaxpoolÚzipÚrangeÚappendÚsetattrÚconv5ZLinearÚfc)r<   rO   rP   rQ   rR   Zinput_channelsZoutput_channelsZstage_namesÚnameZrepeatsÚseqr?   r=   r$   r%   r2   i   sB    


ý 

ýzShuffleNetV2.__init__rA   c                 C   sX   |   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}| ddg¡}|  |¡}|S )Nr   r+   )rY   rZ   Zstage2Zstage3Zstage4r_   Zmeanr`   ©r<   r   r$   r$   r%   Ú_forward_impl™   s    






zShuffleNetV2._forward_implc                 C   s
   |   |¡S )N)rd   rc   r$   r$   r%   rG   ¥   s    zShuffleNetV2.forward)rH   rI   rJ   r'   r   rK   r   r4   ÚModuler2   r   rd   rG   rN   r$   r$   r=   r%   r   h   s   ûú0)ÚweightsÚprogressÚargsÚkwargsr   c                 O   sH   | d k	rt |dt| jd ƒƒ t||Ž}| d k	rD| | j|dd�¡ |S )NrQ   Ú
categoriesT)rg   Z
check_hash)r   rX   Úmetar   Zload_state_dictZget_state_dict)rf   rg   rh   ri   Úmodelr$   r$   r%   Ú_shufflenetv2©   s    
rm   )r   r   z2https://github.com/ericsun99/Shufflenet-v2-Pytorch)Zmin_sizerj   Úrecipec                
   @   s@   e Zd Zedeedd�edddddœid	d
ddœ–d�ZeZdS )r   zDhttps://download.pytorch.org/models/shufflenetv2_x0.5-f707e7126e.pthéà   ©Ú	crop_sizeiÛ úImageNet-1Kg-²�ï§FN@g9´Èv¾oT@©zacc@1zacc@5g{®Gáz¤?gTã¥›Ä @úVThese weights were trained from scratch to reproduce closely the results of the paper.©Ú
num_paramsÚ_metricsÚ_opsÚ
_file_sizeÚ_docs©ÚurlZ
transformsrk   N©	rH   rI   rJ   r   r   r	   Ú_COMMON_METAÚIMAGENET1K_V1ÚDEFAULTr$   r$   r$   r%   r   Á   s    
þÿõüc                
   @   s@   e Zd Zedeedd�edddddœid	d
ddœ–d�ZeZdS )r   zBhttps://download.pytorch.org/models/shufflenetv2_x1-5666bf0f80.pthro   rp   iÌÄ" rr   gºI+WQ@gNbX9V@rs   g�Âõ(\�Â?g¢E¶óý”!@rt   ru   r{   Nr}   r$   r$   r$   r%   r   ×   s    
þÿõüc                   @   sD   e Zd Zedeeddd�eddddd	d
œiddddœ–d�ZeZdS )r   zBhttps://download.pytorch.org/models/shufflenetv2_x1_5-3c479a10.pthro   éè   ©rq   Zresize_sizeú+https://github.com/pytorch/vision/pull/5906iv5 rr   g9´Èv¾?R@g/Ý$�ÅV@rs   g‹lçû©ñÒ?gw¾Ÿ/+@úé
                These weights were trained from scratch by using TorchVision's `new training recipe
                <https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/>`_.
            ©rn   rv   rw   rx   ry   rz   r{   Nr}   r$   r$   r$   r%   r   í   s"   þÿôýc                   @   sD   e Zd Zedeeddd�eddddd	d
œiddddœ–d�ZeZdS )r   zBhttps://download.pytorch.org/models/shufflenetv2_x2_0-8be3c8ee.pthro   r�   r‚   rƒ   iÌÒp rr   g…ëQ¸S@gªñÒMb@W@rs   g-²�ï§â?g+‡Ùn<@r„   r…   r{   Nr}   r$   r$   r$   r%   r     s"   þÿôýZ
pretrained)rf   T)rf   rg   )rf   rg   ri   r   c              	   K   s,   t  | ¡} t| |dddgdddddgf|ŽS )a  
    Constructs a ShuffleNetV2 architecture with 0.5x output channels, as described in
    `ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design
    <https://arxiv.org/abs/1807.11164>`__.

    Args:
        weights (:class:`~torchvision.models.ShuffleNet_V2_X0_5_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ShuffleNet_V2_X0_5_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.shufflenetv2.ShuffleNetV2``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/shufflenetv2.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ShuffleNet_V2_X0_5_Weights
        :members:
    rV   é   é   é0   é`   éÀ   é   )r   Úverifyrm   ©rf   rg   ri   r$   r$   r%   r     s    
c              	   K   s,   t  | ¡} t| |dddgdddddgf|ŽS )a  
    Constructs a ShuffleNetV2 architecture with 1.0x output channels, as described in
    `ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design
    <https://arxiv.org/abs/1807.11164>`__.

    Args:
        weights (:class:`~torchvision.models.ShuffleNet_V2_X1_0_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ShuffleNet_V2_X1_0_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.shufflenetv2.ShuffleNetV2``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/shufflenetv2.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ShuffleNet_V2_X1_0_Weights
        :members:
    rV   r†   r‡   ét   r�   iÐ  r‹   )r   rŒ   rm   r�   r$   r$   r%   r   >  s    
c              	   K   s,   t  | ¡} t| |dddgdddddgf|ŽS )a  
    Constructs a ShuffleNetV2 architecture with 1.5x output channels, as described in
    `ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design
    <https://arxiv.org/abs/1807.11164>`__.

    Args:
        weights (:class:`~torchvision.models.ShuffleNet_V2_X1_5_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ShuffleNet_V2_X1_5_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.shufflenetv2.ShuffleNetV2``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/shufflenetv2.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ShuffleNet_V2_X1_5_Weights
        :members:
    rV   r†   r‡   é°   i`  iÀ  r‹   )r   rŒ   rm   r�   r$   r$   r%   r   ]  s    
c              	   K   s,   t  | ¡} t| |dddgdddddgf|ŽS )a  
    Constructs a ShuffleNetV2 architecture with 2.0x output channels, as described in
    `ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design
    <https://arxiv.org/abs/1807.11164>`__.

    Args:
        weights (:class:`~torchvision.models.ShuffleNet_V2_X2_0_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ShuffleNet_V2_X2_0_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.shufflenetv2.ShuffleNetV2``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/shufflenetv2.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ShuffleNet_V2_X2_0_Weights
        :members:
    rV   r†   r‡   éô   iè  iÐ  i   )r   rŒ   rm   r�   r$   r$   r%   r   |  s    
)*Ú	functoolsr   Útypingr   r   r   r   r    Ztorch.nnr4   r   Ztransforms._presetsr	   Úutilsr
   Z_apir   r   r   Ú_metar   Z_utilsr   r   Ú__all__rK   r&   re   r'   r   rM   rm   r~   r   r   r   r   r   r   r   r   r   r$   r$   r$   r%   Ú<module>   s˜   ÷=Bûý ÿ  þ ÿ  þ ÿ  þ ÿ  þ