U
    ­Ê¹jû  ã                	   @   s6  d dl mZ d dlmZmZmZ d dlmZmZ d dl	m
Z
mZ d dlmZmZmZ ddl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ƒZ"G dd„ deƒZ#G dd„ deƒZ$edd�eddd„ fd�ddddœeee$ef  e%e%ee#d œd!d„ƒƒZ&dS )"é    )Úpartial)ÚAnyÚOptionalÚUnion)ÚnnÚTensor)ÚDeQuantStubÚ	QuantStub)ÚInvertedResidualÚMobileNet_V2_WeightsÚMobileNetV2é   )ÚConv2dNormActivation)ÚImageClassificationé   )Úregister_modelÚWeightsÚWeightsEnum)Ú_IMAGENET_CATEGORIES)Ú_ovewrite_named_paramÚhandle_legacy_interfaceé   )Ú_fuse_modulesÚ_replace_reluÚquantize_modelÚQuantizableMobileNetV2ÚMobileNet_V2_QuantizedWeightsÚmobilenet_v2c                       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 )ÚQuantizableInvertedResidualN©ÚargsÚkwargsÚreturnc                    s   t ƒ j||Ž tj ¡ | _d S ©N)ÚsuperÚ__init__r   Z	quantizedZFloatFunctionalÚskip_add©Úselfr    r!   ©Ú	__class__© új/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/models/quantization/mobilenetv2.pyr%      s    z$QuantizableInvertedResidual.__init__©Úxr"   c                 C   s(   | j r| j ||  |¡¡S |  |¡S d S r#   )Zuse_res_connectr&   ÚaddÚconv©r(   r.   r+   r+   r,   Úforward   s    z#QuantizableInvertedResidual.forward©Úis_qatr"   c                 C   sN   t t| jƒƒD ]:}t| j| ƒtjkrt| jt|ƒt|d ƒg|dd� qd S )Nr   T©Zinplace)ÚrangeÚlenr0   Útyper   ZConv2dr   Ústr)r(   r4   Úidxr+   r+   r,   Ú
fuse_model"   s    z&QuantizableInvertedResidual.fuse_model)N©Ú__name__Ú
__module__Ú__qualname__r   r%   r   r2   r   Úboolr;   Ú__classcell__r+   r+   r)   r,   r      s   r   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                    s"   t ƒ j||Ž tƒ | _tƒ | _dS )zq
        MobileNet V2 main class

        Args:
           Inherits args from floating point MobileNetV2
        N)r$   r%   r	   Úquantr   Údequantr'   r)   r+   r,   r%   )   s    zQuantizableMobileNetV2.__init__r-   c                 C   s"   |   |¡}|  |¡}|  |¡}|S r#   )rB   Z_forward_implrC   r1   r+   r+   r,   r2   4   s    


zQuantizableMobileNetV2.forwardr3   c                 C   sJ   |   ¡ D ]<}t|ƒtkr.t|dddg|dd� t|ƒtkr| |¡ qd S )NÚ0Ú1Ú2Tr5   )Úmodulesr8   r   r   r   r;   )r(   r4   Úmr+   r+   r,   r;   :   s
    z!QuantizableMobileNetV2.fuse_model)Nr<   r+   r+   r)   r,   r   (   s   c                   @   sH   e Zd Zedee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   zOhttps://download.pytorch.org/models/quantized/mobilenet_v2_qnnpack_37f702c5.pthéà   )Z	crop_sizeièz5 )r   r   ÚqnnpackzUhttps://github.com/pytorch/vision/tree/main/references/classification#qat-mobilenetv2zImageNet-1Kg'1¬êQ@gš™™™™‰V@)zacc@1zacc@5gÝ$�•CÓ?gü©ñÒMb@z«
                These weights were produced by doing Quantization Aware Training (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)r=   r>   r?   r   r   r   r   r   ÚIMAGENET1K_V1ÚIMAGENET1K_QNNPACK_V1ÚDEFAULTr+   r+   r+   r,   r   B   s(   
þÿñýZquantized_mobilenet_v2)ÚnameZ
pretrainedc                 C   s   |   dd¡rtjS tjS )NÚquantizeF)Úgetr   rQ   r   rP   )r!   r+   r+   r,   Ú<lambda>b   s    
ÿrV   )ÚweightsNTF)rW   ÚprogressrT   )rW   rX   rT   r!   r"   c                 K   s    |rt nt | ¡} | dk	rLt|dt| jd ƒƒ d| jkrLt|d| jd ƒ | dd¡}tf dti|—Ž}t	|ƒ |r€t
||ƒ | dk	rœ| | j|dd�¡ |S )	aÞ  
    Constructs a MobileNetV2 architecture from
    `MobileNetV2: Inverted Residuals and Linear Bottlenecks
    <https://arxiv.org/abs/1801.04381>`_.

    .. 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.MobileNet_V2_QuantizedWeights` or :class:`~torchvision.models.MobileNet_V2_Weights`, optional): The
            pretrained weights for the model. See
            :class:`~torchvision.models.quantization.MobileNet_V2_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, returns a quantized version of the model. Default is False.
        **kwargs: parameters passed to the ``torchvision.models.quantization.QuantizableMobileNetV2``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/mobilenetv2.py>`_
            for more details about this class.
    .. autoclass:: torchvision.models.quantization.MobileNet_V2_QuantizedWeights
        :members:
    .. autoclass:: torchvision.models.MobileNet_V2_Weights
        :members:
        :noindex:
    NZnum_classesrK   rL   rJ   ÚblockT)rX   Z
check_hash)r   r   Úverifyr   r7   rO   Úpopr   r   r   r   Zload_state_dictZget_state_dict)rW   rX   rT   r!   rL   Úmodelr+   r+   r,   r   ^   s    ,

)'Ú	functoolsr   Útypingr   r   r   Ztorchr   r   Ztorch.ao.quantizationr   r	   Ztorchvision.models.mobilenetv2r
   r   r   Zops.miscr   Ztransforms._presetsr   Z_apir   r   r   Ú_metar   Z_utilsr   r   Úutilsr   r   r   Ú__all__r   r   r   r@   r   r+   r+   r+   r,   Ú<module>   sB   ýþÿ
üú