a
    PSic                      @   s  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 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d fddddd eee'ef  e(e(ee&d!d"d#Zdd$lm)Z) dd%lm*Z* e)d&e'j+j,iZ-dS )'    N)partial)AnyOptionalUnion)Tensor)
functional   )ImageClassification   )WeightsEnumWeights)_IMAGENET_CATEGORIES)handle_legacy_interface_ovewrite_named_param)GoogLeNetOutputsBasicConv2d	InceptionInceptionAux	GoogLeNetGoogLeNet_Weights   )_fuse_modules_replace_reluquantize_model)QuantizableGoogLeNetGoogLeNet_QuantizedWeights	googlenetc                       sL   e Zd Zeedd fddZeedddZdee ddd	d
Z	  Z
S )QuantizableBasicConv2dNargskwargsreturnc                    s    t  j|i | t | _d S N)super__init__nnReLUreluselfr   r    	__class__ e/var/www/html/django/DPS/env/lib/python3.9/site-packages/torchvision/models/quantization/googlenet.pyr$      s    zQuantizableBasicConv2d.__init__xr!   c                 C   s"   |  |}| |}| |}|S r"   convbnr'   r)   r/   r,   r,   r-   forward   s    


zQuantizableBasicConv2d.forwardis_qatr!   c                 C   s   t | g d|dd d S )Nr0   T)inplace)r   )r)   r6   r,   r,   r-   
fuse_model$   s    z!QuantizableBasicConv2d.fuse_model)N)__name__
__module____qualname__r   r$   r   r4   r   boolr8   __classcell__r,   r,   r*   r-   r      s   r   c                       s6   e Zd Zeedd fddZeedddZ  ZS )QuantizableInceptionNr   c                    s&   t  j|dti| tj | _d S N
conv_block)r#   r$   r   r%   	quantizedFloatFunctionalcatr(   r*   r,   r-   r$   )   s    zQuantizableInception.__init__r.   c                 C   s   |  |}| j|dS )Nr   )_forwardrC   )r)   r/   outputsr,   r,   r-   r4   -   s    
zQuantizableInception.forwardr9   r:   r;   r   r$   r   r4   r=   r,   r,   r*   r-   r>   (   s   r>   c                       s6   e Zd Zeedd fddZeedddZ  ZS )QuantizableInceptionAuxNr   c                    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)   rH   r   )	Fadaptive_avg_pool2dr1   torchflattenr'   fc1dropoutfc2r3   r,   r,   r-   r4   8   s    


zQuantizableInceptionAux.forwardrF   r,   r,   r*   r-   rG   2   s   rG   c                       sL   e Zd Zeedd fddZeedddZdee	 ddd	d
Z
  ZS )r   Nr   c                    s<   t  j|dtttgi| tjj | _	tjj
 | _d S )Nblocks)r#   r$   r   r>   rG   rK   aoquantization	QuantStubquantDeQuantStubdequantr(   r*   r,   r-   r$   L   s    zQuantizableGoogLeNet.__init__r.   c                 C   sp   |  |}| |}| |\}}}| |}| jo8| j}tj r^|sRt	
d t|||S | |||S d S )NzCScripted QuantizableGoogleNet always returns GoogleNetOutputs Tuple)_transform_inputrT   rD   rV   training
aux_logitsrK   jitis_scriptingwarningswarnr   eager_outputs)r)   r/   aux1aux2aux_definedr,   r,   r-   r4   S   s    




zQuantizableGoogLeNet.forwardr5   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   r8   )r)   r6   mr,   r,   r-   r8   `   s    zQuantizableGoogLeNet.fuse_model)N)r9   r:   r;   r   r$   r   r   r4   r   r<   r8   r=   r,   r,   r*   r-   r   J   s   r   c                   @   sD   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Z	e	Z
dS )r   zKhttps://download.pytorch.org/models/quantized/googlenet_fbgemm-c00238cf.pth   )	crop_sizeie )   rg   fbgemmzdhttps://github.com/pytorch/vision/tree/main/references/classification#post-training-quantized-modelszImageNet-1Kg/tQ@g`"YV@)zacc@1zacc@5z
                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recipeZunquantized_metrics_docs)url
transformsmetaN)r9   r:   r;   r   r   r	   r   r   IMAGENET1K_V1IMAGENET1K_FBGEMM_V1DEFAULTr,   r,   r,   r-   r   m   s$   
r   
pretrainedc                 C   s   |  ddrtjS tjS )NquantizeF)getr   rt   r   rs   )r    r,   r,   r-   <lambda>   s    
ry   )weightsTF)rz   progressrw   )rz   r{   rw   r    r!   c                 K   s   |rt nt| } |dd}| durd|vr:t|dd t|dd t|dd t|dt| jd  d	| jv rt|d	| jd	  |d	d
}tf i |}t	| |rt
|| | dur|| j|d |sd|_d|_d|_n
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:
    rY   FNtransform_inputTinit_weightsnum_classesrk   rl   rh   )r{   z`auxiliary heads in the pretrained googlenet model are NOT pretrained, so make sure to train them)r   r   verifyrx   r   lenrr   popr   r   r   load_state_dictget_state_dictrY   r_   r`   r\   r]   )rz   r{   rw   r    original_aux_logitsrl   modelr,   r,   r-   r      s2    +

r   )
_ModelURLs)
model_urlsZgooglenet_fbgemm).r\   	functoolsr   typingr   r   r   rK   torch.nnr%   r   r   rI   Ztransforms._presetsr	   _apir   r   _metar   _utilsr   r   r   r   r   r   r   r   r   utilsr   r   r   __all__r   r>   rG   r   r   r<   r   r   rt   rp   Zquant_model_urlsr,   r,   r,   r-   <module>   sN    
#
D