
    ڧg?*                        d dl Z d dlmZ d dlmZmZmZmZ d dlZd dl	m
Z
 d dlm
c mZ d dlmZ d dlmZ d dlmZmZ ddlmZ d	d
lmZmZmZ d	dlmZ d	dlmZmZ ddl m!Z!m"Z"m#Z# g d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/                  Z0 G d dej1                  Z2 G d dej3                  Z4 G d  d!e          Z5 ed"#           ed$d% f&          dd'd(d)d*eee5ef                  d+e6d,e6d-ed.e4f
d/                        Z7dS )0    N)partial)AnyListOptionalUnion)Tensor)	inception)Inception_V3_WeightsInceptionOutputs   )ImageClassification   )register_modelWeightsWeightsEnum)_IMAGENET_CATEGORIES)_ovewrite_named_paramhandle_legacy_interface   )_fuse_modules_replace_reluquantize_model)QuantizableInception3Inception_V3_QuantizedWeightsinception_v3c                   Z     e Zd Zdededdf fdZdedefdZd
dee         ddfd	Z	 xZ
S )QuantizableBasicConv2dargskwargsreturnNc                 j     t                      j        |i | t          j                    | _        d S N)super__init__nnReLUreluselfr   r   	__class__s      e/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/models/quantization/inception.pyr$   zQuantizableBasicConv2d.__init__   s0    $)&)))GII			    xc                     |                      |          }|                     |          }|                     |          }|S r"   convbnr'   )r)   r-   s     r+   forwardzQuantizableBasicConv2d.forward   s4    IIaLLGGAJJIIaLLr,   is_qatc                 0    t          | g d|d           d S )Nr/   T)inplace)r   )r)   r3   s     r+   
fuse_modelz!QuantizableBasicConv2d.fuse_model%   s&    d222FDIIIIIIr,   r"   )__name__
__module____qualname__r   r$   r   r2   r   boolr6   __classcell__r*   s   @r+   r   r      s        c S T       F    J J$ J4 J J J J J J J Jr,   r   c                   <     e Zd Zdededdf fdZdedefdZ xZS )QuantizableInceptionAr   r   r    Nc                      t                      j        |dt          i| t          j                                        | _        d S N
conv_blockr#   r$   r   r%   	quantizedFloatFunctionalmyopr(   s      r+   r$   zQuantizableInceptionA.__init__+   >    $L+ALVLLLL0022			r,   r-   c                 b    |                      |          }| j                            |d          S Nr   _forwardrE   catr)   r-   outputss      r+   r2   zQuantizableInceptionA.forward/   )    --""y}}Wa(((r,   r7   r8   r9   r   r$   r   r2   r;   r<   s   @r+   r>   r>   )   t        3c 3S 3T 3 3 3 3 3 3) )F ) ) ) ) ) ) ) )r,   r>   c                   <     e Zd Zdededdf fdZdedefdZ xZS )QuantizableInceptionBr   r   r    Nc                      t                      j        |dt          i| t          j                                        | _        d S r@   rB   r(   s      r+   r$   zQuantizableInceptionB.__init__6   rF   r,   r-   c                 b    |                      |          }| j                            |d          S rH   rI   rL   s      r+   r2   zQuantizableInceptionB.forward:   rN   r,   rO   r<   s   @r+   rR   rR   4   rP   r,   rR   c                   <     e Zd Zdededdf fdZdedefdZ xZS )QuantizableInceptionCr   r   r    Nc                      t                      j        |dt          i| t          j                                        | _        d S r@   rB   r(   s      r+   r$   zQuantizableInceptionC.__init__A   rF   r,   r-   c                 b    |                      |          }| j                            |d          S rH   rI   rL   s      r+   r2   zQuantizableInceptionC.forwardE   rN   r,   rO   r<   s   @r+   rV   rV   ?   rP   r,   rV   c                   <     e Zd Zdededdf fdZdedefdZ xZS )QuantizableInceptionDr   r   r    Nc                      t                      j        |dt          i| t          j                                        | _        d S r@   rB   r(   s      r+   r$   zQuantizableInceptionD.__init__L   rF   r,   r-   c                 b    |                      |          }| j                            |d          S rH   rI   rL   s      r+   r2   zQuantizableInceptionD.forwardP   rN   r,   rO   r<   s   @r+   rZ   rZ   J   rP   r,   rZ   c                   X     e Zd Zdededdf fdZdedee         fdZdedefdZ xZ	S )	QuantizableInceptionEr   r   r    Nc                     t                      j        |dt          i| t          j                                        | _        t          j                                        | _        t          j                                        | _        d S r@   )	r#   r$   r   r%   rC   rD   myop1myop2myop3r(   s      r+   r$   zQuantizableInceptionE.__init__W   sh    $L+ALVLLL\1133
\1133
\1133


