Ë
    –\;jûO  ã                   ó&  — 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	 g Z
ddd	d
ddddœZd„ Zd„ Z G d„ dej                  «      Z G d„ dej                  «      Z G d„ dej                  «      Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zy) é    N)Únn)ÚAdaptiveAvgPool2DÚLinearÚ	MaxPool2D)Úget_weights_path_from_urlé   )ÚConvNormActivation)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Ú adde0aa3b023e5b0c94a68be1c394b84)Ú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_swishc                 óx   — | dk(  rt         j                  S | dk(  rt         j                  S | €y t        d| › �«      ‚)NÚswishÚreluz*The activation function is not supported: )r   ÚSwishÚReLUÚRuntimeError)Úacts    újG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/vision/models/shufflenetv2.pyÚcreate_activation_layerr    8   s=   € Ø
ˆg‚~Ü�x‰xˆØ	�ŠÜ�w‰wˆØ	ˆØäÐGÈÀuÐMÓNÐNó    c                 óØ   — | j                   dd \  }}}}||z  }t        j                  | |||||g¬«      } t        j                  | g d¢¬«      } t        j                  | ||||g¬«      } | S )Nr   é   )Úshape)r   r   é   é   r#   )Úperm)r$   ÚpaddleÚreshapeÚ	transpose)ÚxÚgroupsÚ
batch_sizeÚnum_channelsÚheightÚwidthÚchannels_per_groups          r   Úchannel_shuffler2   C   sz   € Ø./¯g©g°a¸¨lÑ+€J�˜f eØ%¨Ñ/Ðô 	�‰Ø	�*˜fÐ&8¸&À%ÐHô	€Aô
 	×Ñ˜¢Ô1€Aô 	�‰�q ¨\¸6À5Ð IÔJ€AØ€Hr!   c                   ó<   ‡ — e Zd Zej                  fˆ fd„	Zd„ Zˆ xZS )ÚInvertedResidualc           	      óÖ   •— 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|¬«      | _        y )Nr   r%   r   ©Úin_channelsÚout_channelsÚkernel_sizeÚstrideÚpaddingr,   Úactivation_layerr&   )ÚsuperÚ__init__r	   Ú_conv_pwÚ_conv_dwÚ_conv_linear©Úselfr7   r8   r:   r<   Ú	__class__s        €r   r>   zInvertedResidual.__init__U   s›   ø€ ô 	‰ÑÔÜ*Ø# qÑ(Ø%¨Ñ*ØØØØØ-ô
ˆŒô +Ø$¨Ñ)Ø%¨Ñ*ØØØØ 1Ñ$Ø!ô
ˆŒô /Ø$¨Ñ)Ø%¨Ñ*ØØØØØ-ô
ˆÕr!   c                 ó,  — t        j                  ||j                  d   dz  |j                  d   dz  gd¬«      \  }}| j                  |«      }| j	                  |«      }| j                  |«      }t        j                  ||gd¬«      }t        |d«      S )Nr%   r   )Únum_or_sectionsÚaxis©rG   )r(   Úsplitr$   r?   r@   rA   Úconcatr2   ©rC   ÚinputsÚx1Úx2Úouts        r   ÚforwardzInvertedResidual.forwardu   s‹   € Ü—‘ØØ#Ÿ\™\¨!™_°Ñ1°6·<±<À±?ÀaÑ3GÐHØô
‰ˆˆBð
 �]‰]˜2ÓˆØ�]‰]˜2ÓˆØ×Ñ˜rÓ"ˆÜ�m‰m˜R ˜H¨1Ô-ˆÜ˜s AÓ&Ð&r!   ©Ú__name__Ú
