o
    Qe-$                     @   s   d dl mZ d dl mZ d dlZd dlmZ d dlm  mZ d dlm	Z	m
Z
mZ d dlmZmZmZ d dl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 )    )division)print_functionN)Conv2DLinearDropout)	MaxPool2D	AvgPool2DAdaptiveAvgPool2D)Uniform)	ParamAttr)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   ND:\Projects\ConvertPro\env\Lib\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              	      s2   t t|   t|||||d d |dd| _d S )Nr   r   F)Zin_channelsZout_channelskernel_sizestridepaddinggroupsZ	bias_attr)superr   __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	           	         sv   t 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   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/   r0   paddleconcatFrelu)
r   r   Zconv1Zconv3rZconv3Zconv5rZconv5poolZconvprjcatr   r   r   r!   Q   s   







zInception.forwardr"   r   r   r   r   r'   B   s    r'   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )		GoogLeNetaj  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|   || _|| _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   r8   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   rK   rL   r   r   r   r   ~   sF   

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(   r1   r   )Z
start_axisZ	stop_axis)!r   r/   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rL   rX   rY   rZ   rK   r[   r2   Zsqueezer\   r]   flattenr^   r4   r5   r_   r`   ra   rb   rc   rd   )	r   r   xZince4aZince4dZince5boutZout1Zout2r   r   r   r!      sJ   































zGoogLeNet.forward)r9   T)r#   r$   r%   __doc__r   r!   r&   r   r   r   r   r8   b   s    1r8   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_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   zL{} model do not have a pretrained model now, you should set pretrained=Falser   r   Nr   )r8   
model_urlsformatr   r2   loadZset_dict)Z
pretrainedkwargsmodelarchZweight_pathparamr   r   r   r      s   



)F)
__future__r   r   r2   Z	paddle.nnnnZpaddle.nn.functionalZ
functionalr4   r   r   r   r   r   r	   Zpaddle.nn.initializerr
   Zpaddle.fluid.param_attrr   Zpaddle.utils.downloadr   __all__rj   r   ZLayerr   r'   r8   r   r   r   r   r   <module>   s&    ~