o
    _{j¤  ã                   @   sr   d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z
 ddlmZ ddlmZ e ¡ G d	d
„ d
ejƒƒZdS )é    N)Ú
ConvModuleÚconstant_initÚkaiming_initÚnormal_init)Úload_checkpoint)Ú
_BatchNormé   )Úget_root_loggeré   )Ú	BACKBONESc                       s@   e Zd ZdZ							d‡ fdd„	Zdd	„ Zd
d„ Z‡  ZS )ÚC3Da”  C3D backbone.

    Args:
        pretrained (str | None): Name of pretrained model.
        style (str): ``pytorch`` or ``caffe``. If set to "pytorch", the
            stride-two layer is the 3x3 conv layer, otherwise the stride-two
            layer is the first 1x1 conv layer. Default: 'pytorch'.
        conv_cfg (dict | None): Config dict for convolution layer.
            If set to None, it uses ``dict(type='Conv3d')`` to construct
            layers. Default: None.
        norm_cfg (dict | None): Config for norm layers. required keys are
            ``type``, Default: None.
        act_cfg (dict | None): Config dict for activation layer. If set to
            None, it uses ``dict(type='ReLU')`` to construct layers.
            Default: None.
        dropout_ratio (float): Probability of dropout layer. Default: 0.5.
        init_std (float): Std value for Initiation of fc layers. Default: 0.01.
    NÚpytorchç      à?ç{®Gázt?c           	         s|  t ƒ  ¡  |d u rtdd�}|d u rtdd�}|| _|| _|| _|| _|| _|| _|| _	tdd| j| j| jd�}t
di |¤Ž| _tjd	d	d
�| _t
di |¤Ž| _tjddd
�| _t
di |¤Ž| _t
di |¤Ž| _tjddd
�| _t
di |¤Ž| _t
di |¤Ž| _tjddd
�| _t
di |¤Ž| _t
di |¤Ž| _tjdddd�| _t dd¡| _t dd¡| _t ¡ | _tj| jd�| _ d S )NÚConv3d)ÚtypeÚReLU)r   r   r   )é   r   r   )Úkernel_sizeÚpaddingÚconv_cfgÚnorm_cfgÚact_cfgr   é@   )r   r
   r
   )r   Ústrideé€   )r
   r
   r
   é   é   )r   r   r   )r   r   r   i    i   )Úp)r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )!ÚsuperÚ__init__ÚdictÚ
pretrainedÚstyler   r   r   Údropout_ratioÚinit_stdr   Úconv1aÚnnÚ	MaxPool3dÚpool1Úconv2aÚpool2Úconv3aÚconv3bÚpool3Úconv4aÚconv4bÚpool4Úconv5aÚconv5bÚpool5ÚLinearÚfc6Úfc7r   ÚreluÚDropoutÚdropout)	Úselfr"   r#   r   r   r   r$   r%   Zc3d_conv_param©Ú	__class__© ú3/root/disk_3tb/HAC/mmaction/models/backbones/c3d.pyr       sL   


ûÿ
zC3D.__init__c                 C   s¤   t | jtƒrtƒ }| d| j› �¡ t| | jd|d� dS | jdu rN|  ¡ D ]%}t |tjƒr3t	|ƒ q&t |tj
ƒrAt|| jd� q&t |tƒrKt|dƒ q&dS tdƒ‚)zPInitiate the parameters either from existing checkpoint or from
        scratch.zload model from: F)ÚstrictÚloggerN)Ústdr   z pretrained must be a str or None)Ú
isinstancer"   Ústrr	   Úinfor   Úmodulesr'   r   r   r5   r   r%   r   r   Ú	TypeError)r;   rA   Úmr>   r>   r?   Úinit_weightsT   s   



€ú	zC3D.init_weightsc                 C   s¼   |   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  	|¡}|  
|¡}|  |¡}|  |¡}|jdd�}|  |  |¡¡}|  |¡}|  |  |¡¡}|S )a+  Defines the computation performed at every call.

        Args:
            x (torch.Tensor): The input data.
                the size of x is (num_batches, 3, 16, 112, 112).

        Returns:
            torch.Tensor: The feature of the input
            samples extracted by the backbone.
        r   )Ú	start_dim)r&   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   Úflattenr8   r6   r:   r7   )r;   Úxr>   r>   r?   Úforwardi   s$   













zC3D.forward)Nr   NNNr   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    rI   rM   Ú__classcell__r>   r>   r<   r?   r   
   s    ù5r   )Útorch.nnr'   Zmmcv.cnnr   r   r   r   Úmmcv.runnerr   Z
mmcv.utilsr   Úutilsr	   Úregistryr   Úregister_moduleÚModuler   r>   r>   r>   r?   Ú<module>   s    