U
    ­Ê¹j�  ã                	   @   sj  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" dddg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"œeee(ef  e)e)ee'd#œ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                       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||Ž t ¡ | _d S ©N)ÚsuperÚ__init__ÚnnÚReLUÚrelu©Úselfr    r!   ©Ú	__class__© úh/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/models/quantization/googlenet.pyr%      s    zQuantizableBasicConv2d.__init__©Úxr"   c                 C   s"   |   |¡}|  |¡}|  |¡}|S r#   )ÚconvÚbnr(   ©r*   r0   r-   r-   r.   Úforward   s    


zQuantizableBasicConv2d.forward©Úis_qatr"   c                 C   s   t | dddg|dd� d S )Nr1   r2   r(   T)Zinplace)r   )r*   r6   r-   r-   r.   Ú
fuse_model$   s    z!QuantizableBasicConv2d.fuse_model)N)Ú__name__Ú
__module__Ú__qualname__r   r%   r   r4   r   Úboolr7   Ú__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 ©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*   r0   Úoutputsr-   r-   r.   r4   -   s    
zQuantizableInception.forward©r8   r9   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)é   rD   r   )	ÚFZadaptive_avg_pool2dr1   ÚtorchÚflattenr(   Zfc1ZdropoutZfc2r3   r-   r-   r.   r4   8   s    


zQuantizableInceptionAux.forwardrB   r-   r-   r+   r.   rC   2   s   rC   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=   rC   rF   ZaoZquantizationZ	QuantStubÚquantZ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)Z_transform_inputrI   r@   rJ   ZtrainingÚ
aux_logitsrF   ZjitZis_scriptingÚwarningsÚwarnr   Zeager_outputs)r*   r0   Úaux1Úaux2Zaux_definedr-   r-   r.   r4   S   s    




zQuantizableGoogLeNet.forwardr5   c                 C   s(   |   ¡ D ]}t|ƒtk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   r7   )r*   r6   Úmr-   r-   r.   r7   `   s    zQuantizableGoogLeNet.fuse_model)N)r8   r9   r:   r   r%   r   r   r4   r   r;   r7   r<   r-   r-   r+   r.   r   J   s   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 )é   rT   Ú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)r8   r9   r:   r   r   r	   r   r   ÚIMAGENET1K_V1ÚIMAGENET1K_FBGEMM_V1ÚDEFAULTr-   r-   r-   r.   r   m   s(   
þÿñýZquantized_googlenet)ÚnameZ
pretrainedc                 C   s   |   dd¡rtjS tjS )NÚquantizeF)Úgetr   r\   r   r[   )r!   r-   r-   r.   Ú<lambda>�   s    
ÿra   )ÚweightsTF)rb   Úprogressr_   )rb   rc   r_   r!   r"   c                 K   sò   |rt nt | ¡} | dd¡}| dk	r„d|kr:t|ddƒ t|ddƒ t|ddƒ t|dt| jd ƒƒ d	| jkr„t|d	| jd	 ƒ | d	d
¡}tf |Ž}t	|ƒ |r°t
||ƒ | dk	rî| | j|dd�¡ |säd|_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:
    rK   FNZtransform_inputTZinit_weightsZnum_classesrV   rW   rU   )rc   Z
check_hashz`auxiliary heads in the pretrained googlenet model are NOT pretrained, so make sure to train them)r   r   Úverifyr`   r   ÚlenrZ   Úpopr   r   r   Zload_state_dictZget_state_dictrK   rN   rO   rL   rM   )rb   rc   r_   r!   Zoriginal_aux_logitsrW   Úmodelr-   r-   r.   r   ‰   s2    ,


ÿ)*rL   Ú	functoolsr   Útypingr   r   r   rF   Ztorch.nnr&   r   r   rE   Ztransforms._presetsr	   Z_apir   r   r   Ú_metar   Z_utilsr   r   r   r   r   r   r   r   r   Úutilsr   r   r   Ú__all__r   r=   rC   r   r   r;   r-   r-   r-   r.   Ú<module>   sJ    ý
#þÿ
üú