o
    #j"                     @   st   d dl Z d dl mZ d dlmZ ddlmZ g ZddiZG dd	 d	ejZ	G d
d dejZ
dddZdddZdS )    N)nn)get_weights_path_from_url   )ConvNormActivationzmobilenetv1_1.0)zAhttps://paddle-hapi.bj.bcebos.com/models/mobilenetv1_1.0.pdparamsZ 3033ab1975b1670bef51545feb65fc45c                       s$   e Zd Z fddZdd Z  ZS )DepthwiseSeparablec              	      sX   t    t|t|| d|dt|| d| _tt|| t|| dddd| _d S )N      )kernel_sizestridepaddinggroupsr   )r	   r
   r   )super__init__r   int_depthwise_conv_pointwise_conv)selfin_channelsout_channels1out_channels2
num_groupsr
   scale	__class__ a/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/vision/models/mobilenetv1.pyr       s    
	

	

zDepthwiseSeparable.__init__c                 C   s   |  |}| |}|S )N)r   r   )r   xr   r   r   forward<   s   

zDepthwiseSeparable.forward)__name__
__module____qualname__r   r   __classcell__r   r   r   r   r      s    r   c                       s*   e Zd ZdZd	 fdd	Zdd Z  ZS )
MobileNetV1aw  MobileNetV1 model from
    `"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications" <https://arxiv.org/abs/1704.04861>`_.

    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 MobileNetV1 model.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.vision.models import MobileNetV1

            >>> model = MobileNetV1()

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

            >>> print(out.shape)
            [1, 1000]
          ?  Tc              
      sT  t    || _g | _|| _|| _tdtd| dddd| _| j	t
td| dddd|ddd	}| j| | j	t
td| dd
dd|ddd	}| j| | j	t
td
| d
d
d
d|ddd	}| j| | j	t
td
| d
dd
d|ddd	}| j| | j	t
td| dddd|ddd	}| j| | j	t
td| dddd|ddd	}	| j|	 tdD ]!}
| j	t
td| dddd|ddt|
d  d	}| j| q| j	t
td| dddd|ddd	}| j| | j	t
td| dddd|ddd	}| j| |rtd| _|dkr(ttd| || _d S d S )Nr       r   r   )r   Zout_channelsr	   r
   r   @   )r   r   r   r   r
   r   Zconv2_1)Zsublayername   Zconv2_2Zconv3_1   Zconv3_2Zconv4_1i   Zconv4_2   Zconv5_i   Zconv5_6Zconv6r   )r   r   r   dwslnum_classes	with_poolr   r   conv1Zadd_sublayerr   appendrangestrr   ZAdaptiveAvgPool2D
pool2d_avgZLinearfc)r   r   r,   r-   Zdws21Zdws22Zdws31Zdws32Zdws41Zdws42itmpZdws56Zdws6r   r   r   r   ^   s   











zMobileNetV1.__init__c                 C   sR   |  |}| jD ]}||}q| jr| |}| jdkr't|d}| |}|S )Nr   r   )r.   r+   r-   r2   r,   paddleflattenr3   )r   r   Zdwsr   r   r   r      s   





zMobileNetV1.forward)r#   r$   T)r   r   r    __doc__r   r   r!   r   r   r   r   r"   B   s
     r"   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   )r"   
model_urlsr   r6   load	load_dict)arch
pretrainedkwargsmodelZweight_pathparamr   r   r   
_mobilenet   s   


rA   r#   c                 K   s"   t dt| | fd|i|}|S )a  MobileNetV1 from
    `"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications" <https://arxiv.org/abs/1704.04861>`_.

    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:`MobileNetV1 <api_paddle_vision_models_MobileNetV1>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of MobileNetV1 model.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.vision.models import mobilenet_v1

            >>> # Build model
            >>> model = mobilenet_v1()

            >>> # Build model and load imagenet pretrained weight
            >>> # model = mobilenet_v1(pretrained=True)

            >>> # build mobilenet v1 with scale=0.5
            >>> model_scale = mobilenet_v1(scale=0.5)

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

            >>> print(out.shape)
            [1, 1000]
    Zmobilenetv1_r   )rA   r1   )r=   r   r>   r?   r   r   r   mobilenet_v1  s   "rB   )F)Fr#   )r6   r   Zpaddle.utils.downloadr   opsr   __all__r9   ZLayerr   r"   rA   rB   r   r   r   r   <module>   s   # 
6