o
    _{jÉS  ã                   @   sº   d dl mZ d dlmZmZmZ d dlmZmZ d dl	m
Z
 d dlmZ ddlmZ dd	lmZ G d
d„ dejƒZG dd„ dejƒZ							ddd„Ze ¡ G dd„ dejƒƒZdS )é    N)Ú
ConvModuleÚconstant_initÚkaiming_init)Ú_load_checkpointÚload_checkpoint)Ú
_BatchNorm)Ú
checkpointé   )Úget_root_loggeré   )Ú	BACKBONESc                
       sT   e Zd ZdZdZddddedd�eddd	�ed
dd�df‡ fdd„	Zdd„ Z‡  ZS )Ú
BasicBlockaY  Basic block for ResNet.

    Args:
        inplanes (int): Number of channels for the input in first conv2d layer.
        planes (int): Number of channels produced by some norm/conv2d layers.
        stride (int): Stride in the conv 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'.
        conv_cfg (dict): Config for norm layers. Default: dict(type='Conv').
        norm_cfg (dict):
            Config for norm layers. required keys are `type` and
            `requires_grad`. Default: dict(type='BN2d', requires_grad=True).
        act_cfg (dict): Config for activate layers.
            Default: dict(type='ReLU', inplace=True).
        with_cp (bool): Use checkpoint or not. Using checkpoint will save some
            memory while slowing down the training speed. Default: False.
    é   NÚpytorchÚConv©ÚtypeÚBNT©r   Úrequires_gradÚReLU©r   ÚinplaceFc                    sŠ   t ƒ  ¡  |dv sJ ‚t||d|||d|||	d�
| _t||ddddd||d d�
| _tjdd�| _|| _|| _	|| _
|| _|| _|
rCJ ‚d S )N©r   Úcaffer	   F©Úkernel_sizeÚstrideÚpaddingÚdilationÚbiasÚconv_cfgÚnorm_cfgÚact_cfgr   T©r   )ÚsuperÚ__init__r   Úconv1Úconv2Únnr   ÚreluÚ
downsampleÚstyler   r   r"   ©ÚselfÚinplanesÚplanesr   r   r+   r,   r!   r"   r#   Úwith_cp©Ú	__class__© ú6/root/disk_3tb/HAC/mmaction/models/backbones/resnet.pyr&   "   sB   
öözBasicBlock.__init__c                 C   sB   |}|   |¡}|  |¡}| jdur|  |¡}|| }|  |¡}|S )ú¼Defines the computation performed at every call.

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

        Returns:
            torch.Tensor: The output of the module.
        N)r'   r(   r+   r*   )r.   ÚxÚidentityÚoutr4   r4   r5   ÚforwardO   s   	




zBasicBlock.forward©	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	expansionÚdictr&   r:   Ú__classcell__r4   r4   r2   r5   r      s    

ö-r   c                
       sT   e Zd ZdZdZddddedd�edd	d
�edd	d�df‡ fdd„	Zdd„ Z‡  ZS )Ú
Bottlenecka‹  Bottleneck block for ResNet.

    Args:
        inplanes (int):
            Number of channels for the input feature in first conv layer.
        planes (int):
            Number of channels produced by some norm layes and conv layers
        stride (int): Spatial stride in the conv 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'.
        conv_cfg (dict): Config for norm layers. Default: dict(type='Conv').
        norm_cfg (dict):
            Config for norm layers. required keys are `type` and
            `requires_grad`. Default: dict(type='BN2d', requires_grad=True).
        act_cfg (dict): Config for activate layers.
            Default: dict(type='ReLU', inplace=True).
        with_cp (bool): Use checkpoint or not. Using checkpoint will save some
            memory while slowing down the training speed. Default: False.
    é   r   Nr   r   r   r   Tr   r   r   Fc                    sÎ   t ƒ  ¡  |dv sJ ‚|| _|| _|dkrd| _|| _n|| _d| _t||d| jd|||	d�| _t||d| j||d|||	d�
| _t||| j	 dd||d d�| _
tjd	d
�| _|| _|| _|| _|| _|
| _d S )Nr   r   r   F©r   r   r    r!   r"   r#   r	   r   )r   r    r!   r"   r#   Tr$   )r%   r&   r/   r0   Úconv1_strideÚconv2_strider   r'   r(   r@   Úconv3r)   r   r*   r+   r   r   r"   r1   r-   r2   r4   r5   r&   €   s\   
ø	öù	
zBottleneck.__init__c                    s<   ‡ fdd„}ˆ j r|jrt ||¡}n||ƒ}ˆ  |¡}|S )r6   c                    sB   | }ˆ   | ¡}ˆ  |¡}ˆ  |¡}ˆ jdurˆ  | ¡}|| }|S )z)Forward wrapper for utilizing checkpoint.N)r'   r(   rH   r+   )r7   r8   r9   ©r.   r4   r5   Ú_inner_forwardÄ   s   




z*Bottleneck.forward.<locals>._inner_forward)r1   r   Úcpr   r*   )r.   r7   rJ   r9   r4   rI   r5   r:   º   s   

