o
    #j5"                     @   s   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 g ZddiZ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ZdS )    N)nn)	ParamAttr)AdaptiveAvgPool2D	AvgPool2DConv2DDropoutLinear	MaxPool2D)Uniform)get_weights_path_from_url	googlenet)zWhttps://paddle-imagenet-models-name.bj.bcebos.com/dygraph/GoogLeNet_pretrained.pdparamsZ 80c06f038e905c53ab32c40eca6e26aec                 C   s*   d|d |   d }t t| |d}|S )Ng      @   g      ?)Zinitializer)r   r
   )Zchannelsfilter_sizeZstdvZ
param_attr r   _/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/vision/models/googlenet.pyxavier(   s   r   c                       s(   e Zd Z	d fdd	Zdd Z  ZS )	ConvLayer   c              	      s.   t    t|||||d d |dd| _d S )Nr   r   F)Zin_channelsZout_channelskernel_sizestridepaddinggroupsZ	bias_attr)super__init__r   _conv)selfZnum_channelsZnum_filtersr   r   r   	__class__r   r   r   /   s   

zConvLayer.__init__c                 C   s   |  |}|S )N)r   )r   inputsyr   r   r   forward>   s   
zConvLayer.forward)r   r   __name__
__module____qualname__r   r    __classcell__r   r   r   r   r   .   s    r   c                       s$   e Zd Z fddZdd Z  ZS )	Inceptionc	           	         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   Zinput_channelsZoutput_channelsZfilter1Zfilter3RZfilter3Zfilter5RZfilter5Zprojr   r   r   r   D   s   
zInception.__init__c           
      C   sj   |  |}| |}| |}| |}| |}| |}| |}tj||||gdd}	t	
|	}	|	S )Nr   Zaxis)r)   r*   r+   r,   r-   r.   r/   paddleconcatFrelu)
r   r   Zconv1Zconv3rZconv3Zconv5rZconv5poolZconvprjcatr   r   r   r    Z   s   







zInception.forwardr!   r   r   r   r   r&   C   s    r&   c                       s*   e Zd ZdZd fdd	Z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]
      Tc              	      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  r(   r   g?Zdownscale_in_infer)pmodei   )Zweight_attri  i   gffffff?)&r   r   num_classes	with_poolr   r   r	   r.   _conv_1_conv_2r&   _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   rJ   rK   r   r   r   r      sD   


zGoogLeNet.__init__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}| |}| |}|  |}|||gS )Nr   r   r'   r0   r   )Z
start_axisZ	stop_axis)!r   r.   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rK   rW   rX   rY   rJ   rZ   r1   Zsqueezer[   r\   flattenr]   r3   r4   r^   r_   r`   ra   rb   rc   )	r   r   xZince4aZince4dZince5boutZout1Zout2r   r   r   r       sJ   































zGoogLeNet.forward)r8   T)r"   r#   r$   __doc__r   r    r%   r   r   r   r   r7   k   s    1r7   Fc                 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   )r7   
model_urlsr   r1   loadZset_dict)Z
pretrainedkwargsmodelarchZweight_pathparamr   r   r   r      s   


)F)r1   Zpaddle.nn.functionalr   Z
functionalr3   Zpaddle.base.param_attrr   Z	paddle.nnr   r   r   r   r   r	   Zpaddle.nn.initializerr
   Zpaddle.utils.downloadr   __all__ri   r   ZLayerr   r&   r7   r   r   r   r   r   <module>   s    (~