o
    _{jLœ  ã                	   @   s6  d dl mZ d dlm  mZ d dlmZmZm	Z	m
Z
mZ d dlmZmZ d dlmZ d dlmZmZ ddlmZ dd	lmZ zd d
lmZ d d	lmZ dZW n eefy\   dZY nw G dd„ dejƒZ G dd„ dejƒZ!e "¡ G dd„ dejƒƒZ#e "¡ G dd„ dejƒƒZ$er™e "¡ e$ƒ e "¡ e#ƒ dS dS )é    N)Ú
ConvModuleÚ
NonLocal3dÚbuild_activation_layerÚconstant_initÚkaiming_init)Ú_load_checkpointÚload_checkpoint)Ú
_BatchNorm)Ú_ntupleÚ_tripleé   )Úget_root_loggeré   )Ú	BACKBONES)ÚSHARED_HEADSTFc                       sZ   e Zd ZdZdZdddddddeƒ edd�ed	d�ed
d�df‡ fdd„	Zdd„ Z‡  ZS )ÚBasicBlock3da·  BasicBlock 3d block for ResNet3D.

    Args:
        inplanes (int): Number of channels for the input in first conv3d layer.
        planes (int): Number of channels produced by some norm/conv3d layers.
        spatial_stride (int): Spatial stride in the conv3d layer. Default: 1.
        temporal_stride (int): Temporal stride in the conv3d layer. Default: 1.
        dilation (int): Spacing between kernel elements. Default: 1.
        downsample (nn.Module | None): Downsample layer. Default: None.
        style (str): ``pytorch`` or ``caffe``. If set to "pytorch", the
            stride-two layer is the 3x3 conv layer, otherwise the stride-two
            layer is the first 1x1 conv layer. Default: 'pytorch'.
        inflate (bool): Whether to inflate kernel. Default: True.
        non_local (bool): Determine whether to apply non-local module in this
            block. Default: False.
        non_local_cfg (dict): Config for non-local module. Default: ``dict()``.
        conv_cfg (dict): Config dict for convolution layer.
            Default: ``dict(type='Conv3d')``.
        norm_cfg (dict): Config for norm layers. required keys are ``type``,
            Default: ``dict(type='BN3d')``.
        act_cfg (dict): Config dict for activation layer.
            Default: ``dict(type='ReLU')``.
        with_cp (bool): Use checkpoint or not. Using checkpoint will save some
            memory while slowing down the training speed. Default: False.
    é   NÚpytorchTFÚConv3d©ÚtypeÚBN3dÚReLUc                    sf  t ƒ  ¡  |dv sJ ‚t|ƒ dg¡sJ ‚|| _|| _|| _|| _|| _|| _	|| _
|| _|| _|| _|| _|	| _|
| _|| _d| _|| _d| _| j
rWd}d||f}d}d}nd}d||f}d}d}t|||| j| j| jf|d||fd	| j| j| jd
�
| _t||| j || j| j| jf|d	| j| jd d�	| _|| _t| jƒ| _| jr±t| jjjfi | j¤Ž| _d S d S )N©r   ÚcaffeÚinflate_styler   ©r   r   r   ©r   r   r   ©r   r   r   r   ©r   r   r   F©ÚstrideÚpaddingÚdilationÚbiasÚconv_cfgÚnorm_cfgÚact_cfg©r!   r"   r$   r%   r&   r'   ) ÚsuperÚ__init__ÚsetÚissubsetÚinplanesÚplanesÚspatial_strideÚtemporal_strider#   ÚstyleÚinflater%   r&   r'   Úwith_cpÚ	non_localÚnon_local_cfgÚconv1_stride_sÚconv2_stride_sÚconv1_stride_tÚconv2_stride_tr   Úconv1Ú	expansionÚconv2Ú
downsampler   Úrelur   ÚnormÚnum_featuresÚnon_local_block)Úselfr-   r.   r/   r0   r#   r=   r1   r2   r4   r5   r%   r&   r'   r3   ÚkwargsÚconv1_kernel_sizeÚconv1_paddingÚconv2_kernel_sizeÚconv2_padding©Ú	__class__© ú8/root/disk_3tb/HAC/mmaction/models/backbones/resnet3d.pyr*   0   s~   


