
    khF                     b   d dl mZ d dlmZmZmZmZmZ d dlZd dl	m
Z
 d dlmZ d dlmZmZmZmZmZmZ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&deee$e%f      dee'   dee   de(de(dede&fdZ)deddd d!Z* G d" d#e      Z+ G d$ d%e      Z, G d& d'e      Z- G d( d)e      Z. ed*+       ed,d- f.      dd/d0d1deee+ef      de(de(dede&f
d2              Z/ ed3+       ed,d4 f.      dd/d0d1deee,ef      de(de(dede&f
d5              Z0 ed6+       ed,d7 f.      dd/d0d1deee-ef      de(de(dede&f
d8              Z1 ed9+       ed,d: f.      dd/d0d1deee.ef      de(de(dede&f
d;              Z2y)<    )partial)AnyListOptionalTypeUnionN)Tensor)
BasicBlock
BottleneckResNetResNet18_WeightsResNet50_WeightsResNeXt101_32X8D_WeightsResNeXt101_64X4D_Weights   )ImageClassification   )register_modelWeightsWeightsEnum)_IMAGENET_CATEGORIES)_ovewrite_named_paramhandle_legacy_interface   )_fuse_modules_replace_reluquantize_model)	QuantizableResNetResNet18_QuantizedWeightsResNet50_QuantizedWeights!ResNeXt101_32X8D_QuantizedWeights!ResNeXt101_64X4D_QuantizedWeightsresnet18resnet50resnext101_32x8dresnext101_64x4dc                   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 )QuantizableBasicBlockargskwargsreturnNc                 ~    t        |   |i | t        j                  j                  j                         | _        y N)super__init__torchnn	quantizedFloatFunctionaladd_reluselfr)   r*   	__class__s      b/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/models/quantization/resnet.pyr/   zQuantizableBasicBlock.__init__&   s/    $)&)**::<    xc                 &   |}| j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }| j
                  | j                  |      }| j                  j                  ||      }|S r-   )conv1bn1reluconv2bn2
downsampler4   r6   r:   identityouts       r8   forwardzQuantizableBasicBlock.forward*   s{    jjmhhsmiinjjohhsm??&q)Hmm$$S(3
r9   is_qatc                 ~    t        | g dddgg|d       | j                  rt        | j                  ddg|d       y y )Nr<   r=   r>   r?   r@   Tinplace01r   rA   r6   rF   s     r8   
fuse_modelz QuantizableBasicBlock.fuse_model;   s>    d57GH&Z^_??$//C:vtL r9   r-   __name__
__module____qualname__r   r/   r	   rE   r   boolrO   __classcell__r7   s   @r8   r(   r(   %   sJ    =c =S =T = F "M$ M4 Mr9   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 )QuantizableBottleneckr)   r*   r+   Nc                     t        |   |i | t        j                  j	                         | _        t        j                  d      | _        t        j                  d      | _        y )NFrI   )	r.   r/   r1   r2   r3   skip_add_reluReLUrelu1relu2r5   s      r8   r/   zQuantizableBottleneck.__init__B   sJ    $)&)\\99;WWU+
WWU+
r9   r:   c                    |}| j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }| j                  |      }| j                  |      }| j                  |      }| j                  | j                  |      }| j                  j                  ||      }|S r-   )r<   r=   r\   r?   r@   r]   conv3bn3rA   rZ   r4   rB   s       r8   rE   zQuantizableBottleneck.forwardH   s    jjmhhsmjjojjohhsmjjojjohhsm??&q)H  ))#x8
r9   rF   c                     t        | g dg dddgg|d       | j                  rt        | j                  ddg|d       y y )	N)r<   r=   r\   )r?   r@   r]   r_   r`   TrI   rK   rL   rM   rN   s     r8   rO   z QuantizableBottleneck.fuse_modelZ   sH    ,.G'SXIYZ\blp	
 ??$//C:vtL r9   r-   rP   rV   s   @r8   rX   rX   A   sJ    ,c ,S ,T , F $M$ M4 Mr9   rX   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        j                  j                  j                         | _        t        j                  j                  j                         | _        y r-   )	r.   r/   r0   aoquantization	QuantStubquantDeQuantStubdequantr5   s      r8   r/   zQuantizableResNet.__init__c   sI    $)&)XX**446
xx,,88:r9   r:   c                 l    | j                  |      }| j                  |      }| j                  |      }|S r-   )rg   _forward_implri   )r6   r:   s     r8   rE   zQuantizableResNet.forwardi   s3    JJqM q!LLOr9   rF   c                     t        | g d|d       | j                         D ]6  }t        |      t        u st        |      t        u s&|j                  |       8 y)a  Fuse conv/bn/relu modules in resnet models

        Fuse conv+bn+relu/ Conv+relu/conv+Bn modules to prepare for quantization.
        Model is modified in place.  Note that this operation does not change numerics
        and the model after modification is in floating point
        rH   TrI   N)r   modulestyperX   r(   rO   )r6   rF   ms      r8   rO   zQuantizableResNet.fuse_modelr   sM     	d4fdK 	%AAw//47>S3SV$	%r9   r-   rP   rV   s   @r8   r   r   b   sG    ;c ;S ;T ; F 
%$ 
%4 
%r9   r   blocklayersweightsprogressquantizer*   r+   c                 X   |Kt        |dt        |j                  d                d|j                  v rt        |d|j                  d          |j                  dd      }t	        | |fi |}t        |       |rt        ||       |"|j                  |j                  |d             |S )Nnum_classes
categoriesbackendfbgemmT)rs   
check_hash)	r   lenmetapopr   r   r   load_state_dictget_state_dict)rp   rq   rr   rs   rt   r*   rx   models           r8   _resnetr      s     fmSl9S5TU$!&)W\\)5LMjjH-GeV6v6E%ug&g44hSW4XYLr9   )r   r   ry   zdhttps://github.com/pytorch/vision/tree/main/references/classification#post-training-quantized-modelsz
        These weights were produced by doing Post Training Quantization (eager mode) on top of the unquantized
        weights listed below.
    )min_sizerw   rx   recipe_docsc                   h    e Zd Z ed eed      i edej                  ddddid	d
d      Z	e	Z
y)r   zJhttps://download.pytorch.org/models/quantized/resnet18_fbgemm_16fa66dd.pth   	crop_sizei(^ ImageNet-1KgV-_Q@g r8V@zacc@1zacc@5g/$?g`"y&@
num_paramsunquantized_metrics_ops
_file_sizeurl
transformsr|   N)rQ   rR   rS   r   r   r   _COMMON_METAr   IMAGENET1K_V1IMAGENET1K_FBGEMM_V1DEFAULT r9   r8   r   r      s[    "X.#>

"+99##   
" #Gr9   r   c                       e Zd Z ed eed      i edej                  ddddid	d
d      Z	 ed eedd      i edej                  ddddid	dd      ZeZy)r    zJhttps://download.pytorch.org/models/quantized/resnet50_fbgemm_bf931d71.pthr   r   i(r   g{GR@gjt4W@r   gB`"[@gM8@r   r   zJhttps://download.pytorch.org/models/quantized/resnet50_fbgemm-23753f79.pth   r   resize_sizeg5^IT@gX9vW@g8@N)rQ   rR   rS   r   r   r   r   r   r   r   IMAGENET1K_V2IMAGENET1K_FBGEMM_V2r   r   r9   r8   r    r       s    "X.#>

