o
    + i$                     @  s   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
  mZ d dlm
Z
 d dlmZ d dlmZmZmZmZmZmZ d dlmZ d d	lmZ erad d
lmZ d dlmZ G dd deZg ZddiZd#ddZ G dd de
j!Z"G dd de
j!Z#G dd de
j!Z$	d$d%d"dZ%dS )&    )annotations)TYPE_CHECKING	TypedDict)NotRequiredUnpackN)nn)	ParamAttr)AdaptiveAvgPool2D	AvgPool2DConv2DDropoutLinear	MaxPool2D)Uniform)get_weights_path_from_url)Tensor)Size2c                   @  s   e Zd ZU ded< ded< dS )_GoogLeNetOptionszNotRequired[int]num_classeszNotRequired[bool]	with_poolN)__name__
__module____qualname____annotations__ r   r   j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/vision/models/googlenet.pyr   +   s   
 r   	googlenet)zWhttps://paddle-imagenet-models-name.bj.bcebos.com/dygraph/GoogLeNet_pretrained.pdparamsZ 80c06f038e905c53ab32c40eca6e26aechannelsintfilter_sizereturnr   c                 C  s*   d|d |   d }t t| |d}|S )Ng      @   g      ?)Zinitializer)r   r   )r   r   ZstdvZ
param_attrr   r   r   xavier:   s   r"   c                      s.   e Zd Z		dd fd	d
ZdddZ  ZS )	ConvLayer   num_channelsr   num_filtersr   strider   groupsc              	     s.   t    t|||||d d |dd| _d S )Nr$   r!   F)Zin_channelsZout_channelskernel_sizer'   paddingr(   Z	bias_attr)super__init__r   _conv)selfr%   r&   r   r'   r(   	__class__r   r   r,   A   s   

zConvLayer.__init__inputsr   r    c                 C  s   |  |}|S )N)r-   )r.   r1   yr   r   r   forwardU   s   
zConvLayer.forward)r$   r$   )
r%   r   r&   r   r   r   r'   r   r(   r   r1   r   r    r   r   r   r   r,   r3   __classcell__r   r   r/   r   r#   @   s
    r#   c                      s(   e Zd Zd fd
dZdddZ  ZS )	Inceptioninput_channelsr   output_channelsfilter1filter3Rfilter3filter5Rfilter5projc	           	        sr   t    t||d| _t||d| _t||d| _t||d| _t||d| _tdddd| _	t||d| _
d S )Nr$         )r)   r'   r*   )r+   r,   r#   _conv1_conv3r_conv3_conv5r_conv5r   _pool_convprj)	r.   r8   r9   r:   r;   r<   r=   r>   r?   r/   r   r   r,   [   s   
zInception.__init__r1   r   r    c           
      C  sj   |  |}| |}| |}| |}| |}| |}| |}tj||||gdd}	t	
|	}	|	S )Nr$   Zaxis)rB   rC   rD   rE   rF   rG   rH   paddleconcatFrelu)
r.   r1   Zconv1Zconv3rZconv3Zconv5rZconv5poolZconvprjcatr   r   r   r3   q   s   







zInception.forward)r8   r   r9   r   r:   r   r;   r   r<   r   r=   r   r>   r   r?   r   r4   r5   r   r   r/   r   r7   Z   s    r7   c                      s@   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 )	GoogLeNetao  GoogLeNet (Inception v1) model architecture from
    `"Going Deeper with Convolutions" <https://arxiv.org/pdf/1409.4842.pdf>`_.

    Args:
        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 GoogLeNet (Inception v1) model.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.vision.models import GoogLeNet

            >>> # Build model
            >>> model = GoogLeNet()

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

            >>> print(out.shape, out1.shape, out2.shape)
            [1, 1000] [1, 1000] [1, 1000]
    r   r   boolr     Tr    Nonec              	     s  t    || _|| _tdddd| _tddd| _tddd| _tddd| _	t
ddddd	d
dd| _t
ddd	d	dddd| _t
dddddd
dd| _t
dddddddd| _t
ddd	d	dddd| _t
dddddddd| _t
ddddddd	d	| _t
ddddddd	d	| _t
ddddddd	d	| _|rtd| _tddd| _tddd| _|dkr
tddd| _td |td dd!| _tdd	d| _td"d td#dd!| _td$dd| _ td |td dd!| _!tdd	d| _"td"d td#dd!| _#td$dd| _$td |td dd!| _%d S d S )%Nr@   @      r!   )r)   r'   r$      `                i     0   i      p            i   i  i@  i@  i  rA   r   g?Zdownscale_in_infer)pmodei   )Zweight_attri  i   gffffff?)&r+   r,   r   r   r#   r-   r   rG   _conv_1_conv_2r7   _ince3a_ince3b_ince4a_ince4b_ince4c_ince4d_ince4e_ince5a_ince5br	   _pool_5r
   _pool_o1_pool_o2r   _dropr   r"   _fc_out_conv_o1_fc_o1_drop_o1_out1_conv_o2_fc_o2_drop_o2_out2)r.   r   r   r/   r   r   r,      sD   


zGoogLeNet.__init__r1   r   tuple[Tensor, Tensor, Tensor]c           	      C  s  |  |}| |}| |}| |}| |}| |}| |}| |}| |}| |}| |}| 	|}| 
|}| |}| |}| |}|||}}}| jrj| |}| |}| |}| jdkr| |}tj|ddgd}| |}| |}tj|ddd}| |}t|}| |}| |}| |}tj|ddd}| |}| |}|  |}|||fS )Nr   r!   r@   rI   r$   )Z
start_axisZ	stop_axis)!r-   rG   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   r   rp   rq   rr   r   rs   rJ   Zsqueezert   ru   flattenrv   rL   rM   rw   rx   ry   rz   r{   r|   )	r.   r1   xZince4aZince4dZince5boutZout1Zout2r   r   r   r3      sJ   































zGoogLeNet.forward)rR   T)r   r   r   rQ   r    rS   )r1   r   r    r}   )r   r   r   __doc__r   r,   r3   r6   r   r   r/   r   rP      s   
 1rP   F
pretrainedrQ   kwargsUnpack[_GoogLeNetOptions]c                 K  s^   t di |}d}| r-|tv sJ | dtt| d t| d }t|}|| |S )a  GoogLeNet (Inception v1) model architecture from
    `"Going Deeper with Convolutions" <https://arxiv.org/pdf/1409.4842.pdf>`_.

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

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of GoogLeNet (Inception v1) model.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.vision.models import googlenet

            >>> # Build model
            >>> model = googlenet()

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

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

            >>> print(out.shape, out1.shape, out2.shape)
            [1, 1000] [1, 1000] [1, 1000]
    r   zJ model do not have a pretrained model now, you should set pretrained=Falser   r$   Nr   )rP   
model_urlsr   rJ   loadZset_dict)r   r   modelarchZweight_pathparamr   r   r   r     s    


)r   r   r   r   r    r   )F)r   rQ   r   r   r    rP   )&
__future__r   typingr   r   Ztyping_extensionsr   r   rJ   Zpaddle.nn.functionalr   Z
functionalrL   Zpaddle.base.param_attrr   Z	paddle.nnr	   r
   r   r   r   r   Zpaddle.nn.initializerr   Zpaddle.utils.downloadr   r   Zpaddle._typingr   r   __all__r   r"   ZLayerr#   r7   rP   r   r   r   r   r   <module>   s0    
( 