
    |j$                       d dl mZ d dlmZmZ d dlmZmZ d dlZd dlm	Z	 d dl
mZ ddlmZ erd d	lmZ d d
lmZ  G d de          Zg ZddiZ G d de	j                  Z G d de	j                  Z	 d d!dZ	 	 d"d#dZdS )$    )annotations)TYPE_CHECKING	TypedDict)NotRequiredUnpackN)nn)get_weights_path_from_url   )ConvNormActivation)Tensor)Size2c                  $    e Zd ZU ded<   ded<   dS )_MobileNetV1OptionszNotRequired[int]num_classeszNotRequired[bool]	with_poolN)__name__
__module____qualname____annotations__     p/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/vision/models/mobilenetv1.pyr   r   "   s*         %%%%$$$$$$r   r   zmobilenetv1_1.0)zAhttps://paddle-hapi.bj.bcebos.com/models/mobilenetv1_1.0.pdparams 3033ab1975b1670bef51545feb65fc45c                  (     e Zd Zd fdZddZ xZS )DepthwiseSeparablein_channelsintout_channels1out_channels2
num_groupsstrider   scalefloatreturnNonec                .   t                                                       t          |t          ||z            d|dt          ||z                      | _        t          t          ||z            t          ||z            ddd          | _        d S )N      )kernel_sizer!   paddinggroupsr   )r)   r!   r*   )super__init__r   r   _depthwise_conv_pointwise_conv)selfr   r   r   r    r!   r"   	__class__s          r   r-   zDepthwiseSeparable.__init__3   s     	1%&&zE)** 
  
  
  2%&&%&& 
  
  
r   xr   c                Z    |                      |          }|                     |          }|S )N)r.   r/   )r0   r2   s     r   forwardzDepthwiseSeparable.forwardO   s-      ##  ##r   )r   r   r   r   r   r   r    r   r!   r   r"   r#   r$   r%   r2   r   r$   r   )r   r   r   r-   r4   __classcell__r1   s   @r   r   r   2   sQ        
 
 
 
 
 
8       r   r   c                  T     e Zd ZU dZded<   ded<   ded<   	 	 	 dd fdZddZ xZS )MobileNetV1a  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:: pycon

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

            >>> model = MobileNetV1()

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    r#   r"   r   r   boolr         ?  Tr$   r%   c                   t                                                       || _        g | _        || _        || _        t          dt          d|z            ddd          | _        | 	                    t          t          d|z            dddd|          d	          }| j                            |           | 	                    t          t          d|z            dd
dd|          d	          }| j                            |           | 	                    t          t          d
|z            d
d
d
d|          d	          }| j                            |           | 	                    t          t          d
|z            d
dd
d|          d	          }| j                            |           | 	                    t          t          d|z            dddd|          d	          }| j                            |           | 	                    t          t          d|z            dddd|          d	          }	| j                            |	           t          d          D ]i}
| 	                    t          t          d|z            dddd|          dt          |
dz             z   	          }| j                            |           j| 	                    t          t          d|z            dddd|          d	          }| j                            |           | 	                    t          t          d|z            dddd|          d	          }| j                            |           |rt          j        d          | _        |dk    r,t          j        t          d|z            |          | _        d S d S )Nr'       r
   r(   )r   out_channelsr)   r!   r*   @   )r   r   r   r    r!   r"   conv2_1)sublayername   conv2_2conv3_1   conv3_2conv4_1i   conv4_2   conv5_i   conv5_6conv6r   )r,   r-   r"   dwslr   r   r   r   conv1add_sublayerr   appendrangestrr   AdaptiveAvgPool2D
pool2d_avgLinearfc)r0   r"   r   r   dws21dws22dws31dws32dws41dws42itmpdws56dws6r1   s                 r   r-   zMobileNetV1.__init__u   sx    	
	&"'R%Z
 
 

 !!'U
OO      " 

 

 		!!'U
OO !    " 

 

 		!!'e,,!!    " 

 

 		!!'e,,!!    " 

 

 		!!'e,,!!    " 

 

 		!!'e,,!!    " 

 

 		q 	" 	"A##+ #C%K 0 0"%"%"   AE

* $ 
 
C IS!!!!!!'e,,!"    " 

 

 		  'u--""    ! 

 

 		 	6 2155DO??iD5L 1 1;??DGGG ?r   r2   r   c                    |                      |          }| j        D ]} ||          }| j        r|                     |          }| j        dk    r*t          j        |d          }|                     |          }|S )Nr   r(   )rP   rO   r   rV   r   paddleflattenrX   )r0   r2   dwss      r   r4   zMobileNetV1.forward  s    JJqMM9 	 	CAAA> 	#""Aaq!$$A

Ar   )r;   r<   T)r"   r#   r   r   r   r:   r$   r%   r5   )r   r   r   __doc__r   r-   r4   r6   r7   s   @r   r9   r9   U   s          6 LLLOOO 	N@ N@ N@ N@ N@ N@ N@`       r   r9   FarchrT   
pretrainedr:   kwargsUnpack[_MobileNetV1Options]r$   c                   t          di |}|rq| t          v sJ |  d            t          t          |          d         t          |          d                   }t          j        |          }|                    |           |S )NzJ model do not have a pretrained model now, you should set pretrained=Falser   r(   r   )r9   
model_urlsr	   rd   load	load_dict)rh   ri   rj   modelweight_pathparams         r   
_mobilenetrs     s     !!&!!E 	z!!!___ "!! 0tQD!1!!4
 
 K((Lr   r;   r"   r#   c                F    t          dt          |          z   | 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:: pycon

            >>> 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)
            paddle.Size([1, 1000])
    mobilenetv1_r"   )rs   rT   )ri   r"   rj   rp   s       r   mobilenet_v1rv   %  s@    L U#Z 7<@F E Lr   )F)rh   rT   ri   r:   rj   rk   r$   r9   )Fr;   )ri   r:   r"   r#   rj   rk   r$   r9   )
__future__r   typingr   r   typing_extensionsr   r   rd   r   paddle.utils.downloadr	   opsr   r   paddle._typingr   r   __all__rm   Layerr   r9   rs   rv   r   r   r   <module>r      s   # " " " " "       
 2 1 1 1 1 1 1 1        ; ; ; ; ; ; $ $ $ $ $ $ %$$$$$$% % % % %i % % %
   
               F{ { { { {"( { { {~ #(    & ) ) ) ) ) ) )r   