Ë
    –\;jXE  ã                   ó,  — d dl 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	 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„Zdd„Zdd„Zy)é    )ÚpartialN)Únn)Úget_weights_path_from_urlé   )ÚConvNormActivationé   ©Ú_make_divisible)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                   ó\   ‡ — e Zd ZdZej
                  ej                  fˆ fd„	Zd„ Z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``.
    c                 óö   •— t         ‰| �  «        t        j                  d«      | _        t        j
                  ||d«      | _        t        j
                  ||d«      | _         |«       | _         |«       | _	        y )Nr   )
ÚsuperÚ__init__r   ÚAdaptiveAvgPool2DÚavgpoolÚConv2DÚfc1Úfc2Ú
activationÚscale_activation)ÚselfÚinput_channelsÚsqueeze_channelsr   r   Ú	__class__s        €úiG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/vision/models/mobilenetv3.pyr   zSqueezeExcitation.__init__4   s`   ø€ ô 	‰ÑÔÜ×+Ñ+¨AÓ.ˆŒÜ—9‘9˜^Ð-=¸qÓAˆŒÜ—9‘9Ð-¨~¸qÓAˆŒÙ$›,ˆŒÙ 0Ó 2ˆÕó    c                 ó¬   — | j                  |«      }| j                  |«      }| j                  |«      }| j                  |«      }| j	                  |«      S ©N)r   r   r   r   r   ©r   ÚinputÚscales      r   Ú_scalezSqueezeExcitation._scaleB   sI   € Ø—‘˜UÓ#ˆØ—‘˜“ˆØ—‘ Ó&ˆØ—‘˜“ˆØ×$Ñ$ UÓ+Ð+r   c                 ó.   — | j                  |«      }||z  S r    )r$   r!   s      r   ÚforwardzSqueezeExcitation.forwardI   s   € Ø—‘˜EÓ"ˆØ�u‰}Ðr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚReLUÚSigmoidr   r$   r&   Ú__classcell__©r   s   @r   r   r   &   s'   ø„ ñð" —7‘7ØŸ™õ3ò,ör   r   c                   ó(   — e Zd Z	 dd„Zedd„«       Zy)ÚInvertedResidualConfigc	                 ót  — | j                  ||¬«      | _        || _        | j                  ||¬«      | _        | j                  ||¬«      | _        || _        |€d | _        || _        y |dk(  rt        j                  | _        || _        y |dk(  rt        j                  | _        || _        y t        d|› �«      ‚)N)r#   ÚreluÚ	hardswishz*The activation function is not supported: )Úadjust_channelsÚin_channelsÚkernelÚexpanded_channelsÚout_channelsÚuse_seÚactivation_layerr   r+   Ú	HardswishÚRuntimeErrorÚstride)	r   r5   r6   r7   r8   r9   r   r=   r#   s	            r   r   zInvertedResidualConfig.__init__O   sÏ   € ð  ×/Ñ/°À5Ð/ÓIˆÔØˆŒØ!%×!5Ñ!5Ø Uð "6ó "
ˆÔð !×0Ñ0°ÀUÐ0ÓKˆÔØˆŒØÐØ$(ˆDÔ!ð ˆ�ð ˜6Ò!Ü$&§G¡GˆDÔ!ð ˆ�ð ˜;Ò&Ü$&§L¡LˆDÔ!ð
 ˆ�ô Ø<¸Z¸LÐIóð r   c                 ó    — t        | |z  d«      S )Né   r	   )Úchannelsr#   s     r   r4   z&InvertedResidualConfig.adjust_channelsm   s   € ä˜x¨%Ñ/°Ó3Ð3r   N)ç      ð?)r'   r(   r)   r   Ústaticmethodr4   © r   r   r0   r0   N   s    „ ð óð< ò4ó ñ4r   r0   c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚInvertedResidualc	           
      óš  •— t         ‰	| �  «        |dk(  xr ||k(  | _        || _        ||k7  | _        | j                  rt        ||ddd||¬«      | _        t        ||||t        |dz
  dz  «      |||¬«      | _        | j                  r-t        |t        |dz  «      t        j                  ¬«      | _        t        ||ddd|d ¬«      | _        y )Nr   r   )r5   r8   Úkernel_sizer=   ÚpaddingÚ
norm_layerr:   r   ©r5   r8   rG   r=   rH   ÚgroupsrI   r:   é   )r   )r   r   Úuse_res_connectr9   Úexpandr   Úexpand_convÚintÚbottleneck_convr   r
   r   ÚHardsigmoidÚmid_seÚlinear_conv)
r   r5   r7   r8   Úfilter_sizer=   r9   r:   rI   r   s
            €r   r   zInvertedResidual.__init__s   sé   ø€ ô 	‰ÑÔØ%¨™{ÒJ¨{¸lÑ/JˆÔØˆŒØ!Ð%6Ñ6ˆŒà�;Š;Ü1Ø'Ø.ØØØØ%Ø!1ô ˆDÔô  2Ø)Ø*Ø#ØÜ˜ q™¨QÑ.Ó/Ø$Ø!Ø-ô	 
ˆÔð �;Š;Ü+Ø!ÜÐ 1°QÑ 6Ó7Ü!#§¡ôˆDŒKô .Ø)Ø%ØØØØ!Ø!ô
ˆÕr   c                 ó  — |}| j                   r| j                  |«      }| j                  |«      }| j                  r| j	                  |«      }| j                  |«      }| j                  rt        j                  ||«      }|S r    )	rN   rO   rQ   r9   rS   rT   rM   ÚpaddleÚadd)r   ÚxÚidentitys      r   r&   zInvertedResidual.forwardª   sp   € ØˆØ�;Š;Ø× Ñ  Ó#ˆAØ× Ñ  Ó#ˆØ�;Š;Ø—‘˜A“ˆAØ×Ñ˜QÓˆØ×ÒÜ—
‘
˜8 QÓ'ˆAØˆr   )r'   r(   r)   r   r&   r-   r.   s   @r   rE   rE   r   s   ø„ ô5
ön
r   rE   c                   ó,   ‡ — e Zd ZdZ	 dˆ fd„	Z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.
    c                 óX  •— t         ‰| �  «        || _        || _        || _        || _        || _        |d   j                  | _        |d   j                  | _	        | j                  dz  | _
        t        t        j                  dd¬«      }t        d| j                  ddd	d	t        j                  |¬
«      | _        t        j"                  | j                  D �cg c][  }t%        |j                  |j&                  |j(                  |j*                  |j,                  |j.                  |j0                  |¬«      ‘Œ] c}Ž | _        t        | j                  | j                  d	d	dd	|t        j                  ¬«      | _        |rt        j6                  d	«      | _        |dkD  rŠt        j"                  t        j:                  | j                  | j                  «      t        j                  «       t        j<                  d¬«      t        j:                  | j                  |«      «      | _        y y c c}w )Nr   éÿÿÿÿé   gü©ñÒMbP?g®Gáz®ï?)ÚepsilonÚmomentumé   r   r   )r5   r8   rG   r=   rH   rK   r:   rI   )r5   r7   r8   rU   r=   r9   r:   rI   rJ   gš™™™™™É?)Úp) r   r   Úconfigr#   Úlast_channelÚnum_classesÚ	with_poolr5   Úfirstconv_in_channelsÚlastconv_in_channelsÚlastconv_out_channelsr   r   ÚBatchNorm2Dr   r;   ÚconvÚ
SequentialrE   r7   r8   r6   r=   r9   r:   ÚblocksÚlastconvr   r   ÚLinearÚDropoutÚ
classifier)	r   rd   re   r#   rf   rg   rI   Úcfgr   s	           €r   r   zMobileNetV3.__init__Ä   sÓ  ø€ ô 	‰ÑÔàˆŒØˆŒ
Ø(ˆÔØ&ˆÔØ"ˆŒØ%+¨A¡Y×%:Ñ%:ˆÔ"Ø$*¨2¡J×$:Ñ$:ˆÔ!Ø%)×%>Ñ%>ÀÑ%BˆÔ"ÜœRŸ^™^°UÀTÔJˆ
ä&ØØ×3Ñ3ØØØØÜŸ\™\Ø!ô	
ˆŒ	ô —m‘mð  Ÿ;š;óñ '�Cô !Ø #§¡Ø&)×&;Ñ&;Ø!$×!1Ñ!1Ø #§
¡
ØŸ:™:ØŸ:™:Ø%(×%9Ñ%9Ø)ö	ð 'ñð
ˆŒô  +Ø×1Ñ1Ø×3Ñ3ØØØØØ!ÜŸ\™\ô	
ˆŒñ Ü×/Ñ/°Ó2ˆDŒLà˜Š?Ü Ÿm™mÜ—	‘	˜$×4Ñ4°d×6GÑ6GÓHÜ—‘“Ü—
‘
˜SÔ!Ü—	‘	˜$×+Ñ+¨[Ó9ó	ˆD�Oð ùò;s   ÃA H'c                 ó  — | j                  |«      }| j                  |«      }| j                  |«      }| j                  r| j	                  |«      }| j
                  dkD  r't        j                  |d«      }| j                  |«      }|S )Nr   r   )	rl   rn   ro   rg   r   rf   rW   Úflattenrr   )r   rY   s     r   r&   zMobileNetV3.forward  so   € Ø�I‰I�a‹LˆØ�K‰K˜‹NˆØ�M‰M˜!Óˆà�>Š>Ø—‘˜Q“ˆAà×Ñ˜aÒÜ—‘˜q !Ó$ˆAØ—‘ Ó"ˆAàˆr   ©rA   iè  T)r'   r(   r)   r*   r   r&   r-   r.   s   @r   r\   r\   ·   s   ø„ ñ
ð LPõ>ö@r   r\   c                   ó$   ‡ — e Zd ZdZ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:: python

            >>> 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)
            [1, 1000]
    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
|«      g}t        d|z  d«      }t        ‰| �  |||||¬«       y )Né   rb   Tr2   r   éH   é   FéX   r   é   é`   é(   r3   éð   éx   é0   é�   i   i@  i   r?   ©re   r#   rg   rf   ©r0   r
   r   r   ©r   r#   rf   rg   rd   re   r   s         €r   r   zMobileNetV3Small.__init__0  s/  ø€ ä" 2 q¨"¨b°$¸ÀÀ5ÓIÜ" 2 q¨"¨b°%¸ÀÀEÓJÜ" 2 q¨"¨b°%¸ÀÀEÓJÜ" 2 q¨"¨b°$¸ÀQÈÓNÜ" 2 q¨#¨r°4¸ÀaÈÓOÜ" 2 q¨#¨r°4¸ÀaÈÓOÜ" 2 q¨#¨r°4¸ÀaÈÓOÜ" 2 q¨#¨r°4¸ÀaÈÓOÜ" 2 q¨#¨r°4¸ÀaÈÓOÜ" 2 q¨#¨r°4¸ÀaÈÓOÜ" 2 q¨#¨r°4¸ÀaÈÓOð
ˆô ' t¨e¡|°QÓ7ˆÜ‰ÑØØ%ØØØ#ð 	õ 	
r   rv   ©r'   r(   r)   r*   r   r-   r.   s   @r   rx   rx     s   ø„ ñ÷8
ñ 
r   rx   c                   ó$   ‡ — e Zd ZdZ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:: python

            >>> 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)
            [1, 1000]
    c                 óN  •— 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        ‰| �  |||||¬«       y )Nrz   rb   Fr2   r   é@   r|   r   r{   r~   r€   Tr‚   r�   éP   r3   éÈ   é¸   ià  ép   i   é    iÀ  i   r?   r…   r†   r‡   s         €r   r   zMobileNetV3Large.__init__e  s³  ø€ ä" 2 q¨"¨b°%¸ÀÀEÓJÜ" 2 q¨"¨b°%¸ÀÀEÓJÜ" 2 q¨"¨b°%¸ÀÀEÓJÜ" 2 q¨"¨b°$¸ÀÀ5ÓIÜ" 2 q¨#¨r°4¸ÀÀEÓJÜ" 2 q¨#¨r°4¸ÀÀEÓJÜ"Ø�A�s˜B  {°A°uóô #Ø�A�s˜B  {°A°uóô #Ø�A�s˜B  {°A°uóô #Ø�A�s˜B  {°A°uóô #Ø�A�s˜C  {°A°uóô #Ø�Q˜˜S $¨°Q¸óô #Ø�Q˜˜S $¨°Q¸óô #Ø�Q˜˜S $¨°Q¸óô #Ø�Q˜˜S $¨°Q¸óð?"
ˆôF ' t¨e¡|°QÓ7ˆÜ‰ÑØØ%ØØØ#ð 	õ 	
r   rv   rˆ   r.   s   @r   rŠ   rŠ   H  s   ø„ ñ÷8+
ñ +
r   rŠ   c                 ó  — | dk(  rt        dd|i|¤Ž}nt        dd|i|¤Ž}|r_| › d|› �} | t        v s
J | › d�«       ‚t        t        |    d   t        |    d   «      }t	        j
                  |«      }|j                  |«       |S )NÚmobilenet_v3_larger#   Ú_xzJ model do not have a pretrained model now, you should set pretrained=Falser   r   rC   )rŠ   rx   Ú