ÿõÿöÿÿzBasicBlock3d.__init__c                    óL   ‡ fdd„}ˆ j r|jrt ||¡}n||ƒ}ˆ  |¡}ˆ jr$ˆ  |¡}|S )ú0Defines the computation performed at every call.c                    s8   | }ˆ   | ¡}ˆ  |¡}ˆ jdurˆ  | ¡}|| }|S ©z)Forward wrapper for utilizing checkpoint.N)r:   r<   r=   ©ÚxÚidentityÚout©rB   rJ   rK   Ú_inner_forward†   s   



z,BasicBlock3d.forward.<locals>._inner_forward©r3   Úrequires_gradÚcpÚ
checkpointr>   r4   rA   ©rB   rP   rT   rR   rJ   rS   rK   Úforwardƒ   s   

zBasicBlock3d.forward©	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r;   Údictr*   rZ   Ú__classcell__rJ   rJ   rH   rK   r      s"    òSr   c                       s\   e Zd ZdZdZddddddddeƒ ed	d
�edd
�edd
�df‡ fdd„	Zdd„ Z‡  ZS )ÚBottleneck3dao  Bottleneck 3d block for ResNet3D.

    Args:
        inplanes (int): Number of channels for the input in first conv3d layer.
        planes (int): Number of channels produced by some norm/conv3d layers.
        spatial_stride (int): Spatial stride in the conv3d layer. Default: 1.
        temporal_stride (int): Temporal stride in the conv3d layer. Default: 1.
        dilation (int): Spacing between kernel elements. Default: 1.
        downsample (nn.Module | None): Downsample layer. Default: None.
        style (str): ``pytorch`` or ``caffe``. If set to "pytorch", the
            stride-two layer is the 3x3 conv layer, otherwise the stride-two
            layer is the first 1x1 conv layer. Default: 'pytorch'.
        inflate (bool): Whether to inflate kernel. Default: True.
        inflate_style (str): ``3x1x1`` or ``3x3x3``. which determines the
            kernel sizes and padding strides for conv1 and conv2 in each block.
            Default: '3x1x1'.
        non_local (bool): Determine whether to apply non-local module in this
            block. Default: False.
        non_local_cfg (dict): Config for non-local module. Default: ``dict()``.
        conv_cfg (dict): Config dict for convolution layer.
            Default: ``dict(type='Conv3d')``.
        norm_cfg (dict): Config for norm layers. required keys are ``type``,
            Default: ``dict(type='BN3d')``.
        act_cfg (dict): Config dict for activation layer.
            Default: ``dict(type='ReLU')``.
        with_cp (bool): Use checkpoint or not. Using checkpoint will save some
            memory while slowing down the training speed. Default: False.
    é   r   Nr   TÚ3x1x1Fr   r   r   r   c                    sÆ  t ƒ  ¡  |dv sJ ‚|	dv sJ ‚|| _|| _|| _|| _|| _|| _|| _|	| _	|| _
|| _|| _|| _|
| _|| _| jdkrMd| _|| _d| _|| _n|| _d| _|| _d| _| jrx|	dkrld}d}d}d	||f}nd
}d}d}d||f}nd
}d}d}d	||f}t|||| j| j| jf|d| j| j
| jd�	| _t|||| j| j| jf|d||fd| j| j
| jd�
| _t||| j dd| j| j
d d�| _|| _t| jƒ| _| jrát| jjjfi | j¤Ž| _d S d S )Nr   )rd   Z3x3x3r   r   rd   )r   r   r   )r   r   r   r   r   r   )r   r   r   r   Fr(   r    )r$   r%   r&   r'   ) r)   r*   r-   r.   r/   r0   r#   r1   r2   r   r&   r%   r'   r3   r4   r5   r6   r7   r8   r9   r   r:   r<   r;   Úconv3r=   r   r>   r   r?   r@   rA   )rB   r-   r.   r/   r0   r#   r=   r1   r2   r   r4   r5   r%   r&   r'   r3   rD   rE   rF   rG   rH   rJ   rK   r*   ¾   s¦   


