o
    Qe                     @   s   d dl mZ d dl mZ d dl mZ d dl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 d dlmZ d dlmZ d d	lmZ d
diZg ZG dd dejZG dd dejZdd Zddd
ZdS )    )absolute_import)division)print_functionN)LinearDropoutReLU)Conv2D	MaxPool2D)Uniform)	ParamAttr)get_weights_path_from_urlalexnet)zUhttps://paddle-imagenet-models-name.bj.bcebos.com/dygraph/AlexNet_pretrained.pdparamsZ 7f0f9f737132e02732d75a1459d98a43c                       s*   e Zd Z		d fdd	Zdd Z  ZS )ConvPoolLayer   Nc	           	         sl   t t|   |dkrt nd | _t||||||tt| |dtt| |dd| _t	dddd| _
d S )NreluZinitializer)Zin_channelsZout_channelskernel_sizestridepaddinggroupsweight_attr	bias_attr      r   )r   r   r   )superr   __init__r   r   r   r   r
   _convr	   _pool)	selfZinput_channelsZoutput_channelsZfilter_sizer   r   stdvr   act	__class__ LD:\Projects\ConvertPro\env\Lib\site-packages\paddle/vision/models/alexnet.pyr   *   s   		zConvPoolLayer.__init__c                 C   s,   |  |}| jd ur| |}| |}|S )N)r   r   r   r   Zinputsxr#   r#   r$   forwardB   s
   



zConvPoolLayer.forward)r   N)__name__
__module____qualname__r   r'   __classcell__r#   r#   r!   r$   r   (   s
    	r   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )AlexNeta  AlexNet model from
    `"ImageNet Classification with Deep Convolutional Neural Networks"
    <https://proceedings.neurips.cc/paper/2012/file/c399862d3b9d6b76c8436e924a68c45b-Paper.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.

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

    Examples:
        .. code-block:: python

            import paddle
            from paddle.vision.models import AlexNet

            alexnet = AlexNet()

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

            print(out.shape)
            # [1, 1000]
      c                    s  t t|   || _dtd }tddddd|dd	| _dtd
 }tddddd|dd	| _dtd }t	dddddt
t| |dt
t| |dd| _dtd }t	dddddt
t| |dt
t| |dd| _dtd }tddddd|dd	| _| jdkrdtd }tddd| _tddt
t| |dt
t| |dd| _tddd| _tddt
t| |dt
t| |dd| _td|t
t| |dt
t| |dd| _d S d S )Ng      ?ik  r   @         r   r   )r    i@        r   i  i  r   )r   r   r   r   i     i 	  r   i $  g      ?Zdownscale_in_infer)pmodei   )Zin_featuresZout_featuresr   r   )r   r,   r   num_classesmathsqrtr   _conv1_conv2r   r   r
   _conv3_conv4_conv5r   _drop1r   _fc6_drop2_fc7_fc8)r   r6   r   r!   r#   r$   r   e   sf   
zAlexNet.__init__c                 C   s   |  |}| |}| |}t|}| |}t|}| |}| jdkrStj	|ddd}| 
|}| |}t|}| |}| |}t|}| |}|S )Nr   r   )Z
start_axisZ	stop_axis)r9   r:   r;   Fr   r<   r=   r6   paddleflattenr>   r?   r@   rA   rB   r%   r#   r#   r$   r'      s"   














zAlexNet.forward)r-   )r(   r)   r*   __doc__r   r'   r+   r#   r#   r!   r$   r,   J   s    1r,   c                 K   sZ   t di |}|r+| tv sJ d| tt|  d t|  d }t|}|| |S )NzL{} model do not have a pretrained model now, you should set pretrained=Falser   r   r#   )r,   
model_urlsformatr   rE   load	load_dict)arch
pretrainedkwargsmodelZweight_pathparamr#   r#   r$   _alexnet   s   


rQ   Fc                 K   s   t d| fi |S )a  AlexNet model from
    `"ImageNet Classification with Deep Convolutional Neural Networks"
    <https://proceedings.neurips.cc/paper/2012/file/c399862d3b9d6b76c8436e924a68c45b-Paper.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:`AlexNet <api_paddle_vision_AlexNet>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of AlexNet model.
    
    Examples:
        .. code-block:: python

            import paddle
            from paddle.vision.models import alexnet

            # build model
            model = alexnet()

            # build model and load imagenet pretrained weight
            # model = alexnet(pretrained=True)

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

            print(out.shape)
            # [1, 1000]
    r   )rQ   )rM   rN   r#   r#   r$   r      s   )F)
__future__r   r   r   r7   rE   Z	paddle.nnnnZpaddle.nn.functionalZ
functionalrD   r   r   r   r   r	   Zpaddle.nn.initializerr
   Zpaddle.fluid.param_attrr   Zpaddle.utils.downloadr   rH   __all__ZLayerr   r,   rQ   r   r#   r#   r#   r$   <module>   s&   "b