o
    #jgC                     @   s   d dl mZ d dlZd dlmZ d dlmZ ddlmZ ddlm	Z	 g Z
d	d
dZG dd dejZG dd dZG dd dejZG dd dejZG dd deZG dd deZd ddZd ddZd ddZdS )!    )partialN)nn)get_weights_path_from_url   )ConvNormActivation   _make_divisible)zIhttps://paddle-hapi.bj.bcebos.com/models/mobilenet_v3_small_x1.0.pdparamsZ 34fe0e7c1f8b00b2b056ad6788d0590c)zIhttps://paddle-hapi.bj.bcebos.com/models/mobilenet_v3_large_x1.0.pdparamsZ 118db5792b4e183b925d8e8e334db3df)zmobilenet_v3_small_x1.0zmobilenet_v3_large_x1.0c                       s:   e Zd ZdZejejf fdd	Zdd Zdd Z	  Z
S )SqueezeExcitationa  
    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                    sJ   t    td| _t||d| _t||d| _| | _| | _	d S )Nr   )
super__init__r   AdaptiveAvgPool2DavgpoolZConv2Dfc1fc2
activationscale_activation)selfZinput_channelsZsqueeze_channelsr   r   	__class__ a/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/vision/models/mobilenetv3.pyr   4   s   
zSqueezeExcitation.__init__c                 C   s2   |  |}| |}| |}| |}| |S N)r   r   r   r   r   r   inputscaler   r   r   _scaleB   s
   




zSqueezeExcitation._scalec                 C   s   |  |}|| S r   )r   r   r   r   r   forwardI   s   
zSqueezeExcitation.forward)__name__
__module____qualname____doc__r   ReLUZSigmoidr   r   r   __classcell__r   r   r   r   r
   &   s    r
   c                   @   s&   e Zd Z	dddZedddZdS )InvertedResidualConfig      ?c	           	      C   s   | j ||d| _|| _| j ||d| _| j ||d| _|| _|d u r&d | _n|dkr/tj| _n|dkr8tj	| _nt
d| || _d S )N)r   relu	hardswishz*The activation function is not supported: )adjust_channelsin_channelskernelexpanded_channelsout_channelsuse_seactivation_layerr   r"   	HardswishRuntimeErrorstride)	r   r)   r*   r+   r,   r-   r   r1   r   r   r   r   r   O   s"   


zInvertedResidualConfig.__init__c                 C   s   t | | dS )N   r   )Zchannelsr   r   r   r   r(   m   s   z&InvertedResidualConfig.adjust_channelsN)r%   )r   r   r    r   staticmethodr(   r   r   r   r   r$   N   s
    

r$   c                       s$   e Zd Z fddZdd Z  ZS )InvertedResidualc	           	   
      s   t    |dko||k| _|| _||k| _| jr%t||ddd||d| _t||||t|d d |||d| _| jrHt	|t
|d tjd| _t||ddd|d d| _d S )Nr   r   )r)   r,   kernel_sizer1   padding
norm_layerr.   r   r)   r,   r5   r1   r6   groupsr7   r.      )r   )r   r   use_res_connectr-   expandr   expand_convintbottleneck_convr
   r	   r   ZHardsigmoidmid_selinear_conv)	r   r)   r+   r,   filter_sizer1   r-   r.   r7   r   r   r   r   s   sN   



zInvertedResidual.__init__c                 C   sN   |}| j r
| |}| |}| jr| |}| |}| jr%t||}|S r   )	r<   r=   r?   r-   r@   rA   r;   paddleadd)r   xidentityr   r   r   r      s   



zInvertedResidual.forward)r   r   r    r   r   r#   r   r   r   r   r4   r   s    7r4   c                       s,   e Zd ZdZ	d	 fdd	Zdd Z  ZS )
MobileNetV3aO  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%     Tc              
      s
  t    || _|| _|| _|| _|| _|d j| _|d j| _	| j	d | _
