o
    )i                     @   sp  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 deZ$G dd deZ%G dd deZ&G dd deZ'G dd deZ(eddeddd fddd d!d"d#eee(ef  d$e)d%e)d&ed'e'f
d(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                       sT   e Zd Zdededdf fddZdedefdd	Zdd
ee ddfddZ	  Z
S )QuantizableBasicConv2dargskwargsreturnNc                    s    t  j|i | t | _d S N)super__init__nnReLUreluselfr   r    	__class__ x/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torchvision/models/quantization/googlenet.pyr$      s   zQuantizableBasicConv2d.__init__xc                 C   s"   |  |}| |}| |}|S r"   convZbnr'   r)   r.   r,   r,   r-   forward   s   


zQuantizableBasicConv2d.forwardis_qatc                 C   s   t | g d|dd d S )Nr/   T)Zinplace)r   )r)   r3   r,   r,   r-   
fuse_model$   s   z!QuantizableBasicConv2d.fuse_modelr"   )__name__
__module____qualname__r   r$   r   r2   r   boolr4   __classcell__r,   r,   r*   r-   r      s     r   c                       <   e Zd Zdededdf fddZdedefdd	Z  ZS )
QuantizableInceptionr   r    r!   Nc                    s&   t  j|dti| tj | _d S NZ
conv_block)r#   r$   r   r%   Z	quantizedZFloatFunctionalcatr(   r*   r,   r-   r$   )   s   zQuantizableInception.__init__r.   c                 C   s   |  |}| j|dS )Nr   )_forwardr=   )r)   r.   Zoutputsr,   r,   r-   r2   -   s   
zQuantizableInception.forwardr5   r6   r7   r   r$   r   r2   r9   r,   r,   r*   r-   r;   (   s    r;   c                       r:   )
QuantizableInceptionAuxr   r    r!   Nc                    s$   t  j|dti| t | _d S r<   )r#   r$   r   r%   r&   r'   r(   r*   r,   r-   r$   4   s   z QuantizableInceptionAux.__init__r.   c                 C   sJ   t |d}| |}t|d}| | |}| |}| |}|S )N)   rA   r   )	FZadaptive_avg_pool2dr0   torchflattenr'   Zfc1ZdropoutZfc2r1   r,   r,   r-   r2   8   s   


zQuantizableInceptionAux.forwardr?   r,   r,   r*   r-   r@   2   s    r@   c                       sT   e Zd Zdededdf fddZdedefdd	Zdd
ee	 ddfddZ
  ZS )r   r   r    r!   Nc                    s<   t  j|dtttgi| tjj | _	tjj
 | _d S )Nblocks)r#   r$   r   r;   r@   rC   ZaoZquantizationZ	QuantStubquantZDeQuantStubdequantr(   r*   r,   r-   r$   L   s   zQuantizableGoogLeNet.__init__r.   c                 C   sl   |  |}| |}| |\}}}| |}| jo| j}tj r/|s)t	
d t|||S | |||S )NzCScripted QuantizableGoogleNet always returns GoogleNetOutputs Tuple)Z_transform_inputrF   r>   rG   Ztraining
aux_logitsrC   ZjitZis_scriptingwarningswarnr   Zeager_outputs)r)   r.   aux1aux2Zaux_definedr,   r,   r-   r2   S   s   




zQuantizableGoogLeNet.forwardr3   c                 C   s(   |   D ]}t|tu r|| q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   r4   )r)   r3   mr,   r,   r-   r4   `   s
   
zQuantizableGoogLeNet.fuse_modelr"   )r5   r6   r7   r   r$   r   r   r2   r   r8   r4   r9   r,   r,   r*   r-   r   J   s     r   c                   @   sH   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
dZ	e	Z
dS )r   zKhttps://download.pytorch.org/models/quantized/googlenet_fbgemm-c81f6644.pth   )Z	crop_sizeie )   rQ   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.
            )
Z
num_paramsZmin_size
categoriesbackendZrecipeZunquantizedZ_metricsZ_ops
_file_sizeZ_docs)urlZ
transformsmetaN)r5   r6   r7   r   r   r	   r   r   IMAGENET1K_V1IMAGENET1K_FBGEMM_V1DEFAULTr,   r,   r,   r-   r   m   s*    
r   Zquantized_googlenet)nameZ
pretrainedc                 C   s   |  ddr	tjS tjS )NquantizeF)getr   rY   r   rX   )r    r,   r,   r-   <lambda>   s   
r^   )weightsTF)r_   progressr\   r_   r`   r\   r    r!   c                 K   s   |rt nt| } |dd}| durBd|vrt|dd t|dd t|dd t|dt| jd  d	| jv rBt|d	| jd	  |d	d
}tdi |}t	| |rZt
|| | durz|| j|dd |sud|_d|_d|_|S t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:
    rH   FNZtransform_inputTZinit_weightsZnum_classesrS   rT   rR   )r`   Z
check_hashz`auxiliary heads in the pretrained googlenet model are NOT pretrained, so make sure to train themr,   )r   r   verifyr]   r   lenrW   popr   r   r   Zload_state_dictZget_state_dictrH   rK   rL   rI   rJ   )r_   r`   r\   r    Zoriginal_aux_logitsrT   modelr,   r,   r-   r      s4   ,

r   )*rI   	functoolsr   typingr   r   r   rC   Ztorch.nnr%   r   r   rB   Ztransforms._presetsr	   Z_apir   r   r   _metar   _utilsr   r   r   r   r   r   r   r   r   utilsr   r   r   __all__r   r;   r@   r   r   r8   r,   r,   r,   r-   <module>   sN     
#