__module__Ú__qualname__r   r   r>   rP   Ú__classcell__©rD   s   @r   r4   r4   T   s   ø„ àBDÇ'Á'õ
ö@
'r!   r4   c                   ó<   ‡ — e Zd Zej                  fˆ fd„	Zd„ Zˆ xZS )ÚInvertedResidualDSc           	      ó2  •— 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|¬«      | _        y )Nr&   r%   r6   r   r   )r=   r>   r	   Ú
_conv_dw_1Ú_conv_linear_1Ú
_conv_pw_2Ú
_conv_dw_2Ú_conv_linear_2rB   s        €r   r>   zInvertedResidualDS.__init__ƒ   sä   ø€ ô 	‰ÑÔô -Ø#Ø$ØØØØØ!ô
ˆŒô 1Ø#Ø%¨Ñ*ØØØØØ-ô
ˆÔô -Ø#Ø%¨Ñ*ØØØØØ-ô
ˆŒô -Ø$¨Ñ)Ø%¨Ñ*ØØØØ 1Ñ$Ø!ô
ˆŒô 1Ø$¨Ñ)Ø%¨Ñ*ØØØØØ-ô
ˆÕr!   c                 óö   — | j                  |«      }| j                  |«      }| j                  |«      }| j                  |«      }| j	                  |«      }t        j                  ||gd¬«      }t        |d«      S )Nr%   rH   r   )rZ   r[   r\   r]   r^   r(   rJ   r2   rK   s        r   rP   zInvertedResidualDS.forward¸   sm   € Ø�_‰_˜VÓ$ˆØ× Ñ  Ó$ˆØ�_‰_˜VÓ$ˆØ�_‰_˜RÓ ˆØ× Ñ  Ó$ˆÜ�m‰m˜R ˜H¨1Ô-ˆä˜s AÓ&Ð&r!   rQ   rV   s   @r   rX   rX   ‚   s   ø„ àBDÇ'Á'õ3
öj'r!   rX   c                   ó*   ‡ — e Zd ZdZdˆ fd„	Z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:: 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]
    c           
      óÐ  •— t         ‰| �  «        || _        || _        || _        g d¢}t        |«      }|dk(  rg d¢}nL|dk(  rg d¢}nB|dk(  rg d¢}n8|dk(  rg d	¢}n.|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(  rK| j                  t!        ||dz      ||dz      d|¬«      t        |dz   «      dz   t        |
dz   «      z   ¬«      }nJ| j                  t#        ||dz      ||dz      d|¬«      t        |dz   «      dz   t        |
dz   «      z   ¬«      }| j                  j%                  |«       Œ¹ ŒÌ t        |d   |d   ddd|¬«      | _        |rt)        d«      | _        |dkD  r|d   | _        t/        |d   |«      | _        y y )N)r#   é   r#   ç      Ð?)éÿÿÿÿé   rf   é0   é`   é   ç…ëQ¸Õ?)re   rf   é    é@   é€   ri   ç      à?)re   rf   rg   rh   éÀ   é   ç      ð?)re   rf   ét   éè   iÐ  rp   ç      ø?)re   rf   é°   i`  iÀ  rp   ç       @)re   rf   éà   iè  iÐ  i   zThis scale size:[z] is not implemented!r&   r%   r   )r7   r8   r9   r:   r;   r<   )r9   r:   r;   r   )r7   r8   r:   r<   Ú_)ÚsublayerÚnameéþÿÿÿre   )r=   r>   ÚscaleÚnum_classesÚ	with_poolr    ÚNotImplementedErrorÚstrr	   Ú_conv1r   Ú	_max_poolÚ_block_listÚ	enumerateÚrangeÚadd_sublayerrX   r4   ÚappendÚ
_last_convr   Ú_pool2d_avgÚ_out_cr   Ú_fc)rC   r|   r   r}   r~   Ústage_repeatsr<   Ústage_out_channelsÚstage_idÚ
num_repeatÚiÚblockrD   s               €r   r>   zShuffleNetV2.__init__Þ   sD  ø€ Ü‰ÑÔØˆŒ
Ø&ˆÔØ"ˆŒÚ!ˆÜ2°3Ó7Ðà�DŠ=Ú!:ÑØ�dŠ]Ú!;ÑØ�cŠ\Ú!<ÑØ�cŠ\Ú!>ÑØ�cŠ\Ú!>ÑØ�cŠ\Ú!>Ñä%Ø#¤c¨%£jÑ0Ð3JÑJóð ô )ØØ+¨AÑ.ØØØØ-ô
ˆŒô #¨q¸ÀAÔFˆŒð ˆÔÜ$-¨mÖ$<Ñ ˆH�jÜ˜:Ö&�Ø˜’6Ø ×-Ñ-Ü!3Ø(:¸8Àa¹<Ñ(HØ);¸HÀq¹LÑ)IØ#$Ø-=ô	"ô ! ¨A¡Ó.°Ñ4´s¸1¸q¹5³zÑAð .ó ‘Eð !×-Ñ-Ü!1Ø(:¸8Àa¹<Ñ(HØ);¸HÀq¹LÑ)IØ#$Ø-=ô	"ô ! ¨A¡Ó.°Ñ4´s¸1¸q¹5³zÑAð .ó �Eð × Ñ ×'Ñ'¨Õ.ñ+ 'ð %=ô0 -Ø*¨2Ñ.Ø+¨BÑ/ØØØØ-ô
ˆŒñ Ü0°Ó3ˆDÔð ˜Š?Ø,¨RÑ0ˆDŒKÜÐ0°Ñ4°kÓBˆD�Hð r!   c                 óH  — | j                  |«      }| j                  |«      }| j                  D ]
  } ||«      }Œ | j                  |«      }| j                  r| j                  |«      }| j                  dkD  r)t        j                  |dd¬«      }| j                  |«      }|S )Nr   r%   re   )Ú
