
    |jj*                    0   d dl mZ d dlmZ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 erd dl	mZ d dlmZmZ  G d	 d
e          Zg ZdddZ G d de
j                  Z	 d(d)dZg dg dg dg ddZd*d#Z	 	 d+d,d$Z	 	 d+d,d%Z	 	 d+d,d&Z	 	 d+d,d'ZdS )-    )annotations)TYPE_CHECKINGLiteral	TypedDict)NotRequiredUnpackN)nn)get_weights_path_from_url)Tensor)Layer
Sequentialc                  $    e Zd ZU ded<   ded<   dS )_VGGOptionszNotRequired[int]num_classeszNotRequired[bool]	with_poolN)__name__
__module____qualname____annotations__     h/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/vision/models/vgg.pyr   r   !   s*         %%%%$$$$$$r   r   )z7https://paddle-hapi.bj.bcebos.com/models/vgg16.pdparams 89bbffc0f87d260be9b8cdc169c991c4)z7https://paddle-hapi.bj.bcebos.com/models/vgg19.pdparams 23b18bb13d8894f60f54e642be79a0dd)vgg16vgg19c                  F     e Zd ZU dZded<   ded<   	 dd fdZddZ xZS )VGGag  VGG model from
    `"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`_.

    Args:
        features (nn.Layer): Vgg features create by function make_layers.
        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 three fc layer or not. Default: True.

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

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import VGG
            >>> from paddle.vision.models.vgg import make_layers

            >>> vgg11_cfg = [
            ...     64,
            ...     'M',
            ...     128,
            ...     'M',
            ...     256,
            ...     256,
            ...     'M',
            ...     512,
            ...     512,
            ...     'M',
            ...     512,
            ...     512,
            ...     'M',
            ... ]

            >>> features = make_layers(vgg11_cfg)  # type: ignore

            >>> vgg11 = VGG(features)

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    intr   boolr     Tfeaturesr   returnNonec                   t                                                       || _        || _        || _        |rt          j        d          | _        |dk    rt          j        t          j	        dd          t          j
                    t          j                    t          j	        dd          t          j
                    t          j                    t          j	        d|                    | _        d S d S )N)   r&   r   i b  i   )super__init__r"   r   r   r	   AdaptiveAvgPool2Davgpoolr   LinearReLUDropout
classifier)selfr"   r   r   	__class__s       r   r(   zVGG.__init__f   s     	 &" 	8/77DL?? m	+t,,		
	$%%		
	$,, DOOO ?r   xr   c                    |                      |          }| j        r|                     |          }| j        dk    r*t	          j        |d          }|                     |          }|S )Nr      )r"   r   r*   r   paddleflattenr.   )r/   r1   s     r   forwardzVGG.forward|   sa    MM!> 	 QAaq!$$A""Ar   )r!   T)r"   r   r   r   r   r    r#   r$   )r1   r   r#   r   )r   r   r   __doc__r   r(   r6   __classcell__)r0   s   @r   r   r   4   s         , ,\ OOO KO      ,
 
 
 
 
 
 
 
