
    {-jM                       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rd dl	mZ  G d de          Zg ZddiZ G d de
j                  Z G d de
j                  Z G d de
j                  Z  G d de
j                  Z! G d de
j                  Z" G d de
j                  Z# G d de
j                  Z$	 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                  $    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__     `/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/vision/models/inceptionv3.pyr   r   (   s*         %%%%$$$$$$r   r   inception_v3)z>https://paddle-hapi.bj.bcebos.com/models/inception_v3.pdparams 649a4547c3243e8b59c656f41fe330b8c                  (     e Zd Zd fdZddZ xZS )	InceptionStemreturnNonec                   t                                                       t          dddddt          j                  | _        t          dddddt          j                  | _        t          ddddt          j                  | _        t          ddd	          | _	        t          dd
ddt          j                  | _
        t          d
dddt          j                  | _        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__s    r   r5   zInceptionStem.__init__8   s   -W
 
 
 .W
 
 
 .W
 
 
 "a1EEE-W
 
 
 .W
 
 
r   xr   c                ,   |                      |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }|S )N)r7   r8   r9   r:   r;   r<   )r=   r?   s     r   forwardzInceptionStem.forwardb   s    QQQMM!QQMM!r   )r#   r$   r?   r   r#   r   r   r   r   r5   rA   __classcell__r>   s   @r   r"   r"   7   sR        (
 (
 (
 (
 (
 (
T       r   r"   c                  (     e Zd Zd
 fdZdd	Z xZS )
InceptionAnum_channelsintpool_featuresr#   r$   c                b   t                                                       t          |dddt          j                  | _        t          |dddt          j                  | _        t          ddddt          j                  | _        t          |dddt          j                  | _        t          ddd	dt          j                  | _	        t          ddd	dt          j                  | _
        t          d	ddd
          | _        t          ||ddt          j                  | _        d S )Nr0   r/   r   r1   0      r   `   r&   Fr+   r,   r-   	exclusive)r4   r5   r   r   r6   	branch1x1branch5x5_1branch5x5_2branch3x3dbl_1branch3x3dbl_2branch3x3dbl_3r   branch_poolbranch_pool_conv)r=   rH   rJ   r>   s      r   r5   zInceptionA.__init__n   sd   +$W
 
 
 .$W
 
 
 .W
 
 
 1$W
 
 
 1W
 
 
 1W
 
 
 %!Q%
 
 
 !3$&W!
 !
 !
r   r?   r   c                   |                      |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }t          j	        ||||gd          }|S Nr/   )axis)
rQ   rR   rS   rT   rU   rV   rW   rX   paddleconcat)r=   r?   rQ   	branch5x5branch3x3dblrW   s         r   rA   zInceptionA.forward   s    NN1%%	$$Q''	$$Y//	**1--**<88**<88&&q))++K88M	<=A
 
 
 r   )rH   rI   rJ   rI   r#   r$   rB   rC   rE   s   @r   rG   rG   m   sR        8
 8
 8
 8
 8
 8
t       r   rG   c                  (     e Zd Zd	 fdZd
dZ xZS )
InceptionBrH   rI   r#   r$   c                   t                                                       t          |ddddt          j                  | _        t          |dddt          j                  | _        t          dd	ddt          j                  | _        t          d	d	dddt          j                  | _        t          dd
          | _
        d S )N  r&   r   r   r(   r0   r/   r1   rN   r+   r,   )r4   r5   r   r   r6   	branch3x3rT   rU   rV   r   rW   r=   rH   r>   s     r   r5   zInceptionB.__init__   s    +$W
 
 
 1$W
 
 
 1W
 
 
 1W
 
 
 %1===r   r?   r   c                
   |                      |          }|                     |          }|                     |          }|                     |          }|                     |          }t          j        |||gd          }|S rZ   )re   rT   rU   rV   rW   r\   r]   )r=   r?   re   r_   rW   s        r   rA   zInceptionB.forward   s    NN1%%	**1--**<88**<88&&q))M9lK@qIIIr   rH   rI   r#   r$   rB   rC   rE   s   @r   ra   ra      sR        "> "> "> "> "> ">H       r   ra   c                  (     e Zd Zd
 fdZdd	Z xZS )
InceptionCrH   rI   channels_7x7r#   r$   c                :   t                                                       t          |dddt          j                  | _        t          ||dddt          j                  | _        t          ||dddt          j                  | _        t          |dddd	t          j                  | _        t          ||ddt          j                  | _	        t          ||dd	t          j                  | _
        t          ||ddt          j                  | _        t          ||dd	t          j                  | _        t          |dddt          j                  | _        t          d
