o
    + iM                     @  sP  d dl mZ d dlZd dlmZmZ d dlmZm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 erZd dl	mZ G dd de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%dZ%dS )(    )annotationsN)TYPE_CHECKING	TypedDict)NotRequiredUnpack)nn)	ParamAttr)AdaptiveAvgPool2D	AvgPool2DDropoutLinear	MaxPool2D)Uniform)get_weights_path_from_url   )ConvNormActivation)Tensorc                   @  s   e Zd ZU ded< ded< dS )_InceptionV3OptionszNotRequired[int]num_classeszNotRequired[bool]	with_poolN)__name__
__module____qualname____annotations__ r   r   l/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/vision/models/inceptionv3.pyr   (   s   
 r   inception_v3)z>https://paddle-hapi.bj.bcebos.com/models/inception_v3.pdparamsZ 649a4547c3243e8b59c656f41fe330b8c                      s(   e Zd Zd	 fddZd
ddZ  ZS )InceptionStemreturnNonec                   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__r   r   r/   8   sN   
zInceptionStem.__init__xr   c                 C  sJ   |  |}| |}| |}| |}| |}| |}| |}|S )N)r1   r2   r3   r4   r5   r6   )r7   r:   r   r   r   forwardb   s   






zInceptionStem.forward)r   r   r:   r   r   r   r   r   r   r/   r;   __classcell__r   r   r8   r   r   7   s    *r   c                      (   e Zd Zd fddZdd
dZ  ZS )
InceptionAnum_channelsintpool_featuresr   r   c                   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   r0   	branch1x1branch5x5_1branch5x5_2branch3x3dbl_1branch3x3dbl_2branch3x3dbl_3r
   branch_poolbranch_pool_conv)r7   rA   rC   r8   r   r   r/   n   sj   
zInceptionA.__init__r:   r   c                 C  sj   |  |}| |}| |}| |}| |}| |}| |}| |}tj	||||gdd}|S Nr)   )Zaxis)
rH   rI   rJ   rK   rL   rM   rN   rO   paddleconcat)r7   r:   rH   Z	branch5x5branch3x3dblrN   r   r   r   r;      s   







zInceptionA.forward)rA   rB   rC   rB   r   r   r<   r=   r   r   r8   r   r@   m   s    :r@   c                      (   e Zd Zd fddZdd	d
Z  ZS )
InceptionBrA   rB   r   r   c                   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+   rF   r%   r&   )r.   r/   r   r   r0   	branch3x3rK   rL   rM   r   rN   r7   rA   r8   r   r   r/      s@   
		zInceptionB.__init__r:   r   c                 C  sJ   |  |}| |}| |}| |}| |}tj|||gdd}|S rP   )rX   rK   rL   rM   rN   rQ   rR   )r7   r:   rX   rS   rN   r   r   r   r;      s   




zInceptionB.forwardrA   rB   r   r   r<   r=   r   r   r8   r   rU      s    $rU   c                      r?   )
InceptionCrA   rB   channels_7x7r   r   c                   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    r^   r)   r    r   r    FrG   )r.   r/   r   r   r0   rH   branch7x7_1branch7x7_2branch7x7_3branch7x7dbl_1branch7x7dbl_2branch7x7dbl_3branch7x7dbl_4branch7x7dbl_5r
   rN   rO   )r7   rA   r\   r8   r   r   r/      s   
	zInceptionC.__init__r:   r   c                 C  s   |  |}| |}| |}| |}| |}| |}| |}| |}| |}| 	|}| 
|}tj||||gdd}|S rP   )rH   rb   rc   rd   re   rf   rg   rh   ri   rN   rO   rQ   rR   )r7   r:   rH   Z	branch7x7Zbranch7x7dblrN   r   r   r   r;   ?  s   










zInceptionC.forward)rA   rB   r\   rB   r   r   r<   r=   r   r   r8   r   r[      s    Rr[   c                      rT   )
InceptionDrA   rB   r   r   c                   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"   r]   r_   r`   ra   rW   )r.   r/   r   r   r0   branch3x3_1branch3x3_2branch7x7x3_1branch7x7x3_2branch7x7x3_3branch7x7x3_4r   rN   rY   r8   r   r   r/   W  s\   
		zInceptionD.__init__r:   r   c                 C  s^   |  |}| |}| |}| |}| |}| |}| |}tj|||gdd}|S rP   )	rl   rm   rn   ro   rp   rq   rN   rQ   rR   )r7   r:   rX   Zbranch7x7x3rN   r   r   r   r;     s   






zInceptionD.forwardrZ   r<   r=   r   r   r8   r   rj   V  s    2rj   c                      rT   )
InceptionErA   rB   r   r   c                   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 )Nrk   r)   r   r+   rV   )r)   r    )r   r)   )r    r)   )r)   r   i  r    FrG   r-   )r.   r/   r   r   r0   rH   rl   branch3x3_2abranch3x3_2brK   rL   branch3x3dbl_3abranch3x3dbl_3br
   rN   rO   rY   r8   r   r   r/     s   
zInceptionE.__init__r:   r   c                 C  s   |  |}| |}| || |g}tj|dd}| |}| |}| || 	|g}tj|dd}| 
|}| |}tj||||gdd}|S rP   )rH   rl   rs   rt   rQ   rR   rK   rL   ru   rv   rN   rO   )r7   r:   rH   rX   rS   rN   r   r   r   r;     s$   





zInceptionE.forwardrZ   r<   r=   r   r   r8   r   rr     s    Grr   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 )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]
    rB   r   boolr     Tr   r   c                   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*   r{   )   r|   r|   r|   )      r~   r-   r|   i      )inception_ainception_binception_cinception_dinception_er   r   r   r   r   r   r)   g?Zdownscale_in_infer)pmodeg      ?g      @)Zinitializer)Zweight_attrZ	bias_attr)r.   r/   r   r   Zlayers_configr   inception_stemr   Z	LayerListinception_block_listrangelenr@   appendrU   r[   rj   rr   r	   avg_poolr   dropoutmathsqrtr   r   r   fc)r7   r   r   Zinception_a_listZinception_c_listZinception_b_listZinception_d_listZinception_e_listir   r   r   r   r   Zstdvr8   r   r   r/     s^   







zInceptionV3.__init__r:   r   c                 C  sb   |  |}| jD ]}||}q| jr| |}| jdkr/tj|ddgd}| |}| |}|S )Nr   r   )shape)	r   r   r   r   r   rQ   Zreshaper   r   )r7   r:   Zinception_blockr   r   r   r;   S  s   






zInceptionV3.forward)ry   T)r   rB   r   rx   r   r   r<   )r   r   r   __doc__r   r/   r;   r>   r   r   r8   r   rw     s   
 :rw   F
pretrainedrx   kwargsUnpack[_InceptionV3Options]r   c                 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   )rw   
model_urlsr   rQ   loadZset_dict)r   r   modelarchZweight_pathparamr   r   r   r   b  s    


)F)r   rx   r   r   r   rw   )&
__future__r   r   typingr   r   Ztyping_extensionsr   r   rQ   r   Zpaddle.base.param_attrr   Z	paddle.nnr	   r
   r   r   r   Zpaddle.nn.initializerr   Zpaddle.utils.downloadr   opsr   r   r   __all__r   ZLayerr   r@   rU   r[   rj   rr   rw   r   r   r   r   r   <module>   s4   6L3jBch