U
    ­Ê¹jôA  ã                	   @   sŒ  d dl mZ d dl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 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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j!ƒZ"G dd„ dej#ƒZ$ee% 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.d/œeee)ef  e&e&ee$d0œd1d„ƒƒZ-ed2d(�ed)d3d+„ fd,�dd-d.d/œeee*ef  e&e&ee$d0œd4d„ƒƒZ.ed5d(�ed)d6d+„ fd,�dd-d.d/œeee+ef  e&e&ee$d0œd7d„ƒƒZ/ed8d(�ed)d9d+„ fd,�dd-d.d/œeee,ef  e&e&ee$d0œd:d„ƒƒZ0dS );é    )Úpartial)ÚAnyÚListÚOptionalÚUnionN)ÚTensor)Úshufflenetv2é   )ÚImageClassificationé   )Úregister_modelÚWeightsÚWeightsEnum)Ú_IMAGENET_CATEGORIES)Ú_ovewrite_named_paramÚhandle_legacy_interface)ÚShuffleNet_V2_X0_5_WeightsÚShuffleNet_V2_X1_0_WeightsÚShuffleNet_V2_X1_5_WeightsÚShuffleNet_V2_X2_0_Weightsé   )Ú_fuse_modulesÚ_replace_reluÚquantize_modelÚQuantizableShuffleNetV2Ú#ShuffleNet_V2_X0_5_QuantizedWeightsÚ#ShuffleNet_V2_X1_0_QuantizedWeightsÚ#ShuffleNet_V2_X1_5_QuantizedWeightsÚ#ShuffleNet_V2_X2_0_QuantizedWeightsÚshufflenet_v2_x0_5Úshufflenet_v2_x1_0Úshufflenet_v2_x1_5Úshufflenet_v2_x2_0c                       s6   e Zd Zeeddœ‡ fdd„Zeedœdd„Z‡  ZS )ÚQuantizableInvertedResidualN©ÚargsÚkwargsÚreturnc                    s   t ƒ j||Ž tj ¡ | _d S ©N)ÚsuperÚ__init__ÚnnZ	quantizedZFloatFunctionalÚcat©Úselfr%   r&   ©Ú	__class__© úk/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/models/quantization/shufflenetv2.pyr*   $   s    z$QuantizableInvertedResidual.__init__©Úxr'   c                 C   sh   | j dkr8|jddd�\}}| jj||  |¡gdd�}n | jj|  |¡|  |¡gdd�}t |d¡}|S )Nr   r   )Údim)ZstrideÚchunkr,   Úbranch2Úbranch1r   Zchannel_shuffle)r.   r4   Úx1Zx2Úoutr1   r1   r2   Úforward(   s    
 z#QuantizableInvertedResidual.forward)Ú__name__Ú
__module__Ú__qualname__r   r*   r   r;   Ú__classcell__r1   r1   r/   r2   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 )r   Nr$   c                    s6   t ƒ j|dti|—Ž tjj ¡ | _tjj ¡ | _	d S )NZinverted_residual)
r)   r*   r#   ÚtorchZaoZquantizationZ	QuantStubÚquantZDeQuantStubÚdequantr-   r/   r1   r2   r*   6   s    z QuantizableShuffleNetV2.__init__r3   c                 C   s"   |   |¡}|  |¡}|  |¡}|S r(   )rA   Z_forward_implrB   )r.   r4   r1   r1   r2   r;   ;   s    


zQuantizableShuffleNetV2.forward)Úis_qatr'   c                 C   s¶   | j  ¡ D ]0\}}|dkr
|dk	r
t|dddgg|dd� q
|  ¡ D ]l}t|ƒtkrDt|jj  ¡ ƒdkrˆt|jddgdd	d
gg|dd� t|jdddgd	d
gdddgg|dd� qDdS )aB  Fuse conv/bn/relu modules in shufflenetv2 model

        Fuse conv+bn+relu/ conv+relu/conv+bn modules to prepare for quantization.
        Model is modified in place.

        .. note::
            Note that this operation does not change numerics
            and the model after modification is in floating point
        )Zconv1Zconv5NÚ0Ú1Ú2T)Zinplacer   Ú3Ú4Ú5Ú6Ú7)	Z_modulesÚitemsr   ÚmodulesÚtyper#   Úlenr8   r7   )r.   rC   ÚnameÚmr1   r1   r2   Ú
