o
    + iU                     @  s  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red 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 deZG dd deZg ZddddddddZded< dId d!ZdJd&d'ZG d(d) d)ej Z!G d*d+ d+ej Z"G d,d- d-ej Z#	.	/dKdLd8d9Z$	.dMdNd:d;Z%	.dMdNd<d=Z&	.dMdNd>d?Z'	.dMdNd@dAZ(	.dMdNdBdCZ)	.dMdNdDdEZ*	.dMdOdGdHZ+dS )P    )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                   @  s&   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__ r&   r&   m/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/vision/models/shufflenetv2.pyr   .   s   
 r   c                   @  s   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(   )zEhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_x0_25.pdparamsZ 1e509b4c140eeb096bb16e214796d03b)zEhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_x0_33.pdparamsZ 3d7b3ab0eaa5c0927ff1026d31b729bd)zDhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_x0_5.pdparamsZ 5e5cee182a7793c4e4c73949b1a71bd4)zDhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_x1_0.pdparamsZ 122d42478b9e81eb49f8a9ede327b1a4)zDhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_x1_5.pdparamsZ faced5827380d73531d0ee027c67826d)zDhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_x2_0.pdparamsZ cd3dddcd8305e7bcd8ad14d1c69a5784)zEhttps://paddle-hapi.bj.bcebos.com/models/shufflenet_v2_swish.pdparamsZ adde0aa3b023e5b0c94a68be1c394b84zdict[str, tuple[str, str]]
model_urlsr   _ActivationType | Nonereturnnn.Layer | Nonec                 C  s6   | dkrt jS | dkrt jS | d u rd S td|  )Nr   r   z*The activation function is not supported: )r   ZSwishReLURuntimeError)r   r&   r&   r'   create_activation_layerZ   s   r/   xr   groupsintc                 C  sb   | j dd \}}}}|| }tj| |||||gd} tj| g dd} tj| ||||gd} | S )Nr      )shape)r   r
         r3   )perm)r4   paddleZreshapeZ	transpose)r0   r1   Z
batch_sizeZnum_channelsheightwidthZchannels_per_groupr&   r&   r'   channel_shufflee   s   r;   c                      .   e Zd Zejfd fd
dZdddZ  ZS )InvertedResidualin_channelsr2   out_channelsstrider   activation_layerCallable[..., nn.Layer]r+   Nonec              	     sr   t    t|d |d dddd|d| _t|d |d d|d|d d d| _t|d |d dddd|d| _d S )Nr
   r5   r   r>   r?   kernel_sizer@   paddingr1   rA   r6   )super__init__r   _conv_pw_conv_dw_conv_linearselfr>   r?   r@   rA   	__class__r&   r'   rH   w   s8   
		zInvertedResidual.__init__inputsr   c                 C  sf   t j||jd d |jd d gdd\}}| |}| |}| |}t j||gdd}t|dS )Nr5   r
   )Znum_or_sectionsaxisrQ   )r8   splitr4   rI   rJ   rK   concatr;   rM   rP   x1Zx2outr&   r&   r'   forward   s   




zInvertedResidual.forward
r>   r2   r?   r2   r@   r   rA   rB   r+   rC   rP   r   r+   r   r"   r#   r$   r   r-   rH   rX   __classcell__r&   r&   rN   r'   r=   v   s    $r=   c                      r<   )InvertedResidualDSr>   r2   r?   r@   r   rA   rB   r+   rC   c              	     s   t    t||d|d|d d| _t||d dddd|d| _t||d dddd|d| _t|d |d d|d|d d d| _t|d |d dddd|d| _d S )Nr6   r5   rD   r
   r   )rG   rH   r   
