o
    ߥi((                     @   sr   d dl 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G dd	 d	ejZG d
d dejZdS )    Nc                       s,   e Zd ZdZ	d fdd	Zdd Z  ZS )InceptionBaseConv3Dz~
    Constructs basic inception 3D conv.
    Modified from https://github.com/TengdaHan/CoCLR/blob/main/backbone/s3dg.py.
    r   c                    sv   t t|   tj|||||dd| _t|| _tjdd| _	| jj
jjddd | jj
jd | jjj  d S )	NFkernel_sizestridepaddingbiasTinplacer   {Gz?meanstd   )superr   __init__nnConv3dconvBatchNorm3dbnReLUreluweightdatanormal_fill_r   zero_)selfcfg	in_planes
out_planesr   r   r   	__class__ `/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/action_recognition/s3dg.pyr      s    
zInceptionBaseConv3D.__init__c                 C   s"   |  |}| |}| |}|S N)r   r   r   r   xr#   r#   r$   forward&   s   


zInceptionBaseConv3D.forwardr   __name__
__module____qualname____doc__r   r(   __classcell__r#   r#   r!   r$   r      s
    r   c                       s(   e Zd ZdZ fddZdd Z  ZS )InceptionBlock3D
    Element constructing the S3D/S3DG.
    See models/base/backbone.py L99-186.

    Modified from https://github.com/TengdaHan/CoCLR/blob/main/backbone/s3dg.py.
    c                    s.  t t|   |jjjj}t|dksJ t|t	sJ |\}}}}}	}
t
t|||ddd| _t
t|||dddt|||dddd| _t
t|||dddt|||	dddd| _t
t
jddddt|||
ddd| _t|||	|
g| _|| _|rt|| _t|| _t|	| _t|
| _d S d S )N   r   r   r      r   r   r   r4   r4   r4   )r   r0   r   VIDEOBACKBONEBRANCHGATINGlen
isinstancelistr   
Sequentialr   branch0STConv3dbranch1branch2	MaxPool3dbranch3sumout_channelsgating
SelfGating	gating_b0	gating_b1	gating_b2	gating_b3)r   r   r   r    _gatingnum_out_0_0anum_out_1_0anum_out_1_0bnum_out_2_0anum_out_2_0bnum_out_3_0br!   r#   r$   r   5   sn   







zInceptionBlock3D.__init__c                 C   sn   |  |}| |}| |}| |}| jr+| |}| |}| |}| |}t	
||||fd}|S )Nr   )r?   rA   rB   rD   rG   rI   rJ   rK   rL   torchcat)r   r'   x0x1x2x3outr#   r#   r$   r(   k   s   







zInceptionBlock3D.forwardr*   r#   r#   r!   r$   r0   -   s    6r0   c                       s$   e Zd Z fddZdd Z  ZS )rH   c                    s    t t|   t||| _d S r%   )r   rH   r   r   Linearfc)r   	input_dimr!   r#   r$   r   }   s   zSelfGating.__init__c                 C   sD   t j|g dd}| |}t |}|dddddddf | S )zFeature gating as used in S3D-G)   r4      )dimN)rT   r   r\   sigmoid)r   input_tensorspatiotemporal_averageweightsr#   r#   r$   r(      s   

zSelfGating.forward)r+   r,   r-   r   r(   r/   r#   r#   r!   r$   rH   {   s    rH   c                       s8   e Zd ZdZ	d	 fdd	Z	d	ddZdd Z  ZS )
r@   r1   r   c              	      s`   t t|   t|tr|d }|d }n|}|jj| _t|jj	| _
| ||||||| d S )Nr   )r   r@   r   r<   tupleBNMOMENTUMbn_mmtfloatEPSbn_eps_construct_branch)r   r   r   r    r   r   r   t_strider!   r#   r$   r      s   


zSTConv3d.__init__c                 C   s   t j||d||fd||fd||fdd| _t j|||ddf|ddf|ddfdd| _t j|| j| jd| _t j|| j| jd| _t j	dd| _
| jjjjddd	 | jjjjddd	 | jjjd | jjj  | jjjd | jjj  d S )
Nr   r   Fr   )epsmomentumTr   r
   r   )r   r   conv1conv2r   rl   ri   bn1bn2r   r   r   r   r   r   r   r   )r   r   r   r    r   r   rn   r   r#   r#   r$   rm      sB   



zSTConv3d._construct_branchc                 C   s@   |  |}| |}| |}| |}| |}| |}|S r%   )rq   rs   r   rr   rt   r&   r#   r#   r$   r(      s   





zSTConv3d.forwardr)   )r+   r,   r-   r.   r   rm   r(   r/   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 )Inception3Dz~
    Backbone architecture for I3D/S3DG.
    Modified from https://github.com/TengdaHan/CoCLR/blob/main/backbone/s3dg.py.
    c                    s&   t t|   |jj}| || dS )zG
        Args:
            cfg (Config): global config object.
        N)r   ru   r   DATANUM_INPUT_CHANNELS_construct_backbone)r   r   _input_channelr!   r#   r$   r      s   zInception3D.__init__c                 C   s  t ||ddddd| _t| j| _tjdddd| _t|ddd	d	d
| _t |dddd	d	d| _	t| j| j| j	| _
tjdddd| _t|dg dd| _t|dg dd| _t| j| j| j| _tjdddd| _t|dg dd| _t|dg dd| _t|dg dd| _t|dg dd| _t|dg dd| _t| j| j| j| j| j| j| _tjdddd| _t|dg dd| _t|dg dd| _t| j| j| j| _d S )N@      r^   r4   r5   )r   r4   r4   )r   r^   r^   )r   r   r   r   r3      )rz   `             r   )r   r       )r~   r~   r|   r   r}   rz   r6   )r^   r^   r^   )r   r   r   i  )r|   r}      r   0   rz   i   )   p         rz   rz   )r~   r~   r   r   rz   rz   )r      i   r   rz   rz   i  )r   r   i@  r   r~   r~   )r   r   r   i@  )  r|   r   r   r~   r~   )r@   Conv_1ar   r>   block1rC   
MaxPool_2ar   Conv_2bConv_2cblock2
MaxPool_3ar0   Mixed_3bMixed_3cblock3
MaxPool_4aMixed_4bMixed_4cMixed_4dMixed_4eMixed_4fblock4
MaxPool_5aMixed_5bMixed_5cblock5)r   r   input_channelr#   r#   r$   rx      s   







	


zInception3D._construct_backbonec                 C   sH   t |tr	|d }| |}| |}| |}| |}| |}|S )Nvideo)r<   dictr   r   r   r   r   r&   r#   r#   r$   r(   (  s   





zInception3D.forward)r+   r,   r-   r.   r   rx   r(   r/   r#   r#   r!   r$   ru      s
    	Dru   )	rT   torch.nnr   Moduler   r0   rH   r@   ru   r#   r#   r#   r$   <module>   s   %NL