ÿöÿõø
ÿÿzBottleneck3d.__init__c                    rL   )rM   c                    sB   | }ˆ   | ¡}ˆ  |¡}ˆ  |¡}ˆ jdurˆ  | ¡}|| }|S rN   )r:   r<   re   r=   rO   rS   rJ   rK   rT   *  s   




z,Bottleneck3d.forward.<locals>._inner_forwardrU   rY   rJ   rS   rK   rZ   '  s   

zBottleneck3d.forwardr[   rJ   rJ   rH   rK   rb   Ÿ   s$    ñirb   c                       s0  e Zd ZdZedfedfedfedfedfdœZddd	d
ddddddddddddedd�eddd�eddd�dddeƒ df‡ fdd„	Ze	dddddddeƒ ddddfd d!„ƒZ
e	d"d#„ ƒZe	d$d%„ ƒZe	d&d'„ ƒZd(d)„ Zd*d+„ Zd,d-„ Ze	d:d.d/„ƒZd:d0d1„Zd2d3„ Zd4d5„ Zd6d7„ Zd;‡ fd8d9„	Z‡  ZS )<ÚResNet3daê  ResNet 3d backbone.

    Args:
        depth (int): Depth of resnet, from {18, 34, 50, 101, 152}.
        pretrained (str | None): Name of pretrained model.
        pretrained2d (bool): Whether to load pretrained 2D model.
            Default: True.
        in_channels (int): Channel num of input features. Default: 3.
        base_channels (int): Channel num of stem output features. Default: 64.
        out_indices (Sequence[int]): Indices of output feature. Default: (3, ).
        num_stages (int): Resnet stages. Default: 4.
        spatial_strides (Sequence[int]):
            Spatial strides of residual blocks of each stage.
            Default: ``(1, 2, 2, 2)``.
        temporal_strides (Sequence[int]):
            Temporal strides of residual blocks of each stage.
            Default: ``(1, 1, 1, 1)``.
        dilations (Sequence[int]): Dilation of each stage.
            Default: ``(1, 1, 1, 1)``.
        conv1_kernel (Sequence[int]): Kernel size of the first conv layer.
            Default: ``(5, 7, 7)``.
        conv1_stride_t (int): Temporal stride of the first conv layer.
            Default: 2.
        pool1_stride_t (int): Temporal stride of the first pooling layer.
            Default: 2.
        with_pool2 (bool): Whether to use pool2. Default: True.
        style (str): `pytorch` or `caffe`. If set to "pytorch", the stride-two
            layer is the 3x3 conv layer, otherwise the stride-two layer is
            the first 1x1 conv layer. Default: 'pytorch'.
        frozen_stages (int): Stages to be frozen (all param fixed). -1 means
            not freezing any parameters. Default: -1.
        inflate (Sequence[int]): Inflate Dims of each block.
            Default: (1, 1, 1, 1).
        inflate_style (str): ``3x1x1`` or ``1x1x1``. which determines the
            kernel sizes and padding strides for conv1 and conv2 in each block.
            Default: '3x1x1'.
        conv_cfg (dict): Config for conv layers. required keys are ``type``
            Default: ``dict(type='Conv3d')``.
        norm_cfg (dict): Config for norm layers. required keys are ``type`` and
            ``requires_grad``.
            Default: ``dict(type='BN3d', requires_grad=True)``.
        act_cfg (dict): Config dict for activation layer.
            Default: ``dict(type='ReLU', inplace=True)``.
        norm_eval (bool): Whether to set BN layers to eval mode, namely, freeze
            running stats (mean and var). Default: False.
        with_cp (bool): Use checkpoint or not. Using checkpoint will save some
            memory while slowing down the training speed. Default: False.
        non_local (Sequence[int]): Determine whether to apply non-local module
            in the corresponding block of each stages. Default: (0, 0, 0, 0).
        non_local_cfg (dict): Config for non-local module. Default: ``dict()``.
        zero_init_residual (bool):
            Whether to use zero initialization for residual block,
            Default: True.
        kwargs (dict, optional): Key arguments for "make_res_layer".
    )r   r   r   r   )r   rc   é   r   )r   rc   é   r   )r   é   é$   r   )é   é"   é2   ée   é˜   Tr   rc   é@   )r   r   )r   r   r   r   )r   r   r   r   )é   é   rr   r   r   éÿÿÿÿrd   r   r   r   ©r   rV   r   ©r   ÚinplaceF)r   r   r   r   c           %         sD  t ƒ  ¡  || jvrtd|› d�ƒ‚|| _|| _|| _|| _|| _|| _	d|  kr/dks2J ‚ J ‚|| _