ddd          | _        t          |dddt          j                  | _        d S )Nr3   r/   r   r1   r(   r/      r   r&   rn   r/   r&   r   r&   FrO   )r4   r5   r   r   r6   rQ   branch7x7_1branch7x7_2branch7x7_3branch7x7dbl_1branch7x7dbl_2branch7x7dbl_3branch7x7dbl_4branch7x7dbl_5r   rW   rX   )r=   rH   rk   r>   s      r   r5   zInceptionC.__init__   s   +$W
 
 
 .$%W
 
 
 .$%W
 
 
 .$W
 
 
 1$%W
 
 
 1$%W
 
 
 1$%W
 
 
 1$%W
 
 
 1$W
 
 
 %!Q%
 
 
 !3$W!
 !
 !
r   r?   r   c                   |                      |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }| 	                    |          }| 
                    |          }t          j        ||||gd          }|S rZ   )rQ   rr   rs   rt   ru   rv   rw   rx   ry   rW   rX   r\   r]   )r=   r?   rQ   	branch7x7branch7x7dblrW   s         r   rA   zInceptionC.forward?  s    NN1%%	$$Q''	$$Y//	$$Y//	**1--**<88**<88**<88**<88&&q))++K88M	<=A
 
 
 r   )rH   rI   rk   rI   r#   r$   rB   rC   rE   s   @r   rj   rj      sX        P
 P
 P
 P
 P
 P
d       r   rj   c                  (     e Zd Zd	 fdZd
dZ xZS )
InceptionDrH   rI   r#   r$   c                   t                                                       t          |dddt          j                  | _        t          dddddt          j                  | _        t          |dddt          j                  | _        t          ddd	d
t          j                  | _        t          ddddt          j                  | _	        t          dddddt          j                  | _
        t          dd          | _        d S )Nr3   r/   r   r1   @  r&   r   r(   rm   ro   rp   rq   rd   )r4   r5   r   r   r6   branch3x3_1branch3x3_2branch7x7x3_1branch7x7x3_2branch7x7x3_3branch7x7x3_4r   rW   rf   s     r   r5   zInceptionD.__init__W  s6   -$W
 
 
 .W
 
 
 0$W
 
 
 0W
 
 
 0W
 
 
 0W
 
 
 %1===r   r?   r   c                ^   |                      |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }t          j        |||gd          }|S rZ   )	r   r   r   r   r   r   rW   r\   r]   )r=   r?   re   branch7x7x3rW   s        r   rA   zInceptionD.forward  s    $$Q''	$$Y//	((++((55((55((55&&q))M9k;?aHHHr   rh   rB   rC   rE   s   @r   r~   r~   V  sR        0> 0> 0> 0> 0> 0>d       r   r~   c                  (     e Zd Zd	 fdZd
dZ xZS )
InceptionErH   rI   r#   r$   c                   t                                                       t          |dddt          j                  | _        t          |dddt          j                  | _        t          ddddt          j                  | _        t          dddd	t          j                  | _        t          |d
ddt          j                  | _	        t          d
dddt          j                  | _
        t          ddddt          j                  | _        t          dddd	t          j                  | _        t          dddd          | _        t          |dddt          j                  | _        d S )Nr   r/   r   r1   rc   )r/   r&   )r   r/   )r&   r/   )r/   r   i  r&   FrO   r3   )r4   r5   r   r   r6   rQ   r   branch3x3_2abranch3x3_2brT   rU   branch3x3dbl_3abranch3x3dbl_3br   rW   rX   rf   s     r   r5   zInceptionE.__init__  s   +$W
 
 
 .$W
 
 
 /W
 
 
 /W
 
 
 1$W
 
 
 1W
 
 
  2W 
  
  
  2W 
  
  
 %!Q%
 
 
 !3$W!
 !
 !
r   r?   r   c                6   |                      |          }|                     |          }|                     |          |                     |          g}t	          j        |d          }|                     |          }|                     |          }|                     |          | 	                    |          g}t	          j        |d          }| 
                    |          }|                     |          }t	          j        ||||gd          }|S rZ   )rQ   r   r   r   r\   r]   rT   rU   r   r   rW   rX   )r=   r?   rQ   re   r_   rW   s         r   rA   zInceptionE.forward  s   NN1%%	$$Q''	i((i((
	 M)!444	**1--**<88  ..  ..
 }\:::&&q))++K88M	<=A
 
 
 r   rh   rB   rC   rE   s   @r   r   r     sX        E
 E
 E
 E
 E
 E
