
    ёiH                       S SK Jr  S SKJr  S SKJrJrJr  S SKJ	r	J
r
  S SKrS SKJr  S SKJr  SS	KJr  S
SKJr  \(       a  S SKJr   " S S\5      r/ rSSS.r " S S\R.                  5      r " S S5      r " S S\R.                  5      r " S S\R.                  5      r " S S\5      r " S S\5      r  S!         S"S jjr  S!       S#S jjr  S!       S$S  jjr g)%    )annotations)partial)TYPE_CHECKINGCallable	TypedDict)NotRequiredUnpackN)nn)get_weights_path_from_url   )ConvNormActivation   _make_divisible)Tensorc                  *    \ rS rSr% S\S'   S\S'   Srg)_MobileNetV3Options$   zNotRequired[int]num_classeszNotRequired[bool]	with_pool N)__name__
__module____qualname____firstlineno____annotations____static_attributes__r       `/var/www/html/banglarbhumi/venv/lib/python3.13/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                     ^  \ rS rSrSr\R                  \R                  4         SU 4S jjjrSS jr	SS jr
SrU =r$ )	SqueezeExcitation7   a  
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U ]  5         [        R                  " S5      U l        [        R
                  " XS5      U l        [        R
                  " X!S5      U l        U" 5       U l        U" 5       U l	        g )Nr   )
super__init__r
   AdaptiveAvgPool2DavgpoolConv2Dfc1fc2
activationscale_activation)selfinput_channelssqueeze_channelsr-   r.   	__class__s        r   r'   SqueezeExcitation.__init__E   s[     	++A.99^qA99-qA$, 0 2r   c                    U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R	                  U5      $ N)r)   r+   r-   r,   r.   r/   inputscales      r   _scaleSqueezeExcitation._scaleS   sI    U#&$$U++r   c                ,    U R                  U5      nX!-  $ r5   )r9   r6   s      r   forwardSqueezeExcitation.forwardZ   s    E"}r   )r-   r)   r+   r,   r.   )
r0   intr1   r>   r-   Callable[..., nn.Layer]r.   r?   returnNone)r7   r   r@   r   )r   r   r   r   __doc__r
   ReLUSigmoidr'   r9   r<   r   __classcell__r2   s   @r   r#   r#   7   sa    " /1gg46JJ33 3 ,	3
 23 
3 3, r   r#   c                  V    \ rS rSr S               SS jjr\SS j5       rSrg)InvertedResidualConfig_   c	                B   U R                  XS9U l        X l        U R                  X8S9U l        U R                  XHS9U l        XPl        Uc  S U l        OFUS:X  a  [        R                  U l        O*US:X  a  [        R                  U l        O[        SU 35      eXpl        g )N)r8   relu	hardswishz*The activation function is not supported: )adjust_channelsin_channelskernelexpanded_channelsout_channelsuse_seactivation_layerr
   rC   	HardswishRuntimeErrorstride)	r/   rN   rO   rP   rQ   rR   r-   rV   r8   s	            r   r'   InvertedResidualConfig.__init__`   s      ///I!%!5!5 "6 "
 !000K$(D!6!$&GGD!;&$&LLD!<ZLI  r   c                    [        X-  S5      $ )N   r   )channelsr8   s     r   rM   &InvertedResidualConfig.adjust_channels~   s    x/33r   )rS   rP   rN   rO   rQ   rV   rR   N)      ?)rN   r>   rO   r>   rP   r>   rQ   r>   rR   boolr-   strrV   r>   r8   float)r   r   r   r   r'   staticmethodrM   r   r   r   r   rH   rH   _   sn       	
     < 4 4r   rH   c                  Z   ^  \ rS rSr                  SU 4S jjrSS jrSrU =r$ )InvertedResidual   c	                  > [         T	U ]  5         US:H  =(       a    X:H  U l        X`l        X:g  U l        U R                  (       a  [        UUSSSUUS9U l        [        UUUU[        US-
  S-  5      UUUS9U l        U R                  (       a*  [        U[        US-  5      [        R                  S9U l        [        UUSSSUS S9U l        g )Nr   r   )rN   rQ   kernel_sizerV   padding
norm_layerrS   r   rN   rQ   re   rV   rf   groupsrg   rS      )r.   )r&   r'   use_res_connectrR   expandr   expand_convr>   bottleneck_convr#   r   r
   Hardsigmoidmid_selinear_conv)
r/   rN   rP   rQ   filter_sizerV   rR   rS   rg   r2   s
            r   r'   InvertedResidual.__init__   s     	%{J{/J!6;;1'.%!1 D  2)*#qQ./$!-	 
 ;;+! 1Q 67!#DK .)%!!
r   c                $   UnU R                   (       a  U R                  U5      nU R                  U5      nU R                  (       a  U R	                  U5      nU R                  U5      nU R                  (       a  [        R                  " X!5      nU$ r5   )	rl   rm   rn   rR   rp   rq   rk   paddleadd)r/   xidentitys      r   r<   InvertedResidual.forward   sn    ;;  #A  #;;AAQ

8'Ar   )rn   rl   rm   rq   rp   rk   rR   )rN   r>   rP   r>   rQ   r>   rr   r>   rV   r>   rR   r]   rS   r?   rg   r?   r@   rA   rw   r   r@   r   )r   r   r   r   r'   r<   r   rE   rF   s   @r   rb   rb      sk    5
5
 5
 	5

 5
 5
 5
 25
 ,5
 
5
n
 
r   rb   c                  z   ^  \ rS rSr% SrS\S'   S\S'   S\S'      S           SU 4S	 jjjrSS
 jrSrU =r	$ )MobileNetV3   a+  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.
r_   r8   r>   r   r]   r   c                P  > [         TU ]  5         Xl        X0l        X l        X@l        XPl        US   R                  U l        US   R                  U l	        U R                  S-  U l
        [        [        R                  SSS9n[        SU R                  SSS	S	[        R                  US
9U l        [        R"                  " U R                   Vs/ s HY  n[%        UR                  UR&                  UR(                  UR*                  UR,                  UR.                  UR0                  US9PM[     sn6 U l        [        U R                  U R                  S	S	SS	U[        R                  S9U l        U(       a  [        R6                  " S	5      U l        US:  a  [        R"                  " [        R:                  " U R                  U R                  5      [        R                  " 5       [        R<                  " SS9[        R:                  " U R                  U5      5      U l        g g s  snf )Nr      gMbP?gGz?)epsilonmomentum   r   r   )rN   rQ   re   rV   rf   ri   rS   rg   )rN   rP   rQ   rr   rV   rR   rS   rg   rh   g?)p) r&   r'   configr8   last_channelr   r   rN   firstconv_in_channelslastconv_in_channelslastconv_out_channelsr   r
   BatchNorm2Dr   rT   conv
Sequentialrb   rP   rQ   rO   rV   rR   rS   blockslastconvr(   r)   LinearDropout
classifier)	r/   r   r   r8   r   r   rg   cfgr2   s	           r   r'   MobileNetV3.__init__   s    	
(&"%+AY%:%:"$*2J$:$:!%)%>%>%B"R^^UTJ
&33\\!	
	 mm  ;; 'C ! #&)&;&;!$!1!1 #

::::%(%9%9)	 '
  +1133!\\	
 //2DL? mm		$44d6G6GH

S!		$++[9	DO ;s   A H#c                    U R                  U5      nU R                  U5      nU R                  U5      nU R                  (       a  U R	                  U5      nU R
                  S:  a(  [        R                  " US5      nU R                  U5      nU$ )Nr   r   )	r   r   r   r   r)   r   ru   flattenr   )r/   rw   s     r   r<   MobileNetV3.forward  so    IIaLKKNMM!>>QAaq!$A"Ar   )r)   r   r   r   r   r   r   r   r   r   r   r8   r   r\   i  T)r   zlist[InvertedResidualConfig]r   r>   r8   r_   r   r>   r   r]   r@   rA   rz   )
r   r   r   r   rB   r   r'   r<   r   rE   rF   s   @r   r|   r|      s|    
 LO C,C C 	C
 C C 
C CJ r   r|   c                  H   ^  \ rS rSrSr   S       SU 4S jjjrSrU =r$ )MobileNetV3Smalli-  aU  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])
c                  > [        SSSSSSSU5      [        SSSSSSSU5      [        SSS	SSSS
U5      [        SSSSSSSU5      [        SSSSSSS
U5      [        SSSSSSS
U5      [        SSSSSSS
U5      [        SSSSSSS
U5      [        SSSSSSSU5      [        SSSSSSS
U5      [        SSSSSSS
U5      /n[        SU-  S5      n[        TU ]  UUUUUS9  g )N   r   TrK   r   H      FX   r      `   (   rL      x   0      i   i@  i   rY   r   r8   r   r   rH   r   r&   r'   r/   r8   r   r   r   r   r2   s         r   r'   MobileNetV3Small.__init__J  s1    #2q"b$5I"2q"b%EJ"2q"b%EJ"2q"b$QN"2q#r4aO"2q#r4aO"2q#r4aO"2q#r4aO"2q#r4aO"2q#r4aO"2q#r4aO
 'te|Q7%# 	 	
r   r   r   r8   r_   r   r>   r   r]   r@   rA   r   r   r   r   rB   r'   r   rE   rF   s   @r   r   r   -  sC    < 	

 
 	

 

 
r   r   c                  H   ^  \ rS rSrSr   S       SU 4S jjjrSrU =r$ )MobileNetV3Largeig  aU  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])
c                H  > [        SSSSSSSU5      [        SSSSSSSU5      [        SSS	SSSSU5      [        SS
S	SSSSU5      [        SS
SSSSSU5      [        SS
SSSSSU5      [        SSSSSSSU5      [        SSSSSSSU5      [        SSSSSSSU5      [        SSSSSSSU5      [        SSSSSSSU5      [        SSSSSSSU5      [        SS
SSSSSU5      [        SS
SSSSSU5      [        SS
SSSSSU5      /n[        SU-  S5      n[        TU ]  UUUUUS9  g )Nr   r   FrK   r   @   r   r   r   r   r   Tr   r   P   rL   r}      i  p   i     i  i   rY   r   r   r   s         r   r'   MobileNetV3Large.__init__  s    #2q"b%EJ"2q"b%EJ"2q"b%EJ"2q"b$5I"2q#r4EJ"2q#r4E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%# 	 	
r   r   r   r   r   rF   s   @r   r   r   g  sC    < 	0
0
 0
 	0

 
0
 0
r   r   c                   U S:X  a  [        SSU0UD6nO[        SSU0UD6nU(       ab  U  SU 3n U [        ;   d
   U  S35       e[        [        U    S   [        U    S   5      n[        R
                  " U5      nUR                  U5        U$ )Nmobilenet_v3_larger8   _xzJ model do not have a pretrained model now, you should set pretrained=Falser   r   r   )r   r   
model_urlsr   ru   loadset_dict)arch
pretrainedr8   kwargsmodelweight_pathparams          r   _mobilenet_v3r     s     ## 7u77 7u77r%!z! 	
f^_	
! 0tQD!1!!4
 K(uLr   c                "    [         SXS.UD6nU$ )as  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])
r8   r   )mobilenet_v3_smallr   r   r8   r   r   s       r   r   r     )    J $)DJE Lr   c                "    [         SXS.UD6nU$ )as  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   s       r   r   r     r   r   )Fr\   )
r   r^   r   r]   r8   r_   r   Unpack[_MobileNetV3Options]r@   r|   )r   r]   r8   r_   r   r   r@   r   )r   r]   r8   r_   r   r   r@   r   )!
__future__r   	functoolsr   typingr   r   r   typing_extensionsr   r	   ru   r
   paddle.utils.downloadr   opsr   _utilsr   r   r   __all__r   Layerr#   rH   rb   r|   r   r   r   r   r   r   r   r   <module>r      sQ   #   2   ; $ #%i %
   	
% %P!4 !4HBrxx BJb"(( bJ7
{ 7
tM
{ M
d 
  *	
 2 ((( *( 	(X ((( *( 	(r   