t|ƒ|k s=J ‚|| _|	| _|
| _t|ƒt|	ƒ  kr\t|
ƒ  kr\|ks_J ‚ J ‚|| _|| _|| _|| _|| _|| _t|ƒ|ƒ| _t|ƒ|ƒ| _|| _|| _|| _|| _|| _|| _|| _| j| \| _ }|d |… | _!| j| _"|| _#|  $¡  g | _%t&| j!ƒD ]X\}}|| }|	| } |
| }!| jd|  }"| j'| j | j"|"|f|| |!| j| j| j| j| j| | j#| j| | j|dœ|¤Ž}#|"| j j( | _"d|d › �}$|  )|$|#¡ | j% *|$¡ q¶| j j(| j dt| j!ƒd   | _+d S )Nzinvalid depth z for resnetr   rc   r   )r/   r0   r#   r1   r&   r%   r'   r4   r5   r2   r   r3   Úlayer),r)   r*   Úarch_settingsÚKeyErrorÚdepthÚ
pretrainedÚpretrained2dÚin_channelsÚbase_channelsÚ
num_stagesÚout_indicesÚmaxÚspatial_stridesÚtemporal_stridesÚ	dilationsÚlenÚconv1_kernelr8   Úpool1_stride_tÚ
with_pool2r1   Úfrozen_stagesr
   Zstage_inflationsÚnon_local_stagesr   r%   r&   r'   Ú	norm_evalr3   Úzero_init_residualÚblockÚstage_blocksr-   r5   Ú_make_stem_layerÚ
res_layersÚ	enumerateÚmake_res_layerr;   Ú
add_moduleÚappendÚfeat_dim)%rB   rz   r{   r|   r}   r   r~   r€   r‚   rƒ   r„   r†   r8   r‡   rˆ   r1   r‰   r2   r   r%   r&   r'   r‹   r3   r4   r5   rŒ   rC   rŽ   ÚiÚ
num_blocksr/   r0   r#   r.   Ú	res_layerÚ
layer_namerH   rJ   rK   r*   ‡  s”   


