
    |jyU                       U d dl mZ d dlmZmZ d dlZd dlmZ d dlmZm	Z	m
Z
 d dlmZ ddlmZ erHd d	lmZmZ d d
lmZmZ d dlmZ d dlmZ ed         Zed         Z G d de          Z G d de          Zg ZddddddddZded<   d?d Zd@d%Z G d& d'ej                   Z! G d( d)ej                   Z" G d* d+ej                   Z#	 	 dAdBd6Z$	 dCdDd7Z%	 dCdDd8Z&	 dCdDd9Z'	 dCdDd:Z(	 dCdDd;Z)	 dCdDd<Z*	 dCdEd>Z+dS )F    )annotations)TYPE_CHECKINGCallableN)nn)AdaptiveAvgPool2DLinear	MaxPool2D)get_weights_path_from_url   )ConvNormActivation)Literal	TypedDict)NotRequiredUnpack)Tensor)Size2)shufflenet_v2_x0_25shufflenet_v2_x0_33shufflenet_v2_x0_5shufflenet_v2_x1_0shufflenet_v2_x1_5shufflenet_v2_x2_0shufflenet_v2_swish)reluswishc                  .    e Zd ZU ded<   ded<   ded<   dS )_ShuffleNetOptionsz#NotRequired[_ActivationType | None]actNotRequired[bool]	with_poolNotRequired[int]num_classesN__name__
__module____qualname____annotations__     q/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/vision/models/shufflenetv2.pyr   r   .   s6         0000$$$$%%%%%%r)   r   c                  $    e Zd ZU ded<   ded<   dS )_ShuffleNetSwishOptionsr   r    r!   r"   Nr#   r(   r)   r*   r,   r,   3   s*         $$$$%%%%%%r)   r,   )zEhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_x0_25.pdparams 1e509b4c140eeb096bb16e214796d03b)zEhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_x0_33.pdparams 3d7b3ab0eaa5c0927ff1026d31b729bd)zDhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_x0_5.pdparams 5e5cee182a7793c4e4c73949b1a71bd4)zDhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_x1_0.pdparams 122d42478b9e81eb49f8a9ede327b1a4)zDhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_x1_5.pdparams faced5827380d73531d0ee027c67826d)zDhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_x2_0.pdparams cd3dddcd8305e7bcd8ad14d1c69a5784)zEhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_swish.pdparams adde0aa3b023e5b0c94a68be1c394b84zdict[str, tuple[str, str]]
model_urlsr   _ActivationType | Nonereturnnn.Layer | Nonec                v    | dk    rt           j        S | dk    rt           j        S | d S t          d|            )Nr   r   z*The activation function is not supported: )r   SwishReLURuntimeError)r   s    r*   create_activation_layerr<   Z   sC    
g~~x	w	tMMMNNNr)   xr   groupsintc                    | j         dd         \  }}}}||z  }t          j        | |||||g          } t          j        | g d          } t          j        | ||||g          } | S )Nr      )shape)r   r         rA   )perm)rB   paddlereshape	transpose)r=   r>   
batch_sizenum_channelsheightwidthchannels_per_groups          r*   channel_shufflerN   e   s    ./gacl+Jfe%/ 		*f&8&%H	 	 	A
 	111A 	q\65 IJJJAHr)   c                  6     e Zd Zej        fd fd
ZddZ xZS )InvertedResidualin_channelsr?   out_channelsstrider   activation_layerCallable[..., nn.Layer]r6   Nonec           	        t                                                       t          |dz  |dz  dddd|          | _        t          |dz  |dz  d|d|dz  d           | _        t          |dz  |dz  dddd|          | _        d S )Nr   rC   r   rQ   rR   kernel_sizerS   paddingr>   rT   rD   )super__init__r   _conv_pw_conv_dw_conv_linearselfrQ   rR   rS   rT   	__class__s        r*   r\   zInvertedResidual.__init__w   s     	*#q(%*-
 
 
 +$)%*1$!
 
 
 /$)%*-
 
 