start_axisÚ	stop_axis)
r�   r‚   rƒ   rˆ   r~   r‰   r}   r(   Úflattenr‹   )rC   rL   r+   Úinvs       r   rP   zShuffleNetV2.forward,  sŽ   € Ø�K‰K˜ÓˆØ�N‰N˜1ÓˆØ×#Ô#ˆCÙ�A“‰Að $à�O‰O˜AÓˆà�>Š>Ø× Ñ  Ó#ˆAà×Ñ˜aÒÜ—‘˜q¨Q¸"Ô=ˆAØ—‘˜“ˆAØˆr!   )rq   r   iè  T)rR   rS   rT   Ú__doc__r>   rP   rU   rV   s   @r   ra   ra   Ã   s   ø„ ñõ4LCö\r!   ra   c                 óÐ   — t        di |¤Ž}|rX| t        v s
J | › d�«       ‚t        t        |    d   t        |    d   «      }t        j                  |«      }|j                  |«       |S )NzJ model do not have a pretrained model now, you should set pretrained=Falser   r%   © )ra   Ú
model_urlsr   r(   ÚloadÚset_dict)ÚarchÚ
pretrainedÚkwargsÚmodelÚweight_pathÚparams         r   Ú_shufflenet_v2r£   <  sx   € ÜÑ"˜6Ñ"€EÙà”JÑð	_àˆVÐ]Ð^ó	_Øä/Ü�tÑ˜QÑ¤¨DÑ!1°!Ñ!4ó
ˆô —‘˜KÓ(ˆØ�‰�uÔØ€Lr!   c                 ó    — t        	 dd| dœ|¤ŽS )aR  ShuffleNetV2 with 0.25x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Ecient 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]
    rd   ©r|   rž   )r   ©r£   ©rž   rŸ   s     r   r   r   K  ó&   € ô< ØðØ%)°jñØDJñð r!   c                 ó    — t        	 dd| dœ|¤ŽS )aR  ShuffleNetV2 with 0.33x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Ecient 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]
    rj   r¥   )r   r¦   r§   s     r   r   r   n  r¨   r!   c                 ó    — t        	 dd| dœ|¤ŽS )aM  ShuffleNetV2 with 0.5x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Ecient 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]
    rn   r¥   )r   r¦   r§   s     r   r   r   ‘  ó&   € ô< ØðØ$'°JñØBHñð r!   c                 ó    — t        	 dd| dœ|¤ŽS )aM  ShuffleNetV2 with 1.0x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Ecient 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]
    rq   r¥   )r   r¦   r§   s     r   r   r   ´  r«   r!   c                 ó    — t        	 dd| dœ|¤ŽS )aM  ShuffleNetV2 with 1.5x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Ecient 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]
    rt   r¥   )r   r¦   r§   s     r   r   r   ×  r«   r!   c                 ó    — t        	 dd| dœ|¤ŽS )aM  ShuffleNetV2 with 2.0x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Ecient 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]
    rv   r¥   )r   r¦   r§   s     r   r   r   ú  r«   r!   c                 ó"   — t        	 ddd| dœ|¤ŽS )aZ  ShuffleNetV2 with swish activation function, as described in
    `"ShuffleNet V2: Practical Guidelines for Ecient 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]
    rq   r   )r|   r   rž   )r   r¦   r§   s     r   r   r     s,   € ô< ØðàØØñ	ð
 ñð r!   )F)r(   r   Ú	paddle.nnr   r   r   Úpaddle.utils.downloadr   Úopsr	   Ú__all__rš   r    r2   ÚLayerr4   rX   ra   r£   r   r   r   r   r   r   r   r™   r!   r   Ú<module>rµ      s·   ðó Ý ß :Ñ :Ý ;å $à
€ðððððððñ3€
ò@Oòô"+'�r—x‘xô +'ô\>'˜Ÿ™ô >'ôBv�2—8‘8ô vóró óF óF óF óF óF ôF$r!   