o
    _{j[  ã                   @   sl   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
 G dd	„ d	e	ƒZe ¡ G d
d„ de
ƒƒZdS )é    N)Ú
ConvModule)Ú
_BatchNormé   )Ú	BACKBONESé   )ÚBottleneck3dÚResNet3dc                       s&   e Zd ZdZddœ‡ fdd„
Z‡  ZS )ÚCSNBottleneck3daÐ  Channel-Separated Bottleneck Block.

    This module is proposed in
    "Video Classification with Channel-Separated Convolutional Networks"
    Link: https://arxiv.org/pdf/1711.11248.pdf

    Args:
        inplanes (int): Number of channels for the input in first conv3d layer.
        planes (int): Number of channels produced by some norm/conv3d layers.
        bottleneck_mode (str): Determine which ways to factorize a 3D
            bottleneck block using channel-separated convolutional networks.
                If set to 'ip', it will replace the 3x3x3 conv2 layer with a
                1x1x1 traditional convolution and a 3x3x3 depthwise
                convolution, i.e., Interaction-preserved channel-separated
                bottleneck block.
                If set to 'ir', it will replace the 3x3x3 conv2 layer with a
                3x3x3 depthwise convolution, which is derived from preserved
                bottleneck block by removing the extra 1x1x1 convolution,
                i.e., Interaction-reduced channel-separated bottleneck block.
            Default: 'ir'.
        args (position arguments): Position arguments for Bottleneck.
        kwargs (dict, optional): Keyword arguments for Bottleneck.
    Úir)Úbottleneck_modec                   sÆ   t t| ƒj||g|¢R i |¤Ž || _g }| jdkr'| tj||dddd�¡ | jjj	}| jjj
}| jjj}	| jjj}
t| jjjƒ}t|||||	|
|| j| j| j|d�| _| | j¡ tj|Ž | _d S )NÚipr   F)Úkernel_sizeÚstrideÚbias)r   ÚpaddingÚdilationr   Úconv_cfgÚnorm_cfgÚact_cfgÚgroups)Úsuperr	   Ú__init__r   ÚappendÚnnÚConv3dÚconv2Úconvr   r   r   r   Úboolr   r   r   r   r   Ú
Sequential)ÚselfÚinplanesÚplanesr   ÚargsÚkwargsr   Úconv2_kernel_sizeÚconv2_strideÚconv2_paddingZconv2_dilationZ
conv2_bias©Ú	__class__© ú</root/disk_3tb/HAC/mmaction/models/backbones/resnet3d_csn.pyr   "   s:   ÿ
ÿ



õzCSNBottleneck3d.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__r)   r)   r'   r*   r	   	   s    ür	   c                	       sJ   e Zd ZdZddddedddd�d	d
df‡ fdd„	Zd‡ fdd„	Z‡  ZS )ÚResNet3dCSNaã  ResNet backbone for CSN.

    Args:
        depth (int): Depth of ResNetCSN, from {18, 34, 50, 101, 152}.
        pretrained (str | None): Name of pretrained model.
        temporal_strides (tuple[int]):
            Temporal strides of residual blocks of each stage.
            Default: (1, 2, 2, 2).
        conv1_kernel (tuple[int]): Kernel size of the first conv layer.
            Default: (3, 7, 7).
        conv1_stride_t (int): Temporal stride of the first conv layer.
            Default: 1.
        pool1_stride_t (int): Temporal stride of the first pooling layer.
            Default: 1.
        norm_cfg (dict): Config for norm layers. required keys are `type` and
            `requires_grad`.
            Default: dict(type='BN3d', requires_grad=True, eps=1e-3).
        inflate_style (str): `3x1x1` or `1x1x1`. which determines the kernel
            sizes and padding strides for conv1 and conv2 in each block.
            Default: '3x3x3'.
        bottleneck_mode (str): Determine which ways to factorize a 3D
            bottleneck block using channel-separated convolutional networks.
                If set to 'ip', it will replace the 3x3x3 conv2 layer with a
                1x1x1 traditional convolution and a 3x3x3 depthwise
                convolution, i.e., Interaction-preserved channel-separated
                bottleneck block.
                If set to 'ir', it will replace the 3x3x3 conv2 layer with a
                3x3x3 depthwise convolution, which is derived from preserved
                bottleneck block by removing the extra 1x1x1 convolution,
                i.e., Interaction-reduced channel-separated bottleneck block.
            Default: 'ip'.
        kwargs (dict, optional): Key arguments for "make_res_layer".
    )r   r   r   r   )é   é   r2   r   ÚBN3dTgü©ñÒMbP?)ÚtypeÚrequires_gradÚepsÚ3x3x3r
   Fc              
      sf   t dft dft dfdœ| _|
| _|	dvrtd|	› d�ƒ‚tt| ƒj||f|||||||	dœ|¤Ž d S )	N)r1   é   é   r1   )r1   r8   é   r1   )r1   é   é$   r1   )é2   ée   é˜   )r   r
   z-Bottleneck mode must be "ip" or "ir",but got Ú.)Útemporal_stridesÚconv1_kernelÚconv1_stride_tÚpool1_stride_tr   Úinflate_styler   )r	   Úarch_settingsÚ	bn_frozenÚ
ValueErrorr   r0   r   )r   ÚdepthÚ
pretrainedrA   rB   rC   rD   r   rE   r   rG   r#   r'   r)   r*   r   h   s.   û
ÿ
þ÷

özResNet3dCSN.__init__c                    sh   t t| ƒ |¡ |  ¡  |r.| jr0|  ¡ D ]}t|tƒr-| ¡  | j	r-| 
¡ D ]}d|_q'qd S d S d S )NF)r   r   ÚtrainÚ_freeze_stagesÚ	norm_evalÚmodulesÚ
isinstancer   ÚevalrG   Ú
parametersr5   )r   ÚmodeÚmÚparamr'   r)   r*   rK   ‹   s   

€úzResNet3dCSN.train)T)r+   r,   r-   r.   Údictr   rK   r/   r)   r)   r'   r*   r0   D   s    %ö#r0   )Útorch.nnr   Úmmcv.cnnr   Ú
mmcv.utilsr   Úregistryr   Úresnet3dr   r   r	   Úregister_moduler0   r)   r)   r)   r*   Ú<module>   s    ;