a
    PSicD                     @   s  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 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ee#e$f  ee& ee e'e'ee%dddZ(deddddZ)G dd deZ*G dd  d eZ+G d!d" d"eZ,G d#d$ d$eZ-ed%d&d' fd(dd)d*d+eee*ef  e'e'ee%d,d-d.Z.ed%d/d' fd(dd)d*d+eee+ef  e'e'ee%d,d0d1Z/ed%d2d' fd(dd)d*d+eee,ef  e'e'ee%d,d3d4Z0dd)d*d+eee-ef  e'e'ee%d,d5d6Z1dd7lm2Z2 dd8l3m4Z4 e2e*j5j6e+j5j6e,j5j6d9Z7dS ):    )partial)AnyTypeUnionListOptionalN)Tensor)
Bottleneck
BasicBlockResNetResNet18_WeightsResNet50_WeightsResNeXt101_32X8D_WeightsResNeXt101_64X4D_Weights   )ImageClassification   )WeightsEnumWeights)_IMAGENET_CATEGORIES)handle_legacy_interface_ovewrite_named_param   )_fuse_modules_replace_reluquantize_model)	QuantizableResNetResNet18_QuantizedWeightsResNet50_QuantizedWeights!ResNeXt101_32X8D_QuantizedWeights!ResNeXt101_64X4D_QuantizedWeightsresnet18resnet50resnext101_32x8dresnext101_64x4dc                       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 )QuantizableBasicBlockNargskwargsreturnc                    s$   t  j|i | tjj | _d S N)super__init__torchnn	quantizedFloatFunctionaladd_reluselfr'   r(   	__class__ b/var/www/html/django/DPS/env/lib/python3.9/site-packages/torchvision/models/quantization/resnet.pyr,   &   s    zQuantizableBasicBlock.__init__xr)   c                 C   s\   |}|  |}| |}| |}| |}| |}| jd urJ| |}| j||}|S r*   )conv1bn1reluconv2bn2
downsampler1   r3   r9   identityoutr6   r6   r7   forward*   s    






zQuantizableBasicBlock.forwardis_qatr)   c                 C   s<   t | g dddgg|dd | jr8t | jddg|dd d S )Nr:   r;   r<   r=   r>   Tinplace01r   r?   r3   rE   r6   r6   r7   
fuse_model;   s    z QuantizableBasicBlock.fuse_model)N__name__
__module____qualname__r   r,   r   rC   r   boolrM   __classcell__r6   r6   r4   r7   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 )QuantizableBottleneckNr&   c                    s>   t  j|i | tj | _tjdd| _tjdd| _d S )NFrG   )	r+   r,   r.   r/   r0   skip_add_reluReLUrelu1relu2r2   r4   r6   r7   r,   B   s    zQuantizableBottleneck.__init__r8   c                 C   sz   |}|  |}| |}| |}| |}| |}| |}| |}| |}| jd urh| |}| j	
||}|S r*   )r:   r;   rW   r=   r>   rX   conv3bn3r?   rU   r1   r@   r6   r6   r7   rC   H   s    









zQuantizableBottleneck.forwardrD   c                 C   sB   t | g dg dddgg|dd | jr>t | jddg|dd d S )	N)r:   r;   rW   )r=   r>   rX   rY   rZ   TrG   rI   rJ   rK   rL   r6   r6   r7   rM   Z   s
    z QuantizableBottleneck.fuse_model)NrN   r6   r6   r4   r7   rT   A   s   rT   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                    s2   t  j|i | tjj | _tjj | _d S r*   )	r+   r,   r-   aoquantization	QuantStubquantDeQuantStubdequantr2   r4   r6   r7   r,   c   s    zQuantizableResNet.__init__r8   c                 C   s"   |  |}| |}| |}|S r*   )r^   _forward_implr`   )r3   r9   r6   r6   r7   rC   i   s    


zQuantizableResNet.forwardrD   c                 C   sH   t | g d|dd |  D ]&}t|tu s8t|tu r|| qdS )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
        rF   TrG   N)r   modulestyperT   r%   rM   )r3   rE   mr6   r6   r7   rM   r   s    zQuantizableResNet.fuse_model)NrN   r6   r6   r4   r7   r   b   s   	r   )blocklayersweightsprogressquantizer(   r)   c                 K   s   |d ur:t |dt|jd  d|jv r:t |d|jd  |dd}t| |fi |}t| |rnt|| |d ur||j|d |S )Nnum_classes
