o
    _{jô3  ã                   @   sž   d dl mZ d dlm  mZ d dl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 d dlmZ G dd	„ d	ejƒZe ¡ G d
d„ dejƒƒZdS )é    N)Ú
ConvModuleÚconstant_initÚkaiming_init)Úload_checkpoint)Ú
_BatchNorm)Ú_ntuple)Ú	BACKBONES)Úget_root_loggerc                       s:   e Zd ZdZdZ						d‡ fdd	„	Zd
d„ Z‡  ZS )ÚBottleneck2dAudioa  Bottleneck2D block for ResNet2D.

    Args:
        inplanes (int): Number of channels for the input in first conv3d layer.
        planes (int): Number of channels produced by some norm/conv3d layers.
        stride (int | tuple[int]): Stride in the conv layer. Default: 1.
        dilation (int): Spacing between kernel elements. Default: 1.
        downsample (nn.Module): Downsample layer. Default: None.
        factorize (bool): Whether to factorize kernel. Default: True.
        norm_cfg (dict):
            Config for norm layers. required keys are `type` and
            `requires_grad`. Default: None.
        with_cp (bool): Use checkpoint or not. Using checkpoint will save some
            memory while slowing down the training speed. Default: False.
    é   é   é   NTFc	                    sâ   t ƒ  ¡  || _|| _|| _|| _|| _|| _|| _d| _	|| _
d}	d}
d}||f}t|||	|
|| jdd�| _t||||||d|rFtdd�ntd	d�d d d
�
| _t|rWd| n||| j dd| jd d�| _tjdd�| _|| _d S )Nr   )r   r   r   )é   r   F)Úkernel_sizeÚpaddingÚdilationÚnorm_cfgÚbiasÚ	ConvAudio©ÚtypeÚConv)r   Ústrider   r   r   Úconv_cfgr   Úact_cfgr   )r   r   r   r   T)Úinplace)ÚsuperÚ__init__ÚinplanesÚplanesr   r   Ú	factorizer   Úwith_cpÚconv1_strideÚconv2_strider   Úconv1ÚdictÚconv2Ú	expansionÚconv3ÚnnÚReLUÚreluÚ
downsample)Úselfr   r   r   r   r,   r    r   r!   Úconv1_kernel_sizeÚconv1_paddingÚconv2_kernel_sizeÚconv2_padding©Ú	__class__© ú</root/disk_3tb/HAC/mmaction/models/backbones/resnet_audio.pyr      s^   
	ùÿõú
zBottleneck2dAudio.__init__c                    s<   ‡ fdd„}ˆ j r|jrt ||¡}n||ƒ}ˆ  |¡}|S )Nc                    sB   | }ˆ   | ¡}ˆ  |¡}ˆ  |¡}ˆ jd urˆ  | ¡}||7 }|S )N)r$   r&   r(   r,   )ÚxÚidentityÚout©r-   r4   r5   Ú_inner_forwardY   s   




z1Bottleneck2dAudio.forward.<locals>._inner_forward)r!   Úrequires_gradÚcpÚ
checkpointr+   )r-   r6   r:   r8   r4   r9   r5   ÚforwardW   s   
zBottleneck2dAudio.forward)r   r   NTNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   r   r>   Ú__classcell__r4   r4   r2   r5   r
      s    ø9r
   c                       s°   e Zd ZdZedfedfedf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f‡ f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 )'ÚResNetAudioa  ResNet 2d audio backbone. Reference:

        <https://arxiv.org/abs/2001.08740>`_.

    Args:
        depth (int): Depth of resnet, from {50, 101, 152}.
        pretrained (str | None): Name of pretrained model.
        in_channels (int): Channel num of input features. Default: 1.
        base_channels (int): Channel num of stem output features. Default: 32.
        num_stages (int): Resnet stages. Default: 4.
        strides (Sequence[int]): Strides of residual blocks of each stage.
            Default: (1, 2, 2, 2).
        dilations (Sequence[int]): Dilation of each stage.
            Default: (1, 1, 1, 1).
        conv1_kernel (int): Kernel size of the first conv layer. Default: 9.
        conv1_stride (int | tuple[int]): Stride of the first conv layer.
            Default: 1.
        frozen_stages (int): Stages to be frozen (all param fixed). -1 means
            not freezing any parameters.
        factorize (Sequence[int]): factorize Dims of each block for audio.
            Default: (1, 1, 0, 0).
        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.
        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).
        zero_init_residual (bool):
            Whether to use zero initialization for residual block,
            Default: True.
    )r   r   é   r   )r   r   é   r   )r   é   é$   r   )é2   ée   é˜   r   r   é    )r   r   r   r   )r   r   r   r   é	   éÿÿÿÿ)r   r   r   r   Fr   r   ÚBN2dT)r   r;   r*   )r   r   c                    sˆ  t ƒ  ¡  || jvrtd|› d�ƒ‚|| _|| _|| _|| _|| _|dkr)|dks+J ‚|| _	|| _
