
    |jH                       d dl mZ d dlmZ d dlmZmZmZ 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 d
dlmZ erd dlmZ  G d de          Zg ZdddZ G d dej                  Z G d d          Z G d dej                  Z G d dej                  Z G d de          Z G d de          Z	 	 d,d-d)Z	 	 d,d.d*Z	 	 d,d/d+Z dS )0    )annotations)partial)TYPE_CHECKINGCallable	TypedDict)NotRequiredUnpackN)nn)get_weights_path_from_url   )ConvNormActivation   _make_divisible)Tensorc                  $    e Zd ZU ded<   ded<   dS )_MobileNetV3OptionszNotRequired[int]num_classeszNotRequired[bool]	with_poolN)__name__
__module____qualname____annotations__     p/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/vision/models/mobilenetv3.pyr   r   $   s*         %%%%$$$$$$r   r   )zIhttps://paddle-hapi.bj.bcebos.com/models/mobilenet_v3_small_x1.0.pdparams 34fe0e7c1f8b00b2b056ad6788d0590c)zIhttps://paddle-hapi.bj.bcebos.com/models/mobilenet_v3_large_x1.0.pdparams 118db5792b4e183b925d8e8e334db3df)zmobilenet_v3_small_x1.0zmobilenet_v3_large_x1.0c                  N     e Zd ZdZej        ej        fd fd
ZddZddZ	 xZ
S )SqueezeExcitationa  
    This block implements the Squeeze-and-Excitation block from https://arxiv.org/abs/1709.01507 (see Fig. 1).
    Parameters ``activation``, and ``scale_activation`` correspond to ``delta`` and ``sigma`` in eq. 3.
    This code is based on the torchvision code with modifications.
    You can also see at https://github.com/pytorch/vision/blob/main/torchvision/ops/misc.py#L127

    Args:
        input_channels (int): Number of channels in the input image.
        squeeze_channels (int): Number of squeeze channels.
        activation (Callable[..., paddle.nn.Layer], optional): ``delta`` activation. Default: ``paddle.nn.ReLU``.
        scale_activation (Callable[..., paddle.nn.Layer]): ``sigma`` activation. Default: ``paddle.nn.Sigmoid``.
    input_channelsintsqueeze_channels
activationCallable[..., nn.Layer]scale_activationreturnNonec                "   t                                                       t          j        d          | _        t          j        ||d          | _        t          j        ||d          | _         |            | _         |            | _	        d S )Nr   )
super__init__r
   AdaptiveAvgPool2DavgpoolConv2Dfc1fc2r$   r&   )selfr!   r#   r$   r&   	__class__s        r   r+   zSqueezeExcitation.__init__E   s}     	+A..9^-=qAA9-~qAA$*,, 0 0 2 2r   inputr   c                    |                      |          }|                     |          }|                     |          }|                     |          }|                     |          S N)r-   r/   r$   r0   r&   r1   r3   scales      r   _scalezSqueezeExcitation._scaleS   sV    U##&&$$U+++r   c                6    |                      |          }||z  S r5   )r8   r6   s      r   forwardzSqueezeExcitation.forwardZ   s    E""u}r   )
