o
    _{jK  ã                   @   sX   d dl Z d dlmZ d dlmZ ddlmZ ddlmZm	Z	 e 
¡ G dd„ de	ƒƒZdS )	é    N)Únormal_inité   )ÚHEADSé   )ÚAvgConsensusÚBaseHeadc                       sP   e Zd ZdZdedd�deddd�d	d
ddf‡ fdd„	Zdd„ Zdd„ Z‡  ZS )ÚTSMHeada…  Class head for TSM.

    Args:
        num_classes (int): Number of classes to be classified.
        in_channels (int): Number of channels in input feature.
        num_segments (int): Number of frame segments. Default: 8.
        loss_cls (dict): Config for building loss.
            Default: dict(type='CrossEntropyLoss')
        spatial_type (str): Pooling type in spatial dimension. Default: 'avg'.
        consensus (dict): Consensus config dict.
        dropout_ratio (float): Probability of dropout layer. Default: 0.4.
        init_std (float): Std value for Initiation. Default: 0.01.
        is_shift (bool): Indicating whether the feature is shifted.
            Default: True.
        temporal_pool (bool): Indicating whether feature is temporal pooled.
            Default: False.
        kwargs (dict, optional): Any keyword argument to be used to initialize
            the head.
    é   ÚCrossEntropyLoss)ÚtypeÚavgr   r   )r   Údimgš™™™™™é?gü©ñÒMbP?TFc                    sÆ   t ƒ j|||fi |¤Ž || _|| _|| _|| _|	| _|
| _| ¡ }| 	d¡}|dkr4t
di |¤Ž| _nd | _| jdkrEtj| jd�| _nd | _t | j| j¡| _| jdkr^t d¡| _d S d | _d S )Nr   r   r   )Úpr   r   © )ÚsuperÚ__init__Úspatial_typeÚdropout_ratioÚnum_segmentsÚinit_stdÚis_shiftÚtemporal_poolÚcopyÚpopr   Ú	consensusÚnnÚDropoutÚdropoutÚLinearÚin_channelsÚnum_classesÚfc_clsÚAdaptiveAvgPool2dÚavg_pool)Úselfr    r   r   Úloss_clsr   r   r   r   r   r   ÚkwargsÚ
consensus_Úconsensus_type©Ú	__class__r   ú4/root/disk_3tb/HAC/mmaction/models/heads/tsm_head.pyr      s&   



zTSMHead.__init__c                 C   s   t | j| jd� dS )z%Initiate the parameters from scratch.)ÚstdN)r   r!   r   )r$   r   r   r+   Úinit_weightsH   s   zTSMHead.init_weightsc                 C   s¤   | j dur
|   |¡}t |d¡}| jdur|  |¡}|  |¡}| jr8| jr8| d| jd f| 	¡ dd…  ¡}n| d| jf| 	¡ dd…  ¡}|  
|¡}| d¡S )a'  Defines the computation performed at every call.

        Args:
            x (torch.Tensor): The input data.
            num_segs (int): Useless in TSMHead. By default, `num_segs`
                is equal to `clip_len * num_clips * num_crops`, which is
                automatically generated in Recognizer forward phase and
                useless in TSM models. The `self.num_segments` we need is a
                hyper parameter to build TSM models.
        Returns:
            torch.Tensor: The classification scores for input samples.
        Nr   éÿÿÿÿr   )r#   ÚtorchÚflattenr   r!   r   r   Úviewr   Úsizer   Úsqueeze)r$   ÚxÚnum_segsÚ	cls_scorer   r   r+   ÚforwardL   s   




ÿÿ

zTSMHead.forward)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Údictr   r-   r7   Ú__classcell__r   r   r)   r+   r   	   s    
ö)r   )r/   Útorch.nnr   Úmmcv.cnnr   Úregistryr   Úbaser   r   Úregister_moduler   r   r   r   r+   Ú<module>   s    