
    ڧg                        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e          Z" G d de          Z# G d de          Z$ ed           edd f          dddddeee$ef                  d e%d!e%d"ed#e#f
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                   Z     e Zd Zdededdf fdZdedefdZd
dee         ddfd	Z	 xZ
S )QuantizableInvertedResidualargskwargsreturnNc                      t                      j        |i | t          j                                        | _        d S N)super__init__r   	quantizedFloatFunctionalskip_addselfr!   r"   	__class__s      g/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/models/quantization/mobilenetv2.pyr'   z$QuantizableInvertedResidual.__init__   s8    $)&)))4466    xc                     | j         r.| j                            ||                     |                    S |                     |          S r%   )use_res_connectr*   addconvr,   r0   s     r.   forwardz#QuantizableInvertedResidual.forward   s?     	 =$$Q		!55599Q<<r/   is_qatc           	         t          t          | j                            D ]_}t          | j        |                   t          j        u r7t          | j        t          |          t          |dz             g|d           `d S )Nr   Tinplace)rangelenr4   typer   Conv2dr   str)r,   r7   idxs      r.   
fuse_modelz&QuantizableInvertedResidual.fuse_model"   sz    TY(( 	Y 	YCDIcN##ry00di#c((CaLL)A6SWXXXX	Y 	Yr/   r%   __name__
__module____qualname__r   r'   r   r6   r   boolrA   __classcell__r-   s   @r.   r    r       s        7c 7S 7T 7 7 7 7 7 7   F        Y Y$ Y4 Y Y Y Y Y Y Y Yr/   r    c                   Z     e Zd Zdededdf fdZdedefdZd
dee         ddfd	Z	 xZ
S )r   r!   r"   r#   Nc                      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+   s      r.   r'   zQuantizableMobileNetV2.__init__)   s;     	$)&)))[[
"}}r/   r0   c                     |                      |          }|                     |          }|                     |          }|S r%   )rK   _forward_implrL   r5   s     r.   r6   zQuantizableMobileNetV2.forward4   s8    JJqMMq!!LLOOr/   r7   c                     |                                  D ]X}t          |          t          u rt          |g d|d           t          |          t          u r|                    |           Yd S )N)012Tr9   )modulesr=   r   r   r    rA   )r,   r7   ms      r.   rA   z!QuantizableMobileNetV2.fuse_model:   su     	% 	%AAww...a&$GGGGAww555V$$$		% 	%r/   r%   rB   rH   s   @r.   r   r   (   s        	%c 	%S 	%T 	% 	% 	% 	% 	% 	% F    % %$ %4 % % % % % % % %r/   r   c                   n    e Zd Z ed eed          ddeddej        dd	d
di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)rC   rD   rE   r   r   r   r   r   IMAGENET1K_V1IMAGENET1K_QNNPACK_V1DEFAULT r/   r.   r   r   B   s        #G]7.#>>>!. m/=##    
 
  0 $GGGr/   r   quantized_mobilenet_v2)name
pretrainedc                 ^    |                      dd          rt          j        nt          j        S )NquantizeF)getr   rg   r   rf   )r"   s    r.   <lambda>rp   b   s,    ::j%((04JJ!/ r/   )weightsNTF)rq   progressrn   rq   rr   rn   r"   r#   c                    |rt           nt                              |           } | Nt          |dt	          | j        d                              d| j        v rt          |d| j        d                    |                    dd          }t          d	dt          i|}t          |           |rt          ||           | *|                    |                     |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_classesr[   r\   rX   blockT)rr   
check_hashri   )r   r   verifyr   r<   re   popr   r    r   r   load_state_dictget_state_dict)rq   rr   rn   r"   r\   models         r.   r   r   ^   s    X 19R,,>RZZ[bccGfmSl9S5T5TUUU$$!&)W\)5LMMMjjI..G"OO)DOOOE% 'ug&&&g44hSW4XXYYYLr/   )'	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   rF   r   ri   r/   r.   <module>r      sg         ' ' ' ' ' ' ' ' ' '         8 8 8 8 8 8 8 8 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ , , , , , , 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 ( ( ( ( ( ( C C C C C C C C ? ? ? ? ? ? ? ? ? ?  Y Y Y Y Y"2 Y Y Y"% % % % %[ % % %4$ $ $ $ $K $ $ $8 -...	0 	0   UY	3 3 3e9;OOPQ3 3 	3
 3 3 3 3  /.3 3 3r/   