U
    ­Ê¹j?*  ã                	   @   sÀ  d dl Z d dlmZ d dlmZmZmZmZ d dlZd dl	m
Z
 d dlm
  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mZ d	dlmZ d	dlmZmZ ddl m!Z!m"Z"m#Z# dddgZ$G dd„ dej%ƒZ&G dd„ dej'ƒZ(G dd„ dej)ƒZ*G dd„ dej+ƒZ,G dd„ dej-ƒZ.G dd„ dej/ƒZ0G dd„ dej1ƒZ2G d d„ dej3ƒZ4G d!d„ deƒZ5ed"d#�ed$d%d&„ fd'�dd(d)d*œeee5ef  e6e6ee4d+œd,d„ƒƒZ7dS )-é    N)Úpartial)ÚAnyÚListÚOptionalÚUnion)ÚTensor)Ú	inception)ÚInception_V3_WeightsÚInceptionOutputsé   )ÚImageClassificationé   )Úregister_modelÚWeightsÚWeightsEnum)Ú_IMAGENET_CATEGORIES)Ú_ovewrite_named_paramÚhandle_legacy_interfaceé   )Ú_fuse_modulesÚ_replace_reluÚquantize_modelÚQuantizableInception3ÚInception_V3_QuantizedWeightsÚinception_v3c                       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__ÚnnZReLUÚrelu©Úselfr   r   ©Ú	__class__© úh/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/models/quantization/inception.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 | dddg|dd� d S )Nr-   r.   r$   T)Zinplace)r   )r&   r1   r)   r)   r*   Ú
fuse_model%   s    z!QuantizableBasicConv2d.fuse_model)N)Ú__name__Ú
__module__Ú__qualname__r   r"   r   r/   r   Úboolr2   Ú__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 )ÚQuantizableInceptionANr   c                    s&   t ƒ j|dti|—Ž tj ¡ | _d S ©NZ
conv_block©r!   r"   r   r#   Ú	quantizedÚFloatFunctionalÚmyopr%   r'   r)   r*   r"   +   s    zQuantizableInceptionA.__init__r+   c                 C   s   |   |¡}| j |d¡S ©Nr   ©Ú_forwardr=   Úcat©r&   r,   Úoutputsr)   r)   r*   r/   /   s    
zQuantizableInceptionA.forward©r3   r4   r5   r   r"   r   r/   r7   r)   r)   r'   r*   r8   )   s   r8   c                       s6   e Zd Zeeddœ‡ fdd„Zeedœdd„Z‡  ZS )ÚQuantizableInceptionBNr   c                    s&   t ƒ j|dti|—Ž tj ¡ | _d S r9   r:   r%   r'   r)   r*   r"   6   s    zQuantizableInceptionB.__init__r+   c                 C   s   |   |¡}| j |d¡S r>   r?   rB   r)   r)   r*   r/   :   s    
zQuantizableInceptionB.forwardrD   r)   r)   r'   r*   rE   4   s   rE   c                       s6   e Zd Zeeddœ‡ fdd„Zeedœdd„Z‡  ZS )ÚQuantizableInceptionCNr   c                    s&   t ƒ j|dti|—Ž tj ¡ | _d S r9   r:   r%   r'   r)   r*   r"   A   s    zQuantizableInceptionC.__init__r+   c                 C   s   |   |¡}| j |d¡S r>   r?   rB   r)   r)   r*   r/   E   s    
zQuantizableInceptionC.forwardrD   r)   r)   r'   r*   rF   ?   s   rF   c                       s6   e Zd Zeeddœ‡ fdd„Zeedœdd„Z‡  ZS )ÚQuantizableInceptionDNr   c                    s&   t ƒ j|dti|—Ž tj ¡ | _d S r9   r:   r%   r'   r)   r*   r"   L   s    zQuantizableInceptionD.__init__r+   c                 C   s   |   |¡}| j |d¡S r>   r?   rB   r)   r)   r*   r/   P   s    
zQuantizableInceptionD.forwardrD   r)   r)   r'   r*   rG   J   s   rG   c                       sJ   e Zd Zeeddœ‡ fdd„Zeee dœdd„Zeedœdd	„Z‡  Z	S )
ÚQuantizableInceptionENr   c                    s>   t ƒ j|dti|—Ž tj ¡ | _tj ¡ | _tj ¡ | _d S r9   )	r!   r"   r   r#   r;   r<   Úmyop1Úmyop2Úmyop3r%   r'   r)   r*   r"   W   s    zQuantizableInceptionE.__init__r+   c                 C   s˜   |   |¡}|  |¡}|  |¡|  |¡g}| j |d¡}|  |¡}|  |¡}|  |¡|  	|¡g}| j
 |d¡}tj|dddd�}|  |¡}||||g}|S )Nr   r   )Zkernel_sizeZstrideÚpadding)Ú	branch1x1Zbranch3x3_1Zbranch3x3_2aZbranch3x3_2brI   rA   Zbranch3x3dbl_1Zbranch3x3dbl_2Zbranch3x3dbl_3aZbranch3x3dbl_3brJ   ÚFZ
avg_pool2dÚbranch_pool)r&   r,   rM   Z	branch3x3Zbranch3x3dblrO   rC   r)   r)   r*   r@   ]   s    



