o
    _{j­J  ã                   @   s´   d dl Z 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 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e ¡ G dd„ dejƒƒZdS )é    N)Ú
ConvModuleÚSwishÚbuild_activation_layerÚconstant_initÚkaiming_init)Úload_checkpoint)Ú
_BatchNormé   )Úget_root_loggeré   )Ú	BACKBONESc                       s2   e Zd Z‡ fdd„Zeddd„ƒZdd„ Z‡  ZS )	ÚSEModulec                    sh   t ƒ  ¡  t d¡| _|  ||¡| _tj|| jddd�| _t 	¡ | _
tj| j|ddd�| _t ¡ | _d S )Né   r   )Úkernel_sizeÚpadding)ÚsuperÚ__init__ÚnnÚAdaptiveAvgPool3dÚavg_poolÚ_round_widthZ
bottleneckÚConv3dÚfc1ÚReLUÚreluÚfc2ÚSigmoidÚsigmoid)ÚselfÚchannelsÚ	reduction©Ú	__class__© ú3/root/disk_3tb/HAC/mmaction/models/backbones/x3d.pyr      s   

ÿ

ÿzSEModule.__init__é   c                 C   sJ   | |9 } |p|}t |t| |d  ƒ| | ƒ}|d|  k r!||7 }t|ƒS )Nr   çÍÌÌÌÌÌì?©ÚmaxÚint)ÚwidthÚ
multiplierZ	min_widthÚdivisorÚ	width_outr#   r#   r$   r      s   ÿzSEModule._round_widthc                 C   s>   |}|   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|| S )N)r   r   r   r   r   )r   ÚxZmodule_inputr#   r#   r$   Úforward%   s   




zSEModule.forward©r%   r%   )Ú__name__Ú
__module__Ú__qualname__r   Ústaticmethodr   r/   Ú__classcell__r#   r#   r!   r$   r      s
    	r   c                	       sL   e Zd ZdZddddedd�edd�edd�d	f‡ fd
d„	Zdd„ Z‡  ZS )ÚBlockX3Da»  BlockX3D 3d building block for X3D.

    Args:
        inplanes (int): Number of channels for the input in first conv3d layer.
        planes (int): Number of channels produced by some norm/conv3d layers.
        outplanes (int): Number of channels produced by final the conv3d layer.
        spatial_stride (int): Spatial stride in the conv3d layer. Default: 1.
        downsample (nn.Module | None): Downsample layer. Default: None.
        se_ratio (float | None): The reduction ratio of squeeze and excitation
            unit. If set as None, it means not using SE unit. Default: None.
        use_swish (bool): Whether to use swish as the activation function
            before and after the 3x3x3 conv. Default: True.
        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   NTr   ©ÚtypeÚBN3dr   Fc                    sö   t ƒ  ¡  || _|| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _tdd�| _|| _t||dddd| j	| j
