o
    #jM                     @   s   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d Zdd ZG dd dejZG dd dejZG dd dejZd*ddZd*ddZd*ddZd*d d!Zd*d"d#Zd*d$d%Zd*d&d'Zd*d(d)ZdS )+    N)nn)AdaptiveAvgPool2DLinear	MaxPool2D)get_weights_path_from_url   )ConvNormActivation)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 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                 C   s6   | dkrt jS | dkrt jS | d u rd S td|  )Nswishreluz*The activation function is not supported: )r   ZSwishReLURuntimeError)act r   b/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/vision/models/shufflenetv2.pycreate_activation_layer8   s   r   c                 C   sb   | j dd \}}}}|| }tj| |||||gd} tj| g dd} tj| ||||gd} | S )Nr      )shape)r   r         r   )perm)r   paddleZreshapeZ	transpose)xgroupsZ
batch_sizeZnum_channelsheightwidthZchannels_per_groupr   r   r   channel_shuffleC   s   r"   c                       *   e Zd Zejf fdd	Zdd Z  ZS )InvertedResidualc              	      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   r   r   in_channelsout_channelskernel_sizestridepaddingr   activation_layerr   )super__init__r   _conv_pw_conv_dw_conv_linearselfr&   r'   r)   r+   	__class__r   r   r-   U   s8   
		zInvertedResidual.__init__c                 C   sf   t j||jd d |jd d gdd\}}| |}| |}| |}t j||gdd}t|dS )Nr   r   )Znum_or_sectionsaxisr5   )r   splitr   r.   r/   r0   concatr"   r2   inputsx1Zx2outr   r   r   forwardu   s   




zInvertedResidual.forward__name__
__module____qualname__r   r   r-   r=   __classcell__r   r   r3   r   r$   T   s     r$   c                       r#   )InvertedResidualDSc              	      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 )Nr   r   r%   r   r   )r,   r-   r   
_conv_dw_1_conv_linear_1
_conv_pw_2
_conv_dw_2_conv_linear_2r1   r3   r   r   r-      s\   
	
		zInvertedResidualDS.__init__c                 C   sN   |  |}| |}| |}| |}| |}tj||gdd}t|dS )Nr   r6   r   )rD   rE   rF   rG   rH   r   r8   r"   r9   r   r   r   r=      s   





zInvertedResidualDS.forwardr>   r   r   r3   r   rC      s    5rC   c                       s*   e Zd ZdZd
 fdd	Z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]
          ?r     Tc              	      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)r      r         ?)   rO   0   `      Q?)rN   rO       @      rR         ?)rN   rO   rP   rQ         rJ   )rN   rO   t      i  rY         ?)rN   rO      i`  i  rY          @)rN   rO      i  i  i   zThis scale size:[z] is not implemented!r   r   r   )r&   r'   r(   r)   r*   r+   )r(   r)   r*   r   )r&   r'   r)   r+   _)ZsublayernamerN   )r,   r-   scalenum_classes	with_poolr   NotImplementedErrorstrr   _conv1r   	_max_pool_block_list	enumeraterangeZadd_sublayerrC   r$   append
_last_convr   _pool2d_avgZ_out_cr   _fc)r2   rc   r   rd   re   Zstage_repeatsr+   Zstage_out_channelsZstage_idZ
num_repeatiblockr3   r   r   r-      s   











		

zShuffleNetV2.__init__c                 C   sj   |  |}| |}| jD ]}||}q| |}| jr!| |}| jdkr3tj|ddd}| 	|}|S )Nr   r   rN   )Z
start_axisZ	stop_axis)
rh   ri   rj   rn   re   ro   rd   r   flattenrp   )r2   r:   r   invr   r   r   r=   ,  s   







zShuffleNetV2.forward)rJ   r   rK   T)r?   r@   rA   __doc__r-   r=   rB   r   r   r3   r   rI      s    NrI   Fc                 K   sZ   t di |}|r+| tv sJ |  dtt|  d t|  d }t|}|| |S )NzJ model do not have a pretrained model now, you should set pretrained=Falser   r   r   )rI   
model_urlsr   r   loadZset_dict)arch
pretrainedkwargsmodelZweight_pathparamr   r   r   _shufflenet_v2<  s   


r}   c                 K      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]
    r	   rM   rc   ry   N)r	   r}   ry   rz   r   r   r   r	   K     r	   c                 K   r~   )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]
    r
   rS   r   N)r
   r   r   r   r   r   r
   n  r   r
   c                 K   r~   )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]
    r   rW   r   N)r   r   r   r   r   r   r     r   r   c                 K   r~   )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]
    r   rJ   r   N)r   r   r   r   r   r   r     r   r   c                 K   r~   )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]
    r   r\   r   N)r   r   r   r   r   r   r     r   r   c                 K   r~   )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]
    r   r^   r   N)r   r   r   r   r   r   r     r   r   c                 K   s   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]
    r   rJ   r   )rc   r   ry   N)r   r   r   r   r   r   r     s   r   )F)r   r   Z	paddle.nnr   r   r   Zpaddle.utils.downloadr   opsr   __all__rv   r   r"   ZLayerr$   rC   rI   r}   r	   r
   r   r   r   r   r   r   r   r   r   <module>   s6    .A
y

#
#
#
#
##