o
    #j/K                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZmZ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G dd dejZG dd dejZG dd dejZG dd dej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DropoutLinear	MaxPool2D)Uniform)get_weights_path_from_url   )ConvNormActivationinception_v3)z>https://paddle-hapi.bj.bcebos.com/models/inception_v3.pdparamsZ 649a4547c3243e8b59c656f41fe330b8c                       $   e Zd Z fddZdd Z  ZS )InceptionStemc                    s   t    tdddddtjd| _tdddddtjd| _tddddtjd| _tdddd	| _	tdd
ddtjd| _
td
dddtjd| _d S )N       r   r   in_channelsout_channelskernel_sizestridepaddingactivation_layer   @   r   r   r   r   r   )r   r   r   P      )super__init__r   r   ReLUconv_1a_3x3conv_2a_3x3conv_2b_3x3r   max_poolconv_3b_1x1conv_4a_3x3)self	__class__ a/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/vision/models/inceptionv3.pyr   %   sN   
zInceptionStem.__init__c                 C   sJ   |  |}| |}| |}| |}| |}| |}| |}|S )N)r!   r"   r#   r$   r%   r&   )r'   xr*   r*   r+   forwardO   s   






zInceptionStem.forward__name__
__module____qualname__r   r-   __classcell__r*   r*   r(   r+   r   $   s    *r   c                       r   )
InceptionAc                    s   t    t|dddtjd| _t|dddtjd| _tddddtjd| _t|dddtjd| _tddd	dtjd| _	tddd	dtjd| _
td	ddd
d| _t||ddtjd| _d S )Nr   r   r   r   0      r   `   r   Fr   r   r   Z	exclusive)r   r   r   r   r    	branch1x1branch5x5_1branch5x5_2branch3x3dbl_1branch3x3dbl_2branch3x3dbl_3r   branch_poolbranch_pool_conv)r'   num_channelsZpool_featuresr(   r*   r+   r   [   sj   
zInceptionA.__init__c                 C   sj   |  |}| |}| |}| |}| |}| |}| |}| |}tj	||||gdd}|S Nr   )Zaxis)
r8   r9   r:   r;   r<   r=   r>   r?   paddleconcat)r'   r,   r8   Z	branch5x5branch3x3dblr>   r*   r*   r+   r-      s   







zInceptionA.forwardr.   r*   r*   r(   r+   r3   Z   s    :r3   c                       r   )
InceptionBc                    sx   t    t|ddddtjd| _t|dddtjd| _tdd	ddtjd| _td	d	dddtjd| _t	ddd
| _
d S )N  r   r   r   r   r   r   r   r6   r   r   )r   r   r   r   r    	branch3x3r;   r<   r=   r   r>   r'   r@   r(   r*   r+   r      s@   
		zInceptionB.__init__c                 C   sJ   |  |}| |}| |}| |}| |}tj|||gdd}|S rA   )rH   r;   r<   r=   r>   rB   rC   )r'   r,   rH   rD   r>   r*   r*   r+   r-      s   




zInceptionB.forwardr.   r*   r*   r(   r+   rE      s    $rE   c                       r   )
InceptionCc                    s  t    t|dddtjd| _t||dddtjd| _t||dddtjd| _t|dddd	tjd| _t||ddtjd| _	t||dd	tjd| _
t||ddtjd| _t||dd	tjd| _t|dddtjd| _td
dddd| _t|dddtjd| _d S )Nr   r   r   r   r   r      r   r   rL   r   r   r   r   Fr7   )r   r   r   r   r    r8   branch7x7_1branch7x7_2branch7x7_3branch7x7dbl_1branch7x7dbl_2branch7x7dbl_3branch7x7dbl_4branch7x7dbl_5r   r>   r?   )r'   r@   Zchannels_7x7r(   r*   r+   r      s   
	zInceptionC.__init__c                 C   s   |  |}| |}| |}| |}| |}| |}| |}| |}| |}| 	|}| 
|}tj||||gdd}|S rA   )r8   rP   rQ   rR   rS   rT   rU   rV   rW   r>   r?   rB   rC   )r'   r,   r8   Z	branch7x7Zbranch7x7dblr>   r*   r*   r+   r-   ,  s   










zInceptionC.forwardr.   r*   r*   r(   r+   rJ      s    RrJ   c                       r   )
InceptionDc                    s   t    t|dddtjd| _tdddddtjd| _t|dddtjd| _tddd	d
tjd| _tddddtjd| _	tdddddtjd| _
tddd| _d S )Nr   r   r   r   @  r   r   r   rK   rM   rN   rO   rG   )r   r   r   r   r    branch3x3_1branch3x3_2branch7x7x3_1branch7x7x3_2branch7x7x3_3branch7x7x3_4r   r>   rI   r(   r*   r+   r   D  s\   
		zInceptionD.__init__c                 C   s^   |  |}| |}| |}| |}| |}| |}| |}tj|||gdd}|S rA   )	rZ   r[   r\   r]   r^   r_   r>   rB   rC   )r'   r,   rH   Zbranch7x7x3r>   r*   r*   r+   r-   v  s   






zInceptionD.forwardr.   r*   r*   r(   r+   rX   C  s    2rX   c                       r   )
InceptionEc                    s   t    t|dddtjd| _t|dddtjd| _tddddtjd| _tdddd	tjd| _t|d
ddtjd| _	td
dddtjd| _
tddddtjd| _tdddd	tjd| _tddddd| _t|dddtjd| _d S )NrY   r   r   r   rF   )r   r   )r   r   )r   r   )r   r   i  r   Fr7   r   )r   r   r   r   r    r8   rZ   branch3x3_2abranch3x3_2br;   r<   branch3x3dbl_3abranch3x3dbl_3br   r>   r?   rI   r(   r*   r+   r     s   
zInceptionE.__init__c                 C   s   |  |}| |}| || |g}tj|dd}| |}| |}| || 	|g}tj|dd}| 
|}| |}tj||||gdd}|S rA   )r8   rZ   ra   rb   rB   rC   r;   r<   rc   rd   r>   r?   )r'   r,   r8   rH   rD   r>   r*   r*   r+   r-     s$   





zInceptionE.forwardr.   r*   r*   r(   r+   r`     s    Gr`   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	InceptionV3a"  Inception v3 model from
    `"Rethinking the Inception Architecture for Computer Vision" <https://arxiv.org/pdf/1512.00567.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 Inception v3 model.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.vision.models import InceptionV3

            >>> inception_v3 = InceptionV3()

            >>> x = paddle.rand([1, 3, 299, 299])
            >>> out = inception_v3(x)

            >>> print(out.shape)
            [1, 1000]
      Tc                    s  t    || _|| _g dg dgdgg dg dgdgddgd	| _| jd
 }| jd }| jd }| jd }| jd }t | _t | _	t