categoriesbackendfbgemm)rh   )	r   lenmetapopr   r   r   load_state_dictget_state_dict)re   rf   rg   rh   ri   r(   rl   modelr6   r6   r7   _resnet   s    

rt   )r   r   rm   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_sizerk   rl   recipe_docsc                
   @   sB   e Zd Zedeeddi edejddddid	d
Z	e	Z
dS )r   zJhttps://download.pytorch.org/models/quantized/resnet18_fbgemm_16fa66dd.pth   	crop_sizei(^ ImageNet-1KgV-_Q@g r8V@zacc@1zacc@5
num_paramsunquantized_metricsurl
transformsro   N)rO   rP   rQ   r   r   r   _COMMON_METAr   IMAGENET1K_V1IMAGENET1K_FBGEMM_V1DEFAULTr6   r6   r6   r7   r      s    
r   c                
   @   sv   e Zd Zedeeddi edejddddid	d
Z	edeedddi edej
ddddid	d
ZeZdS )r   zJhttps://download.pytorch.org/models/quantized/resnet50_fbgemm_bf931d71.pthrx   ry   i(r{   g{GR@gjt4W@r|   r}   r   zJhttps://download.pytorch.org/models/quantized/resnet50_fbgemm-23753f79.pth   rz   resize_sizeg5^IT@gX9vW@N)rO   rP   rQ   r   r   r   r   r   r   r   IMAGENET1K_V2IMAGENET1K_FBGEMM_V2r   r6   r6   r6   r7   r      s>   
r   c                
   @   sv   e Zd Zedeeddi edejddddid	d
Z	edeedddi edej
ddddid	d
ZeZdS )r   zQhttps://download.pytorch.org/models/quantized/resnext101_32x8_fbgemm_09835ccf.pthrx   ry   i(Jr{   gvS@gQW@r|   r}   r   zQhttps://download.pytorch.org/models/quantized/resnext101_32x8_fbgemm-ee16d00c.pthr   r   g~jT@g rX@N)rO   rP   rQ   r   r   r   r   r   r   r   r   r   r   r6   r6   r6   r7   r      s>   
r   c                   @   sF   e Zd Zedeedddi eddejddd	d
iddZ	e	Z
dS )r    zRhttps://download.pytorch.org/models/quantized/resnext101_64x4d_fbgemm-605a1cb3.pthrx   r   r   i(mz+https://github.com/pytorch/vision/pull/5935r{   gxT@g/X@r|   )r~   rv   r   r   r   N)rO   rP   rQ   r   r   r   r   r   r   r   r   r6   r6   r6   r7   r       s"   r    
pretrainedc                 C   s   |  ddrtjS tjS Nri   F)getr   r   r   r   r(   r6   r6   r7   <lambda>  s    
r   )rg   TF)rg   rh   ri   )rg   rh   ri   r(   r)   c                 K   s.   |rt nt| } 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   verifyrt   r%   rg   rh   ri   r(   r6   r6   r7   r!     s    ,r!   c                 C   s   |  ddrtjS tjS r   )r   r   r   r   r   r   r6   r6   r7   r   C  s    
c                 K   s.   |rt nt| } 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   rt   rT   r   r6   r6   r7   r"   @  s    ,r"   c                 C   s   |  ddrtjS tjS r   )r   r   r   r   r   r   r6   r6   r7   r   t  s    
c                 K   sF   |rt nt| } 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   rt   rT   r   r6   r6   r7   r#   q  s    ,r#   c                 K   sF   |rt nt| } 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   rt   rT   r   r6   r6   r7   r$     s    $r$   )
_ModelURLs)
model_urls)Zresnet18_fbgemmZresnet50_fbgemmZresnext101_32x8d_fbgemm)8	functoolsr   typingr   r   r   r   r   r-   torch.nnr.   r   Ztorchvision.models.resnetr	   r
   r   r   r   r   r   Ztransforms._presetsr   _apir   r   _metar   _utilsr   r   utilsr   r   r   __all__r%   rT   r   intrR   rt   r   r   r   r   r    r!   r"   r#   r$   r   resnetr   r   r   quant_model_urlsr6   r6   r6   r7   <module>   s   $
!""
)
)
-,