fuse_modelA   s    
 üz"QuantizableShuffleNetV2.fuse_model)N)r<   r=   r>   r   r*   r   r;   r   ÚboolrR   r?   r1   r1   r/   r2   r   4   s   )Ústages_repeatsÚstages_out_channelsÚ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)rW   Z
check_hash)	r   rO   ÚmetaÚpopr   r   r   Zload_state_dictZget_state_dict)rT   rU   rV   rW   rX   r&   rZ   Úmodelr1   r1   r2   Ú_shufflenetv2Z   s    	

r_   )r   r   r[   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_sizerY   rZ   Ú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   zShttps://download.pytorch.org/models/quantized/shufflenetv2_x0.5_fbgemm-00845098.pthéà   ©Ú	crop_sizeiÛ úImageNet-1Kg#Ûù~jüL@gR¸…ëñS@©zacc@1zacc@5g{®Gáz¤?gj¼t“ø?©Ú
num_paramsÚunquantizedÚ_metricsÚ_opsÚ
_file_size©ÚurlZ
transformsr\   N)r<   r=   r>   r   r   r
   Ú_COMMON_METAr   ÚIMAGENET1K_V1ÚIMAGENET1K_FBGEMM_V1ÚDEFAULTr1   r1   r1   r2   r   €   s    
þÿõýc                
   @   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   zQhttps://download.pytorch.org/models/quantized/shufflenetv2_x1_fbgemm-1e62bb32.pthra   rb   iÌÄ" rd   g×£p=
Q@gh‘í|?åU@re   g�Âõ(\�Â?gyé&1¬@rf   rl   N)r<   r=   r>   r   r   r
   rn   r   ro   rp   rq   r1   r1   r1   r2   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   zShttps://download.pytorch.org/models/quantized/shufflenetv2_x1_5_fbgemm-d7401f05.pthra   éè   ©rc   Zresize_sizeú+https://github.com/pytorch/vision/pull/5906iv5 rd   gÙÎ÷SR@gÍÌÌÌÌ¬V@re   g‹lçû©ñÒ?gÇK7‰A`@©r`   rg   rh   ri   rj   rk   rl   N)r<   r=   r>   r   r   r
   rn   r   ro   rp   rq   r1   r1   r1   r2   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   zShttps://download.pytorch.org/models/quantized/shufflenetv2_x2_0_fbgemm-5cac526c.pthra   rr   rs   rt   iÌÒp rd   g-²�ï§ÖR@g¬Zd;W@re   g-²�ï§â?g‘í|?5Þ@ru   rl   N)r<   r=   r>   r   r   r
   rn   r   ro   rp   rq   r1   r1   r1   r2   r   À   s"   þÿôýZquantized_shufflenet_v2_x0_5)rP   Z
pretrainedc                 C   s   |   dd¡rtjS tjS ©NrX   F)Úgetr   rp   r   ro   ©r&   r1   r1   r2   Ú<lambda>Ú   s    
ÿry   )rV   TF©rV   rW   rX   )rV   rW   rX   r&   r'   c                 K   s<   |rt nt | ¡} tdddgdddddgf| ||dœ|—ŽS )	aQ  
    Constructs a ShuffleNetV2 with 0.5x output channels, as described in
    `ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design
    <https://arxiv.org/abs/1807.11164>`__.

    .. 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.ShuffleNet_V2_X0_5_QuantizedWeights` or :class:`~torchvision.models.ShuffleNet_V2_X0_5_Weights`, optional): The
            pretrained weights for the model. See
            :class:`~torchvision.models.quantization.ShuffleNet_V2_X0_5_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.ShuffleNet_V2_X0_5_QuantizedWeights``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/shufflenetv2.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.quantization.ShuffleNet_V2_X0_5_QuantizedWeights
        :members:

    .. autoclass:: torchvision.models.ShuffleNet_V2_X0_5_Weights
        :members:
        :noindex:
    é   é   é   é0   é`   éÀ   é   rz   )r   r   Úverifyr_   ©rV   rW   rX   r&   r1   r1   r2   r   Ö   s    0 ÿ  ÿÿZquantized_shufflenet_v2_x1_0c                 C   s   |   dd¡rtjS tjS rv   )rw   r   rp   r   ro   rx   r1   r1   r2   ry     s    
ÿc                 K   s<   |rt nt | ¡} tdddgdddddgf| ||dœ|—ŽS )	aQ  
    Constructs a ShuffleNetV2 with 1.0x output channels, as described in
    `ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design
    <https://arxiv.org/abs/1807.11164>`__.

    .. 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.ShuffleNet_V2_X1_0_QuantizedWeights` or :class:`~torchvision.models.ShuffleNet_V2_X1_0_Weights`, optional): The
            pretrained weights for the model. See
            :class:`~torchvision.models.quantization.ShuffleNet_V2_X1_0_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.ShuffleNet_V2_X1_0_QuantizedWeights``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/shufflenetv2.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.quantization.ShuffleNet_V2_X1_0_QuantizedWeights
        :members:

    .. autoclass:: torchvision.models.ShuffleNet_V2_X1_0_Weights
        :members:
        :noindex:
    r{   r|   r}   ét   rr   iÐ  r�   rz   )r   r   r‚   r_   rƒ   r1   r1   r2   r      s    0 ÿ  ÿÿZquantized_shufflenet_v2_x1_5c                 C   s   |   dd¡rtjS tjS rv   )rw   r   rp   r   ro   rx   r1   r1   r2   ry   F  s    
