3
Á3W^æ*  ã               @   sð   d dl Z d dljZddlmZ dddgZd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G dd„ dejƒZG dd„ dejƒZG dd„ dej	ƒZG dd„ dej	ƒZG dd„ dejƒ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Úr3d_18Úmc3_18Úr2plus1d_18z7https://download.pytorch.org/models/r3d_18-b3b3357e.pthz7https://download.pytorch.org/models/mc3_18-a90a0ba3.pthz<https://download.pytorch.org/models/r2plus1d_18-91a641e6.pth)r   r   r   c                   s*   e Zd Zd‡ fdd„	Zedd„ ƒZ‡  ZS )ÚConv3DSimpleNé   c                s    t t| ƒj||d||dd� d S )Né   F)Úin_channelsÚout_channelsÚkernel_sizeÚstrideÚpaddingÚbias)r	   r	   r	   )Úsuperr   Ú__init__)ÚselfÚ	in_planesÚ
out_planesÚ	midplanesr   r   )Ú	__class__© úF/tmp/pip-build-7wfi2gtz/torchvision/torchvision/models/video/resnet.pyr      s    
zConv3DSimple.__init__c             C   s
   | | | fS )Nr   )r   r   r   r   Úget_downsample_stride    s    z"Conv3DSimple.get_downsample_stride)Nr   r   )Ú__name__Ú
__module__Ú__qualname__r   Ústaticmethodr   Ú__classcell__r   r   )r   r   r      s     
r   c                   s*   e Zd Zd‡ fdd„	Zedd„ ƒZ‡  ZS )ÚConv2Plus1Dr   c                sd   t t| ƒjtj||dd||fd||fdd�tj|ƒtjdd�tj||d	|ddf|ddfdd�ƒ d S )
Nr   r	   r   F)r   r   r   r   T)Úinplace)r   r	   r	   )r	   r   r   )r   r   r   ÚnnÚConv3dÚBatchNorm3dÚReLU)r   r   r   r   r   r   )r   r   r   r   '   s    



zConv2Plus1D.__init__c             C   s
   | | | fS )Nr   )r   r   r   r   r   7   s    z!Conv2Plus1D.get_downsample_stride)r   r   )r   r   r   r   r   r   r   r   r   )r   r   r   %   s    r   c                   s*   e Zd Zd‡ fdd„	Zedd„ ƒZ‡  ZS )ÚConv3DNoTemporalNr   c                s,   t t| ƒj||dd||fd||fdd� d S )Nr   r	   r   F)r
   r   r   r   r   r   )r   r	   r	   )r   r%   r   )r   r   r   r   r   r   )r   r   r   r   >   s    
zConv3DNoTemporal.__init__c             C   s
   d| | fS )Nr   r   )r   r   r   r   r   M   s    z&Conv3DNoTemporal.get_downsample_stride)Nr   r   )r   r   r   r   r   r   r   r   r   )r   r   r%   <   s     
r%   c                   s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )Ú
BasicBlockr   Nc                s˜   || d d d |d d d|   }t t| ƒjƒ  tj|||||ƒtj|ƒtjdd�ƒ| _tj||||ƒtj|ƒƒ| _tjdd�| _	|| _
|| _d S )Nr	   T)r    )r   r&   r   r!   Ú
Sequentialr#   r$   Úconv1Úconv2ÚreluÚ
downsampler   )r   ÚinplanesÚplanesÚconv_builderr   r+   r   )r   r   r   r   V   s    (
zBasicBlock.__init__c             C   sB   |}| j |ƒ}| j|ƒ}| jd k	r,| j|ƒ}||7 }| j|ƒ}|S )N)r(   r)   r+   r*   )r   ÚxÚresidualÚoutr   r   r   Úforwardg   s    




zBasicBlock.forward)r   N)r   r   r   Ú	expansionr   r2   r   r   r   )r   r   r&   R   s   r&   c                   s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )	Ú
Bottlenecké   r   Nc                sÖ   t t| ƒjƒ  || d d d |d d d|   }tjtj||ddd�tj|ƒtjdd�ƒ| _tj|||||ƒtj|ƒtjdd�ƒ| _	tjtj||| j
 ddd�tj|| j
 ƒƒ| _tjdd�| _|| _|| _d S )Nr	   r   F)r   r   T)r    )r   r4   r   r!   r'   r"   r#   r$   r(   r)   r3   Úconv3r*   r+   r   )r   r,   r-   r.   r   r+   r   )r   r   r   r   x   s     (zBottleneck.__init__c             C   sL   |}| j |ƒ}| j|ƒ}| j|ƒ}| jd k	r6| j|ƒ}||7 }| j|ƒ}|S )N)r(   r)   r6   r+   r*   )r   r/   r0   r1   r   r   r   r2   “   s    





zBottleneck.forward)r   N)r   r   r   r3   r   r2   r   r   r   )r   r   r4   u   s   r4   c                   s    e Zd ZdZ‡ fdd„Z‡  ZS )Ú	BasicStemz)The default conv-batchnorm-relu stem
    c          	      s8   t t| ƒjtjddd
dddd�tjdƒtjdd	�ƒ d S )Nr	   é@   é   r   r   F)r   r   r   r   T)r    )r	   r9   r9   )r   r   r   )r   r	   r	   )r   r7   r   r!   r"   r#   r$   )r   )r   r   r   r   ¦   s
    
zBasicStem.__init__)r   r   r   Ú__doc__r   r   r   r   )r   r   r7   £   s   r7   c                   s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚR2Plus1dStemzWR(2+1)D stem is different than the default one as it uses separated 3D convolution
    c                s^   t t| ƒjtjddddddd�tjdƒtjd	d