r   r   Fcfglist[int | Literal['M']]
batch_normr    r#   r   c                2   g }d}| D ]}|dk    r|t          j        dd          gz  }#t          j        ||dd          }|r,||t          j        |          t          j                    gz  }n||t          j                    gz  }|}t          j        | S )N   M   )kernel_sizestrider3   )r@   padding)r	   	MaxPool2DConv2DBatchNorm2Dr,   r   )r9   r;   layersin_channelsvconv2ds         r   make_layersrJ      s     FK 	 	88r|!<<<==FFY{A1aHHHF .62>!#4#4bgii@@62799--KK=&!!r   )@   r>      r>      rM   r>      rN   r>   rN   rN   r>   )rK   rK   r>   rL   rL   r>   rM   rM   r>   rN   rN   r>   rN   rN   r>   )rK   rK   r>   rL   rL   r>   rM   rM   rM   r>   rN   rN   rN   r>   rN   rN   rN   r>   )rK   rK   r>   rL   rL   r>   rM   rM   rM   rM   r>   rN   rN   rN   rN   r>   rN   rN   rN   rN   r>   )ABDEarchstrLiteral['A', 'B', 'D', 'E']
pretrainedkwargsUnpack[_VGGOptions]c                :   t          t          t          |         |          fi |}|rq| t          v sJ |  d            t	          t          |          d         t          |          d                   }t          j        |          }|                    |           |S )N)r;   zJ model do not have a pretrained model now, you should set pretrained=Falser   r3   )r   rJ   cfgs
model_urlsr
   r4   load	load_dict)rS   r9   r;   rV   rW   modelweight_pathparams           r   _vggra      s     DI*===HHHHE 	z!!!___ "!! 0tQD!1!!4
 
 K((Lr   c                4    d}|r|dz  }t          |d|| fi |S )a<  VGG 11-layer model from
    `"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
                            on ImageNet. Default: False.
        batch_norm (bool, optional): If True, returns a model with batch_norm layer. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`VGG <api_paddle_vision_models_VGG>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of VGG 11-layer model.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import vgg11

            >>> # build model
            >>> model = vgg11()

            >>> # build vgg11 model with batch_norm
            >>> model = vgg11(batch_norm=True)

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    vgg11_bnrO   ra   rV   r;   rW   
model_names       r   rc   rc      8    F J e

CZBB6BBBr   c                4    d}|r|dz  }t          |d|| fi |S )a2  VGG 13-layer model from
    `"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
                            on ImageNet. Default: False.
        batch_norm (bool): If True, returns a model with batch_norm layer. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`VGG <api_paddle_vision_models_VGG>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of VGG 13-layer model.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import vgg13

            >>> # build model
            >>> model = vgg13()

            >>> # build vgg13 model with batch_norm
            >>> model = vgg13(batch_norm=True)

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    vgg13rd   rP   re   rf   s       r   rj   rj      rh   r   c                4    d}|r|dz  }t          |d|| fi |S )a<  VGG 16-layer model from
    `"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
                            on ImageNet. Default: False.
        batch_norm (bool, optional): If True, returns a model with batch_norm layer. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`VGG <api_paddle_vision_models_VGG>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of VGG 16-layer model.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import vgg16

            >>> # build model
            >>> model = vgg16()

            >>> # build vgg16 model with batch_norm
            >>> model = vgg16(batch_norm=True)

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    r   rd   rQ   re   rf   s       r   r   r   $  rh   r   c                4    d}|r|dz  }t          |d|| fi |S )a<  VGG 19-layer model from
    `"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
                            on ImageNet. Default: False.
        batch_norm (bool, optional): If True, returns a model with batch_norm layer. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`VGG <api_paddle_vision_models_VGG>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of VGG 19-layer model.

    Examples:
        .. code-block:: pycon

            >>> import paddle
            >>> from paddle.vision.models import vgg19

            >>> # build model
            >>> model = vgg19()

            >>> # build vgg19 model with batch_norm
            >>> model = vgg19(batch_norm=True)

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

            >>> print(out.shape)
            paddle.Size([1, 1000])
    r   rd   rR   re   rf   s       r   r   r   M  rh   r   )F)r9   r:   r;   r    r#   r   )rS   rT   r9   rU   r;   r    rV   r    rW   rX   r#   r   )FF)rV   r    r;   r    rW   rX   r#   r   )
__future__r   typingr   r   r   typing_extensionsr   r   r4   r	   paddle.utils.downloadr
   r   	paddle.nnr   r   r   __all__r[   r   rJ   rZ   ra   rc   rj   r   r   r   r   r   <module>rs      sb   # " " " " "          2 1 1 1 1 1 1 1        ; ; ; ; ; ; %++++++++% % % % %i % % %
 	 	
R R R R R"( R R Rl 7<" " " " "&
 
 

 
 

 
 

 
 
- @   0 &C &C &C &C &CT &C &C &C &C &CT &C &C &C &C &CT &C &C &C &C &C &C &Cr   