a
    áñhâ  ã                   @   s(  d dl Z d dl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 d dlmZ ddlmZmZ d	d
gZddiZdd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j$ƒZ%G dd	„ d	ej&ƒZ'dS ) é    N)Ú
namedtuple)Ú	inception)ÚInceptionOutputs)ÚOptional)Úload_state_dict_from_urlé   )Ú_replace_reluÚquantize_modelÚQuantizableInception3Úinception_v3Zinception_v3_google_fbgemmzUhttps://download.pytorch.org/models/quantized/inception_v3_google_fbgemm-71447a44.pthFTc           	      K   sÂ   | r2d|vrd|d< d|v r.|d }d|d< nd}t f i |¤Ž}t|ƒ |r\d}t||ƒ n| dv shJ ‚| r¾|rŒ|s~d|_|`td|  }n
tjd }t||d	�}| 	|¡ |s¾|s¾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 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:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
        aux_logits (bool): If True, add an auxiliary branch that can improve training.
            Default: *True*
        transform_input (bool): If True, preprocesses the input according to the method with which it
            was trained on ImageNet. Default: *False*
    Útransform_inputTÚ
aux_logitsFZfbgemm)TFZinception_v3_google_Zinception_v3_google)Úprogress)
r
   r   r	   r   Z	AuxLogitsÚquant_model_urlsÚinception_moduleZ
model_urlsr   Zload_state_dict)	Z
pretrainedr   ÚquantizeÚkwargsZoriginal_aux_logitsÚmodelÚbackendZ	model_urlZ
state_dict© r   úi/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torchvision/models/quantization/inception.pyr      s:    

ÿ
c                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚQuantizableBasicConv2dc                    s$   t t| ƒj|i |¤Ž t ¡ | _d S ©N)Úsuperr   Ú__init__ÚnnZReLUÚrelu©ÚselfÚargsr   ©Ú	__class__r   r   r   X   s    zQuantizableBasicConv2d.__init__c                 C   s"   |   |¡}|  |¡}|  |¡}|S r   ©ÚconvZbnr   )r   Úxr   r   r   Úforward\   s    


zQuantizableBasicConv2d.forwardc                 C   s   t jj| g d¢dd� d S )Nr"   T)Zinplace)ÚtorchÚquantizationZfuse_modules)r   r   r   r   Ú
fuse_modelb   s    z!QuantizableBasicConv2d.fuse_model©Ú__name__Ú
__module__Ú__qualname__r   r%   r(   Ú__classcell__r   r   r    r   r   W   s   r   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚQuantizableInceptionAc                    s*   t t| ƒj|dti|¤Ž tj ¡ | _d S ©NZ
conv_block)r   r.   r   r   r   Ú	quantizedÚFloatFunctionalÚmyopr   r    r   r   r   g   s    zQuantizableInceptionA.__init__c                 C   s   |   |¡}| j |d¡S ©Nr   ©Ú_forwardr2   Úcat©r   r$   Úoutputsr   r   r   r%   k   s    
zQuantizableInceptionA.forward©r*   r+   r,   r   r%   r-   r   r   r    r   r.   f   s   r.   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚQuantizableInceptionBc                    s*   t t| ƒj|dti|¤Ž tj ¡ | _d S r/   )r   r:   r   r   r   r0   r1   r2   r   r    r   r   r   q   s    zQuantizableInceptionB.__init__c                 C   s   |   |¡}| j |d¡S r3   r4   r7   r   r   r   r%   u   s    
zQuantizableInceptionB.forwardr9   r   r   r    r   r:   p   s   r:   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚQuantizableInceptionCc                    s*   t t| ƒj|dti|¤Ž tj ¡ | _d S r/   )r   r;   r   r   r   r0   r1   r2   r   r    r   r   r   {   s    zQuantizableInceptionC.__init__c                 C   s   |   |¡}| j |d¡S r3   r4   r7   r   r   r   r%      s    
zQuantizableInceptionC.forwardr9   r   r   r    r   r;   z   s   r;   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚQuantizableInceptionDc                    s*   t t| ƒj|dti|¤Ž tj ¡ | _d S r/   )r   r<   r   r   r   r0   r1   r2   r   r    r   r   r   …   s    zQuantizableInceptionD.__init__c                 C   s   |   |¡}| j |d¡S r3   r4   r7   r   r   r   r%   ‰   s    
zQuantizableInceptionD.forwardr9   r   r   r    r   r<   „   s   r<   c                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚQuantizableInceptionEc                    sB   t t| ƒj|dti|¤Ž tj ¡ | _tj ¡ | _tj ¡ | _	d S r/   )
r   r=   r   r   r   r0   r1   Úmyop1Úmyop2Úmyop3r   r    r   r   r   �   s    zQuantizableInceptionE.__init__c                 C   s˜   |   |¡}|  |¡}|  |¡|  |¡g}| j |d¡}|  |¡}|  |¡}|  |¡|  	|¡g}| j
 |d¡}tj|dddd�}|  |¡}||||g}|S )Nr   é   )Zkernel_sizeZstrideÚpadding)Ú	branch1x1Zbranch3x3_1Zbranch3x3_2aZbranch3x3_2br>   r6   Zbranch3x3dbl_1Zbranch3x3dbl_2Zbranch3x3dbl_3aZbranch3x3dbl_3br?   ÚFZ