�tjddddddd�tjdƒtjd	d
�ƒ d S )Nr	   é-   r   r9   r   r   F)r   r   r   r   T)r    r8   )r   r9   r9   )r   r   r   )r   r	   r	   )r	   r   r   )r   r   r   )r   r   r   )r   r;   r   r!   r"   r#   r$   )r   )r   r   r   r   ±   s    



zR2Plus1dStem.__init__)r   r   r   r:   r   r   r   r   )r   r   r;   ®   s   r;   c                   s8   e Zd Zd‡ fdd„	Zdd„ Zddd	„Zd
d„ Z‡  ZS )ÚVideoResNeté�  Fc                sò   t t| ƒjƒ  d| _|ƒ | _| j||d d|d 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 |ƒ| _| jƒ  |rîx,| jƒ D ] }t|tƒrÊt
jj|jjdƒ qÊW d
S )a  Generic resnet video generator.

        Args:
            block (nn.Module): resnet building block
            conv_makers (list(functions)): generator function for each layer
            layers (List[int]): number of blocks per layer
            stem (nn.Module, optional): Resnet stem, if None, defaults to conv-bn-relu. Defaults to None.
            num_classes (int, optional): Dimension of the final FC layer. Defaults to 400.
            zero_init_residual (bool, optional): Zero init bottleneck residual BN. Defaults to False.
        r8   r   r   )r   é€   r   é   r	   i   N)r   r   r   )r   r=   r   r,   ÚstemÚ_make_layerÚlayer1Úlayer2Úlayer3Úlayer4r!   ZAdaptiveAvgPool3dÚavgpoolÚLinearr3   ÚfcÚ_initialize_weightsÚmodulesÚ
isinstancer4   ÚinitÚ	constant_Zbn3Úweight)r   ÚblockÚconv_makersÚlayersrA   Znum_classesZzero_init_residualÚm)r   r   r   r   Á   s    
zVideoResNet.__init__c             C   sT   | j |ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}| j|ƒ}|jdƒ}| j|ƒ}|S )Nr   )rA   rC   rD   rE   rF   rG   ÚflattenrI   )r   r/   r   r   r   r2   ã   s    







zVideoResNet.forwardr   c       
      C   s°   d }|dks| j ||j krV|j|ƒ}tjtj| j ||j d|dd�tj||j ƒƒ}g }|j|| j ||||ƒƒ ||j | _ x&td|ƒD ]}	|j|| j ||ƒƒ qŠW tj|Ž S )Nr   F)r   r   r   )	r,   r3   r   r!   r'   r"   r#   ÚappendÚrange)
r   rP   r.   r-   Úblocksr   r+   Z	ds_striderR   Úir   r   r   rB   ò   s    

zVideoResNet._make_layerc             C   s®   x¨| j ƒ D ]œ}t|tjƒrJtjj|jddd� |jd k	r¦tjj|jdƒ q
t|tj	ƒrxtjj|jdƒ tjj|jdƒ q
t|tj
ƒr
tjj|jddƒ tjj|jdƒ q
W d S )NZfan_outr*   )ÚmodeZnonlinearityr   r   g{®Gáz„?)rK   rL   r!   r"   rM   Zkaiming_normal_rO   r   rN   r#   rH   Znormal_)r   rS   r   r   r   rJ     s    
zVideoResNet._initialize_weights)r>   F)r   )r   r   r   r   r2   rB   rJ   r   r   r   )r   r   r=   ¿   s
     
r=   FTc             K   s,   t f |Ž}|r(tt|  |d�}|j|ƒ |S )N)Úprogress)r=   r   Ú
model_urlsZload_state_dict)ÚarchÚ
pretrainedrZ   ÚkwargsÚmodelZ
state_dictr   r   r   Ú_video_resnet  s    

r`   c             K   s,   t d| |fttgd ddddgtdœ|—ŽS )a0  Construct 18 layer Resnet3D model as in
    https://arxiv.org/abs/1711.11248

    Args:
        pretrained (bool): If True, returns a model pre-trained on Kinetics-400
        progress (bool): If True, displays a progress bar of the download to stderr

    Returns:
        nn.Module: R3D-18 network
    r   r5   r   )rP   rQ   rR   rA   )r`   r&   r   r7   )r]   rZ   r^   r   r   r   r     s    
c             K   s2   t d| |fttgtgd  ddddgtdœ|—ŽS )aI  Constructor for 18 layer Mixed Convolution network as in
    https://arxiv.org/abs/1711.11248

    Args:
        pretrained (bool): If True, returns a model pre-trained on Kinetics-400
        progress (bool): If True, displays a progress bar of the download to stderr

    Returns:
        nn.Module: MC3 Network definition
    r   r	   r   )rP   rQ   rR   rA   )r`   r&   r   r%   r7   )r]   rZ   r^   r   r   r   r   2  s    
c             K   s,   t d| |fttgd ddddgtdœ|—ŽS )aD  Constructor for the 18 layer deep R(2+1)D network as in
    https://arxiv.org/abs/1711.11248

    Args:
        pretrained (bool): If True, returns a model pre-trained on Kinetics-400
        progress (bool): If True, displays a progress bar of the download to stderr

    Returns:
        nn.Module: R(2+1)D-18 network
    r   r5   r   )rP   rQ   rR   rA   )r`   r&   r   r;   )r]   rZ   r^   r   r   r   r   E  s    
)FT)FT)FT)FT)ZtorchZtorch.nnr!   Úutilsr   Ú__all__r[   r"   r   r'   r   r%   ÚModuler&   r4   r7   r;   r=   r`   r   r   r   r   r   r   r   Ú<module>   s$   

#.U



