o
    ߥi                     @   s^   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G d	d
 d
ejZ	dS )    N   )Inception3D)TadaConvNeXtc                       (   e Zd ZdZ fddZdd Z  ZS )BaseVideoModela$  
    Standard video model.
    The model is divided into the backbone and the head, where the backbone
    extracts features and the head performs classification.

    The backbones can be defined in model/base/backbone.py or anywhere else
    as long as the backbone is registered by the BACKBONE_REGISTRY.
    The heads can be defined in model/module_zoo/heads/ or anywhere else
    as long as the head is registered by the HEAD_REGISTRY.

    The registries automatically finds the registered modules and construct
    the base video model.
    c                    s   t t|   |jjdkrt|| _n|jjdkrt|| _nd|jj}t	||j
jjdkr8t|| _dS |j
jjdkrFt|| _dS d|j
jj}t	|)G
        Args:
            cfg (Config): global config object.
        ConvNeXt_tinyS3DGz@backbone {} is not supported, ConvNeXt_tiny or S3DG is supportedBaseHeadAvgHeadz:head {} is not supported, BaseHead or AvgHead is supportedN)superr   __init__MODELNAMEr   backboner   formatNotImplementedErrorVIDEOHEADr
   headr   )selfcfg	error_str	__class__ b/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/action_recognition/models.pyr      s"   zBaseVideoModel.__init__c                 C   s   |  |}| |}|S )N)r   r   )r   xr   r   r   forward5   s   

zBaseVideoModel.forward__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r   
   s    r   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )r
   
    Constructs base head.
    c                    sP   t t|   || _|jjj}|jjj}|jjj	}|jjj
}| |||| dS )r   N)r   r
   r   r   r   BACKBONENUM_OUT_FEATURESr   NUM_CLASSESDROPOUT_RATE
ACTIVATION_construct_head)r   r   dimnum_classesdropout_rateactivation_funcr   r   r   r   @   s   



zBaseHead.__init__c                 C   sp   t d| _|dkrt || _t j||dd| _|dkr&t jdd| _d S |dkr1t 	 | _d S t
d	|)
Nr   g        T)biassoftmax)r,   sigmoidz-{} is not supported as an activationfunction.)nnAdaptiveAvgPool3dglobal_avg_poolDropoutdropoutLinearoutSoftmax
activationSigmoidr   r   )r   r,   r-   r.   r/   r   r   r   r+   P   s   zBaseHead._construct_headc                 C   sx   t |jdkr| |}|d}t| dr| |}n|}| |}| |}||jd d}|||jd dfS )N   r            r   r8   r   r2   )	lenshaper6   permutehasattrr8   r:   r<   viewr   r   r:   r   r   r   r   `   s   




zBaseHead.forward)r    r!   r"   r#   r   r+   r   r$   r   r   r   r   r
   ;   s
    r
   c                       r   )r   r%   c                    s$   t t|   || _td| _dS )r   r   N)r   r   r   r   r4   r5   r6   )r   r   r   r   r   r   t   s   zAvgHead.__init__c                 C   sJ   t |jdkr| |}|d}||jd d}|||jd dfS )Nr>   r?   r   r2   )rC   rD   r6   rE   rG   rH   r   r   r   r      s
   

zAvgHead.forwardr   r   r   r   r   r   o   s    r   )
torch.nnr4   s3dgr   tada_convnextr   Moduler   r
   r   r   r   r   r   <module>   s   14