3
Á3W^  ã               @   sš   d dl Z d dljZddlmZ dddddgZd	d
dddœZdd„ ZG dd„ dejƒZ	G dd„ dejƒZ
dd„ Zddd„Zddd„Zddd„Zddd„ZdS )é    Né   )Úload_state_dict_from_urlÚShuffleNetV2Úshufflenet_v2_x0_5Úshufflenet_v2_x1_0Úshufflenet_v2_x1_5Úshufflenet_v2_x2_0zDhttps://download.pytorch.org/models/shufflenetv2_x0.5-f707e7126e.pthzBhttps://download.pytorch.org/models/shufflenetv2_x1-5666bf0f80.pth)zshufflenetv2_x0.5zshufflenetv2_x1.0zshufflenetv2_x1.5zshufflenetv2_x2.0c             C   sR   | j jƒ \}}}}|| }| j|||||ƒ} tj| ddƒjƒ } | j|d||ƒ} | S )Nr   é   éÿÿÿÿ)ÚdataÚsizeÚviewÚtorchZ	transposeÚ
contiguous)ÚxÚgroupsZ	batchsizeZnum_channelsÚheightÚwidthZchannels_per_group© r   úF/tmp/pip-build-7wfi2gtz/torchvision/torchvision/models/shufflenetv2.pyÚchannel_shuffle   s    
r   c                   s2   e Zd Z‡ fdd„Zed
dd„ƒZdd	„ Z‡  ZS )ÚInvertedResidualc                s0  t t| ƒjƒ  d|  ko dkn  s.tdƒ‚|| _|d }| jdksV||d> ksVt‚| jdkr¬tj| j||d| jdd�tj	|ƒtj
||ddddd�tj	|ƒtjd	d
�ƒ| _tjtj
| jdkrÂ|n||ddddd�tj	|ƒtjd	d
�| j||d| jdd�tj	|ƒtj
||ddddd�tj	|ƒtjd	d
�ƒ| _d S )Nr   é   zillegal stride valuer	   )Úkernel_sizeÚstrideÚpaddingr   F)r   r   r   ÚbiasT)Úinplace)Úsuperr   Ú__init__Ú
ValueErrorr   ÚAssertionErrorÚnnÚ
SequentialÚdepthwise_convÚBatchNorm2dÚConv2dÚReLUÚbranch1Úbranch2)ÚselfÚinpZoupr   Zbranch_features)Ú	__class__r   r   r   $   s.    

zInvertedResidual.__init__r   r   Fc          	   C   s   t j| |||||| d�S )N)r   r   )r"   r&   )ÚiÚor   r   r   r   r   r   r   r$   C   s    zInvertedResidual.depthwise_convc             C   sb   | j dkr6|jddd�\}}tj|| j|ƒfdd�}ntj| j|ƒ| j|ƒfdd�}t|dƒ}|S )Nr   r	   )Zdim)r   Úchunkr   Úcatr)   r(   r   )r*   r   Úx1Zx2Úoutr   r   r   ÚforwardG   s    

zInvertedResidual.forward)r   r   F)Ú__name__Ú
__module__Ú__qualname__r   Ústaticmethodr$   r3   Ú__classcell__r   r   )r,   r   r   #   s   r   c                   s&   e Zd Zd‡ fdd„	Zdd„ Z‡  ZS )r   éè  c          	      s\  t t| ƒjƒ  t|ƒdkr"tdƒ‚t|ƒdkr6tdƒ‚|| _d}| jd }tjtj||ddddd	�tj	|ƒtj
d
d�ƒ| _|}tjdddd�| _dd„ dD ƒ}xpt||| jdd … ƒD ]V\}}}t||dƒg}	x&t|d ƒD ]}
|	jt||dƒƒ qÜW t| |tj|	Ž ƒ |}q¶W | jd }tjtj||ddddd	�tj	|ƒtj
d
d�ƒ| _tj||ƒ| _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   T)r   )r   r   r   c             S   s   g | ]}d j |ƒ‘qS )zstage{})Úformat)Ú.0r-   r   r   r   ú
<listcomp>h   s    z)ShuffleNetV2.__init__.<locals>.<listcomp>é   )r	   r   r>   r
   )r   r   r   Úlenr    Z_stage_out_channelsr"   r#   r&   r%   r'   Úconv1Z	MaxPool2dÚmaxpoolÚzipr   ÚrangeÚappendÚsetattrÚconv5ZLinearÚfc)r*   Zstages_repeatsZstages_out_channelsZnum_classesZinput_channelsZoutput_channelsZstage_namesÚnameZrepeatsÚseqr-   )r,   r   r   r   T   s8    

zShuffleNetV2.__init__c             C   sX   | j |ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}|jddgƒ}| j|ƒ}|S )Nr	   r   )r@   rA   Zstage2Zstage3Zstage4rF   ZmeanrG   )r*   r   r   r   r   r3   z   s    






zShuffleNetV2.forward)r9   )r4   r5   r6   r   r3   r8   r   r   )r,   r   r   S   s   &c             O   sH   t ||Ž}|rDt|  }|d kr.tdj| ƒƒ‚nt||d�}|j|ƒ |S )Nz(pretrained {} is not supported as of now)Úprogress)r   Ú
model_urlsÚNotImplementedErrorr;   r   Zload_state_dict)ÚarchÚ
pretrainedrJ   ÚargsÚkwargsÚmodelZ	model_urlZ
state_dictr   r   r   Ú_shufflenetv2†   s    

rR   FTc          
   K   s$   t d| |dddgdddddgf|ŽS )	at  
    Constructs a ShuffleNetV2 with 0.5x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
    <https://arxiv.org/abs/1807.11164>`_.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    zshufflenetv2_x0.5r>   é   é   é0   é`   éÀ   i   )rR   )rN   rJ   rP   r   r   r   r   ”   s    
c          
   K   s$   t d| |dddgdddddgf|ŽS )	at  
    Constructs a ShuffleNetV2 with 1.0x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
    <https://arxiv.org/abs/1807.11164>`_.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    zshufflenetv2_x1.0r>   rS   rT   ét   éè   iÐ  i   )rR   )rN   rJ   rP   r   r   r   r   ¢   s    
c          
   K   s$   t d| |dddgdddddgf|ŽS )	at  
    Constructs a ShuffleNetV2 with 1.5x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
    <https://arxiv.org/abs/1807.11164>`_.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    zshufflenetv2_x1.5r>   rS   rT   é°   i`  iÀ  i   )rR   )rN   rJ   rP   r   r   r   r   °   s    
c          
   K   s$   t d| |dddgdddddgf|ŽS )	at  
    Constructs a ShuffleNetV2 with 2.0x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
    <https://arxiv.org/abs/1807.11164>`_.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    zshufflenetv2_x2.0r>   rS   rT   éô   iè  iÐ  i   )rR   )rN   rJ   rP   r   r   r   r   ¾   s    
)FT)FT)FT)FT)r   Ztorch.nnr"   Úutilsr   Ú__all__rK   r   ÚModuler   r   rR   r   r   r   r   r   r   r   r   Ú<module>   s    
03