þ
zQuantizableInceptionE._forwardc                 C   s   |   |¡}| j |d¡S r>   )r@   rK   rA   rB   r)   r)   r*   r/   r   s    
zQuantizableInceptionE.forward)
r3   r4   r5   r   r"   r   r   r@   r/   r7   r)   r)   r'   r*   rH   U   s   rH   c                       s&   e Zd Zeeddœ‡ fdd„Z‡  ZS )ÚQuantizableInceptionAuxNr   c                    s   t ƒ j|dti|—Ž d S r9   )r!   r"   r   r%   r'   r)   r*   r"   y   s    z QuantizableInceptionAux.__init__)r3   r4   r5   r   r"   r7   r)   r)   r'   r*   rP   w   s   rP   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              
      sD   t ƒ j|dtttttttgi|—Ž t	j
j ¡ | _t	j
j ¡ | _d S )NZinception_blocks)r!   r"   r   r8   rE   rF   rG   rH   rP   ÚtorchZaoZquantizationZ	QuantStubÚquantZDeQuantStubÚdequantr%   r'   r)   r*   r"   ~   s     ÿùþõzQuantizableInception3.__init__r+   c                 C   sj   |   |¡}|  |¡}|  |¡\}}|  |¡}| jo6| j}tj ¡ rZ|sPt	 
d¡ t||ƒS |  ||¡S d S )NzIScripted QuantizableInception3 always returns QuantizableInception3 Tuple)Z_transform_inputrR   r@   rS   ZtrainingÚ
aux_logitsrQ   ZjitZis_scriptingÚwarningsÚwarnr
   Zeager_outputs)r&   r,   ZauxZaux_definedr)   r)   r*   r/   �   s    





zQuantizableInception3.forwardr0   c                 C   s(   |   ¡ D ]}t|ƒtkr| |¡ qdS )a  Fuse conv/bn/relu modules in inception 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   r2   )r&   r1   Úmr)   r)   r*   r2   œ   s    z QuantizableInception3.fuse_model)N)r3   r4   r5   r   r"   r   r
   r/   r   r6   r2   r7   r)   r)   r'   r*   r   }   s   c                   @   sJ   e Zd Zedeed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   zUhttps://download.pytorch.org/models/quantized/inception_v3_google_fbgemm-a2837893.pthi+  iV  )Z	crop_sizeZresize_sizei°rž)éK   rZ   Úfbgemmzdhttps://github.com/pytorch/vision/tree/main/references/classification#post-training-quantized-modelszImageNet-1Kg%�•CKS@g-²�ï§VW@)zacc@1zacc@5g'1¬Ú@gL7‰A`%7@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)r3   r4   r5   r   r   r   r   r	   ÚIMAGENET1K_V1ÚIMAGENET1K_FBGEMM_V1ÚDEFAULTr)   r)   r)   r*   r   ©   s(   þÿñýZquantized_inception_v3)ÚnameZ
pretrainedc                 C   s   |   dd¡rtjS tjS )NÚquantizeF)Úgetr   rb   r	   ra   )r   r)   r)   r*   Ú<lambda>É   s    
ÿrg   )ÚweightsTF)rh   Úprogressre   )rh   ri   re   r   r   c                 K   sì   |rt nt | ¡} | dd¡}| dk	rxd|kr:t|ddƒ t|ddƒ t|dt| jd ƒƒ d| jkrxt|d| jd ƒ | dd	¡}tf |Ž}t	|ƒ |r¤t
||ƒ | dk	rè|rÀ|sÀd|_d|_| | j|dd
�¡ |sè|sèd|_d|_|S )a¦  Inception v3 model architecture from
    `Rethinking the Inception Architecture for Computer Vision <http://arxiv.org/abs/1512.00567>`__.

    .. note::
        **Important**: In contrast to the other models the inception_v3 expects tensors with a size of
        N x 3 x 299 x 299, so ensure your images are sized accordingly.

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

    .. autoclass:: torchvision.models.quantization.Inception_V3_QuantizedWeights
        :members:

    .. autoclass:: torchvision.models.Inception_V3_Weights
        :members:
        :noindex:
    rT   FNZtransform_inputTZnum_classesr\   r]   r[   )ri   Z
check_hash)r   r	   Úverifyrf   r   Úlenr`   Úpopr   r   r   rT   Z	AuxLogitsZload_state_dictZget_state_dict)rh   ri   re   r   Zoriginal_aux_logitsr]   Úmodelr)   r)   r*   r   Å   s.    2


)8rU   Ú	functoolsr   Útypingr   r   r   r   rQ   Ztorch.nnr#   Ztorch.nn.functionalZ
functionalrN   r   Ztorchvision.modelsr   Zinception_moduleZtorchvision.models.inceptionr	   r
   Ztransforms._presetsr   Z_apir   r   r   Ú_metar   Z_utilsr   r   Úutilsr   r   r   Ú__all__ZBasicConv2dr   Z
InceptionAr8   Z
InceptionBrE   Z
InceptionCrF   Z
InceptionDrG   Z
InceptionErH   ZInceptionAuxrP   Z
Inception3r   r   r6   r   r)   r)   r)   r*   Ú<module>   sT   ý",þÿ
üú