a
    áñhç  ã                   @   sÎ   d dl 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mZmZ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ƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    N)Ú
functional)ÚOptional)Úload_state_dict_from_url)ÚGoogLeNetOutputsÚBasicConv2dÚ	InceptionÚInceptionAuxÚ	GoogLeNetÚ
model_urlsé   )Ú_replace_reluÚquantize_modelÚQuantizableGoogLeNetÚ	googlenetZgooglenet_fbgemmzKhttps://download.pytorch.org/models/quantized/googlenet_fbgemm-c00238cf.pthFTc           	      K   sÒ   | rNd|vrd|d< d|vr$d|d< |d r6t  d¡ |d }d|d< d|d< tf i |¤Ž}t|ƒ |rxd}t||ƒ n| dv s„J ‚| rÎ|rštd	|  }ntd
 }t||d�}| |¡ |sÎd|_	d|_
d|_|S )a  GoogLeNet (Inception v1) model architecture from
    `"Going Deeper with Convolutions" <http://arxiv.org/abs/1409.4842>`_.

    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, adds two auxiliary branches that can improve training.
            Default: *False* when pretrained is True otherwise *True*
        transform_input (bool): If True, preprocesses the input according to the method with which it
            was trained on ImageNet. Default: *False*
    Ztransform_inputTÚ
aux_logitsFz`auxiliary heads in the pretrained googlenet model are NOT pretrained, so make sure to train themZinit_weightsZfbgemm)TFZ
googlenet_r   )ÚprogressN)ÚwarningsÚwarnr   r   r   Úquant_model_urlsr
   r   Zload_state_dictr   Úaux1Úaux2)	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/googlenet.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__ÚnnÚReLUÚrelu©ÚselfÚargsr   ©Ú	__class__r   r   r    P   s    zQuantizableBasicConv2d.__init__c                 C   s"   |   |¡}|  |¡}|  |¡}|S r   ©ÚconvZbnr#   ©r%   Úxr   r   r   ÚforwardT   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_modelZ   s    z!QuantizableBasicConv2d.fuse_model©Ú__name__Ú
__module__Ú__qualname__r    r-   r0   Ú__classcell__r   r   r'   r   r   N   s   r   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚQuantizableInceptionc                    s*   t t| ƒj|dti|¤Ž tj ¡ | _d S )NÚ
conv_block)r   r6   r    r   r!   Z	quantizedZFloatFunctionalÚcatr$   r'   r   r   r    `   s    
ÿÿÿzQuantizableInception.__init__c                 C   s   |   |¡}| j |d¡S )Nr   )Ú_forwardr8   )r%   r,   Úoutputsr   r   r   r-   e   s    
zQuantizableInception.forward©r2   r3   r4   r    r-   r5   r   r   r'   r   r6   ^   s   r6   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚQuantizableInceptionAuxc                    s4   t t| ƒj|dti|¤Ž t ¡ | _t d¡| _d S )Nr7   gffffffæ?)	r   r<   r    r   r!   r"   r#   ZDropoutÚdropoutr$   r'   r   r   r    l   s    
ÿÿÿ
z QuantizableInceptionAux.__init__c                 C   sJ   t  |d¡}|  |¡}t |d¡}|  |  |¡¡}|  |¡}|  |¡}|S )N)é   r>   r   )	ÚFZadaptive_avg_pool2dr*   r.   Úflattenr#   Zfc1r=   Zfc2r+   r   r   r   r-   r   s    


zQuantizableInceptionAux.forwardr;   r   r   r'   r   r<   j   s   r<   c                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )r   c                    s<   t t| ƒj|dtttgi|¤Ž tj ¡ | _	tj 
¡ | _d S )NÚblocks)r   r   r    r   r6   r<   r.   r/   Z	QuantStubÚquantZDeQuantStubÚdequantr$   r'   r   r   r    †   s    
þÿýzQuantizableGoogLeNet.__init__c                 C   sp   |   |¡}|  |¡}|  |¡\}}}|  |¡}| jo8| j}tj ¡ r^|sRt	 
d¡ t|||ƒS |  |||¡S d S )NzCScripted QuantizableGoogleNet always returns GoogleNetOutputs Tuple)Z_transform_inputrB   r9   rC   Ztrainingr   r.   ZjitZis_scriptingr   r   r   Zeager_outputs)r%   r,   r   r   Zaux_definedr   r   r   r-   �   s    




zQuantizableGoogLeNet.forwardc                 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   r0   )r%   Úmr   r   r   r0   œ   s    zQuantizableGoogLeNet.fuse_modelr1   r   r   r'   r   r   „   s   	)FTF)r   r.   Ztorch.nnr!   r   r?   Ztorch.jit.annotationsr   Ztorchvision.models.utilsr   Ztorchvision.models.googlenetr   r   r   r   r	   r
   Úutilsr   r   Ú__all__r   r   r   r6   r<   r   r   r   r   r   Ú<module>   s    þ
8