
    kh                     j   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&y)%    )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ZdedefdZd
dee   ddfd	Z	 xZ
S )QuantizableInvertedResidualargskwargsreturnNc                 j    t        |   |i | t        j                  j	                         | _        y N)super__init__r   	quantizedFloatFunctionalskip_addselfr!   r"   	__class__s      g/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/models/quantization/mobilenetv2.pyr'   z$QuantizableInvertedResidual.__init__   s)    $)&)446    xc                     | j                   r+| j                  j                  || j                  |            S | j                  |      S r%   )use_res_connectr*   addconvr,   r0   s     r.   forwardz#QuantizableInvertedResidual.forward   s8    ==$$Q		!5599Q<r/   is_qatc           	          t        t        | j                              D ][  }t        | j                  |         t        j
                  u s,t        | j                  t        |      t        |dz         g|d       ] y )Nr   Tinplace)rangelenr4   typer   Conv2dr   str)r,   r7   idxs      r.   
fuse_modelz&QuantizableInvertedResidual.fuse_model"   s]    TYY( 	YCDIIcN#ryy0dii#c(CaL)A6SWX	Yr/   r%   __name__
__module____qualname__r   r'   r   r6   r   boolrA   __classcell__r-   s   @r.   r    r       sJ    7c 7S 7T 7   F  Y$ Y4 Yr/   r    c                   T     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        |   |i | t               | _        t	               | _        y)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                 l    | j                  |      }| j                  |      }| j                  |      }|S r%   )rK   _forward_implrL   r5   s     r.   r6   zQuantizableMobileNetV2.forward4   s1    JJqMq!LLOr/   r7   c                     | j                         D ]G  }t        |      t        u rt        |g d|d       t        |      t        u s7|j                  |       I y )N)012Tr9   )modulesr=   r   r   r    rA   )r,   r7   ms      r.   rA   z!QuantizableMobileNetV2.fuse_model:   sK     	%AAw..a&$GAw55V$		%r/   r%   rB   rH   s   @r.   r   r   (   sG    	%c 	%S 	%T 	% F %$ %4 %r/   r   c                   j    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
y)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_    #].#>!. m/==##  
0 $Gr/   r   quantized_mobilenet_v2)name
pretrainedc                 f    | j                  dd      rt        j                  S t        j                  S )NquantizeF)getr   rg   r   rf   )r"   s    r.   <lambda>rp   b   s+    ::j%( 5JJ !// r/   )weightsNTF)rq   progressrn   rq   rr   rn   r"   r#   c                    |rt         nt        j                  |       } | Kt        |dt	        | j
                  d                d| j
                  v rt        |d| j
                  d          |j                  dd      }t        ddt        i|}t        |       |rt        ||       | "|j                  | j                  |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:
    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ZZ[bcGfmSl9S5TU$!&)W\\)5LMjjI.G"O)DOOE%ug&g44hSW4X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      s     ' '  8 ^ ^ , 6 7 7 ( C ? ?Y"2 Y"%[ %4$K $8 -.	0 UY	3e9;OOPQ3 3 	3
 3 3 /3r/   