t|d D ]}t|d | |d | }	| j	|	 qNt
t|D ]}t|| }
| j	|
 qjt
t|d D ]}t|d | |d | }| j	| qt
t|D ]}t|| }| j	| qt
t|D ]}t|| }| j	| q|rtd| _|dkrtddd| _dtd }td|tt| |dt d| _d S d S )N)r         )r   r   r   rh   )   ri   ri   ri   )      rk   r   ri   i      )inception_ainception_binception_cinception_dinception_erm   ro   rn   rp   rq   r   r   g?Zdownscale_in_infer)pmodeg      ?g      @)Zinitializer)Zweight_attrZ	bias_attr)r   r   num_classes	with_poolZlayers_configr   inception_stemr   Z	LayerListinception_block_listrangelenr3   appendrE   rJ   rX   r`   r   avg_poolr   dropoutmathsqrtr   r   r	   fc)r'   rt   ru   Zinception_a_listZinception_c_listZinception_b_listZinception_d_listZinception_e_listirm   rn   ro   rp   rq   Zstdvr(   r*   r+   r     s^   







zInceptionV3.__init__c                 C   sb   |  |}| jD ]}||}q| jr| |}| jdkr/tj|ddgd}| |}| |}|S )Nr   rl   )shape)	rv   rw   ru   r{   rt   rB   Zreshaper|   r   )r'   r,   Zinception_blockr*   r*   r+   r-   =  s   






zInceptionV3.forward)rf   T)r/   r0   r1   __doc__r   r-   r2   r*   r*   r(   r+   re     s    :re   Fc                 K   s^   t di |}d}| r-|tv sJ | dtt| d t| d }t|}|| |S )a  Inception v3 model from
    `"Rethinking the Inception Architecture for Computer Vision" <https://arxiv.org/pdf/1512.00567.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:`InceptionV3 <api_paddle_vision_models_InceptionV3>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of Inception v3 model.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.vision.models import inception_v3

            >>> # Build model
            >>> model = inception_v3()

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

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

            >>> print(out.shape)
            [1, 1000]
    r   zJ model do not have a pretrained model now, you should set pretrained=Falser   r   Nr*   )re   
model_urlsr
   rB   loadZset_dict)Z
pretrainedkwargsmodelarchZweight_pathparamr*   r*   r+   r   L  s   


)F)r}   rB   r   Zpaddle.base.param_attrr   Z	paddle.nnr   r   r   r   r   Zpaddle.nn.initializerr	   Zpaddle.utils.downloadr
   opsr   __all__r   ZLayerr   r3   rE   rJ   rX   r`   re   r   r*   r*   r*   r+   <module>   s&   6L3jBcd