zBottleneck.forwardr;   r4   r4   r2   r5   rC   f   s    

ö:rC   r   r   Fc                 C   s¢   d}|dks||| j  krt||| j  d|d||dd�}g }| | |||||||||	|
d�
¡ || j  }td|ƒD ]}| | ||d|||||	|
d�	¡ q9tj|Ž S )aG  Build residual layer for ResNet.

    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.
        stride (int): Stride in the conv layer. 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'.
        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): 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   FrE   )r,   r!   r"   r#   r1   )r@   r   ÚappendÚranger)   Ú
Sequential)Úblockr/   r0   Úblocksr   r   r,   r!   r"   r#   r1   r+   ÚlayersÚ_r4   r4   r5   Úmake_res_layerÝ   sV    ø
öÿ
÷ÿ
rS   c                       sÒ   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edd�eddd�eddd�dddf‡ fdd„	Zdd„ Z	e
dd„ ƒZe
dd„ ƒZd+dd „Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd,‡ fd)d*„	Z‡  ZS )-ÚResNeta"  ResNet backbone.

    Args:
        depth (int): Depth of resnet, from {18, 34, 50, 101, 152}.
        pretrained (str | None): Name of pretrained model. Default: None.
        in_channels (int): Channel num of input features. Default: 3.
        num_stages (int): Resnet stages. Default: 4.
        strides (Sequence[int]): Strides of the first block of each stage.
        out_indices (Sequence[int]): Indices of output feature. Default: (3, ).
        dilations (Sequence[int]): Dilation of each stage.
        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.
        conv_cfg (dict): Config for norm layers. Default: dict(type='Conv').
        norm_cfg (dict):
            Config for norm layers. required keys are `type` and
            `requires_grad`. Default: dict(type='BN2d', requires_grad=True).
        act_cfg (dict): Config for activate layers.
            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.
        partial_bn (bool): Whether to use partial bn. Default: False.
        with_cp (bool): Use checkpoint or not. Using checkpoint will save some
            memory while slowing down the training speed. Default: False.
    )r   r   r   r   )r	   rD   é   r	   )r	   rD   é   r	   )r	   é   é$   r	   )é   é"   é2   ée   é˜   NTr	   rD   )r	   )r   r   r   r   )r   r   r   r   r   éÿÿÿÿr   r   ÚBN2dr   r   r   Fc                    s²  t ƒ  ¡  || jvrtd|› d�ƒ‚|| _|| _|| _|| _|| _d|  kr,dks/J ‚ J ‚|| _	t
|ƒ|k s:J ‚|| _|| _t|ƒt|ƒ  krO|ksRJ ‚ J ‚|	| _|
| _|| _|| _|| _|| _|| _|| _| j| \| _}|d |… | _d| _|  ¡  g | _t| jƒD ]>\}}|| }|| }dd|  }t| j| j||||| j||||d�}|| jj | _d|d › �}|  ||¡ | j |¡ qˆ| jjd dt| jƒd   | _ d S )	Nzinvalid depth z for resnetr   rD   é@   r   )r   r   r,   r!   r"   r#   r1   Úlayer)!r%   r&   Úarch_settingsÚKeyErrorÚdepthÚin_channelsÚ
pretrainedÚtorchvision_pretrainÚ
num_stagesÚout_indicesÚmaxÚstridesÚ	dilationsÚlenr,   Úfrozen_stagesr!   r"   r#   Ú	norm_evalÚ
partial_bnr1   rO   Ústage_blocksr/   Ú_make_stem_layerÚ
res_layersÚ	enumeraterS   r@   Ú
add_modulerL   Úfeat_dim)r.   rd   rf   rg   re   rh   ri   rk   rl   r,   rn   r!   r"   r#   ro   rp   r1   rq   ÚiÚ
num_blocksr   r   r0   Ú	res_layerÚ
layer_namer2   r4   r5   r&   M  sf   