r)   inputsr   c                @   t          j        ||j        d         dz  |j        d         dz  gd          \  }}|                     |          }|                     |          }|                     |          }t          j        ||gd          }t          |d          S )NrC   r   )num_or_sectionsaxisrf   )rF   splitrB   r]   r^   r_   concatrN   ra   rc   x1x2outs        r*   forwardzInvertedResidual.forward   s    #\!_16<?a3GH
 
 
B
 ]]2]]2r""mRH1---sA&&&r)   
rQ   r?   rR   r?   rS   r   rT   rU   r6   rV   rc   r   r6   r   r$   r%   r&   r   r:   r\   rn   __classcell__rb   s   @r*   rP   rP   v   s^         57G"
 "
 "
 "
 "
 "
 "
H
' 
' 
' 
' 
' 
' 
' 
'r)   rP   c                  6     e Zd Zej        fd fd
ZddZ xZS )InvertedResidualDSrQ   r?   rR   rS   r   rT   rU   r6   rV   c           	        t                                                       t          ||d|d|d           | _        t          ||dz  dddd|          | _        t          ||dz  dddd|          | _        t          |dz  |dz  d|d|dz  d           | _        t          |dz  |dz  dddd|          | _        d S )NrD   rC   rX   r   r   )r[   r\   r   
_conv_dw_1_conv_linear_1
_conv_pw_2
_conv_dw_2_conv_linear_2r`   s        r*   r\   zInvertedResidualDS.__init__   s'    	 -#$!
 
 
 1#%*-
 
 
 -#%*-
 
 
 -$)%*1$!
 
 
 1$)%*-
 
 
r)   rc   r   c                $   |                      |          }|                     |          }|                     |          }|                     |          }|                     |          }t          j        ||gd          }t          |d          S )NrC   rg   r   )rw   rx   ry   rz   r{   rF   ri   rN   rj   s        r*   rn   zInvertedResidualDS.forward   s    __V$$  $$__V$$__R    $$mRH1---sA&&&r)   ro   rp   rq   rs   s   @r*   ru   ru      s^         57G7
 7
 7
 7
 7
 7
 7
r' ' ' ' ' ' ' 'r)   ru   c                  V     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 )ShuffleNetV2a  ShuffleNetV2 model from
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design" <https://arxiv.org/pdf/1807.11164.pdf>`_.

    Args:
        scale (float, optional): Scale of output channels. Default: True.
        act (str, optional): Activation function of neural network. Default: "relu".
        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 ShuffleNetV2 model.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import ShuffleNetV2

            >>> shufflenet_v2_swish = ShuffleNetV2(scale=1.0, act="swish")
            >>> x = paddle.rand([1, 3, 224, 224])
            >>> out = shufflenet_v2_swish(x)
            >>> print(out.shape)
            paddle.Size([1, 1000])
    floatscaler?   r"   boolr          ?r     Tr   r5   r6   rV   c           
        t                                                       || _        || _        || _        g d}t          |          }|dk    rg d}nY|dk    rg d}nN|dk    rg d}nC|dk    rg d	}n8|d
