
    ڧg                        d dl Z d dlmZ d dlmZmZmZ d dlZd dlm	Z	 d dlm
Z
 d dl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mZmZmZ ddlm Z m!Z!m"Z" g dZ# G d de          Z$ G d de          Z% G d de          Z& G d de          Z' G d de          Z( ed           edd f          ddd d!d"eee(ef                  d#e)d$e)d%ed&e'f
d'                        ZdS )(    N)partial)AnyOptionalUnion)Tensor)
functional   )ImageClassification   )register_modelWeightsWeightsEnum)_IMAGENET_CATEGORIES)_ovewrite_named_paramhandle_legacy_interface)BasicConv2d	GoogLeNetGoogLeNet_WeightsGoogLeNetOutputs	InceptionInceptionAux   )_fuse_modules_replace_reluquantize_model)QuantizableGoogLeNetGoogLeNet_QuantizedWeights	googlenetc                   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/googlenet.pyr'   zQuantizableBasicConv2d.__init__   s0    $)&)))GII			    xc                     |                      |          }|                     |          }|                     |          }|S r%   convbnr*   r,   r0   s     r.   forwardzQuantizableBasicConv2d.forward   s4    IIaLLGGAJJIIaLLr/   is_qatc                 0    t          | g d|d           d S )Nr2   T)inplace)r   )r,   r7   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   r6   r   boolr:   __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 )QuantizableInceptionr!   r"   r#   Nc                      t                      j        |dt          i| t          j                                        | _        d S N
conv_block)r&   r'   r    r(   	quantizedFloatFunctionalcatr+   s      r.   r'   zQuantizableInception.__init__)   s>    $L+ALVLLL<//11r/   r0   c                 b    |                      |          }| j                            |d          S )Nr   )_forwardrH   )r,   r0   outputss      r.   r6   zQuantizableInception.forward-   s)    --""x||GQ'''r/   r;   r<   r=   r   r'   r   r6   r?   r@   s   @r.   rB   rB   (   st        2c 2S 2T 2 2 2 2 2 2( (F ( ( ( ( ( ( ( (r/   rB   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 )QuantizableInceptionAuxr!   r"   r#   Nc                 x     t                      j        |dt          i| t          j                    | _        d S rD   )r&   r'   r    r(   r)   r*   r+   s      r.   r'   z QuantizableInceptionAux.__init__4   s6    $L+ALVLLLGII			r/   r0   c                 (   t          j        |d          }|                     |          }t          j        |d          }|                     |                     |                    }|                     |          }|                     |          }|S )N)   rQ   r   )	Fadaptive_avg_pool2dr3   torchflattenr*   fc1dropoutfc2r5   s     r.   r6   zQuantizableInceptionAux.forward8   st    !!V,,IIaLLM!QIIdhhqkk""LLOOHHQKK r/   rL   r@   s   @r.   rN   rN   2   st        c S T       F        r/   rN   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                     t                      j        |dt          t          t          gi| t
          j        j                                        | _	        t
          j        j        
                                | _        d S )Nblocks)r&   r'   r    rB   rN   rT   aoquantization	QuantStubquantDeQuantStubdequantr+   s      r.   r'   zQuantizableGoogLeNet.__init__L   sp    	