r!   r"   r#   r"   r$   r%   r&   r%   r'   r(   )r3   r   r'   r   )r   r   r   __doc__r
   ReLUSigmoidr+   r8   r:   __classcell__r2   s   @r   r    r    7   s         " /1g46J3 3 3 3 3 3 3, , , ,       r   r    c                  2    e Zd Z	 dddZedd            ZdS )InvertedResidualConfig      ?in_channelsr"   kernelexpanded_channelsout_channelsuse_seboolr$   strstrider7   floatc	                p   |                      ||          | _        || _        |                      ||          | _        |                      ||          | _        || _        |d | _        nB|dk    rt          j        | _        n*|dk    rt          j	        | _        nt          d|           || _        d S )N)r7   relu	hardswishz*The activation function is not supported: )adjust_channelsrC   rD   rE   rF   rG   activation_layerr
   r<   	HardswishRuntimeErrorrJ   )	r1   rC   rD   rE   rF   rG   r$   rJ   r7   s	            r   r+   zInvertedResidualConfig.__init__`   s      //5/II!%!5!5U "6 "
 "
 !00U0KK$(D!!6!!$&GD!!;&&$&LD!!IZII   r   c                (    t          | |z  d          S )N   r   )channelsr7   s     r   rO   z&InvertedResidualConfig.adjust_channels~   s    x%/333r   N)rB   )rC   r"   rD   r"   rE   r"   rF   r"   rG   rH   r$   rI   rJ   r"   r7   rK   )r   r   r   r+   staticmethodrO   r   r   r   rA   rA   _   sR             < 4 4 4 \4 4 4r   rA   c                  (     e Zd Zd fdZddZ xZS )InvertedResidualrC   r"   rE   rF   filter_sizerJ   rG   rH   rP   r%   
norm_layerr'   r(   c	           
        t                                                       |dk    o||k    | _        || _        ||k    | _        | j        rt          ||ddd||          | _        t          ||||t          |dz
  dz            |||          | _        | j        r1t          |t          |dz            t          j                  | _        t          ||ddd|d           | _        d S )Nr   r   )rC   rF   kernel_sizerJ   paddingrZ   rP   r   rC   rF   r\   rJ   r]   groupsrZ   rP      )r&   )r*   r+   use_res_connectrG   expandr   expand_convr"   bottleneck_convr    r   r
   Hardsigmoidmid_selinear_conv)
r1   rC   rE   rF   rY   rJ   rG   rP   rZ   r2   s
            r   r+   zInvertedResidual.__init__   s,    	%{J{l/J!%66; 		1'.%!1     D  2)*#qQ.//$!-	 
 	 
 	 
 ; 	+! 1Q 677!#  DK .)%!!
 
 
r   xr   c                   |}| j         r|                     |          }|                     |          }| j        r|                     |          }|                     |          }| j        rt          j        ||          }|S r5   )	rb   rc   rd   rG   rf   rg   ra   paddleadd)r1   rh   identitys      r   r:   zInvertedResidual.forward   s    ; 	$  ##A  ##; 	AAQ 	(
8Q''Ar   )rC   r"   rE   r"   rF   r"   rY   r"   rJ   r"   rG   rH   rP   r%   rZ   r%   r'   r(   rh   r   r'   r   )r   r   r   r+   r:   r>   r?   s   @r   rX   rX      sR        5
 5
 5
 5
 5
 5
n
 
 
 
 
 
 
 
r   rX   c                  T     e Zd ZU dZded<   ded<   ded<   	 	 	 dd fdZddZ xZS )MobileNetV3aO  MobileNetV3 model from
    `"Searching for MobileNetV3" <https://arxiv.org/abs/1905.02244>`_.

    Args:
        config (list[InvertedResidualConfig]): MobileNetV3 depthwise blocks config.
        last_channel (int): The number of channels on the penultimate layer.
        scale (float, optional): Scale of channels in each layer. Default: 1.0.
        num_classes (int, optional): Output dim of last fc layer. If num_classes <=0, last fc layer
            will not be defined. Default: 1000.
        with_pool (bool, optional): Use pool before the last fc layer or not. Default: True.
    rK   r7   r"   r   rH   r   rB     Tconfiglist[InvertedResidualConfig]last_channelr'   r(   c           
     \   t                                                       || _        || _        || _        || _        || _        |d         j        | _        |d         j        | _	        | j	        dz  | _
        t          t          j        dd          t          d| j        ddd	d	t          j        
          | _        t          j        fd| j        D              | _        t          | j	        | j
        d	d	dd	t          j                  | _        |rt          j        d	          | _        |dk    rwt          j        t          j        | j
        | j                  t          j                    t          j        d          t          j        | j        |                    | _        d S d S )Nr      gMbP?gGz?)epsilonmomentum   r   r   )rC   rF   r\   rJ   r]   r_   rP   rZ   c                    g | ]<}t          |j        |j        |j        |j        |j        |j        |j                   =S ))rC   rE   rF   rY   rJ   rG   rP   rZ   )rX   rC   rE   rF   rD   rJ   rG   rP   ).0cfgrZ   s     r   
<listcomp>z(MobileNetV3.__init__.<locals>.<listcomp>   sc         ! #&)&;!$!1 #
::%(%9)	 	 	  r   r^   g?)p)r*   r+   rq   r7   rs   r   r   rC   firstconv_in_channelslastconv_in_channelslastconv_out_channelsr   r
   BatchNorm2Dr   rQ   conv
