U
    ­Ê¹jF  ã                	   @   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mZ dd
lmZ ddlmZmZ ddlm Z m!Z!m"Z" dddddddddg	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)�ed*d+d,„ fd-�dd.d/d0œeee+ef  e(e(ee&d1œd2d„ƒƒZ/ed3d)�ed*d4d,„ fd-�dd.d/d0œeee,ef  e(e(ee&d1œd5d„ƒƒZ0ed6d)�ed*d7d,„ fd-�dd.d/d0œeee-ef  e(e(ee&d1œd8d„ƒƒZ1ed9d)�ed*d:d,„ fd-�dd.d/d0œeee.ef  e(e(ee&d1œd;d„ƒƒZ2dS )<é    )Ú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                       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||Ž tjj ¡ | _d S ©N)ÚsuperÚ__init__ÚtorchÚnnÚ	quantizedÚFloatFunctionalÚadd_relu©Úselfr(   r)   ©Ú	__class__© úe/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/models/quantization/resnet.pyr-   &   s    zQuantizableBasicBlock.__init__©Úxr*   c                 C   s\   |}|   |¡}|  |¡}|  |¡}|  |¡}|  |¡}| jd k	rJ|  |¡}| j ||¡}|S r+   )Úconv1Úbn1ÚreluÚconv2Úbn2Ú
downsampler2   ©r4   r:   ÚidentityÚoutr7   r7   r8   Úforward*   s    






zQuantizableBasicBlock.forward©Úis_qatr*   c                 C   s>   t | dddgddgg|dd� | jr:t | jdd	g|dd� d S )
Nr;   r<   r=   r>   r?   T©ZinplaceÚ0Ú1©r   r@   ©r4   rF   r7   r7   r8   Ú
fuse_model;   s    z QuantizableBasicBlock.fuse_model)N©Ú__name__Ú
__module__Ú__qualname__r   r-   r   rD   r   ÚboolrL   Ú__classcell__r7   r7   r5   r8   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||Ž tj ¡ | _tjdd�| _tjdd�| _d S )NFrG   )	r,   r-   r/   r0   r1   Úskip_add_reluZReLUÚrelu1Úrelu2r3   r5   r7   r8   r-   B   s    zQuantizableBottleneck.__init__r9   c                 C   sz   |}|   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}| jd k	rh|  |¡}| j	 
||¡}|S r+   )r;   r<   rU   r>   r?   rV   Úconv3Úbn3r@   rT   r2   rA   r7   r7   r8   rD   H   s    









zQuantizableBottleneck.forwardrE   c                 C   sF   t | dddgdddgddgg|d	d
� | jrBt | jddg|d	d
� d S )Nr;   r<   rU   r>   r?   rV   rW   rX   TrG   rH   rI   rJ   rK   r7   r7   r8   rL   Z   s       ÿz QuantizableBottleneck.fuse_model)NrM   r7   r7   r5   r8   rS   A   s   rS   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                    s.   t ƒ j||Ž tjj ¡ | _tjj ¡ | _d S r+   )	r,   r-   r.   ZaoZquantizationZ	QuantStubÚquantZDeQuantStubÚdequantr3   r5   r7   r8   r-   c   s    zQuantizableResNet.__init__r9   c                 C   s"   |   |¡}|  |¡}|  |¡}|S r+   )rY   Z_forward_implrZ   )r4   r:   r7   r7   r8   rD   i   s    


zQuantizableResNet.forwardrE   c                 C   sJ   t | dddg|dd� |  ¡ D ]&}t|ƒtks:t|ƒtk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
        r;   r<   r=   TrG   N)r   ÚmodulesÚtyperS   r&   rL   )r4   rF   Úmr7   r7   r8   rL   r   s    zQuantizableResNet.fuse_model)NrM   r7   r7   r5   r8   r   b   s   	)ÚblockÚlayersÚweightsÚprogressÚquantizer)   r*   c                 K   sŠ   |d k	r:t |dt|jd ƒƒ d|jkr:t |d|jd ƒ | dd¡}t| |f|Ž}t|ƒ |rjt||ƒ |d k	r†| |j|dd�¡ |S )NZnum_classesÚ
categoriesÚbackendÚfbgemmT)ra   Z
check_hash)	r   ÚlenÚmetaÚpopr   r   r   Zload_state_dictZget_state_dict)r^   r_   r`   ra   rb   r)   rd   Úmodelr7   r7   r8   Ú_resnet   s    

rj   )r   r   re   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.
    )Zmin_sizerc   rd   ÚrecipeZ_docsc                
   @   sB   e Zd Zedeedd�edejddddœid	d
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@5g /Ý$ý?g`åÐ"Ûy&@©Ú
num_paramsÚunquantizedÚ_metricsÚ_opsÚ
_file_size©ÚurlZ
transformsrg   N)rN   rO   rP   r   r   r   Ú_COMMON_METAr   ÚIMAGENET1K_V1ÚIMAGENET1K_FBGEMM_V1ÚDEFAULTr7   r7   r7   r8   r   ¤   s    
þÿõýc                
   @   sv   e Zd Zedeedd�edejddddœid	d
