o
    _{j»  ã                   @   sH   d dl mZ d dlm  mZ d dlmZmZmZ G dd„ dej	ƒZ
dS )é    N)Úconstant_initÚkaiming_initÚnormal_initc                       s@   e Zd ZdZ							d‡ fdd„	Zd	d
„ Zdd„ Z‡  ZS )ÚTAMa4  Temporal Adaptive Module(TAM) for TANet.

    This module is proposed in `TAM: TEMPORAL ADAPTIVE MODULE FOR VIDEO
    RECOGNITION <https://arxiv.org/pdf/2005.06803>`_

    Args:
        in_channels (int): Channel num of input features.
        num_segments (int): Number of frame segments.
        alpha (int): ```alpha``` in the paper and is the ratio of the
            intermediate channel number to the initial channel number in the
            global branch. Default: 2.
        adaptive_kernel_size (int): ```K``` in the paper and is the size of the
            adaptive kernel size in the global branch. Default: 3.
        beta (int): ```beta``` in the paper and is set to control the model
            complexity in the local branch. Default: 4.
        conv1d_kernel_size (int): Size of the convolution kernel of Conv1d in
            the local branch. Default: 3.
        adaptive_convolution_stride (int): The first dimension of strides in
            the adaptive convolution of ```Temporal Adaptive Aggregation```.
            Default: 1.
        adaptive_convolution_padding (int): The first dimension of paddings in
            the adaptive convolution of ```Temporal Adaptive Aggregation```.
            Default: 1.
        init_std (float): Std value for initiation of `nn.Linear`. Default:
            0.001.
    é   é   é   é   çü©ñÒMbP?c
           
         s  t ƒ  ¡  |dkr|dksJ ‚|| _|| _|| _|| _|| _|| _|| _|| _	|	| _
t tj||| dd�t || ¡tjdd�tj|| |dd�t d¡¡| _t tj||| |d|d dd	�t || ¡tjdd�tj|| |ddd�t ¡ ¡| _|  ¡  d S )
Nr   F)ÚbiasT)Úinplaceéÿÿÿÿr	   r   )ÚstrideÚpaddingr   )ÚsuperÚ__init__Úin_channelsÚnum_segmentsÚalphaÚadaptive_kernel_sizeÚbetaÚconv1d_kernel_sizeÚadaptive_convolution_strideÚadaptive_convolution_paddingÚinit_stdÚnnÚ
SequentialÚLinearÚBatchNorm1dÚReLUÚSoftmaxÚGÚConv1dÚSigmoidÚLÚinit_weights)
Úselfr   r   r   r   r   r   r   r   r   ©Ú	__class__© ú0/root/disk_3tb/HAC/mmaction/models/common/tam.pyr   "   s@   

üú
özTAM.__init__c                 C   sZ   |   ¡ D ]&}t|tjƒrt|ƒ qt|tjƒrt|dƒ qt|tjƒr*t|| j	d� qdS )z%Initiate the parameters from scratch.r	   )ÚstdN)
ÚmodulesÚ
isinstancer   r"   r   r   r   r   r   r   )r&   Úmr)   r)   r*   r%   M   s   
€úzTAM.init_weightsc                 C   s*  |  ¡ \}}}}| j}|| }|| jksJ ‚| |||||¡}| ddddd¡ ¡ }t | d|||¡d¡}|  | d|¡¡ || ddd¡}	|  	| d||¡¡ |||dd¡}
||
 }tj
| d|| ||| ¡|	d| jdf| jdf|| d	�}| |||||¡}| ddddd¡ ¡  ||||¡}|S )
z¼Defines the computation performed at every call.

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

        Returns:
            torch.Tensor: The output of the module.
        r   r   r	   r   r   r   )r	   r	   N)r   r   r   Úgroups)Úsizer   r   ÚviewÚpermuteÚ
contiguousÚFÚadaptive_avg_pool2dr!   r$   Úconv2dr   r   )r&   ÚxÚnÚcÚhÚwr   Únum_batchesZ	theta_outZconv_kernelZlocal_activationZnew_xÚyr)   r)   r*   ÚforwardW   s6   
ÿÿ
ÿú	"zTAM.forward)r   r   r   r   r	   r	   r
   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r>   Ú__classcell__r)   r)   r'   r*   r      s    ÷+
r   )Útorch.nnr   Útorch.nn.functionalÚ
functionalr4   Úmmcv.cnnr   r   r   ÚModuler   r)   r)   r)   r*   Ú<module>   s    