|	| _|
| _t|ƒ|ƒ| _|| _|| _|| _|| _|| _|| _| j| \| _}|d |… | _| j| _|  ¡  g | _t| jƒD ]A\}}|| }|| }| jd|  }| j| j| j||||| j| | j|d�	}|| jj | _d|d › �}|  ||¡ | j |¡ qo| jj| j dt| jƒd   | _ d S )Nzinvalid depth z for resnetr   r   r   )r   r   r    r   r!   Úlayer)!r   r   Úarch_settingsÚKeyErrorÚdepthÚ
pretrainedÚin_channelsÚbase_channelsÚ
num_stagesÚ	dilationsÚconv1_kernelr"   Úfrozen_stagesr   Zstage_factorizationÚ	norm_evalr!   r   r   r   Úzero_init_residualÚblockÚstage_blocksr   Ú_make_stem_layerÚ
res_layersÚ	enumerateÚmake_res_layerr'   Ú
add_moduleÚappendÚlenÚfeat_dim)r-   rS   rT   rU   rW   rV   ÚstridesrX   rY   r"   rZ   r    r[   r!   r   r   r   r\   r^   ÚiÚ
num_blocksr   r   r   Ú	res_layerÚ
layer_namer2   r4   r5   r   �   s^   

÷
ÿzResNetAudio.__init__Nc
                 C   sÐ   t |tƒs|n|f| }t|ƒ|ksJ ‚d}
|dks!|||j kr/t|||j d|d|dd�}
g }| ||||||
|d dk||	d�¡ ||j }td|ƒD ]}| |||d||| dk||	d�¡ qNtj|Ž S )a4  Build residual layer for ResNetAudio.

        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.
            strides (Sequence[int]): Strides of residual blocks of each stage.
                Default: (1, 2, 2, 2).
            dilation (int): Spacing between kernel elements. Default: 1.
            factorize (int | Sequence[int]): Determine whether to factorize
                for each block. Default: 1.
            norm_cfg (dict):
                Config for norm layers. required keys are `type` and
                `requires_grad`. Default: None.
            with_cp (bool): Use checkpoint or not. Using checkpoint will save
                some memory while slowing down the training speed.
                Default: False.

        Returns:
            A residual layer for the given config.
        Nr   F)r   r   r   r   r   r   )r    r   r!   )	Ú
isinstanceÚintre   r'   r   rd   Úranger)   Ú
Sequential)r-   r]   r   r   Úblocksr   r   r    r   r!   r,   Úlayersrh   r4   r4   r5   rb   á   sX   "ÿÿù	
øÿ


ùÿ

zResNetAudio.make_res_layerc              
   C   s2   t | j| j| j| jdtddd�| j| jd�| _dS )zYConstruct the stem layers consists of a conv+norm+act module and a
        pooling layer.Fr   Úsum)r   Úop)r   r   r   r   r   r   N)	r   rU   rV   rY   r"   r%   r   r   r$   r9   r4   r4   r5   r_   *  s   
øzResNetAudio._make_stem_layerc                 C   s†   | j dkr | jj ¡  | jj| jjfD ]}| ¡ D ]}d|_q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   rP   N)	rZ   r$   ÚbnÚevalÚconvÚ
parametersr;   rn   Úgetattr)r-   ÚmÚparamrh   r4   r4   r5   Ú_freeze_stages7  s   
ÿÿýzResNetAudio._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)rl   rT   Ústrr	   Úinfor   Úmodulesr)   ÚConv2dr   r   r   r\   r
   r(   rt   Ú	TypeError)r-   r}   ry   r4   r4   r5   Úinit_weightsF  s&   



€
€ýzResNetAudio.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`   rx   )r-   r6   rk   rj   r4   r4   r5   r>   ^  s
   




zResNetAudio.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{   r[   r€   rl   r   ru   )r-   Úmodery   r2   r4   r5   r„   n  s   

€ýzResNetAudio.train)r   r   r   NF)T)r?   r@   rA   rB   r
   rQ   r%   r   rb   r_   r{   rƒ   r>   r„   rC   r4   r4   r2   r5   rD   o   sB    'û

ïI
÷IrD   )Útorch.nnr)   Útorch.utils.checkpointÚutilsr=   r<   Úmmcv.cnnr   r   r   Úmmcv.runnerr   Útorch.nn.modules.batchnormr   Ztorch.nn.modules.utilsr   Zmmaction.models.registryr   Úmmaction.utilsr	   ÚModuler
   Úregister_modulerD   r4   r4   r4   r5   Ú<module>   s    c