o
    Vh                     @   s<  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  g d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d eee$ef  d!e%d"e%d#ed$e#f
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                       T   e Zd Zdededdf fddZdedefdd	Zdd
ee ddfddZ	  Z
S )QuantizableInvertedResidualargskwargsreturnNc                    s"   t  j|i | tj | _d S N)super__init__r   	quantizedFloatFunctionalskip_addselfr    r!   	__class__ _/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/models/quantization/mobilenetv2.pyr%      s   z$QuantizableInvertedResidual.__init__xc                 C   s$   | j r| j|| |S | |S r#   )use_res_connectr(   addconvr*   r/   r-   r-   r.   forward   s   
z#QuantizableInvertedResidual.forwardis_qatc                 C   sN   t t| jD ]}t| j| tju r$t| jt|t|d g|dd qd S )Nr   Tinplace)rangelenr2   typer   Conv2dr   str)r*   r5   idxr-   r-   r.   
fuse_model"   s
   "z&QuantizableInvertedResidual.fuse_modelr#   __name__
__module____qualname__r   r%   r   r4   r   boolr>   __classcell__r-   r-   r+   r.   r      s     r   c                       r   )r   r    r!   r"   Nc                    s&   t  j|i | 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#   )rE   _forward_implrF   r3   r-   r-   r.   r4   4   s   


zQuantizableMobileNetV2.forwardr5   c                 C   sH   |   D ]}t|tu rt|g d|dd t|tu r!|| qd S )N)012Tr6   )modulesr:   r   r   r   r>   )r*   r5   mr-   r-   r.   r>   :   s   
z!QuantizableMobileNetV2.fuse_modelr#   r?   r-   r-   r+   r.   r   (   s     r   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   )	crop_sizeiz5 )r   r   qnnpackzUhttps://github.com/pytorch/vision/tree/main/references/classification#qat-mobilenetv2zImageNet-1Kg'1Q@gV@)zacc@1zacc@5g$C?gMb@z
                These weights were produced by doing Quantization Aware Training (eager mode) on top of the unquantized
                weights listed below.
            )

num_paramsmin_size
categoriesbackendrecipeunquantized_metrics_ops
_file_size_docs)url
transformsmetaN)r@   rA   rB   r   r   r   r   r   IMAGENET1K_V1IMAGENET1K_QNNPACK_V1DEFAULTr-   r-   r-   r.   r   B   s*    
r   quantized_mobilenet_v2)name
pretrainedc                 C   s   |  ddr	tjS tjS )NquantizeF)getr   r^   r   r]   )r!   r-   r-   r.   <lambda>b   s   
re   )weightsNTF)rf   progressrc   rf   rg   rc   r!   r"   c                 K   s   |rt nt| } | dur&t|dt| jd  d| jv r&t|d| jd  |dd}td	dti|}t	| |r@t
|| | durN|| 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:
    Nnum_classesrR   rS   rO   blockT)rg   
check_hashr-   )r   r   verifyr   r9   r\   popr   r   r   r   load_state_dictget_state_dict)rf   rg   rc   r!   rS   modelr-   r-   r.   r   ^   s   ,

r   )'	functoolsr   typingr   r   r   torchr   r   torch.ao.quantizationr   r	   torchvision.models.mobilenetv2r
   r   r   ops.miscr   transforms._presetsr   _apir   r   r   _metar   _utilsr   r   utilsr   r   r   __all__r   r   r   rC   r   r-   r-   r-   r.   <module>   sF    