ÿÿüðïÿzResNet3d.__init__r   r   Nc                 K   s8  t |tƒs|n|f| }t |
tƒs|
n|
f| }
t|ƒ|kr$t|
ƒ|ks&J ‚d}|dks3||| j krEt||| j d|||fd||dd�}g }| | ||f||||||d dk|	|
d dk|||||dœ|¤Ž¡ || j }td|ƒD ]#}| | ||fdd|||| dk|	|
| dk|||||dœ|¤Ž¡ qstj|Ž S )aã  Build residual layer for ResNet3D.

        Args:
            block (nn.Module): Residual module to be built.
            inplanes (int): Number of channels for the input feature
                in each block.
            planes (int): Number of channels for the output feature
                in each block.
            blocks (int): Number of residual blocks.
            spatial_stride (int | Sequence[int]): Spatial strides in
                residual and conv layers. Default: 1.
            temporal_stride (int | Sequence[int]): Temporal strides in
                residual and conv layers. Default: 1.
            dilation (int): Spacing between kernel elements. Default: 1.
            style (str): ``pytorch`` or ``caffe``. If set to ``pytorch``,
                the stride-two layer is the 3x3 conv layer, otherwise
                the stride-two layer is the first 1x1 conv layer.
                Default: ``pytorch``.
            inflate (int | Sequence[int]): Determine whether to inflate
                for each block. Default: 1.
            inflate_style (str): ``3x1x1`` or ``1x1x1``. which determines
                the kernel sizes and padding strides for conv1 and conv2
                in each block. Default: '3x1x1'.
            non_local (int | Sequence[int]): Determine whether to apply
                non-local module in the corresponding block of each stages.
                Default: 0.
            non_local_cfg (dict): Config for non-local module.
                Default: ``dict()``.
            conv_cfg (dict | None): Config for norm layers. Default: None.
            norm_cfg (dict | None): Config for norm layers. Default: None.
            act_cfg (dict | None): Config for activate layers. Default: None.
            with_cp (bool | None): Use checkpoint or not. Using checkpoint
                will save some memory while slowing down the training speed.
                Default: False.

        Returns:
            nn.Module: A residual layer for the given config.
        Nr   F)Úkernel_sizer!   r$   r%   r&   r'   r   )r/   r0   r#   r=   r1   r2   r   r4   r5   r&   r%   r'   r3   )r/   r0   r#   r1   r2   r   r4   r5   r&   r%   r'   r3   )	Ú
isinstanceÚintr…   r;   r   r”   ÚrangeÚnnÚ
Sequential)r�   r-   r.   Úblocksr/   r0   r#   r1   r2   r   r4   r5   r&   r'   r%   r3   rC   r=   Úlayersr–   rJ   rJ   rK   r’   ì  sŒ   8ÿÿÿÿø
þ

ñðÿ
þ

òñÿ
zResNet3d.make_res_layerc           	      C   sˆ   |d }|| }| j jjd }|j d¡ | j ¡| }| j j |¡ | |¡ t| dƒdurB|d }| jj || ¡ | |¡ dS dS )až  Inflate a conv module from 2d to 3d.

        Args:
            conv3d (nn.Module): The destination conv3d module.
            state_dict_2d (OrderedDict): The state dict of pretrained 2d model.
            module_name_2d (str): The name of corresponding conv module in the
                2d model.
            inflated_param_names (list[str]): List of parameters that have been
                inflated.
        ú.weightr   r$   Nz.bias)	ÚweightÚdataÚshapeÚ	unsqueezeÚ	expand_asÚcopy_r”   Úgetattrr$   )	Úconv3dÚstate_dict_2dÚmodule_name_2dÚinflated_param_namesZweight_2d_nameÚconv2d_weightZkernel_tZ
new_weightZbias_2d_namerJ   rJ   rK   Ú_inflate_conv_params^  s   ÿÿ
ýzResNet3d._inflate_conv_paramsc                 C   sˆ   |   ¡ D ]\}}|› d|› �}|| }|j |¡ | |¡ q|  ¡ D ]\}}|› d|› �}||v rA|| }|j |¡ | |¡ q#dS )a˜  Inflate a norm module from 2d to 3d.

        Args:
            bn3d (nn.Module): The destination bn3d module.
            state_dict_2d (OrderedDict): The state dict of pretrained 2d model.
            module_name_2d (str): The name of corresponding bn module in the
                2d model.
            inflated_param_names (list[str]): List of parameters that have been
                inflated.
        Ú.N)Únamed_parametersr¤   r¨   r”   Únamed_buffers)Zbn3dr«   r¬   r­   Ú
