o
    + i)                     @  s  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
 d
eZg ZdddZG dd de
jZ	d.d/ddZg dg dg dg ddZd0d$d%Z		d1d2d&d'Z		d1d2d(d)Z		d1d2d*d+Z		d1d2d,d-ZdS )3    )annotations)TYPE_CHECKINGLiteral	TypedDict)NotRequiredUnpackN)nn)get_weights_path_from_url)Tensor)Layer
Sequentialc                   @  s   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__ r   r   d/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/vision/models/vgg.pyr   !   s   
 r   )z7https://paddle-hapi.bj.bcebos.com/models/vgg16.pdparamsZ 89bbffc0f87d260be9b8cdc169c991c4)z7https://paddle-hapi.bj.bcebos.com/models/vgg19.pdparamsZ 23b18bb13d8894f60f54e642be79a0dd)vgg16vgg19c                      sB   e Zd ZU dZded< ded< 	dd fddZdddZ  ZS )VGGaD  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:: python

            >>> 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)
            [1, 1000]
    intr   boolr     Tfeaturesr   returnNonec                   s|   t    || _|| _|| _|rtd| _|dkr<tt	ddt
 t t	ddt
 t t	d|| _d S d S )N)   r   r   i b  i   )super__init__r   r   r   r   ZAdaptiveAvgPool2Davgpoolr   ZLinearReLUZDropout
classifier)selfr   r   r   	__class__r   r   r!   X   s"   




zVGG.__init__xr
   c                 C  s>   |  |}| jr| |}| jdkrt|d}| |}|S )Nr      )r   r   r"   r   paddleflattenr$   )r%   r(   r   r   r   forwardn   s   



zVGG.forward)r   T)r   r   r   r   r   r   r   r   )r(   r
   r   r
   )r   r   r   __doc__r   r!   r,   __classcell__r   r   r&   r   r   4   s   
  r   Fcfglist[int | Literal['M']]
batch_normr   r   r   c                 C  s~   g }d}| D ]3}|dkr|t jdddg7 }qt j||ddd}|r/||t |t  g7 }n||t  g7 }|}qt j| S )N   M   )kernel_sizeZstrider)   )r5   padding)r   Z	MaxPool2DZConv2DZBatchNorm2Dr#   r   )r/   r1   ZlayersZin_channelsvZconv2dr   r   r   make_layers{   s   
r8   )@   r3      r3      r;   r3      r<   r3   r<   r<   r3   )r9   r9   r3   r:   r:   r3   r;   r;   r3   r<   r<   r3   r<   r<   r3   )r9   r9   r3   r:   r:   r3   r;   r;   r;   r3   r<   r<   r<   r3   r<   r<   r<   r3   )r9   r9   r3   r:   r:   r3   r;   r;   r;   r;   r3   r<   r<   r<   r<   r3   r<   r<   r<   r<   r3   )ABDEarchstrLiteral['A', 'B', 'D', 'E']
pretrainedkwargsUnpack[_VGGOptions]c                 K  sh   t tt| |dfi |}|r2| tv sJ |  dtt|  d t|  d }t|}|| |S )N)r1   zJ model do not have a pretrained model now, you should set pretrained=Falser   r)   )r   r8   cfgs
model_urlsr	   r*   load	load_dict)rA   r/   r1   rD   rE   modelZweight_pathparamr   r   r   _vgg   s   


rM   c                 K  &   d}|r|d7 }t |d|| fi |S )a0  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:: python

            >>> 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)
            [1, 1000]
    vgg11_bnr=   rM   rD   r1   rE   Z
model_namer   r   r   rO         #rO   c                 K  rN   )a&  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:: python

            >>> 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)
            [1, 1000]
    vgg13rP   r>   rQ   rR   r   r   r   rT      rS   rT   c                 K  rN   )a0  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:: python

            >>> 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)
            [1, 1000]
    r   rP   r?   rQ   rR   r   r   r   r     rS   r   c                 K  rN   )a0  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:: python

            >>> 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)
            [1, 1000]
    r   rP   r@   rQ   rR   r   r   r   r   ?  rS   r   )F)r/   r0   r1   r   r   r   )rA   rB   r/   rC   r1   r   rD   r   rE   rF   r   r   )FF)rD   r   r1   r   rE   rF   r   r   )
__future__r   typingr   r   r   Ztyping_extensionsr   r   r*   r   Zpaddle.utils.downloadr	   r
   Z	paddle.nnr   r   r   __all__rH   r   r8   rG   rM   rO   rT   r   r   r   r   r   r   <module>   sF   H
 ***