r,   r-   c                 (   |                      |          }|                     |          }|                     |          |                     |          g}| j                            |d          }|                     |          }|                     |          }|                     |          | 	                    |          g}| j
                            |d          }t          j        |ddd          }|                     |          }||||g}|S )Nr   r   )kernel_sizestridepadding)	branch1x1branch3x3_1branch3x3_2abranch3x3_2br`   rK   branch3x3dbl_1branch3x3dbl_2branch3x3dbl_3abranch3x3dbl_3bra   F
avg_pool2dbranch_pool)r)   r-   rg   	branch3x3branch3x3dblrq   rM   s          r+   rJ   zQuantizableInceptionE._forward]   s   NN1%%	$$Q''	&&y1143D3DY3O3OP	JNN9a00	**1--**<88  ..  ..
 z~~lA66l1!AqIII&&{33i{Cr,   c                 b    |                      |          }| j                            |d          S rH   )rJ   rb   rK   rL   s      r+   r2   zQuantizableInceptionE.forwardr   s)    --""z~~gq)))r,   )
r7   r8   r9   r   r$   r   r   rJ   r2   r;   r<   s   @r+   r^   r^   U   s        4c 4S 4T 4 4 4 4 4 4& T&\    ** *F * * * * * * * *r,   r^   c                   ,     e Zd Zdededdf fdZ xZS )QuantizableInceptionAuxr   r   r    Nc                 H     t                      j        |dt          i| d S r@   )r#   r$   r   r(   s      r+   r$   z QuantizableInceptionAux.__init__y   s+    $L+ALVLLLLLr,   )r7   r8   r9   r   r$   r;   r<   s   @r+   rv   rv   w   s_        Mc MS MT M M M M M M M M M Mr,   rv   c                   Z     e Zd Zdededdf fdZdedefdZd
dee	         ddfd	Z
 xZS )r   r   r   r    Nc                 2    t                      j        |dt          t          t          t
          t          t          t          gi| t          j
        j                                        | _        t          j
        j                                        | _        d S )Ninception_blocks)r#   r$   r   r>   rR   rV   rZ   r^   rv   torchaoquantization	QuantStubquantDeQuantStubdequantr(   s      r+   r$   zQuantizableInception3.__init__~   s    	
 '%%%%%'	
 	
 	
 	
 X*4466
x,88::r,   r-   c                    |                      |          }|                     |          }|                     |          \  }}|                     |          }| j        o| j        }t          j                                        r&|st          j
        d           t          ||          S |                     ||          S )NzIScripted QuantizableInception3 always returns QuantizableInception3 Tuple)_transform_inputr   rJ   r   training
aux_logitsr{   jitis_scriptingwarningswarnr   eager_outputs)r)   r-   auxaux_defineds       r+   r2   zQuantizableInception3.forward   s    !!!$$JJqMMq!!3LLOOm79!!## 	. kijjj#As+++%%a---r,   r3   c                     |                                  D ]-}t          |          t          u r|                    |           .dS )a  Fuse conv/bn/relu modules in inception model

        Fuse conv+bn+relu/ conv+relu/conv+bn modules to prepare for quantization.
        Model is modified in place.  Note that this operation does not change numerics
        and the model after modification is in floating point
        N)modulestyper   r6   )r)   r3   ms      r+   r6   z QuantizableInception3.fuse_model   sJ      	% 	%AAww000V$$$	% 	%r,   r"   )r7   r8   r9   r   r$   r   r   r2   r   r:   r6   r;   r<   s   @r+   r   r   }   s        ;c ;S ;T ; ; ; ; ; ;". .$4 . . . .
% 
%$ 
%4 
% 
% 
% 
% 
% 
% 
% 
%r,   r   c                   p    e Zd Z ed eedd          ddeddej        d	d
ddidddd
          Z	e	Z
dS )r   zUhttps://download.pytorch.org/models/quantized/inception_v3_google_fbgemm-a2837893.pthi+  iV  )	crop_sizeresize_sizeir)K   r   fbgemmzdhttps://github.com/pytorch/vision/tree/main/references/classification#post-training-quantized-modelszImageNet-1Kg%CKS@g-VW@)zacc@1zacc@5g'1@gL7A`%7@z
                These weights were produced by doing Post Training Quantization (eager mode) on top of the unquantized
                weights listed below.
            )