| jd�	| _t||dd| j| jfd|d| j	| j
d d�
| _tƒ | _t||dddd| j	| j
d d�	| _| jd urst|| jƒ| _t| jƒ| _d S )	Nr   r7   r   r   F)	Úin_channelsÚout_channelsr   Ústrider   ÚbiasÚconv_cfgÚnorm_cfgÚact_cfgr	   )
r:   r;   r   r<   r   Úgroupsr=   r>   r?   r@   )r   r   ÚinplanesÚplanesÚ	outplanesÚspatial_strideÚ
downsampleÚse_ratioÚ	use_swishr>   r?   r@   ÚdictZact_cfg_swishÚwith_cpr   Úconv1Úconv2r   ÚswishÚconv3r   Ú	se_moduler   r   )r   rB   rC   rD   rE   rF   rG   rH   r>   r?   r@   rJ   r!   r#   r$   r   F   sf   
÷ö÷
zBlockX3D.__init__c                    s<   ‡ fdd„}ˆ j r|jrt ||¡}n||ƒ}ˆ  |¡}|S )z0Defines the computation performed at every call.c                    s`   | }ˆ   | ¡}ˆ  |¡}ˆ jdurˆ  |¡}ˆ  |¡}ˆ  |¡}ˆ jdur*ˆ  | ¡}|| }|S )z)Forward wrapper for utilizing checkpoint.N)rK   rL   rG   rO   rM   rN   rF   )r.   ÚidentityÚout©r   r#   r$   Ú_inner_forward�   s   







z(BlockX3D.forward.<locals>._inner_forward)rJ   Úrequires_gradÚcpÚ
checkpointr   )r   r.   rS   rQ   r#   rR   r$   r/   Š   s   
zBlockX3D.forward)r1   r2   r3   Ú__doc__rI   r   r/   r5   r#   r#   r!   r$   r6   /   s    õDr6   c                       s¼   e Zd ZdZ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
f‡ fdd„	Zed&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 ))ÚX3Da  X3D backbone. https://arxiv.org/pdf/2004.04730.pdf.

    Args:
        gamma_w (float): Global channel width expansion factor. Default: 1.
        gamma_b (float): Bottleneck channel width expansion factor. Default: 1.
        gamma_d (float): Network depth expansion factor. Default: 1.
        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.
        spatial_strides (Sequence[int]):
            Spatial strides of residual blocks of each stage.
            Default: ``(1, 2, 2, 2)``.
        frozen_stages (int): Stages to be frozen (all param fixed). If set to
            -1, it means not freezing any parameters. Default: -1.
        se_style (str): The style of inserting SE modules into BlockX3D, 'half'
            denotes insert into half of the blocks, while 'all' denotes insert
            into all blocks. Default: 'half'.
        se_ratio (float | None): The reduction ratio of squeeze and excitation
            unit. If set as None, it means not using SE unit. Default: 1 / 16.
        use_swish (bool): Whether to use swish as the activation function
            before and after the 3x3x3 conv. Default: True.
        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".
    g      ð?Nr	   é   )r   r   r   r   éÿÿÿÿÚhalfg      °?Tr   r7   r9   )r8   rT   r   )r8   ÚinplaceFc                    s&  t ƒ  ¡  |ˆ _|ˆ _|ˆ _|ˆ _|ˆ _dˆ _g d¢ˆ _ˆ  	ˆ jˆ j¡ˆ _‡ fdd„ˆ jD ƒˆ _|ˆ _
d|  kr>dksAJ ‚ J ‚|ˆ _t|ƒ|ksLJ ‚|ˆ _|	ˆ _ˆ jdv sYJ ‚|
ˆ _ˆ jd u shˆ jdkshJ ‚|ˆ _|ˆ _|ˆ _|ˆ _|ˆ _|ˆ _|ˆ _tˆ _ˆ jd |… ˆ _ˆ jˆ _ˆ  ¡  g ˆ _tˆ jƒD ]J\}}|| }ˆ jd	|  }t|ˆ j ƒ}ˆ jˆ jˆ j|||f|ˆ jˆ jˆ jˆ jˆ jˆ j|d
œ|¤Ž}|ˆ _d|d › �}ˆ  ||¡ ˆ j  |¡ q˜ˆ jd	tˆ jƒd   ˆ _!t"ˆ j!tˆ j!ˆ j ƒddddˆ jˆ jˆ jd�	ˆ _#tˆ j!ˆ j ƒˆ _!d S )Né   )r   r   é   r	   c                    s   g | ]	}ˆ   |ˆ j¡‘qS r#   )Ú_round_repeatsÚgamma_d)Ú.0r.   rR   r#   r$   Ú
<listcomp>ó   s    ÿz X3D.__init__.<locals>.<listcomp>r   rY   )Úallr[   r   r   )rE   Úse_stylerG   rH   r?   r>   r@   rJ   ÚlayerF©r   r<   r   r=   r>   r?   r@   )$r   r   Úgamma_wÚgamma_br`   Ú
pretrainedr:   Úbase_channelsÚstage_blocksr   Ú
num_stagesÚspatial_stridesÚlenÚfrozen_stagesrd   rG   rH   r>   r?   r@   Ú	norm_evalrJ   Úzero_init_residualr6   ÚblockÚlayer_inplanesÚ_make_stem_layerÚ
res_layersÚ	enumerater)   Úmake_res_layerÚ
add_moduleÚappendÚfeat_dimr   Úconv5)r   rg   rh   r`   ri   r:   rl   rm   ro   rd   rG   rH   r>   r?   r@   rp   rJ   rq   ÚkwargsÚiÚ
num_blocksrE   rB   rC   Ú	res_layerÚ
layer_namer!   rR   r$   r   Ñ   s”   

ÿ
ÿûóò÷
zX3D.__init__r%   c                 C   sR   |s| S | |9 } |p|}t |t| |d  ƒ| | ƒ}|d|  k r%||7 }t|ƒS )z1Round width of filters based on width multiplier.r   r&   r'   )r*   r+   Ú	min_depthr,   Znew_filtersr#   r#   r$   r   6  s   ÿzX3D._round_widthc                 C   s   |s| S t t ||  ¡ƒS )z1Round number of layers based on depth multiplier.)r)   ÚmathÚceil)Úrepeatsr+   r#   r#   r$   r_   D  s   zX3D._round_repeatsr   c                 K   s  d}|dks
||krt ||dd||fdd||
dd�	}dg| }| jdkr*dg| }n| jdkr9d	d
„ t|ƒD ƒ}nt‚g }| ||||f|||d rL|nd|	|
|||dœ|¤Ž¡ td|ƒD ]}| ||||fd|| rn|nd|	|
|||dœ|¤Ž¡ q^tj|Ž S )a¬  Build residual layer for ResNet3D.

        Args:
            block (nn.Module): Residual module to be built.
            layer_inplanes (int): Number of channels for the input feature
                of the res layer.
            inplanes (int): Number of channels for the input feature in each
                block, which equals to base_channels * gamma_w.
            planes (int): Number of channels for the output feature in each
                block, which equals to base_channel * gamma_w * gamma_b.
            blocks (int): Number of residual blocks.
            spatial_stride (int): Spatial strides in residual and conv layers.
                Default: 1.
            se_style (str): The style of inserting SE modules into BlockX3D,
                'half' denotes insert into half of the blocks, while 'all'
                denotes insert into all blocks. Default: 'half'.
            se_ratio (float | None): The reduction ratio of squeeze and
                excitation unit. If set as None, it means not using SE unit.
                Default: None.
            use_swish (bool): Whether to use swish as the activation function
                before and after the 3x3x3 conv. Default: True.
            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   r   Frf   rc   Tr[   c                 S   s   g | ]}|d  dk‘qS )r   r   r#   )ra   r}   r#   r#   r$   rb   Œ  s    z&X3D.make_res_layer.<locals>.<listcomp>)rE   rF   rG   rH   r?   r>   r@   rJ   )rE   rG   rH   r?   r>   r@   rJ   )r   rd   ÚrangeÚNotImplementedErrorry   r   Ú
Sequential)r   rr   rs   rB   rC   ÚblocksrE   rd   rG   rH   r?   r@   r>   rJ   r|   rF   Úuse_seÚlayersr}   r#   r#   r$   rw   M  sr   .÷


ýõôÿýöõÿ
zX3D.make_res_layerc                 C   sP   t | j| jdddd| jddd�	| _t | j| jddd	| jd| j| j| jd
�
| _dS )zYConstruct the stem layers consists of a conv+norm+act module and a
        pooling layer.)r   r	   r	   )r   r   r   )r   r   r   FNrf   )r^   r   r   )r   r   r   )r   r   r   )r   r<   r   rA   r=   r>   r?   r@   )r   r:   rj   r>   Úconv1_sr?   r@   Úconv1_trR   r#   r#   r$   rt   ±  s.   ÷
özX3D._make_stem_layerc                 C   s�   | j dkr%| j ¡  | j ¡  | j ¡ D ]}d|_q| j ¡ D ]}d|_qtd| j d ƒD ]}t| d|› �ƒ}| ¡  | ¡ D ]}d|_q?q-dS )zVPrevent all the parameters from being optimized before
        ``self.frozen_stages``.r   Fr   re   N)ro   r‹   ÚevalrŒ   Ú
parametersrT   r…   Úgetattr)r   Úparamr}   Úmr#   r#   r$   Ú_freeze_stagesÊ  s   


ÿýzX3D._freeze_stagesc                 C   s¸   t | jtƒrtƒ }| d| j› �¡ t| | jd|d� dS | jdu rX|  ¡ D ]}t |tjƒr3t	|ƒ q&t |t
ƒr=t|dƒ q&| jrT|  ¡ D ]}t |tƒrSt|jjdƒ qEdS dS tdƒ‚)zPInitiate the parameters either from existing checkpoint or from
        scratch.zload model from: F)ÚstrictÚloggerNr   r   z pretrained must be a str or None)Ú
isinstanceri   Ústrr
   Úinfor   Úmodulesr   r   r   r   r   rq   r6   rN   ÚbnÚ	TypeError)r   r”   r‘   r#   r#   r$   Úinit_weightsÛ  s&   



€
€ýzX3D.init_weightsc                 C   s@   |   |¡}|  |¡}| jD ]}t| |ƒ}||ƒ}q|  |¡}|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Œ   ru   r�   r{   )r   r.   r€   r   r#   r#   r$   r/   ò  s   






zX3D.forwardc                    sJ   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’   rp   r˜   r•   r   r�   )r   Úmoder‘   r!   r#   r$   rœ     s   

€ýz	X3D.trainr0   )r   r[   NTNNNF)T)r1   r2   r3   rW   rI   r   r4   r   r_   rw   rt   r’   r›   r/   rœ   r5   r#   r#   r!   r$   rX   ©   sL    '

ïe

ódrX   )r‚   Útorch.nnr   Útorch.utils.checkpointÚutilsrV   rU   Úmmcv.cnnr   r   r   r   r   Úmmcv.runnerr   Z
mmcv.utilsr   r
   Úregistryr   ÚModuler   r6   Úregister_modulerX   r#   r#   r#   r$   Ú<module>   s    !z