param_nameÚparamZparam_2d_nameZparam_2drJ   rJ   rK   Ú_inflate_bn_paramsz  s   
€ùzResNet3d._inflate_bn_paramsc              
   C   sJ  t | jƒ}d|v r|d }g }|  ¡ D ]y\}}t|tƒrŒd|v r)|d }|d }n|}| dd¡}|d |vr@| d|› �¡ n4||d  j}|jj	j
j}	||	d	d
… |	dd	…  krk| d|› d|	› d|› d�¡ n	|  |j|||¡ |d |vrƒ| d|› �¡ q|  |j|||¡ qt| ¡ ƒt|ƒ }
|
r£| d|
› �¡ d	S d	S )a°  Inflate the resnet2d parameters to resnet3d.

        The differences between resnet3d and resnet2d mainly lie in an extra
        axis of conv kernel. To utilize the pretrained parameters in 2d model,
        the weight of conv2d models should be inflated to fit in the shapes of
        the 3d counterpart.

        Args:
            logger (logging.Logger): The logger used to print
                debugging infomation.
        Ú
state_dictr=   z.0z.1ÚconvÚbnr¢   z(Module not exist in the state_dict_r2d: Nr   r   zWeight shape mismatch for : z : 3d weight shape: z; 2d weight shape: z. z6These parameters in the 2d checkpoint are not loaded: )r   r{   Únamed_modulesr›   r   ÚreplaceÚwarningr¥   r·   r£   r¤   r¯   rµ   r¸   r+   ÚkeysÚinfo)rB   ÚloggerZstate_dict_r2dr­   ÚnameÚmoduleÚoriginal_conv_nameÚoriginal_bn_nameZshape_2dZshape_3dÚremaining_namesrJ   rJ   rK   Ú_inflate_weights–  sh   



ÿÿÿÿþý
þ
ÿ
þ€ÿÿÿÿzResNet3d._inflate_weightsc                 C   ó   |   | |¡ d S ©N©rÄ   ©rB   r¾   rJ   rJ   rK   Úinflate_weights×  ó   zResNet3d.inflate_weightsc                 C   sr   t | j| j| j| jddftdd„ t| jƒD ƒƒd| j| j| j	d�	| _
tjd| jddfdd�| _tjd	d	d
�| _dS )zYConstruct the stem layers consists of a conv+norm+act module and a
        pooling layer.r   c                 S   s   g | ]}|d  d ‘qS )r   r   rJ   )Ú.0ÚkrJ   rJ   rK   Ú
<listcomp>â  s    z-ResNet3d._make_stem_layer.<locals>.<listcomp>F)rš   r!   r"   r$   r%   r&   r'   r   r   )rš   r!   r"   )r   r   r   )rš   r!   N)r   r}   r~   r†   r8   Útupler   r%   r&   r'   r:   rž   Ú	MaxPool3dr‡   ÚmaxpoolÚpool2rS   rJ   rJ   rK   r�   Ú  s"   
÷
ýzResNet3d._make_stem_layerc                 C   sp   | j dkr| j ¡  | j ¡ D ]}d|_qtd| j d ƒD ]}t| d|› �ƒ}| ¡  | ¡ D ]}d|_q/qdS )úVPrevent all the parameters from being optimized before
        ``self.frozen_stages``.r   Fr   rw   N)r‰   r:   ÚevalÚ
parametersrV   r�   r©   )rB   r´   r–   ÚmrJ   rJ   rK   Ú_freeze_stagesï  s   

