a
    PSic+                     @   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 d	dlmZ d	dlmZmZ ddlm Z m!Z!m"Z" g d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.Z/G dd dej0Z1G dd dej2Z3G d d! d!eZ4ed"d#d$ fd%dd&d'd(eee4ef  e5e5ee3d)d*d+Z6d	d,lm7Z7 d	d-lm8Z8 e7d.e4j9j:iZ;dS )/    N)partial)AnyListOptionalUnion)Tensor)	inception)InceptionOutputsInception_V3_Weights   )ImageClassification   )WeightsEnumWeights)_IMAGENET_CATEGORIES)handle_legacy_interface_ovewrite_named_param   )_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|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/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 | g d|dd d S )Nr-   T)inplace)r   )r&   r2   r)   r)   r*   
fuse_model%   s    z!QuantizableBasicConv2d.fuse_model)N)__name__
__module____qualname__r   r!   r   r0   r   boolr4   __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 N
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*   r0   /   s    
zQuantizableInceptionA.forwardr5   r6   r7   r   r!   r   r0   r9   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 )QuantizableInceptionBNr   c                    s&   t  j|dti| tj | _d S r;   r=   r%   r'   r)   r*   r!   6   s    zQuantizableInceptionB.__init__r+   c                 C   s   |  |}| j|dS rA   rB   rE   r)   r)   r*   r0   :   s    
zQuantizableInceptionB.forwardrG   r)   r)   r'   r*   rH   4   s   rH   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 r;   r=   r%   r'   r)   r*   r!   A   s    zQuantizableInceptionC.__init__r+   c                 C   s   |  |}| j|dS rA   rB   rE   r)   r)   r*   r0   E   s    
zQuantizableInceptionC.forwardrG   r)   r)   r'   r*   rI   ?   s   rI   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 r;   r=   r%   r'   r)   r*   r!   L   s    zQuantizableInceptionD.__init__r+   c                 C   s   |  |}| j|dS rA   rB   rE   r)   r)   r*   r0   P   s    
zQuantizableInceptionD.forwardrG   r)   r)   r'   r*   rJ   J   s   rJ   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 r;   )	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   )kernel_sizestridepadding)	branch1x1branch3x3_1branch3x3_2abranch3x3_2brL   rD   branch3x3dbl_1branch3x3dbl_2branch3x3dbl_3abranch3x3dbl_3brM   F
avg_pool2dbranch_pool)r&   r,   rR   	branch3x3branch3x3dblr\   rF   r)   r)   r*   rC   ]   s    




zQuantizableInceptionE._forwardc                 C   s   |  |}| j|dS rA   )rC   rN   rD   rE   r)   r)   r*   r0   r   s    
zQuantizableInceptionE.forward)
r5   r6   r7   r   r!   r   r   rC   r0   r9   r)   r)   r'   r*   rK   U   s   rK   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 r;   )r    r!   r   r%   r'   r)   r*   r!   y   s    z QuantizableInceptionAux.__init__)r5   r6   r7   r   r!   r9   r)   r)   r'   r*   r_   w   s   r_   c                       sP   e Zd Zde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     TFN)num_classes
aux_logitstransform_inputr   c                    sB   t  j|||tttttttgd t	j
j | _t	j
j | _d S )N)ra   rb   rc   inception_blocks)r    r!   r   r:   rH   rI   rJ   rK   r_   torchaoquantization	QuantStubquantDeQuantStubdequant)r&   ra   rb   rc   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)_transform_inputri   rC   rk   trainingrb   re   jitis_scriptingwarningswarnr	   eager_outputs)r&   r,   auxaux_definedr)   r)   r*   r0      s    





zQuantizableInception3.forwardr1   c                 C   s(   |   D ]}t|tu 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   r4   )r&   r2   mr)   r)   r*   r4      s    z QuantizableInception3.fuse_model)r`   TF)N)r5   r6   r7   intr8   r!   r   r	   r0   r   r4   r9   r)   r)   r'   r*   r   }   s      r   c                   @   sF   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Z	e	Z
dS )r   zUhttps://download.pytorch.org/models/quantized/inception_v3_google_fbgemm-71447a44.pthi+  iV  )	crop_sizeresize_sizeir)K   r{   fbgemmzdhttps://github.com/pytorch/vision/tree/main/references/classification#post-training-quantized-modelszImageNet-1Kg%CKS@g-VW@)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recipeunquantized_metrics_docs)url
transformsmetaN)r5   r6   r7   r   r   r   r   r
   IMAGENET1K_V1IMAGENET1K_FBGEMM_V1DEFAULTr)   r)   r)   r*   r      s$   r   
pretrainedc                 C   s   |  ddrtjS tjS )NquantizeF)getr   r   r
   r   )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rxd|vr:t|dd t|dd t|dt| jd  d| jv rxt|d| jd  |dd	}tf i |}t	| |rt
|| | dur|r|sd|_d|_|| j|d
 |s|sd|_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:
    rb   FNrc   Tra   r   r   r|   )r   )r   r
   verifyr   r   lenr   popr   r   r   rb   	AuxLogitsload_state_dictget_state_dict)r   r   r   r   original_aux_logitsr   modelr)   r)   r*   r      s.    1

r   )
_ModelURLs)
model_urlsZinception_v3_google_fbgemm)<rp   	functoolsr   typingr   r   r   r   re   torch.nnr"   torch.nn.functional
functionalrZ   r   Ztorchvision.modelsr   Zinception_moduleZtorchvision.models.inceptionr	   r
   Ztransforms._presetsr   _apir   r   _metar   _utilsr   r   utilsr   r   r   __all__BasicConv2dr   
InceptionAr:   
InceptionBrH   
InceptionCrI   
InceptionDrJ   
InceptionErK   InceptionAuxr_   
Inception3r   r   r8   r   r   r   r   r   quant_model_urlsr)   r)   r)   r*   <module>   sX   "2
G