13GI`a	
ek	
 	
 	
 X*4466
x,88::r/   r0   c                    |                      |          }|                     |          }|                     |          \  }}}|                     |          }| j        o| j        }t          j                                        r'|st          j
        d           t          |||          S |                     |||          S )NzCScripted QuantizableGoogleNet always returns GoogleNetOutputs Tuple)_transform_inputr_   rJ   ra   training
aux_logitsrT   jitis_scriptingwarningswarnr   eager_outputs)r,   r0   aux1aux2aux_defineds        r.   r6   zQuantizableGoogLeNet.forwardS   s    !!!$$JJqMMa((4LLOOm79!!## 	5 ecddd#AtT222%%at444r/   r7   c                     |                                  D ]-}t          |          t          u r|                    |           .dS )a  Fuse conv/bn/relu modules in googlenet 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    r:   )r,   r7   ms      r.   r:   zQuantizableGoogLeNet.fuse_model`   sJ      	% 	%AAww000V$$$	% 	%r/   r%   )r;   r<   r=   r   r'   r   r   r6   r   r>   r:   r?   r@   s   @r.   r   r   J   s        ;c ;S ;T ; ; ; ; ; ;5 5$4 5 5 5 5
% 
%$ 
%4 
% 
% 
% 
% 
% 
% 
% 
%r/   r   c                   n    e Zd Z ed eed          ddeddej        dd	d
didddd
          Z	e	Z
dS )r   zKhttps://download.pytorch.org/models/quantized/googlenet_fbgemm-c81f6644.pth   )	crop_sizeie )   ru   fbgemmzdhttps://github.com/pytorch/vision/tree/main/references/classification#post-training-quantized-modelszImageNet-1Kg/tQ@g`"YV@)zacc@1zacc@5g+?g#~j<)@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)r;   r<   r=   r   r   r
   r   r   IMAGENET1K_V1IMAGENET1K_FBGEMM_V1DEFAULT r/   r.   r   r   m   s        "7Y7.#>>>! .|,:##     
 
  0 #GGGr/   r   quantized_googlenet)name
pretrainedc                 ^    |                      dd          rt          j        nt          j        S )NquantizeF)getr   r   r   r   )r"   s    r.   <lambda>r      s,    ::j%((-1FF, r/   )weightsTF)r   progressr   r   r   r   r"   r#   c                    |rt           nt                              |           } |                    dd          }| d|vrt	          |dd           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          ||           | V|                    |                     |d                     |sd|_        d|_        d|_        nt!          j        d           |S )a  GoogLeNet (Inception v1) model architecture from `Going Deeper with Convolutions <http://arxiv.org/abs/1409.4842>`__.

    .. 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.GoogLeNet_QuantizedWeights` or :class:`~torchvision.models.GoogLeNet_Weights`, optional): The
            pretrained weights for the model. See
            :class:`~torchvision.models.quantization.GoogLeNet_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.QuantizableGoogLeNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/googlenet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.quantization.GoogLeNet_QuantizedWeights
        :members:

    .. autoclass:: torchvision.models.GoogLeNet_Weights
        :members:
        :noindex:
    re   FNtransform_inputTinit_weightsnum_classesry   rz   rv   )r   
check_hashz`auxiliary heads in the pretrained googlenet model are NOT pretrained, so make sure to train themr   )r   r   verifyr   r   lenr   popr   r   r   load_state_dictget_state_dictre   rk   rl   rh   ri   )r   r   r   r"   original_aux_logitsrz   models          r.   r   r      s   X .6L));LTTU\]]G **\599F**!&*;TBBBflD999fne<<<fmSl9S5T5TUUU$$!&)W\)5LMMMjjH--G **6**E% 'ug&&&g44hSW4XXYYY" 	$EEJEJJMr   Lr/   )*rh   	functoolsr   typingr   r   r   rT   torch.nnr(   r   r   rR   transforms._presetsr
   _apir   r   r   _metar   _utilsr   r   r   r   r   r   r   r   r   utilsr   r   r   __all__r    rB   rN   r   r   r>   r   r/   r.   <module>r      s          ' ' ' ' ' ' ' ' ' '              $ $ $ $ $ $ 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 ( ( ( ( ( ( C C C C C C C C l l l l l l l l l l l l l l l l ? ? ? ? ? ? ? ? ? ?  J J J J J[ J J J( ( ( ( (9 ( ( (    l   0 %  %  %  %  %9  %  %  %F# # # # # # # #8 *+++	- 	-   OS	@ @ @e68IIJK@ @ 	@
 @ @ @ @  ,+@ @ @r/   