
    kh                        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y)(    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                   T     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                 V    t        |   |i | t        j                         | _        y N)super__init__nnReLUreluselfr!   r"   	__class__s      e/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/models/quantization/googlenet.pyr'   zQuantizableBasicConv2d.__init__   s"    $)&)GGI	    xc                 l    | j                  |      }| j                  |      }| j                  |      }|S r%   convbnr*   r,   r0   s     r.   forwardzQuantizableBasicConv2d.forward   s.    IIaLGGAJIIaLr/   is_qatc                 &    t        | g d|d       y )Nr2   T)inplace)r   )r,   r7   s     r.   
fuse_modelz!QuantizableBasicConv2d.fuse_model$   s    d2FDIr/   r%   )__name__
__module____qualname__r   r'   r   r6   r   boolr:   __classcell__r-   s   @r.   r    r       sJ    c S T  F J$ J4 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                 v    t        |   |dt        i| t        j                  j                         | _        y N
conv_block)r&   r'   r    r(   	quantizedFloatFunctionalcatr+   s      r.   r'   zQuantizableInception.__init__)   s/    $L+ALVL<<//1r/   r0   c                 \    | j                  |      }| j                  j                  |d      S )Nr   )_forwardrH   )r,   r0   outputss      r.   r6   zQuantizableInception.forward-   s%    --"xx||GQ''r/   r;   r<   r=   r   r'   r   r6   r?   r@   s   @r.   rB   rB   (   s0    2c 2S 2T 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                 b    t        |   |dt        i| t        j                         | _        y rD   )r&   r'   r    r(   r)   r*   r+   s      r.   r'   z QuantizableInceptionAux.__init__4   s(    $L+ALVLGGI	r/   r0   c                    t        j                  |d      }| j                  |      }t        j                  |d      }| j                  | j                  |            }| j                  |      }| j                  |      }|S )N)   rQ   r   )	Fadaptive_avg_pool2dr3   torchflattenr*   fc1dropoutfc2r5   s     r.   r6   zQuantizableInceptionAux.forward8   sh    !!!V,IIaLMM!QIIdhhqk"LLOHHQK r/   rL   r@   s   @r.   rN   rN   2   s0    c S T  F r/   rN   c                   T     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        |   |dt        t        t        gi| t
        j                  j                  j                         | _	        t
        j                  j                  j                         | _        y )Nblocks)r&   r'   r    rB   rN   rT   aoquantization	QuantStubquantDeQuantStubdequantr+   s      r.   r'   zQuantizableGoogLeNet.__init__L   sb    	
13GI`a	
ek	
 XX**446
xx,,88:r/   r0   c                 p   | j                  |      }| j                  |      }| j                  |      \  }}}| j                  |      }| j                  xr | j
                  }t        j                  j                         r$|st        j                  d       t        |||      S | j                  |||      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    !!!$JJqMa(4LLOmm799!!#cd#AtT22%%at44r/   r7   c                 t    | j                         D ]%  }t        |      t        u s|j                  |       ' y)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`   s2      	%AAw00V$	%r/   r%   )r;   r<   r=   r   r'   r   r   r6   r   r>   r:   r?   r@   s   @r.   r   r   J   sH    ;c ;S ;T ;5 5$4 5
%$ 
%4 
%r/   r   c                   j    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
y)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_    "Y.#>! .|,::##   
0 #Gr/   r   quantized_googlenet)name
pretrainedc                 f    | j                  dd      rt        j                  S t        j                  S )NquantizeF)getr   r   r   r   )r"   s    r.   <lambda>r      s+    ::j%( 2FF ,, r/   )weightsTF)r   progressr   r   r   r   r"   r#   c                 d   |rt         nt        j                  |       } |j                  dd      }| vd|vrt	        |dd       t	        |dd       t	        |dd       t	        |dt        | j                  d                d	| j                  v rt	        |d	| j                  d	          |j                  d	d
      }t        di |}t        |       |rt        ||       | P|j                  | j                  |d             |sd|_        d|_        d|_        |S 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TTU\]G **\59F*!&*;TBflD9fne<fmSl9S5TU$!&)W\\)5LMjjH-G *6*E%ug&g44hSW4XY"$EEJEJ L	 MM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 7 7 ( C l l ? ?J[ J(9 (l 0 %9  %F# #8 *+	- OS	@e68IIJK@ @ 	@
 @ @ ,@r/   