ÿýzResNet3d._freeze_stagesc                 C   sð   |r|| _ t| j tƒr,tƒ }| d| j › �¡ | jr!|  |¡ dS t| | j d|d� dS | j du rt|  ¡ D ]}t|t	j
ƒrBt|ƒ q5t|tƒrLt|dƒ q5| jrp|  ¡ D ]}t|tƒrct|jjdƒ qTt|tƒrot|jjdƒ qTdS dS tdƒ‚)a=  Initiate the parameters either from existing checkpoint or from
        scratch.

        Args:
            pretrained (str | None): The path of the pretrained weight. Will
                override the original `pretrained` if set. The arg is added to
                be compatible with mmdet. Default: None.
        zload model from: F)Ústrictr¾   Nr   r   z pretrained must be a str or None)r{   r›   Ústrr   r½   r|   rÉ   r   Úmodulesrž   r   r   r	   r   rŒ   rb   re   r¸   r   r<   Ú	TypeError)rB   r{   r¾   rÕ   rJ   rJ   rK   Ú_init_weightsý  s6   


ÿ



€

€ûzResNet3d._init_weightsc                 C   rÅ   rÆ   ©rÛ   ©rB   r{   rJ   rJ   rK   Úinit_weights&  rÊ   zResNet3d.init_weightsc                 C   sh   |   |¡}|  |¡}g }t| jƒD ] \}}t| |ƒ}||ƒ}|dkr*| jr*|  |¡}|dkr1 |S q|S )Nr   r   )r:   rÐ   r‘   r�   r©   rˆ   rÑ   ©rB   rP   Úoutsr–   r™   r˜   rJ   rJ   rK   Úget_feature)  s   



þzResNet3d.get_featurec                 C   s   t | dƒ}||ƒ}|S )NÚlayer4)r©   )rB   rP   r˜   rJ   rJ   rK   Úget_predict6  s   
zResNet3d.get_predictc                 C   s†   |   |¡}|  |¡}g }t| jƒD ]#\}}t| |ƒ}||ƒ}|dkr*| jr*|  |¡}|| jv r4| |¡ qt	|ƒdkr?|d S t
|ƒS )úêDefines the computation performed at every call.

        Args:
            x (torch.Tensor): The input data.

        Returns:
            torch.Tensor: The feature of the input
            samples extracted by the backbone.
        r   r   )r:   rÐ   r‘   r�   r©   rˆ   rÑ   r€   r”   r…   rÎ   rß   rJ   rJ   rK   rZ   =  s   





€zResNet3d.forwardc                    óJ   t ƒ  |¡ |  ¡  |r| jr!|  ¡ D ]}t|tƒr| ¡  qdS dS dS ©z*Set the optimization status when training.N©r)   ÚtrainrÖ   r‹   rÙ   r›   r	   rÓ   ©rB   ÚmoderÕ   rH   rJ   rK   rè   W  ó   