avg_pool2dÚbranch_pool)r   r$   rC   Z	branch3x3Zbranch3x3dblrE   r8   r   r   r   r5   •   s    



þ
zQuantizableInceptionE._forwardc                 C   s   |   |¡}| j |d¡S r3   )r5   r@   r6   r7   r   r   r   r%   ª   s    
zQuantizableInceptionE.forward)r*   r+   r,   r   r5   r%   r-   r   r   r    r   r=   Ž   s   r=   c                       s   e Zd Z‡ fdd„Z‡  ZS )ÚQuantizableInceptionAuxc                    s   t t| ƒj|dti|¤Ž d S r/   )r   rF   r   r   r   r    r   r   r   °   s    z QuantizableInceptionAux.__init__)r*   r+   r,   r   r-   r   r   r    r   rF   ¯   s   rF   c                       s.   e Zd Zd
‡ fdd„	Zdd„ Zdd	„ Z‡  ZS )r
   éè  TFc                    sB   t t| ƒj|||ttttttt	gd� t
j ¡ | _t
j ¡ | _d S )N)Únum_classesr   r   Zinception_blocks)r   r
   r   r   r.   r:   r;   r<   r=   rF   r&   r'   Z	QuantStubÚquantZDeQuantStubÚdequant)r   rH   r   r   r    r   r   r   µ   s    
ùüzQuantizableInception3.__init__c                 C   sj   |   |¡}|  |¡}|  |¡\}}|  |¡}| jo6| j}tj ¡ rZ|sPt	 
d¡ t||ƒS |  ||¡S d S )NzIScripted QuantizableInception3 always returns QuantizableInception3 Tuple)Z_transform_inputrI   r5   rJ   Ztrainingr   r&   ZjitZis_scriptingÚwarningsÚwarnr   Zeager_outputs)r   r$   ZauxZaux_definedr   r   r   r%   Ç   s    





zQuantizableInception3.forwardc                 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   r(   )r   Úmr   r   r   r(   Ô   s    z QuantizableInception3.fuse_model)rG   TFr)   r   r   r    r   r
   ´   s   )FTF)(rK   Úcollectionsr   r&   Ztorch.nnr   Ztorch.nn.functionalZ
functionalrD   Ztorchvision.modelsr   r   Ztorchvision.models.inceptionr   Ztorch.jit.annotationsr   Ztorchvision.models.utilsr   Úutilsr   r	   Ú__all__r   r   ZBasicConv2dr   Z
InceptionAr.   Z
InceptionBr:   Z
InceptionCr;   Z
InceptionDr<   Z
InceptionEr=   ZInceptionAuxrF   Z
Inception3r
   r   r   r   r   Ú<module>   s0   þý
<



!