a
    áñhL  ã                   @   s’   d dl mZ d dlmZ d dlmZmZmZmZ d dl	m
Z
mZmZ ddlmZmZ ddgZd	d
iZG dd„ deƒZG dd„ deƒZddd„ZdS )é    )Únn)Úload_state_dict_from_url)ÚInvertedResidualÚ
ConvBNReLUÚMobileNetV2Ú
model_urls)Ú	QuantStubÚDeQuantStubÚfuse_modulesé   )Ú_replace_reluÚquantize_modelÚQuantizableMobileNetV2Úmobilenet_v2Zmobilenet_v2_qnnpackzOhttps://download.pytorch.org/models/quantized/mobilenet_v2_qnnpack_37f702c5.pthc                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚQuantizableInvertedResidualc                    s&   t t| ƒj|i |¤Ž tj ¡ | _d S ©N)Úsuperr   Ú__init__r   Z	quantizedZFloatFunctionalÚskip_add©ÚselfÚargsÚkwargs©Ú	__class__© úi/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torchvision/models/quantization/mobilenet.pyr      s    z$QuantizableInvertedResidual.__init__c                 C   s(   | j r| j ||  |¡¡S |  |¡S d S r   )Zuse_res_connectr   ÚaddÚconv©r   Úxr   r   r   Úforward   s    z#QuantizableInvertedResidual.forwardc                 C   sL   t t| jƒƒD ]8}t| j| ƒtjkrt| jt|ƒt|d ƒgdd� qd S )Nr   T©Zinplace)ÚrangeÚlenr   Útyper   ZConv2dr
   Ústr)r   Úidxr   r   r   Ú
fuse_model   s    z&QuantizableInvertedResidual.fuse_model©Ú__name__Ú
__module__Ú__qualname__r   r!   r(   Ú__classcell__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 )r   c                    s*   t t| ƒj|i |¤Ž tƒ | _tƒ | _dS )zq
        MobileNet V2 main class

        Args:
           Inherits args from floating point MobileNetV2
        N)r   r   r   r   Úquantr	   Údequantr   r   r   r   r   "   s    zQuantizableMobileNetV2.__init__c                 C   s"   |   |¡}|  |¡}|  |¡}|S r   )r.   Z_forward_implr/   r   r   r   r   r!   -   s    


zQuantizableMobileNetV2.forwardc                 C   sD   |   ¡ D ]6}t|ƒtkr*t|g d¢dd� t|ƒtkr| ¡  qd S )N)Ú0Ú1Ú2Tr"   )Úmodulesr%   r   r
   r   r(   )r   Úmr   r   r   r(   3   s
    z!QuantizableMobileNetV2.fuse_modelr)   r   r   r   r   r   !   s   FTc                 K   sr   t f dti|¤Ž}t|ƒ |r.d}t||ƒ n| dv s:J ‚| rn|rPtd|  }ntd }t||d�}| |¡ |S )aG  
    Constructs a MobileNetV2 architecture from
    `"MobileNetV2: Inverted Residuals and Linear Bottlenecks"
    <https://arxiv.org/abs/1801.04381>`_.

    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
     quantize(bool): If True, returns a quantized model, else returns a float model
    ÚblockZqnnpack)TFZmobilenet_v2_r   )Úprogress)r   r   r   r   Úquant_model_urlsr   r   Zload_state_dict)Z
pretrainedr6   Úquantizer   ÚmodelÚbackendZ	model_urlZ
state_dictr   r   r   r   ;   s    ÿ
N)FTF)Ztorchr   Ztorchvision.models.utilsr   Ztorchvision.models.mobilenetr   r   r   r   Ztorch.quantizationr   r	   r
   Úutilsr   r   Ú__all__r7   r   r   r   r   r   r   r   Ú<module>   s   þ