Sequentialblockslastconvr,   r-   LinearDropout
classifier)r1   rq   rs   r7   r   r   rZ   r2   s         @r   r+   zMobileNetV3.__init__   s    	
(&"%+AY%:"$*2J$:!%)%>%B"R^UTJJJ
&3\!	
 	
 	
	 m     ;  
  +13!\	
 	
 	
  	3/22DL?? m	$4d6GHH
S!!!	$+[99	 DOOO ?r   rh   r   c                &   |                      |          }|                     |          }|                     |          }| j        r|                     |          }| j        dk    r*t          j        |d          }|                     |          }|S )Nr   r   )	r   r   r   r   r-   r   rj   flattenr   )r1   rh   s     r   r:   zMobileNetV3.forward  s    IIaLLKKNNMM!> 	 QAaq!$$A""Ar   rB   rp   T)rq   rr   rs   r"   r7   rK   r   r"   r   rH   r'   r(   rm   )r   r   r   r;   r   r+   r:   r>   r?   s   @r   ro   ro      s         
 
 LLLOOO C C C C C C CJ       r   ro   c                  ,     e Zd ZdZ	 	 	 dd fdZ xZS )MobileNetV3Smalla  MobileNetV3 Small architecture model from
    `"Searching for MobileNetV3" <https://arxiv.org/abs/1905.02244>`_.

    Args:
        scale (float, optional): Scale of channels in each layer. Default: 1.0.
        num_classes (int, optional): Output dim of last fc layer. If num_classes <= 0, last fc layer
            will not be defined. Default: 1000.
        with_pool (bool, optional): Use pool before the last fc layer or not. Default: True.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of MobileNetV3 Small architecture model.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import MobileNetV3Small

            >>> # Build model
            >>> model = MobileNetV3Small(scale=1.0)

            >>> x = paddle.rand([1, 3, 224, 224])
            >>> out = model(x)

            >>> print(out.shape)
            paddle.Size([1, 1000])
    rB   rp   Tr7   rK   r   r"   r   rH   r'   r(   c                L   t          ddddddd|          t          ddddddd|          t          ddd	dddd
|          t          ddddddd|          t          ddddddd
|          t          ddddddd
|          t          ddddddd
|          t          ddddddd
|          t          ddddddd|          t          ddddddd
|          t          ddddddd
|          g}t          d|z  d          }t                                          |||||           d S )N   ry   TrM   r   H      FX   r      `   (   rN      x   0      i   i@  i   rT   rs   r7   r   r   rA   r   r*   r+   r1   r7   r   r   rq   rs   r2   s         r   r+   zMobileNetV3Small.__init__J  sn    #2q"b$5II"2q"b%EJJ"2q"b%EJJ"2q"b$QNN"2q#r4aOO"2q#r4aOO"2q#r4aOO"2q#r4aOO"2q#r4aOO"2q#r4aOO"2q#r4aOO
 'te|Q77%# 	 	
 	
 	
 	
 	
r   r   r7   rK   r   r"   r   rH   r'   r(   r   r   r   r;   r+   r>   r?   s   @r   r   r   -  sW         < 	
 
 
 
 
 
 
 
 
 
 
r   r   c                  ,     e Zd ZdZ	 	 	 dd fdZ xZS )MobileNetV3Largea  MobileNetV3 Large architecture model from
    `"Searching for MobileNetV3" <https://arxiv.org/abs/1905.02244>`_.

    Args:
        scale (float, optional): Scale of channels in each layer. Default: 1.0.
        num_classes (int, optional): Output dim of last fc layer. If num_classes <= 0, last fc layer
            will not be defined. Default: 1000.
        with_pool (bool, optional): Use pool before the last fc layer or not. Default: True.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of MobileNetV3 Large architecture model.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import MobileNetV3Large

            >>> # Build model
            >>> model = MobileNetV3Large(scale=1.0)

            >>> x = paddle.rand([1, 3, 224, 224])
            >>> out = model(x)

            >>> print(out.shape)
            paddle.Size([1, 1000])
    rB   rp   Tr7   rK   r   r"   r   rH   r'   r(   c                   t          ddddddd|          t          ddddddd|          t          ddd	dddd|          t          dd
