3
Á3W^ô4  ã            
   @   sþ   d dl Z d dljZddlmZ dddddd	d
dddg
Zddddddddddœ	Zd.dd„Zd/dd„ZG dd„ dej	ƒZ
G dd„ dej	ƒZG d d„ dej	ƒZd!d"„ Zd0d%d„Zd1d&d„Zd2d'd„Zd3d(d„Zd4d)d	„Zd5d*d
„Zd6d+d„Zd7d,d„Zd8d-d„ZdS )9é    Né   )Úload_state_dict_from_urlÚResNetÚresnet18Úresnet34Úresnet50Ú	resnet101Ú	resnet152Úresnext50_32x4dÚresnext101_32x8dÚwide_resnet50_2Úwide_resnet101_2z9https://download.pytorch.org/models/resnet18-5c106cde.pthz9https://download.pytorch.org/models/resnet34-333f7ec4.pthz9https://download.pytorch.org/models/resnet50-19c8e357.pthz:https://download.pytorch.org/models/resnet101-5d3b4d8f.pthz:https://download.pytorch.org/models/resnet152-b121ed2d.pthz@https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pthzAhttps://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pthz@https://download.pytorch.org/models/wide_resnet50_2-95faca4d.pthzAhttps://download.pytorch.org/models/wide_resnet101_2-32ee1156.pth)	r   r   r   r   r	   r
   r   r   r   c          
   C   s   t j| |d|||d|d�S )z3x3 convolution with paddingé   F)Úkernel_sizeÚstrideÚpaddingÚgroupsÚbiasÚdilation)ÚnnÚConv2d)Ú	in_planesÚ
out_planesr   r   r   © r   ú@/tmp/pip-build-7wfi2gtz/torchvision/torchvision/models/resnet.pyÚconv3x3   s    r   c             C   s   t j| |d|dd�S )z1x1 convolutionr   F)r   r   r   )r   r   )r   r   r   r   r   r   Úconv1x1   s    r   c                   s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )	Ú
BasicBlockr   Né@   c	       	         s�   t t| ƒjƒ  |d krtj}|dks,|dkr4tdƒ‚|dkrDtdƒ‚t|||ƒ| _||ƒ| _	tj
dd�| _t||ƒ| _||ƒ| _|| _|| _d S )Nr   r   z3BasicBlock only supports groups=1 and base_width=64z(Dilation > 1 not supported in BasicBlockT)Úinplace)Úsuperr   Ú__init__r   ÚBatchNorm2dÚ
ValueErrorÚNotImplementedErrorr   Úconv1Úbn1ÚReLUÚreluÚconv2Úbn2Ú
downsampler   )	ÚselfÚinplanesÚplanesr   r+   r   Ú
base_widthr   Ú
norm_layer)Ú	__class__r   r   r!   &   s    

