o
    _{jÛ*  ã                   @   sœ   d dl mZ d dlm  mZ d dl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 dd
d„ZG dd„ dejƒZe ¡ G dd„ dejƒƒZdS )é    N)Ú
ConvModuleÚconstant_initÚkaiming_init)Úload_checkpoint)Ú
_BatchNormé   )Úget_root_loggeré   )Ú	BACKBONESçÍÌÌÌÌÌì?c                 C   sB   |du r|}t |t| |d  ƒ| | ƒ}|||  k r||7 }|S )aw  Make divisible function.

    This function rounds the channel number down to the nearest value that can
    be divisible by the divisor.
    Args:
        value (int): The original channel number.
        divisor (int): The divisor to fully divide the channel number.
        min_value (int, optional): The minimum value of the output channel.
            Default: None, means that the minimum value equal to the divisor.
        min_ratio (float, optional): The minimum ratio of the rounded channel
            number to the original channel number. Default: 0.9.
    Returns:
        int: The modified output channel number
    Nr	   )ÚmaxÚint)ÚvalueÚdivisorÚ	min_valueÚ	min_ratioÚ	new_value© r   ú</root/disk_3tb/HAC/mmaction/models/backbones/mobilenet_v2.pyÚmake_divisible   s   r   c                       s>   e Zd ZdZdedd�edd�df‡ fdd„	Zd	d
„ Z‡  ZS )ÚInvertedResiduala’  InvertedResidual block for MobileNetV2.

    Args:
        in_channels (int): The input channels of the InvertedResidual block.
        out_channels (int): The output channels of the InvertedResidual block.
        stride (int): Stride of the middle (first) 3x3 convolution.
        expand_ratio (int): adjusts number of channels of the hidden layer
            in InvertedResidual by this amount.
        conv_cfg (dict): Config dict for convolution layer.
            Default: None, which means using conv2d.
        norm_cfg (dict): Config dict for normalization layer.
            Default: dict(type='BN').
        act_cfg (dict): Config dict for activation layer.
            Default: dict(type='ReLU6').
        with_cp (bool): Use checkpoint or not. Using checkpoint will save some
            memory while slowing down the training speed. Default: False.
    Returns:
        Tensor: The output tensor
    NÚBN©ÚtypeÚReLU6Fc	                    sÀ   t t| ƒ ¡  || _|dv sJ d|› d�ƒ‚|| _| jdko!||k| _tt|| ƒƒ}	g }
|dkr>|
 t	||	d|||d�¡ |
 
t	|	|	d|d|	|||d�	t	|	|d||d d�g¡ tj|
Ž | _d S )N)é   r	   z$stride must in [1, 2]. But received Ú.r   )Úin_channelsÚout_channelsÚkernel_sizeÚconv_cfgÚnorm_cfgÚact_cfgr   )	r   r   r   ÚstrideÚpaddingÚgroupsr    r!   r"   )Úsuperr   Ú__init__r#   Úwith_cpÚuse_res_connectr   ÚroundÚappendr   ÚextendÚnnÚ
SequentialÚconv)Úselfr   r   r#   Úexpand_ratior    r!   r"   r(   Ú
hidden_dimÚlayers©Ú	__class__r   r   r'   9   sT   	
ÿúÿ÷
úõzInvertedResidual.__init__c                    s4   ‡ fdd„}ˆ j r|jrt ||¡}|S ||ƒ}|S )Nc                    s   ˆ j r
| ˆ  | ¡ S ˆ  | ¡S ©N)r)   r/   )Úx©r0   r   r   Ú_inner_forwardk   s   
z0InvertedResidual.forward.<locals>._inner_forward)r(   Úrequires_gradÚcpÚ
checkpoint)r0   r7   r9   Úoutr   r8   r   Úforwardi   s   þzInvertedResidual.forward)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Údictr'   r>   Ú__classcell__r   r   r4   r   r   $   s    ø0r   c                
       s¦   e Zd ZdZg d¢g d¢g d¢g d¢g d¢g d¢g d¢gZd	d