d	dddd|          t          dd
ddddd|          t          dd
ddddd|          t          ddddddd|          t          ddddddd|          t          ddddddd|          t          ddddddd|          t          ddddddd|          t          ddddddd|          t          dd
ddddd|          t          dd
ddddd|          t          dd
ddddd|          g}t          d|z  d          }t                                          |||||           d S )Nr   ry   FrM   r   @   r   r   r   r   r   Tr   r   P   rN         i  p   i     i  i   rT   r   r   r   s         r   r+   zMobileNetV3Large.__init__  s    #2q"b%EJJ"2q"b%EJJ"2q"b%EJJ"2q"b$5II"2q#r4EJJ"2q#r4EJJ"AsB{Au  #AsB{Au  #AsB{Au  #AsB{Au  #AsC{Au  #QS$Q  #QS$Q  #QS$Q  #QS$Q ?"
F 'te|Q77%# 	 	
 	
 	
 	
 	
r   r   r   r   r?   s   @r   r   r   g  sW         < 	0
 0
 0
 0
 0
 0
 0
 0
 0
 0
 0
r   r   FrB   archrI   
pretrainedrH   r7   rK   kwargsUnpack[_MobileNetV3Options]r'   c                @   | dk    rt          dd|i|}nt          dd|i|}|rx|  d| } | t          v sJ |  d            t          t          |          d         t          |          d                   }t	          j        |          }|                    |           |S )Nmobilenet_v3_larger7   _xzJ model do not have a pretrained model now, you should set pretrained=Falser   r   r   )r   r   
model_urlsr   rj   loadset_dict)r   r   r7   r   modelweight_pathparams          r   _mobilenet_v3r     s     ### 77u777 77u777 
!!%!!z!!!___ "!! 0tQD!1!!4
 
 K((uLr   c                &    t          	 d|| d|}|S )a  MobileNetV3 Small architecture model from
    `"Searching for MobileNetV3" <https://arxiv.org/abs/1905.02244>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained on ImageNet. Default: False.
        scale (float, optional): Scale of channels in each layer. Default: 1.0.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`MobileNetV3Small <api_paddle_vision_models_MobileNetV3Small>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of MobileNetV3 Small architecture model.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import mobilenet_v3_small

            >>> # Build model
            >>> model = mobilenet_v3_small()

            >>> # Build model and load imagenet pretrained weight
            >>> # model = mobilenet_v3_small(pretrained=True)

            >>> # Build mobilenet v3 small model with scale=0.5
            >>> model = mobilenet_v3_small(scale=0.5)

            >>> x = paddle.rand([1, 3, 224, 224])
            >>> out = model(x)

            >>> print(out.shape)
            paddle.Size([1, 1000])
    mobilenet_v3_smallr7   r   )r   r   r   r7   r   r   s       r   r   r     5    J $)j DJ E Lr   c                &    t          	 d|| d|}|S )a  MobileNetV3 Large architecture model from
    `"Searching for MobileNetV3" <https://arxiv.org/abs/1905.02244>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained on ImageNet. Default: False.
        scale (float, optional): Scale of channels in each layer. Default: 1.0.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`MobileNetV3Large <api_paddle_vision_models_MobileNetV3Large>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of MobileNetV3 Large architecture model.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import mobilenet_v3_large

            >>> # Build model
            >>> model = mobilenet_v3_large()

            >>> # Build model and load imagenet pretrained weight
            >>> # model = mobilenet_v3_large(pretrained=True)

            >>> # Build mobilenet v3 large model with scale=0.5
            >>> model = mobilenet_v3_large(scale=0.5)

            >>> x = paddle.rand([1, 3, 224, 224])
            >>> out = model(x)

            >>> print(out.shape)
            paddle.Size([1, 1000])
    r   r   )r   r   r   s       r   r   r     r   r   )FrB   )
r   rI   r   rH   r7   rK   r   r   r'   ro   )r   rH   r7   rK   r   r   r'   r   )r   rH   r7   rK   r   r   r'   r   )!
__future__r   	functoolsr   typingr   r   r   typing_extensionsr   r	   rj   r
   paddle.utils.downloadr   opsr   _utilsr   r   r   __all__r   Layerr    rA   rX   ro   r   r   r   r   r   r   r   r   <module>r      s   # " " " " "                2 1 1 1 1 1 1 1        ; ; ; ; ; ; $ $ $ $ $ $ # # # # # # %% % % % %i % % %
   	 	
% % % % % % % %P!4 !4 !4 !4 !4 !4 !4 !4HB B B B Brx B B BJb b b b b"( b b bJ7
 7
 7
 7
 7
{ 7
 7
 7
tM
 M
 M
 M
 M
{ M
 M
 M
d     2 ( ( ( ( (X ( ( ( ( ( ( (r   