ÿc                 K   s<   |rt nt | ¡} tdddgdddddgf| ||dœ|—ŽS )	aQ  
    Constructs a ShuffleNetV2 with 1.5x output channels, as described in
    `ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design
    <https://arxiv.org/abs/1807.11164>`__.

    .. 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.ShuffleNet_V2_X1_5_QuantizedWeights` or :class:`~torchvision.models.ShuffleNet_V2_X1_5_Weights`, optional): The
            pretrained weights for the model. See
            :class:`~torchvision.models.quantization.ShuffleNet_V2_X1_5_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.ShuffleNet_V2_X1_5_QuantizedWeights``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/shufflenetv2.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.quantization.ShuffleNet_V2_X1_5_QuantizedWeights
        :members:

    .. autoclass:: torchvision.models.ShuffleNet_V2_X1_5_Weights
        :members:
        :noindex:
    r{   r|   r}   é°   i`  iÀ  r�   rz   )r   r   r‚   r_   rƒ   r1   r1   r2   r!   B  s    0 ÿ  ÿÿZquantized_shufflenet_v2_x2_0c                 C   s   |   dd¡rtjS tjS rv   )rw   r   rp   r   ro   rx   r1   r1   r2   ry   |  s    
ÿc                 K   s<   |rt nt | ¡} tdddgdddddgf| ||dœ|—ŽS )	aQ  
    Constructs a ShuffleNetV2 with 2.0x output channels, as described in
    `ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design
    <https://arxiv.org/abs/1807.11164>`__.

    .. 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.ShuffleNet_V2_X2_0_QuantizedWeights` or :class:`~torchvision.models.ShuffleNet_V2_X2_0_Weights`, optional): The
            pretrained weights for the model. See
            :class:`~torchvision.models.quantization.ShuffleNet_V2_X2_0_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.ShuffleNet_V2_X2_0_QuantizedWeights``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/shufflenetv2.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.quantization.ShuffleNet_V2_X2_0_QuantizedWeights
        :members:

    .. autoclass:: torchvision.models.ShuffleNet_V2_X2_0_Weights
        :members:
        :noindex:
    r{   r|   r}   éô   iè  iÐ  i   rz   )r   r   r‚   r_   rƒ   r1   r1   r2   r"   x  s    0 ÿ  ÿÿ)1Ú	functoolsr   Útypingr   r   r   r   r@   Ztorch.nnr+   r   Ztorchvision.modelsr   Ztransforms._presetsr
   Z_apir   r   r   Ú_metar   Z_utilsr   r   r   r   r   r   Úutilsr   r   r   Ú__all__ZInvertedResidualr#   ZShuffleNetV2r   ÚintrS   r_   rn   r   r   r   r   r   r    r!   r"   r1   r1   r1   r2   Ú<module>   sÒ   ÷'øûþÿ
üú-þÿ
üú-þÿ
üú-þÿ
üú