ddedd�eddd�eddd�ddf	‡ fdd„	Zdd„ Zdd„ Zdd„ Z	dd„ Z
d!‡ fdd „	Z‡  ZS )"ÚMobileNetV2aw  MobileNetV2 backbone.

    Args:
        pretrained (str | None): Name of pretrained model. Default: None.
        widen_factor (float): Width multiplier, multiply number of
            channels in each layer by this amount. Default: 1.0.
        out_indices (None or Sequence[int]): Output from which stages.
            Default: (7, ).
        frozen_stages (int): Stages to be frozen (all param fixed).
            Default: -1, which means not freezing any parameters.
        conv_cfg (dict): Config dict for convolution layer.
            Default: None, which means using conv2d.
        norm_cfg (dict): Config dict for normalization layer.
            Default: dict(type='BN').
        act_cfg (dict): Config dict for activation layer.
            Default: dict(type='ReLU6').
        norm_eval (bool): Whether to set norm layers to eval mode, namely,
            freeze running stats (mean and var). Note: Effect on Batch Norm
            and its variants only. Default: False.
        with_cp (bool): Use checkpoint or not. Using checkpoint will save some
            memory while slowing down the training speed. Default: False.
    )r   é   r   r   )é   é   r	   r	   )rG   é    r   r	   )rG   é@   é   r	   )rG   é`   r   r   )rG   é    r   r	   )rG   i@  r   r   Nç      ð?)é   éÿÿÿÿÚConvr   ÚBN2dT)r   r:   r   )r   ÚinplaceFc
              
      s‚  t ƒ  ¡  || _|| _|| _|D ]}
|
tddƒvr td|
› �ƒ‚q|tddƒvr/td|› �ƒ‚|| _|| _|| _|| _	|| _
|| _|	| _td| dƒ| _td| jddd	| j| j	| j
d
�| _g | _t| jƒD ]-\}}|\}}}}t|| dƒ}| j||||d�}d|d	 › �}|  ||¡ | j |¡ qe|dkrŸtd| ƒ| _nd| _t| j| jd	d	d| j| j	| j
d
�}|  d|¡ | j d¡ d S )Nr   é   z:the item in out_indices must in range(0, 8). But received rP   z4frozen_stages must be in range(-1, 8). But received rI   r   r	   r   )r   r   r   r#   r$   r    r!   r"   )r   Ú
num_blocksr#   r1   ÚlayerrN   i   Úconv2)r&   r'   Ú
pretrainedÚwiden_factorÚout_indicesÚrangeÚ
ValueErrorÚfrozen_stagesr    r!   r"   Ú	norm_evalr(   r   r   r   Úconv1r3   Ú	enumerateÚarch_settingsÚ
make_layerÚ
add_moduler+   r   Zout_channel)r0   rX   rY   rZ   r]   r    r!   r"   r^   r(   ÚindexÚiZ	layer_cfgr1   ÚchannelrU   r#   r   Zinverted_res_layerÚ
layer_namerV   r4   r   r   r'   ˜   sx   

ÿÿÿø
üø	zMobileNetV2.__init__c                 C   sV   g }t |ƒD ]}|dkrd}| t| j|||| j| j| j| jd�¡ || _qtj	|Ž S )a�  Stack InvertedResidual blocks to build a layer for MobileNetV2.

        Args:
            out_channels (int): out_channels of block.
            num_blocks (int): number of blocks.
            stride (int): stride of the first block. Default: 1
            expand_ratio (int): Expand the number of channels of the
                hidden layer in InvertedResidual by this ratio. Default: 6.
        r   )r1   r    r!   r"   r(   )
r[   r+   r   r   r    r!   r"   r(   r-   r.   )r0   r   rU   r#   r1   r3   re   r   r   r   rb   á   s$   
øÿ

zMobileNetV2.make_layerc                 C   s|   t | jtƒrtƒ }t| | jd|d� d S | jd u r:|  ¡ D ]}t |tjƒr*t|ƒ qt |t	tj
fƒr7t|dƒ qd S tdƒ‚)NF)ÚstrictÚloggerr   z pretrained must be a str or None)Ú
isinstancerX   Ústrr   r   Úmodulesr-   ÚConv2dr   r   Ú	GroupNormr   Ú	TypeError)r0   ri   Úmr   r   r   Úinit_weightsý   s   


€üzMobileNetV2.init_weightsc                 C   sd   |   |¡}g }t| jƒD ]\}}t| |ƒ}||ƒ}|| jv r#| |¡ qt|ƒdkr.|d S t|ƒS )Nr   r   )r_   r`   r3   ÚgetattrrZ   r+   ÚlenÚtuple)r0   r7   Úoutsre   rg   rV   r   r   r   r>   
  s   



€zMobileNetV2.forwardc                 C   sf   | j dkr| j ¡ D ]}d|_q
td| j d ƒD ]}t| d|› �ƒ}| ¡  | ¡ D ]}d|_q*qd S )Nr   Fr   rV   )r]   r_   Ú
parametersr:   r[   rr   Úeval)r0   Úparamre   rV   r   r   r   Ú_freeze_stages  s   
ÿýzMobileNetV2._freeze_stagesc                    sN   t t| ƒ |¡ |  ¡  |r!| jr#|  ¡ D ]}t|tƒr | ¡  qd S d S d S r6   )	r&   rE   Útrainry   r^   rl   rj   r   rw   )r0   Úmoderp   r4   r   r   rz   #  s   

€ýzMobileNetV2.train)T)r?   r@   rA   rB   ra   rC   r'   rb   rq   r>   ry   rz   rD   r   r   r4   r   rE   y   s*    þ

÷I
rE   )Nr   )Útorch.nnr-   Útorch.utils.checkpointÚutilsr<   r;   Úmmcv.cnnr   r   r   Úmmcv.runnerr   Útorch.nn.modules.batchnormr   r   Úbuilderr
   r   ÚModuler   Úregister_modulerE   r   r   r   r   Ú<module>   s    
U