zBasicBlock.__init__c             C   s`   |}| j |ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| jd k	rJ| j|ƒ}||7 }| j|ƒ}|S )N)r%   r&   r(   r)   r*   r+   )r,   ÚxÚidentityÚoutr   r   r   Úforward8   s    







zBasicBlock.forward)r   Nr   r   r   N)Ú__name__Ú
__module__Ú__qualname__Ú	expansionr!   r5   Ú__classcell__r   r   )r1   r   r   #   s    r   c                   s*   e Zd ZdZd	‡ fdd„	Zdd„ Z‡  ZS )
Ú
Bottlenecké   r   Nr   c	       
         s¢   t t| ƒjƒ  |d krtj}t||d  ƒ| }	t||	ƒ| _||	ƒ| _t	|	|	|||ƒ| _
||	ƒ| _t|	|| j ƒ| _||| j ƒ| _tjdd�| _|| _|| _d S )Ng      P@T)r   )r    r;   r!   r   r"   Úintr   r%   r&   r   r)   r*   r9   Úconv3Úbn3r'   r(   r+   r   )
r,   r-   r.   r   r+   r   r/   r   r0   Úwidth)r1   r   r   r!   N   s    

zBottleneck.__init__c             C   s~   |}| j |ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| jd k	rh| j|ƒ}||7 }| j|ƒ}|S )N)r%   r&   r(   r)   r*   r>   r?   r+   )r,   r2   r3   r4   r   r   r   r5   _   s    










zBottleneck.forward)r   Nr   r   r   N)r6   r7   r8   r9   r!   r5   r:   r   r   )r1   r   r;   K   s    r;   c                   s0   e Zd Zd‡ fdd„	Zddd	„Zd
d„ Z‡  ZS )r   éè  Fr   r   Nc	       
         s  t t| ƒjƒ  |d krtj}|| _d| _d| _|d kr@dddg}t|ƒdkrZt	dj
|ƒƒ‚|| _|| _tjd| jddddd�| _|| jƒ| _tjd	d
�| _tjdddd�| _| j|d|d ƒ| _| j|d|d d|d d�| _| j|d|d d|d d�| _| j|d|d d|d d�| _tjdƒ| _tjd|j |ƒ| _xj| jƒ D ]^}	t|	tjƒ�rltj j!|	j"ddd� n4t|	tjtj#fƒ�rDtj j$|	j"dƒ tj j$|	j%dƒ �qDW |�rþxP| jƒ D ]D}	t|	t&ƒ�rÚtj j$|	j'j"dƒ nt|	t(ƒ�r¶tj j$|	j)j"dƒ �q¶W d S )Nr   r   Fr   zHreplace_stride_with_dilation should be None or a 3-element tuple, got {}é   é   )r   r   r   r   T)r   )r   r   r   r   é€   )r   Údilateé   i   Zfan_outr(   )ÚmodeZnonlinearity)r   r   )*r    r   r!   r   r"   Ú_norm_layerr-   r   Úlenr#   Úformatr   r/   r   r%   r&   r'   r(   Z	MaxPool2dÚmaxpoolÚ_make_layerÚlayer1Úlayer2Úlayer3Úlayer4ZAdaptiveAvgPool2dÚavgpoolZLinearr9   ÚfcÚmodulesÚ
isinstanceÚinitZkaiming_normal_ZweightZ	GroupNormZ	constant_r   r;   r?   r   r*   )
r,   ÚblockÚlayersZnum_classesZzero_init_residualr   Úwidth_per_groupZreplace_stride_with_dilationr0   Úm)r1   r   r   r!   x   sN    

zResNet.__init__c          
   C   sÖ   | j }d }| j}|r&|  j|9  _d}|dks>| j||j krdtjt| j||j |ƒ|||j ƒƒ}g }	|	j|| j|||| j| j	||ƒƒ ||j | _x4t
d|ƒD ]&}
|	j|| j|| j| j	| j|d�ƒ q¢W tj|	Ž S )Nr   )r   r/   r   r0   )rH   r   r-   r9   r   Z
Sequentialr   Úappendr   r/   Úrange)r,   rV   r.   Úblocksr   rE   r0   r+   Zprevious_dilationrW   Ú_r   r   r   rL   «   s&    zResNet._make_layerc             C   st   | j |ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}t	j
|dƒ}| j|ƒ}|S )Nr   )r%   r&   r(   rK   rM   rN   rO   rP   rQ   ÚtorchÚflattenrR   )r,   r2   r   r   r   r5   Ã   s    









zResNet.forward)rA   Fr   r   NN)r   F)r6   r7   r8   r!   rL   r5   r:   r   r   )r1   r   r   v   s
     1
c             K   s0   t ||f|Ž}|r,tt|  |d�}|j|ƒ |S )N)Úprogress)r   r   Ú
model_urlsZload_state_dict)ÚarchrV   rW   Ú
pretrainedr`   ÚkwargsÚmodelZ
state_dictr   r   r   Ú_resnetÕ   s    
rf   FTc             K   s   t dtddddg| |f|ŽS )a!  ResNet-18 model from
    `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_

    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
    r   rC   )rf   r   )rc   r`   rd   r   r   r   r   Þ   s    c             K   s   t dtddddg| |f|ŽS )a!  ResNet-34 model from
    `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_

    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
    r   r   r<   é   )rf   r   )rc   r`   rd   r   r   r   r   ê   s    c             K   s   t dtddddg| |f|ŽS )a!  ResNet-50 model from
    `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_

    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
    r   r   r<   rg   )rf   r;   )rc   r`   rd   r   r   r   r   ö   s    c             K   s   t dtddddg| |f|ŽS )a"  ResNet-101 model from
    `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_

    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
    r   r   r<   é   )rf   r;   )rc   r`   rd   r   r   r   r     s    c             K   s   t dtddddg| |f|ŽS )a"  ResNet-152 model from
    `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_

    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
    r	   r   é   é$   )rf   r;   )rc   r`   rd   r   r   r   r	     s    c             K   s,   d|d< d|d< t dtddddg| |f|ŽS )a7  ResNeXt-50 32x4d model from
    `"Aggregated Residual Transformation for Deep Neural Networks" <https://arxiv.org/pdf/1611.05431.pdf>`_

    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
    é    r   r<   rX   r
   r   rg   )rf   r;   )rc   r`   rd   r   r   r   r
     s    c             K   s,   d|d< d|d< t dtddddg| |f|ŽS )	a8  ResNeXt-101 32x8d model from
    `"Aggregated Residual Transformation for Deep Neural Networks" <https://arxiv.org/pdf/1611.05431.pdf>`_

    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
    rk   r   ri   rX   r   r   r<   rh   )rf   r;   )rc   r`   rd   r   r   r   r   (  s    c             K   s$   d|d< t dtddddg| |f|ŽS )	a9  Wide ResNet-50-2 model from
    `"Wide Residual Networks" <https://arxiv.org/pdf/1605.07146.pdf>`_

    The model is the same as ResNet except for the bottleneck number of channels
    which is twice larger in every block. The number of channels in outer 1x1
    convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048
    channels, and in Wide ResNet-50-2 has 2048-1024-2048.

    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
    r   rC   rX   r   r   r<   rg   rD   )rf   r;   )rc   r`   rd   r   r   r   r   6  s    c             K   s$   d|d< t dtddddg| |f|ŽS )	a:  Wide ResNet-101-2 model from
    `"Wide Residual Networks" <https://arxiv.org/pdf/1605.07146.pdf>`_

    The model is the same as ResNet except for the bottleneck number of channels
    which is twice larger in every block. The number of channels in outer 1x1
    convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048
    channels, and in Wide ResNet-50-2 has 2048-1024-2048.

    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
    r   rC   rX   r   r   r<   rh   rD   )rf   r;   )rc   r`   rd   r   r   r   r   H  s    )r   r   r   )r   )FT)FT)FT)FT)FT)FT)FT)FT)FT)r^   Ztorch.nnr   Úutilsr   Ú__all__ra   r   r   ÚModuler   r;   r   rf   r   r   r   r   r	   r
   r   r   r   r   r   r   r   Ú<module>   s:   



(+_	