k    rg d}n-|dk    rg d}n"t          dt          |          z   dz             t          d|d         ddd|          | _	        t          ddd          | _        g | _        t          |          D ]\  }}	t          |	          D ]}
|
dk    ra|                     t!          ||dz            ||dz            d|          t          |dz             dz   t          |
dz             z             }n`|                     t#          ||dz            ||dz            d|          t          |dz             dz   t          |
dz             z             }| j                            |           t          |d         |d         ddd|          | _        |rt)          d          | _        |dk    r*|d         | _        t/          |d         |          | _        d S d S )N)rA      rA         ?)   r   0   `      Q?)r   r       @      r         ?)r   r   r   r         r   )r   r   t      i  r         ?)r   r      i`  i  r          @)r   r      i  i  i   zThis scale size:[z] is not implemented!rD   rC   r   )rQ   rR   rY   rS   rZ   rT   )rY   rS   rZ   r   )rQ   rR   rS   rT   _)sublayernamer   )r[   r\   r   r"   r    r<   NotImplementedErrorstrr   _conv1r	   	_max_pool_block_list	enumeraterangeadd_sublayerru   rP   append
_last_convr   _pool2d_avg_out_cr   _fc)ra   r   r   r"   r    stage_repeatsrT   stage_out_channelsstage_id
num_repeatiblockrb   s               r*   r\   zShuffleNetV2.__init__  s    	
&"!		2377D==!:!:!:d]]!;!;!;c\\!<!<!<c\\!>!>!>c\\!>!>!>c\\!>!>!>%#c%jj03JJ   )+A.-
 
 
 #qAFFF $-m$<$< 	/ 	/ Hj:&& / /66 --!3(:8a<(H);HqL)I#$-=	" " " !A..4s1q5zzA .  EE !--!1(:8a<(H);HqL)I#$-=	" " " !A..4s1q5zzA .  E  ''....+/. -*2.+B/-
 
 
  	4033D ??,R0DK04kBBDHHH ?r)   rc   r   c                T   |                      |          }|                     |          }| j        D ]} ||          }|                     |          }| j        r|                     |          }| j        dk    r,t          j        |dd          }| 	                    |          }|S )Nr   rC   r   )
start_axis	stop_axis)
r   r   r   r   r    r   r"   rF   flattenr   )ra   rc   r=   invs       r*   rn   zShuffleNetV2.forward`  s    KKNN1# 	 	CAAAOOA> 	$  ##AaqQ"===AAr)   )r   r   r   T)
r   r   r   r5   r"   r?   r    r   r6   rV   rp   )r$   r%   r&   __doc__r'   r\   rn   rr   rs   s   @r*   r~   r~      s          4 LLLOOO &,RC RC RC RC RC RC RCh       r)   r~   Fr   arch_ShuffleNetArch
pretrainedr   r   r   kwargsUnpack[_ShuffleNetOptions]c                   t          dd|i|}|rq| t          v sJ |  d            t          t          |          d         t          |          d                   }t          j        |          }|                    |           |S )Nr   zJ model do not have a pretrained model now, you should set pretrained=Falser   rC   r(   )r~   r4   r
   rF   loadset_dict)r   r   r   r   modelweight_pathparams          r*   _shufflenet_v2r   p  s     //u///E 	z!!!___ "!! 0tQD!1!!4
 
 K((uLr)   c                "    t          	 dd| d|S )aa  ShuffleNetV2 with 0.25x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design" <https://arxiv.org/pdf/1807.11164.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
                            on ImageNet. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`ShuffleNetV2 <api_paddle_vision_models_ShuffleNetV2>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of ShuffleNetV2 with 0.25x output channels.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import shufflenet_v2_x0_25

            >>> # build model
            >>> model = shufflenet_v2_x0_25()

            >>> # build model and load imagenet pretrained weight
            >>> # model = shufflenet_v2_x0_25(pretrained=True)

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    r   r   r   r   )r   r   r   r   s     r*   r   r     1    @ %)j DJ  r)   c                "    t          	 dd| d|S )aa  ShuffleNetV2 with 0.33x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design" <https://arxiv.org/pdf/1807.11164.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
                            on ImageNet. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`ShuffleNetV2 <api_paddle_vision_models_ShuffleNetV2>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of ShuffleNetV2 with 0.33x output channels.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import shufflenet_v2_x0_33

            >>> # build model
            >>> model = shufflenet_v2_x0_33()

            >>> # build model and load imagenet pretrained weight
            >>> # model = shufflenet_v2_x0_33(pretrained=True)

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    r   r   r   )r   r   r   s     r*   r   r     r   r)   c                "    t          	 dd| d|S )a\  ShuffleNetV2 with 0.5x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design" <https://arxiv.org/pdf/1807.11164.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
                            on ImageNet. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`ShuffleNetV2 <api_paddle_vision_models_ShuffleNetV2>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of ShuffleNetV2 with 0.5x output channels.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import shufflenet_v2_x0_5

            >>> # build model
            >>> model = shufflenet_v2_x0_5()

            >>> # build model and load imagenet pretrained weight
            >>> # model = shufflenet_v2_x0_5(pretrained=True)

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    r   r   r   )r   r   r   s     r*   r   r     1    @ $'J BH  r)   c                "    t          	 dd| d|S )a\  ShuffleNetV2 with 1.0x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design" <https://arxiv.org/pdf/1807.11164.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
                            on ImageNet. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`ShuffleNetV2 <api_paddle_vision_models_ShuffleNetV2>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of ShuffleNetV2 with 1.0x output channels.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import shufflenet_v2_x1_0

            >>> # build model
            >>> model = shufflenet_v2_x1_0()

            >>> # build model and load imagenet pretrained weight
            >>> # model = shufflenet_v2_x1_0(pretrained=True)

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    r   r   r   )r   r   r   s     r*   r   r     r   r)   c                "    t          	 dd| d|S )a\  ShuffleNetV2 with 1.5x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design" <https://arxiv.org/pdf/1807.11164.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
                            on ImageNet. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`ShuffleNetV2 <api_paddle_vision_models_ShuffleNetV2>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of ShuffleNetV2 with 1.5x output channels.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import shufflenet_v2_x1_5

            >>> # build model
            >>> model = shufflenet_v2_x1_5()

            >>> # build model and load imagenet pretrained weight
            >>> # model = shufflenet_v2_x1_5(pretrained=True)

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    r   r   r   )r   r   r   s     r*   r   r     r   r)   c                "    t          	 dd| d|S )a\  ShuffleNetV2 with 2.0x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design" <https://arxiv.org/pdf/1807.11164.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
                            on ImageNet. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`ShuffleNetV2 <api_paddle_vision_models_ShuffleNetV2>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of ShuffleNetV2 with 2.0x output channels.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import shufflenet_v2_x2_0

            >>> # build model
            >>> model = shufflenet_v2_x2_0()

            >>> # build model and load imagenet pretrained weight
            >>> # model = shufflenet_v2_x2_0(pretrained=True)

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    r   r   r   )r   r   r   s     r*   r   r   =  r   r)   Unpack[_ShuffleNetSwishOptions]c                $    t          	 ddd| d|S )ai  ShuffleNetV2 with swish activation function, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design" <https://arxiv.org/pdf/1807.11164.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
                            on ImageNet. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`ShuffleNetV2 <api_paddle_vision_models_ShuffleNetV2>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of ShuffleNetV2 with swish activation function.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import shufflenet_v2_swish

            >>> # build model
            >>> model = shufflenet_v2_swish()

            >>> # build model and load imagenet pretrained weight
            >>> # model = shufflenet_v2_swish(pretrained=True)

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    r   r   r   )r   r   r   )r   r   r   s     r*   r   r   b  s7    @ 	 
   r)   )r   r5   r6   r7   )r=   r   r>   r?   r6   r   )Fr   )
r   r   r   r   r   r   r   r   r6   r~   )F)r   r   r   r   r6   r~   )r   r   r   r   r6   r~   ),
__future__r   typingr   r   rF   r   	paddle.nnr   r   r	   paddle.utils.downloadr
   opsr   r   r   typing_extensionsr   r   r   paddle._typingr   r   _ActivationTyper   r,   __all__r4   r'   r<   rN   LayerrP   ru   r~   r   r   r   r   r   r   r   r   r(   r)   r*   <module>r      sm   # " " " " " " * * * * * * * *        : : : : : : : : : : ; ; ; ; ; ; $ $ $ $ $ $ &))))))))55555555$$$$$$	O o.O& & & & &Y & & &
& & & & &) & & &
 3* *
    @O O O O   "/' /' /' /' /'rx /' /' /'dB' B' B' B' B' B' B' B'J@ @ @ @ @28 @ @ @J     * " " " " "L " " " " "L " " " " "L " " " " "L " " " " "L " " " " "L & & & & & & &r)   