N       r   r   c                  D     e Zd ZU dZded<   ded<   dd fd
ZddZ x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:: pycon

            >>> 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)
            paddle.Size([1, 1000])
    rI   r   boolr     Tr#   r$   c           
        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          j                    | _	        t          t          |d                             D ]D}t          |d         |         |d         |                   }	| j	                            |	           Et          t          |                    D ]1}t          ||                   }
| j	                            |
           2t          t          |d                             D ]D}t          |d         |         |d         |                   }| j	                            |           Et          t          |                    D ]1}t!          ||                   }| j	                            |           2t          t          |                    D ]1}t#          ||                   }| j	                            |           2|rt%          d          | _        |dk    rpt)          dd          | _        dt-          j        d          z  }t1          d|t3          t5          | |                    t3                                | _        d S d S )N)r3         )r'   r0   r0   r   )   r   r   r   )      r   r3   r   i      )inception_ainception_binception_cinception_dinception_er   r   r   r   r   r   r/   g?downscale_in_infer)pmodeg      ?g      @)initializer)weight_attr	bias_attr)r4   r5   r   r   layers_configr"   inception_stemr   	LayerListinception_block_listrangelenrG   appendra   rj   r~   r   r
   avg_poolr   dropoutmathsqrtr   r	   r   fc)r=   r   r   inception_a_listinception_c_listinception_b_listinception_d_listinception_e_listir   r   r   r   r   stdvr>   s                  r   r5   zInceptionV3.__init__  s   &"+OO\\\:50002F2F2FG5 $<
 
  -m<-m<-m<-m<-m<+oo$&LNN!s+A.//00 	: 	:A$ #A&(8(;A(> K %,,[9999s+,,-- 	: 	:A$%5a%899K%,,[9999s+A.//00 	: 	:A$ #A&(8(;A(> K %,,[9999s+,,-- 	: 	:A$%5a%899K%,,[9999s+,,-- 	: 	:A$%5a%899K%,,[9999 	1-a00DM??"S/CDDDDL:...D%'4%2F2FGGG#++	  DGGG ?r   r?   r   c                ,   |                      |          }| j        D ]} ||          }| j        r|                     |          }| j        dk    rBt          j        |ddg          }|                     |          }|                     |          }|S )Nr   r   )shape)	r   r   r   r   r   r\   reshaper   r   )r=   r?   inception_blocks      r   rA   zInceptionV3.forwardS  s    ""#8 	# 	#O""AA> 	!a  AaqT
333AQA

Ar   )r   T)r   rI   r   r   r#   r$   rB   )r   r   r   __doc__r   r5   rA   rD   rE   s   @r   r   r     sz          4 OOO8 8 8 8 8 8 8t       r   r   F
pretrainedr   kwargsUnpack[_InceptionV3Options]r#   c                   t          di |}d}| rq|t          v sJ | d            t          t          |         d         t          |         d                   }t          j        |          }|                    |           |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:: pycon

            >>> 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)
            paddle.Size([1, 1000])
    r   zJ model do not have a pretrained model now, you should set pretrained=Falser   r/   r   )r   
model_urlsr   r\   loadset_dict)r   r   modelarchweight_pathparams         r   r   r   b  s    @ !!&!!ED 	z!!!___ "!! 0tQD!1!!4
 
 K((uLr   )F)r   r   r   r   r#   r   )&
__future__r   r   typingr   r   typing_extensionsr   r   r\   r   paddle.base.param_attrr	   	paddle.nnr
   r   r   r   r   paddle.nn.initializerr   paddle.utils.downloadr   opsr   r   r   __all__r   Layerr"   rG   ra   rj   r~   r   r   r   r   r   r   <module>r      s   # " " " " "        
       
        , , , , , , N N N N N N N N N N N N N N ) ) ) ) ) ) ; ; ; ; ; ; $ $ $ $ $ $ %% % % % %i % % %
   
3 3 3 3 3BH 3 3 3lI I I I I I I IX0 0 0 0 0 0 0 0fg g g g g g g gT? ? ? ? ? ? ? ?D` ` ` ` ` ` ` `Fd d d d d"( d d dP , , , , , , ,r   