ttjddd td| jddd	d	tj d
| _tj fdd| jD  | _t| j	| j
d	d	dd	 tjd| _|rctd	| _|dkrtt| j
| jt tjddt| j|| _d S d S )Nr      gMbP?gGz?)epsilonZmomentum   r   r   )r)   r,   r5   r1   r6   r9   r.   r7   c                    s2   g | ]}t |j|j|j|j|j|j|j d qS ))r)   r+   r,   rB   r1   r-   r.   r7   )r4   r)   r+   r,   r*   r1   r-   r.   ).0cfgr7   r   r   
<listcomp>   s    z(MobileNetV3.__init__.<locals>.<listcomp>r8   g?)p)r   r   configr   last_channelnum_classes	with_poolr)   Zfirstconv_in_channelsZlastconv_in_channelsZlastconv_out_channelsr   r   ZBatchNorm2Dr   r/   convZ
Sequentialblockslastconvr   r   ZLinearZDropout
classifier)r   rR   rS   r   rT   rU   r   rO   r   r      sZ   



zMobileNetV3.__init__c                 C   sR   |  |}| |}| |}| jr| |}| jdkr't|d}| |}|S )Nr   r   )	rV   rW   rX   rU   r   rT   rC   flattenrY   )r   rE   r   r   r   r     s   





zMobileNetV3.forwardr%   rH   T)r   r   r    r!   r   r   r#   r   r   r   r   rG      s
    @rG   c                       "   e Zd ZdZd fdd	Z  ZS )MobileNetV3Smalla  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:: python

            >>> 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)
            [1, 1000]
    r%   rH   Tc                    s  t ddddddd|t ddddddd|t ddd	dddd
|t ddddddd|t ddddddd
|t ddddddd
|t ddddddd
|t ddddddd
|t ddddddd|t ddddddd
|t ddddddd
|g}td| d}t j|||||d d S )N   rL   Tr&   r   H      FX   r      `   (   r'      x   0      i   i@  i   r2   rS   r   rU   rT   r$   r	   r   r   r   r   rT   rU   rR   rS   r   r   r   r   0  s(   
zMobileNetV3Small.__init__r[   r   r   r    r!   r   r#   r   r   r   r   r]         r]   c                       r\   )MobileNetV3Largea  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:: python

            >>> 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)
            [1, 1000]
    r%   rH   Tc                    sX  t ddddddd|t ddddddd|t ddd	dddd|t dd
d	dddd|t dd
ddddd|t dd
ddddd|t ddddddd|t ddddddd|t ddddddd|t ddddddd|t ddddddd|t ddddddd|t dd
ddddd|t dd
ddddd|t dd
ddddd|g}td| d}t j|||||d d S )Nr^   rL   Fr&   r   @   r`   r   r_   rb   rd   Trf   re   P   r'         i  p   i     i  i   r2   ri   rj   rk   r   r   r   r   e  sT   #
zMobileNetV3Large.__init__r[   rl   r   r   r   r   rn   H  rm   rn   Fr%   c                 K   s   | dkrt dd|i|}n	tdd|i|}|rB|  d| } | tv s+J |  dtt|  d t|  d }t|}|| |S )Nmobilenet_v3_larger   Z_xzJ model do not have a pretrained model now, you should set pretrained=Falser   r   r   )rn   r]   
model_urlsr   rC   loadZset_dict)arch
pretrainedr   kwargsmodelZweight_pathparamr   r   r   _mobilenet_v3  s   


r}   c                 K      t 	d|| d|}|S )a  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:: python

            >>> 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)
            [1, 1000]
    mobilenet_v3_smallr   ry   N)r   r}   ry   r   rz   r{   r   r   r   r        !r   c                 K   r~   )a  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:: python

            >>> 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)
            [1, 1000]
    ru   r   N)ru   r   r   r   r   r   ru     r   ru   )Fr%   )	functoolsr   rC   r   Zpaddle.utils.downloadr   opsr   Z_utilsr	   __all__rv   ZLayerr
   r$   r4   rG   r]   rn   r}   r   ru   r   r   r   r   <module>   s&   ($E\5
K
'