o
    _{jY  ã                   @   sh   d dl mZmZ d dl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
ed�ZdS )é    )ÚABCMetaÚabstractmethodNé   )Útop_k_accuracyé   )Ú
build_lossc                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )ÚAvgConsensusz‚Average consensus module.

    Args:
        dim (int): Decide which dim consensus function to apply.
            Default: 1.
    é   c                    s   t ƒ  ¡  || _d S ©N)ÚsuperÚ__init__Údim)Úselfr   ©Ú	__class__© ú0/root/disk_3tb/HAC/mmaction/models/heads/base.pyr      s   

zAvgConsensus.__init__c                 C   s   |j | jdd�S )ú0Defines the computation performed at every call.T)r   Úkeepdim)Úmeanr   ©r   Úxr   r   r   Úforward   s   zAvgConsensus.forward)r	   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__classcell__r   r   r   r   r   
   s    r   c                       sP   e Zd ZdZeddd�ddf‡ fdd„	Zed	d
„ ƒZedd„ ƒZdd„ Z	‡  Z
S )ÚBaseHeadaä  Base class for head.

    All Head should subclass it.
    All subclass should overwrite:
    - Methods:``init_weights``, initializing weights in some modules.
    - Methods:``forward``, supporting to forward both for training and testing.

    Args:
        num_classes (int): Number of classes to be classified.
        in_channels (int): Number of channels in input feature.
        loss_cls (dict): Config for building loss.
            Default: dict(type='CrossEntropyLoss', loss_weight=1.0).
        multi_class (bool): Determines whether it is a multi-class
            recognition task. Default: False.
        label_smooth_eps (float): Epsilon used in label smooth.
            Reference: arxiv.org/abs/1906.02629. Default: 0.
    ÚCrossEntropyLossg      ð?)ÚtypeZloss_weightFg        c                    s0   t ƒ  ¡  || _|| _t|ƒ| _|| _|| _d S r
   )r   r   Únum_classesÚin_channelsr   Úloss_clsÚmulti_classÚlabel_smooth_eps)r   r!   r"   r#   r$   r%   r   r   r   r   .   s   


zBaseHead.__init__c                 C   ó   dS )zPInitiate the parameters either from existing checkpoint or from
        scratch.Nr   )r   r   r   r   Úinit_weights;   ó    zBaseHead.init_weightsc                 C   r&   )r   Nr   r   r   r   r   r   @   r(   zBaseHead.forwardc                 K   sØ   t ƒ }|jt g ¡kr| d¡}| js>t| ¡  ¡  	¡ | ¡  ¡  	¡ dƒ}tj
|d |jd�|d< tj
|d |jd�|d< n| jdkrPd| j | | j| j  }| j||fi |¤Ž}t|t ƒrf| |¡ |S ||d< |S )a_  Calculate the loss given output ``cls_score``, target ``labels``.

        Args:
            cls_score (torch.Tensor): The output of the model.
            labels (torch.Tensor): The target output of the model.

        Returns:
            dict: A dict containing field 'loss_cls'(mandatory)
            and 'top1_acc', 'top5_acc'(optional).
        r   )r	   é   )ÚdeviceZtop1_accr	   Ztop5_accr#   )ÚdictÚshapeÚtorchÚSizeÚ	unsqueezer$   r   ÚdetachÚcpuÚnumpyÚtensorr*   r%   r!   r#   Ú
isinstanceÚupdate)r   Ú	cls_scoreÚlabelsÚkwargsÚlossesÚ	top_k_accr#   r   r   r   ÚlossD   s.   
ÿ

ÿ
ÿ

ÿ

þzBaseHead.loss)r   r   r   r   r+   r   r   r'   r   r;   r   r   r   r   r   r      s    
û

r   )Ú	metaclass)Úabcr   r   r-   Útorch.nnÚnnÚcorer   Úbuilderr   ÚModuler   r   r   r   r   r   Ú<module>   s    