model_urlsr   rW   ÚloadÚset_dict)ÚarchÚ
pretrainedr#   ÚkwargsÚmodelÚweight_pathÚparams          r   Ú_mobilenet_v3rž   “  s¨   € ØÐ#Ò#Ü Ñ7 uÐ7°Ñ7‰ä Ñ7 uÐ7°Ñ7ˆÙØ��r˜%˜Ð!ˆà”JÑð	_àˆVÐ]Ð^ó	_Øä/Ü�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:: python

            >>> 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)
            [1, 1000]
    ©r#   r™   )Úmobilenet_v3_small©rž   ©r™   r#   rš   r›   s       r   r¡   r¡   ¦  ó+   € ôB ØðØ$)°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:: python

            >>> 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)
            [1, 1000]
    r    )r“   r¢   r£   s       r   r“   r“   Í  r¤   r   )FrA   )Ú	functoolsr   rW   r   Úpaddle.utils.downloadr   Úopsr   Ú_utilsr
   Ú__all__r•   ÚLayerr   r0   rE   r\   rx   rŠ   rž   r¡   r“   rC   r   r   Ú<module>r¬      s�   ðõ ã Ý Ý ;å $Ý #à
€ð ð ñ	€
ô%˜Ÿ™ô %÷P!4ñ !4ôHB�r—x‘xô BôJY�"—(‘(ô Yôx2
�{ô 2
ôjH
�{ô H
óVó&$ôN$r   