$õÿzResNet.__init__c                 C   s:   t | jddddd| j| j| jd�	| _tjdddd�| _d	S )
zYConstruct the stem layers consists of a conv+norm+act module and a
        pooling layer.r`   é   r   r	   F)r   r   r   r    r!   r"   r#   r   )r   r   r   N)	r   re   r!   r"   r#   r'   r)   Ú	MaxPool2dÚmaxpoolrI   r4   r4   r5   rr   ”  s   ÷
zResNet._make_stem_layerc                 C   sŠ   |d }| j jj|| jkr| j j || ¡ | |¡ t| dƒdurA|d }| jjj|| jkrC| jj || ¡ | |¡ dS dS dS )aÈ  Load the conv parameters of resnet from torchvision.

        Args:
            conv (nn.Module): The destination conv module.
            state_dict_tv (OrderedDict): The state dict of pretrained
                torchvision model.
            module_name_tv (str): The name of corresponding conv module in the
                torchvision model.
            loaded_param_names (list[str]): List of parameters that have been
                loaded.
        z.weightr    Nz.bias)ÚweightÚdataÚshapeÚcopy_rL   Úgetattrr    )ÚconvÚstate_dict_tvÚmodule_name_tvÚloaded_param_namesZweight_tv_nameZbias_tv_namer4   r4   r5   Ú_load_conv_params£  s   
üzResNet._load_conv_paramsc                 C   s¤   |   ¡ D ]!\}}|› d|› �}|| }|jj|jkr%|j |¡ | |¡ q|  ¡ D ]%\}}|› d|› �}||v rO|| }|jj|jkrO|j |¡ | |¡ q*dS )aÀ  Load the bn parameters of resnet from torchvision.

        Args:
            bn (nn.Module): The destination bn module.
            state_dict_tv (OrderedDict): The state dict of pretrained
                torchvision model.
            module_name_tv (str): The name of corresponding bn module in the
                torchvision model.
            loaded_param_names (list[str]): List of parameters that have been
                loaded.
        Ú.N)Únamed_parametersr   r€   r�   rL   Únamed_buffers)Úbnr„   r…   r†   Ú
param_nameÚparamZparam_tv_nameZparam_tvr4   r4   r5   Ú_load_bn_params½  s    
€
€ùzResNet._load_bn_paramsc           	      C   s¸   t | jƒ}d|v r|d }g }|  ¡ D ]0\}}t|tƒrCd|v r)|d }|d }n|}| dd¡}|  |j|||¡ |  |j	|||¡ qt
| ¡ ƒt
|ƒ }|rZ| d|› �¡ dS dS )	z?Initiate the parameters from torchvision pretrained checkpoint.Ú
state_dictr+   z.0z.1rƒ   r‹   z:These parameters in pretrained checkpoint are not loaded: N)r   rf   Únamed_modulesÚ
isinstancer   Úreplacer‡   rƒ   rŽ   r‹   ÚsetÚkeysÚinfo)	r.   ÚloggerZstate_dict_torchvisionr†   ÚnameÚmoduleZoriginal_conv_nameZoriginal_bn_nameZremaining_namesr4   r4   r5   Ú_load_torchvision_checkpointÛ  s<   



ÿ
ÿ€ÿÿÿÿÿz#ResNet._load_torchvision_checkpointc                 C   sŒ   t | jtƒrtƒ }| jr|  |¡ dS t| | jd|d� dS | jdu rB|  ¡ D ]}t |tj	ƒr4t
|ƒ q't |tjƒr?t|dƒ q'dS tdƒ‚)zPInitiate the parameters either from existing checkpoint or from
        scratch.F)Ústrictr–   Nr   z pretrained must be a str or None)r‘   rf   Ústrr
   rg   r™   r   Úmodulesr)   ÚConv2dr   ÚBatchNorm2dr   Ú	TypeError)r.   r–   Úmr4   r4   r5   Úinit_weightsý  s    

ÿ


€üzResNet.init_weightsc                 C   sn   |   |¡}|  |¡}g }t| jƒD ]\}}t| |ƒ}||ƒ}|| jv r(| |¡ qt|ƒdkr3|d S t|ƒS )zê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}   rt   rs   r‚   ri   rL   rm   Útuple)r.   r7   Úoutsrw   rz   ry   r4   r4   r5   r:     s   





€zResNet.forwardc                 C   s€   | j dkr| jj ¡  | j ¡ D ]}| ¡ D ]}d|_qqtd| j d ƒD ]}t| d|› �ƒ}| ¡  | ¡ D ]}d|_q7q%dS )zVPrevent all the parameters from being optimized before
        ``self.frozen_stages``.r   Fr   ra   N)	rn   r'   r‹   Úevalrœ   Ú
parametersr   rM   r‚   )r.   r    r�   rw   r4   r4   r5   Ú_freeze_stages)  s   
ÿÿýzResNet._freeze_stagesc                 C   sZ   t ƒ }| d¡ d}|  ¡ D ]}t|tjƒr*|d7 }|dkr*| ¡  d|j_d|j	_qd S )Nz*Freezing BatchNorm2D except the first one.r   r   r   F)
r
   r•   rœ   r‘   r)   rž   r¤   r~   r   r    )r.   r–   Zcount_bnr    r4   r4   r5   Ú_partial_bn8  s   
€ùzResNet._partial_bnc                    s\   t ƒ  |¡ |  ¡  |r| jr|  ¡ D ]}t|tƒr| ¡  q|r*| jr,|  	¡  dS dS dS )z*Set the optimization status when training.N)
r%   Útrainr¦   ro   rœ   r‘   r   r¤   rp   r§   )r.   Úmoder    r2   r4   r5   r¨   E  s   

€
ÿzResNet.train)N)T)r<   r=   r>   r?   r   rC   rb   rA   r&   rr   Ústaticmethodr‡   rŽ   r™   r¡   r:   r¦   r§   r¨   rB   r4   r4   r2   r5   rT   '  sF    û


ðG


"rT   )r   r   r   NNNF)Útorch.nnr)   Úmmcv.cnnr   r   r   Úmmcv.runnerr   r   Ú
mmcv.utilsr   Ztorch.utilsr   rK   Úutilsr
   Úregistryr   ÚModuler   rC   rS   Úregister_modulerT   r4   r4   r4   r5   Ú<module>   s&    [{
öJ