"+99##   
" #X.#3O

"+99##   
" #Gr9   r    c                       e Zd Z ed eed      i edej                  ddddid	d
d      Z	 ed eedd      i edej                  ddddid	dd      ZeZy)r!   zQhttps://download.pytorch.org/models/quantized/resnext101_32x8_fbgemm_09835ccf.pthr   r   i(Jr   gvS@gQW@r   gDli0@gV-U@r   r   zQhttps://download.pytorch.org/models/quantized/resnext101_32x8_fbgemm-ee16d00c.pthr   r   g~jT@g rX@gzGU@N)rQ   rR   rS   r   r   r   r   r   r   r   r   r   r   r   r9   r8   r!   r!      s    "_.#>

"3AA##   
" #_.#3O

"3AA##   
" #Gr9   r!   c                   l    e Zd Z ed eedd      i eddej                  ddd	d
iddd      Z	e	Z
y)r"   zRhttps://download.pytorch.org/models/quantized/resnext101_64x4d_fbgemm-605a1cb3.pthr   r   r   i(mz+https://github.com/pytorch/vision/pull/5935r   gxT@g/X@r   gQ.@g$cT@)r   r   r   r   r   r   r   N)rQ   rR   rS   r   r   r   r   r   r   r   r   r   r9   r8   r"   r"     s`    "`.#3O

"C3AA##   
$ #Gr9   r"   quantized_resnet18)name
pretrainedc                 f    | j                  dd      rt        j                  S t        j                  S Nrt   F)getr   r   r   r   r*   s    r8   <lambda>r     +    ::j%( 1EE ++ r9   )rr   TF)rr   rs   rt   c                 h    |rt         nt        j                  |       } t        t        g d| ||fi |S )a  ResNet-18 model from
    `Deep Residual Learning for Image Recognition <https://arxiv.org/abs/1512.03385>`_

    .. 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.ResNet18_QuantizedWeights` or :class:`~torchvision.models.ResNet18_Weights`, optional): The
            pretrained weights for the model. See
            :class:`~torchvision.models.quantization.ResNet18_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, return a quantized version of the model. Default is False.
        **kwargs: parameters passed to the ``torchvision.models.quantization.QuantizableResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/resnet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.quantization.ResNet18_QuantizedWeights
        :members:

    .. autoclass:: torchvision.models.ResNet18_Weights
        :members:
        :noindex:
    )r   r   r   r   )r   r   verifyr   r(   rr   rs   rt   r*   s       r8   r#   r#     6    Z -5(:JRRSZ[G(,8^W]^^r9   quantized_resnet50c                 f    | j                  dd      rt        j                  S t        j                  S r   )r   r    r   r   r   r   s    r8   r   r   Q  r   r9   c                 h    |rt         nt        j                  |       } t        t        g d| ||fi |S )a  ResNet-50 model from
    `Deep Residual Learning for Image Recognition <https://arxiv.org/abs/1512.03385>`_

    .. 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.ResNet50_QuantizedWeights` or :class:`~torchvision.models.ResNet50_Weights`, optional): The
            pretrained weights for the model. See
            :class:`~torchvision.models.quantization.ResNet50_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, return a quantized version of the model. Default is False.
        **kwargs: parameters passed to the ``torchvision.models.quantization.QuantizableResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/resnet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.quantization.ResNet50_QuantizedWeights
        :members:

    .. autoclass:: torchvision.models.ResNet50_Weights
        :members:
        :noindex:
    )r         r   )r    r   r   r   rX   r   s       r8   r$   r$   M  r   r9   quantized_resnext101_32x8dc                 f    | j                  dd      rt        j                  S t        j                  S r   )r   r!   r   r   r   r   s    r8   r   r     +    ::j%( 9MM %33 r9   c                     |rt         nt        j                  |       } t        |dd       t        |dd       t	        t
        g d| ||fi |S )a  ResNeXt-101 32x8d model from
    `Aggregated Residual Transformation for Deep Neural Networks <https://arxiv.org/abs/1611.05431>`_

    .. 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.ResNeXt101_32X8D_QuantizedWeights` or :class:`~torchvision.models.ResNeXt101_32X8D_Weights`, optional): The
            pretrained weights for the model. See
            :class:`~torchvision.models.quantization.ResNet101_32X8D_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, return a quantized version of the model. Default is False.
        **kwargs: parameters passed to the ``torchvision.models.quantization.QuantizableResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/resnet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.quantization.ResNeXt101_32X8D_QuantizedWeights
        :members:

    .. autoclass:: torchvision.models.ResNeXt101_32X8D_Weights
        :members:
        :noindex:
    groups    width_per_group   r   r      r   )r!   r   r   r   r   rX   r   s       r8   r%   r%     O    Z 5=0BZbbcjkG&(B/&"3Q7(-(H_X^__r9   quantized_resnext101_64x4dc                 f    | j                  dd      rt        j                  S t        j                  S r   )r   r"   r   r   r   r   s    r8   r   r     r   r9   c                     |rt         nt        j                  |       } t        |dd       t        |dd       t	        t
        g d| ||fi |S )a  ResNeXt-101 64x4d model from
    `Aggregated Residual Transformation for Deep Neural Networks <https://arxiv.org/abs/1611.05431>`_

    .. 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.ResNeXt101_64X4D_QuantizedWeights` or :class:`~torchvision.models.ResNeXt101_64X4D_Weights`, optional): The
            pretrained weights for the model. See
            :class:`~torchvision.models.quantization.ResNet101_64X4D_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, return a quantized version of the model. Default is False.
        **kwargs: parameters passed to the ``torchvision.models.quantization.QuantizableResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/resnet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.quantization.ResNeXt101_64X4D_QuantizedWeights
        :members:

    .. autoclass:: torchvision.models.ResNeXt101_64X4D_Weights
        :members:
        :noindex:
    r   @   r   r   r   )r"   r   r   r   r   rX   r   s       r8   r&   r&     r   r9   )3	functoolsr   typingr   r   r   r   r   r0   torch.nnr1   r	   torchvision.models.resnetr
   r   r   r   r   r   r   transforms._presetsr   _apir   r   r   _metar   _utilsr   r   utilsr   r   r   __all__r(   rX   r   intrT   r   r   r   r    r!   r"   r#   r$   r%   r&   r   r9   r8   <module>r      sA    3 3      7 7 7 ( C ? ?
MJ M8MJ MB% %:+-BBCDI k" 	
   4 &t	# #*## ##L## ##L# #, )*	, MQ	&_e57GGHI&_ &_ 	&_
 &_ &_ +&_R )*	, MQ	&_e57GGHI&_ &_ 	&_
 &_ &_ +&_R 12	4 ]a	(`e=?WWXY(` (` 	(`
 (` (` 3(`V 12	4 ]a	(`e=?WWXY(` (` 	(`
 (` (` 3(`r9   