U
    Ð‹Ñhô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__© úZ/tmp/user/2202/pip-unpacked-wheel-mjxxo1pc/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   )Z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(   )r@   Z_forward_implrA   )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#   Úlenr7   r6   )r.   rB   ÚnameÚmr1   r1   r2   Ú
fuse_modelA   s    
 üz"QuantizableShuffleNetV2.fuse_model)N)r;   r<   r=   r   r*   r   r:   r   ÚboolrQ   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)rV   Z
check_hash)	r   rN   ÚmetaÚpopr   r   r   Zload_state_dictZget_state_dict)rS   rT   rU   rV   rW   r&   rY   Úmodelr1   r1   r2   Ú_shufflenetv2Z   s    	

r^   )r   r   rZ   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_sizerX   rY   Ú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.pthr`   ra   iÌÄ" rc   g×£p=
Q@gh‘í|?åU@rd   g�Âõ(\�Â?gyé&1¬@re   rk   N)r;   r<   r=   r   r   r
   rm   r   rn   ro   rp   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.pthr`   éè   ©rb   Zresize_sizeú+https://github.com/pytorch/vision/pull/5906iv5 rc   gÙÎ÷SR@gÍÌÌÌÌ¬V@rd   g‹lçû©ñÒ?gÇK7‰A`@©r_   rf   rg   rh   ri   rj   rk   N)r;   r<   r=   r   r   r
   rm   r   rn   ro   rp   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.pthr`   rq   rr   rs   iÌÒp rc   g-²�ï§ÖR@g¬Zd;W@rd   g-²�ï§â?g‘í|?5Þ@rt   rk   N)r;   r<   r=   r   r   r
   rm   r   rn   ro   rp   r1   r1   r1   r2   r   À   s"   þÿôýZquantized_shufflenet_v2_x0_5)rO   Z
pretrainedc                 C   s   |   dd¡rtjS tjS ©NrW   F)Úgetr   ro   r   rn   ©r&   r1   r1   r2   Ú<lambda>Ú   s    
ÿrx   )rU   TF©rU   rV   rW   )rU   rV   rW   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   é`   éÀ   é   ry   )r   r   Úverifyr^   ©rU   rV   rW   r&   r1   r1   r2   r   Ö   s    0 ÿ  ÿÿZquantized_shufflenet_v2_x1_0c                 C   s   |   dd¡rtjS tjS ru   )rv   r   ro   r   rn   rw   r1   r1   r2   rx     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:
    rz   r{   r|   ét   rq   iÐ  r€   ry   )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 ru   )rv   r   ro   r   rn   rw   r1   r1   r2   rx   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:
    rz   r{   r|   é°   i`  iÀ  r€   ry   )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 ru   )rv   r   ro   r   rn   rw   r1   r1   r2   rx   |  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:
    rz   r{   r|   éô   iè  iÐ  i   ry   )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   Z_metar   Ú_utilsr   r   r   r   r   r   Úutilsr   r   r   Ú__all__ZInvertedResidualr#   ZShuffleNetV2r   ÚintrR   r^   rm   r   r   r   r   r   r    r!   r"   r1   r1   r1   r2   Ú<module>   sÒ   ÷'øûþÿ
üú-þÿ
üú-þÿ
üú-þÿ
üú