€ýzResNet3d.trainrÆ   ©T)r\   r]   r^   r_   r   rb   rx   r`   r*   Ústaticmethodr’   r¯   rµ   rÄ   rÉ   r�   rÖ   rÛ   rÞ   rá   rã   rZ   rè   ra   rJ   rJ   rH   rK   rf   E  s~    9û

æeñq


@
(rf   c                       sˆ   e Zd ZdZdddddddddd	ed
d�eddd�eddd�dddf‡ fdd„	Zdd„ Zdd„ Zddd„Zdd„ Z	d‡ fdd„	Z
‡  ZS )ÚResNet3dLayerañ  ResNet 3d Layer.

    Args:
        depth (int): Depth of resnet, from {18, 34, 50, 101, 152}.
        pretrained (str | None): Name of pretrained model.
        pretrained2d (bool): Whether to load pretrained 2D model.
            Default: True.
        stage (int): The index of Resnet stage. Default: 3.
        base_channels (int): Channel num of stem output features. Default: 64.
        spatial_stride (int): The 1st res block's spatial stride. Default 2.
        temporal_stride (int): The 1st res block's temporal stride. Default 1.
        dilation (int): The dilation. Default: 1.
        style (str): `pytorch` or `caffe`. If set to "pytorch", the stride-two
            layer is the 3x3 conv layer, otherwise the stride-two layer is
            the first 1x1 conv layer. Default: 'pytorch'.
        all_frozen (bool): Frozen all modules in the layer. Default: False.
        inflate (int): Inflate Dims of each block. Default: 1.
        inflate_style (str): ``3x1x1`` or ``1x1x1``. which determines the
            kernel sizes and padding strides for conv1 and conv2 in each block.
            Default: '3x1x1'.
        conv_cfg (dict): Config for conv layers. required keys are ``type``
            Default: ``dict(type='Conv3d')``.
        norm_cfg (dict): Config for norm layers. required keys are ``type`` and
            ``requires_grad``.
            Default: ``dict(type='BN3d', requires_grad=True)``.
        act_cfg (dict): Config dict for activation layer.
            Default: ``dict(type='ReLU', inplace=True)``.
        norm_eval (bool): Whether to set BN layers to eval mode, namely, freeze
            running stats (mean and var). Default: False.
        with_cp (bool): Use checkpoint or not. Using checkpoint will save some
            memory while slowing down the training speed. Default: False.
        zero_init_residual (bool):
            Whether to use zero initialization for residual block,
            Default: True.
        kwargs (dict, optional): Key arguments for "make_res_layer".
    Tr   rp   r   r   r   Frd   r   r   r   rt   r   ru   c                    s\  t ƒ  ¡  tj| _|| jv sJ ‚tj| _tj| _tj| _tj| _tj| _|| _	|| _
|| _|| _|dkr8|dks:J ‚|| _|| _|| _|| _|	| _|
| _|| _|| _|| _|| _|| _|| _|| _|| _| j| \}}|| }dd|  }dd|d   |j }| j||||f|||| j| j| j| j| j| j|dœ
|¤Ž}d|d › �| _|  | j|¡ d S )Nr   r   rp   r   r   )
r/   r0   r#   r1   r&   r%   r'   r2   r   r3   rw   )r)   r*   rf   rx   r’   r¯   rµ   rÄ   rÛ   rz   r{   r|   Ústager~   r/   r0   r#   r1   Ú
all_frozenZstage_inflationr   r%   r&   r'   r‹   r3   rŒ   r;   r™   r“   )rB   rz   r{   r|   rï   r~   r/   r0   r#   r1   rð   r2   r   r%   r&   r'   r‹   r3   rŒ   rC   r�   rŽ   Zstage_blockr.   r-   r˜   rH   rJ   rK   r*   ˆ  sh   
üòñzResNet3dLayer.__init__c                 C   rÅ   rÆ   rÇ   rÈ   rJ   rJ   rK   rÉ   Ø  rÊ   zResNet3dLayer.inflate_weightsc                 C   s6   | j rt| | jƒ}| ¡  | ¡ D ]}d|_qdS dS )rÒ   FN)rð   r©   r™   rÓ   rÔ   rV   )rB   rw   r´   rJ   rJ   rK   rÖ   Û  s   üzResNet3dLayer._freeze_stagesNc                 C   rÅ   rÆ   rÜ   rÝ   rJ   rJ   rK   rÞ   ä  rÊ   zResNet3dLayer.init_weightsc                 C   s   t | | jƒ}||ƒ}|S )rä   )r©   r™   )rB   rP   r˜   rR   rJ   rJ   rK   rZ   ç  s   
zResNet3dLayer.forwardc                    rå   ræ   rç   ré   rH   rJ   rK   rè   õ  rë   zResNet3dLayer.trainrÆ   rì   )r\   r]   r^   r_   r`   r*   rÉ   rÖ   rÞ   rZ   rè   ra   rJ   rJ   rH   rK   rî   a  s0    (

îP
	rî   )%Útorch.nnrž   Útorch.utils.checkpointÚutilsrX   rW   Úmmcv.cnnr   r   r   r   r   Úmmcv.runnerr   r   Ú
mmcv.utilsr	   Ztorch.nn.modules.utilsr
   r   r   Úregistryr   Úmmdet.models.builderr   ZMMDET_SHARED_HEADSZmmdet.modelsZMMDET_BACKBONESÚmmdet_importedÚImportErrorÚModuleNotFoundErrorÚModuler   rb   Úregister_modulerf   rî   rJ   rJ   rJ   rK   Ú<module>   s@    ÿ  '     þ