dœ–d�Z	edeeddd�edej
ddddœid	ddœ–d�ZeZdS )r   zJhttps://download.pytorch.org/models/quantized/resnet50_fbgemm_bf931d71.pthrl   rm   i(ø…ro   g{®GáúR@gj¼t“4W@rp   gB`åÐ"[@gü©ñÒMÂ8@rq   rw   zJhttps://download.pytorch.org/models/quantized/resnet50_fbgemm-23753f79.pthéè   ©rn   Zresize_sizeg5^ºIT@gX9´Èv¾W@g‡ÙÎ÷ó8@N)rN   rO   rP   r   r   r   ry   r   rz   r{   ÚIMAGENET1K_V2ÚIMAGENET1K_FBGEMM_V2r|   r7   r7   r7   r8   r   ¹   s>   
þÿõýþÿõýc                
   @   sv   e Zd Zedeedd�edejddddœid	d
dœ–d�Z	edeeddd�edej
ddddœid	ddœ–d�ZeZdS )r    zQhttps://download.pytorch.org/models/quantized/resnext101_32x8_fbgemm_09835ccf.pthrl   rm   i(ÙJro   gÉv¾Ÿ¿S@g…ëQ¸žW@rp   gD‹lçûi0@gV-‚U@rq   rw   zQhttps://download.pytorch.org/models/quantized/resnext101_32x8_fbgemm-ee16d00c.pthr}   r~   gÛù~j¼¤T@gœÄ °rX@gáz®G©U@N)rN   rO   rP   r   r   r   ry   r   rz   r{   r   r€   r|   r7   r7   r7   r8   r    ß   s>   
þÿõýþÿõýc                   @   sF   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!   zRhttps://download.pytorch.org/models/quantized/resnext101_64x4d_fbgemm-605a1cb3.pthrl   r}   r~   i(mùz+https://github.com/pytorch/vision/pull/5935ro   g¶óýÔx¹T@g¾Ÿ/ÝX@rp   gìQ¸…ë.@gÝ$�•cT@)rr   rk   rs   rt   ru   rv   rw   N)rN   rO   rP   r   r   r   ry   r   rz   r{   r|   r7   r7   r7   r8   r!     s"   þÿôýZquantized_resnet18)ÚnameZ
pretrainedc                 C   s   |   dd¡rtjS tjS ©Nrb   F)Úgetr   r{   r   rz   ©r)   r7   r7   r8   Ú<lambda>  s    
ÿr…   )r`   TF)r`   ra   rb   )r`   ra   rb   r)   r*   c                 K   s.   |rt nt | ¡} ttddddg| ||f|Ž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   Úverifyrj   r&   ©r`   ra   rb   r)   r7   r7   r8   r"     s    -Zquantized_resnet50c                 C   s   |   dd¡rtjS tjS r‚   )rƒ   r   r{   r   rz   r„   r7   r7   r8   r…   Q  s    
ÿc                 K   s.   |rt nt | ¡} ttddddg| ||f|Ž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†   rj   rS   r‡   r7   r7   r8   r#   M  s    -Zquantized_resnext101_32x8dc                 C   s   |   dd¡rtjS tjS r‚   )rƒ   r    r{   r   rz   r„   r7   r7   r8   r…   ƒ  s    
ÿc                 K   sF   |rt nt | ¡} t|ddƒ t|ddƒ ttddddg| ||f|Ž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   rj   rS   r‡   r7   r7   r8   r$     s    -Zquantized_resnext101_64x4dc                 C   s   |   dd¡rtjS tjS r‚   )rƒ   r!   r{   r   rz   r„   r7   r7   r8   r…   ·  s    
ÿc                 K   sF   |rt nt | ¡} t|ddƒ t|ddƒ ttddddg| ||f|Ž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   rj   rS   r‡   r7   r7   r8   r%   ³  s    -)3Ú	functoolsr   Útypingr   r   r   r   r   r.   Ztorch.nnr/   r   Ztorchvision.models.resnetr	   r
   r   r   r   r   r   Ztransforms._presetsr   Z_apir   r   r   Ú_metar   Z_utilsr   r   Úutilsr   r   r   Ú__all__r&   rS   r   ÚintrQ   rj   ry   r   r   r    r!   r"   r#   r$   r%   r7   r7   r7   r8   Ú<module>   sÒ   $
÷!ùû&&þÿ
üú)þÿ
üú)þÿ
üú+þÿ
üú