num_paramsmin_size
categoriesbackendrecipeunquantized_metrics_ops
_file_size_docs)url
transformsmetaN)r7   r8   r9   r   r   r   r   r
   IMAGENET1K_V1IMAGENET1K_FBGEMM_V1DEFAULT r,   r+   r   r      s        "7c7.#3OOO" .|/=##     
 
  0 #GGGr,   r   quantized_inception_v3)name
pretrainedc                 ^    |                      dd          rt          j        nt          j        S )NquantizeF)getr   r   r
   r   )r   s    r+   <lambda>r      s,    ::j%((04II!/ r,   )weightsTF)r   progressr   r   r   r   r   r    c                    |rt           nt                              |           } |                    dd          }| td|vrt	          |dd           t	          |dd           t	          |dt          | j        d                              d| j        v rt	          |d| j        d                    |                    dd	          }t          di |}t          |           |rt          ||           | N|r|sd|_        d|_        |                    |                     |d
                     |s|sd|_        d|_        |S )a  Inception v3 model architecture from
    `Rethinking the Inception Architecture for Computer Vision <http://arxiv.org/abs/1512.00567>`__.

    .. note::
        **Important**: In contrast to the other models the inception_v3 expects tensors with a size of
        N x 3 x 299 x 299, so ensure your images are sized accordingly.

    .. note::
        Note that ``quantize = True`` returns a quantized model with 8 bit
        weights. Quantized models only support inference and run on CPUs.
        GPU inference is not yet supported.

    Args:
        weights (:class:`~torchvision.models.quantization.Inception_V3_QuantizedWeights` or :class:`~torchvision.models.Inception_V3_Weights`, optional): The pretrained
            weights for the model. See
            :class:`~torchvision.models.quantization.Inception_V3_QuantizedWeights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr.
            Default is True.
        quantize (bool, optional): If True, return a quantized version of the model.
            Default is False.
        **kwargs: parameters passed to the ``torchvision.models.quantization.QuantizableInception3``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/inception.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.quantization.Inception_V3_QuantizedWeights
        :members:

    .. autoclass:: torchvision.models.Inception_V3_Weights
        :members:
        :noindex:
    r   FNtransform_inputTnum_classesr   r   r   )r   
check_hashr   )r   r
   verifyr   r   lenr   popr   r   r   r   	AuxLogitsload_state_dictget_state_dict)r   r   r   r   original_aux_logitsr   models          r+   r   r      ss   d 19R,,>RZZ[bccG **\599F**!&*;TBBBflD999fmSl9S5T5TUUU$$!&)W\)5LMMMjjH--G!++F++E% 'ug&&& 	#/ 	#$E"EOg44hSW4XXYYY 	# 3 	#$E"EOLr,   )8r   	functoolsr   typingr   r   r   r   r{   torch.nnr%   torch.nn.functional
functionalro   r   torchvision.modelsr	   inception_moduletorchvision.models.inceptionr
   r   transforms._presetsr   _apir   r   r   _metar   _utilsr   r   utilsr   r   r   __all__BasicConv2dr   
InceptionAr>   
InceptionBrR   
InceptionCrV   
InceptionDrZ   
InceptionEr^   InceptionAuxrv   
Inception3r   r   r:   r   r   r,   r+   <module>r      s          - - - - - - - - - - - -                       < < < < < < O O O O O O O O 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 ( ( ( ( ( ( C C C C C C C C ? ? ? ? ? ? ? ? ? ?  J J J J J-9 J J J) ) ) ) ),7 ) ) )) ) ) ) ),7 ) ) )) ) ) ) ),7 ) ) )) ) ) ) ),7 ) ) )* * * * *,7 * * *DM M M M M.; M M M)% )% )% )% )%,7 )% )% )%X# # # # #K # # #8 -...	0 	0   UY	C C Ce9;OOPQC C 	C
 C C C C  /.C C Cr,   