_conv_dw_1_conv_linear_1
_conv_pw_2
_conv_dw_2_conv_linear_2rL   rN   r&   r'   rH      s\   
	
		zInvertedResidualDS.__init__rP   r   c                 C  sN   |  |}| |}| |}| |}| |}tj||gdd}t|dS )Nr5   rR   r
   )r^   r_   r`   ra   rb   r8   rT   r;   rU   r&   r&   r'   rX      s   





zInvertedResidualDS.forwardrY   rZ   r[   r&   r&   rN   r'   r]      s    9r]   c                      sP   e Zd ZU dZded< ded< ded< 				
	dd fddZdddZ  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:: python

            >>> 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)
            [1, 1000]
    floatscaler2   r    boolr         ?r     Tr   r*   r+   rC   c              	     s  t    || _|| _|| _g d}t|}|dkrg d}n7|dkr(g d}n.|dkr1g d}n%|dkr:g d	}n|d
krCg d}n|dkrLg d}n
tdt| d td|d ddd|d| _	t
dddd| _g | _t|D ]X\}}	t|	D ]O}
|
dkr| jt||d  ||d  d|dt|d d t|
d  d}n!| jt||d  ||d  d|dt|d d t|
d  d}| j| qzqrt|d |d ddd|d| _|rtd| _|dkr|d | _t|d || _d S d S )N)r3      r3         ?)   rl   0   `      Q?)rk   rl       @      ro         ?)rk   rl   rm   rn         rg   )rk   rl   t      i  rv         ?)rk   rl      i`  i  rv          @)rk   rl      i  i  i   zThis scale size:[z] is not implemented!r6   r5   r
   )r>   r?   rE   r@   rF   rA   )rE   r@   rF   r   )r>   r?   r@   rA   _)Zsublayernamerk   )rG   rH   re   r    r   r/   NotImplementedErrorstrr   _conv1r   	_max_pool_block_list	enumeraterangeZadd_sublayerr]   r=   append
_last_convr   _pool2d_avgZ_out_cr   _fc)rM   re   r   r    r   Zstage_repeatsrA   Zstage_out_channelsZstage_idZ
num_repeatiblockrN   r&   r'   rH     s   











		

zShuffleNetV2.__init__rP   r   c                 C  sj   |  |}| |}| jD ]}||}q| |}| jr!| |}| jdkr3tj|ddd}| 	|}|S )Nr   r5   rk   )Z
start_axisZ	stop_axis)
r   r   r   r   r   r   r    r8   flattenr   )rM   rP   r0   invr&   r&   r'   rX   `  s   







zShuffleNetV2.forward)rg   r   rh   T)
re   rd   r   r*   r    r2   r   rf   r+   rC   rZ   )r"   r#   r$   __doc__r%   rH   rX   r\   r&   r&   rN   r'   rc      s   
 Trc   Frg   arch_ShuffleNetArch
pretrainedrf   re   rd   kwargsUnpack[_ShuffleNetOptions]c                 K  s^   t dd|i|}|r-| tv sJ |  dtt|  d t|  d }t|}|| |S )Nre   zJ model do not have a pretrained model now, you should set pretrained=Falser   r5   r&   )rc   r)   r	   r8   loadZset_dict)r   r   re   r   modelZweight_pathparamr&   r&   r'   _shufflenet_v2p  s   


r   c                 K     t 	dd| d|S )aU  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:: python

            >>> 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)
            [1, 1000]
    r   rj   re   r   N)r   r   r   r   r&   r&   r'   r         r   c                 K  r   )aU  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:: python

            >>> 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)
            [1, 1000]
    r   rp   r   N)r   r   r   r&   r&   r'   r     r   r   c                 K  r   )aP  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:: python

            >>> 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)
            [1, 1000]
    r   rt   r   N)r   r   r   r&   r&   r'   r     r   r   c                 K  r   )aP  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:: python

            >>> 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)
            [1, 1000]
    r   rg   r   N)r   r   r   r&   r&   r'   r     r   r   c                 K  r   )aP  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:: python

            >>> 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)
            [1, 1000]
    r   ry   r   N)r   r   r   r&   r&   r'   r     r   r   c                 K  r   )aP  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:: python

            >>> 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)
            [1, 1000]
    r   r{   r   N)r   r   r   r&   r&   r'   r   =  r   r   Unpack[_ShuffleNetSwishOptions]c                 K  s   t 	ddd| d|S )a]  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:: python

            >>> 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)
            [1, 1000]
    r   rg   r   )re   r   r   N)r   r   r   r&   r&   r'   r   b  s    r   )r   r*   r+   r,   )r0   r   r1   r2   r+   r   )Frg   )
r   r   r   rf   re   rd   r   r   r+   rc   )F)r   rf   r   r   r+   rc   )r   rf   r   r   r+   rc   ),
__future__r   typingr   r   r8   r   Z	paddle.nnr   r   r   Zpaddle.utils.downloadr	   opsr   r   r   Ztyping_extensionsr   r   r   Zpaddle._typingr   r   Z_ActivationTyper   r(   __all__r)   r%   r/   r;   ZLayerr=   r]   rc   r   r   r   r   r   r   r   r   r&   r&   r&   r'   <module>   sd   

 
2E &&&&&&