U
    (d?                  	   @   sj  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 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(d) fd*dd+d,d-eee(ef  e%e%ee#d.d/dZ,ed'd0d) fd*dd+d,d-eee)ef  e%e%ee#d.d1dZ-dd+d,d-eee*ef  e%e%ee#d.d2dZ.dd+d,d-eee+ef  e%e%ee#d.d3dZ/dd4lm0Z0 dd5lm1Z1 e0e(j2j3e)j2j3d6Z4dS )7    )partial)AnyListOptionalUnionN)Tensor)shufflenetv2   )ImageClassification   )WeightsEnumWeights)_IMAGENET_CATEGORIES)handle_legacy_interface_ovewrite_named_param)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__ P/tmp/pip-unpacked-wheel-vx7f76es/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-   r3   x1Zx2outr0   r0   r1   forward(   s    
 z#QuantizableInvertedResidual.forward)__name__
__module____qualname__r   r)   r   r9   __classcell__r0   r0   r.   r1   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.   r0   r1   r)   6   s    z QuantizableShuffleNetV2.__init__r2   c                 C   s"   |  |}| |}| |}|S r'   )r?   Z_forward_implr@   )r-   r3   r0   r0   r1   r9   ;   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rt|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"   lenr6   r5   )r-   rA   namemr0   r0   r1   
fuse_modelA   s    
 z"QuantizableShuffleNetV2.fuse_model)N)r:   r;   r<   r   r)   r   r9   r   boolrP   r=   r0   r0   r.   r1   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 |S )NZnum_classes
categoriesbackendfbgemm)rU   )	r   rM   metapopr   r   r   Zload_state_dictZget_state_dict)rR   rS   rT   rU   rV   r%   rX   modelr0   r0   r1   _shufflenetv2Z   s    	

r]   )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.
    )Zmin_sizerW   rX   recipeZ_docsc                
   @   s>   e Zd Zedeeddedejddddid	d
Z	e	Z
dS )r   zShttps://download.pytorch.org/models/quantized/shufflenetv2_x0.5_fbgemm-00845098.pth   	crop_sizei ImageNet-1Kg#~jL@gRS@zacc@1zacc@5
num_paramsunquantized_metricsurlZ
transformsrZ   N)r:   r;   r<   r   r   r
   _COMMON_METAr   IMAGENET1K_V1IMAGENET1K_FBGEMM_V1DEFAULTr0   r0   r0   r1   r      s   
c                
   @   s>   e Zd Zedeeddedejddddid	d
Z	e	Z
dS )r   zQhttps://download.pytorch.org/models/quantized/shufflenetv2_x1_fbgemm-db332c57.pthr_   r`   i" rb   gףp=
Q@gh|?U@rc   rd   rh   N)r:   r;   r<   r   r   r
   rj   r   rk   rl   rm   r0   r0   r0   r1   r      s   
c                   @   sB   e Zd Zedeedddeddejddd	d
iddZ	e	Z
dS )r   zShttps://download.pytorch.org/models/quantized/shufflenetv2_x1_5_fbgemm-d7401f05.pthr_      ra   Zresize_size+https://github.com/pytorch/vision/pull/5906iv5 rb   gSR@g̬V@rc   r^   re   rf   rg   rh   N)r:   r;   r<   r   r   r
   rj   r   rk   rl   rm   r0   r0   r0   r1   r      s   c                   @   sB   e Zd Zedeedddeddejddd	d
iddZ	e	Z
dS )r   zShttps://download.pytorch.org/models/quantized/shufflenetv2_x2_0_fbgemm-5cac526c.pthr_   rn   ro   rp   ip rb   g-R@gZd;W@rc   rq   rh   N)r:   r;   r<   r   r   r
   rj   r   rk   rl   rm   r0   r0   r0   r1   r      s   Z
pretrainedc                 C   s   |  ddrtjS tjS NrV   F)getr   rl   r   rk   r%   r0   r0   r1   <lambda>   s    
ru   )rT   TFrT   rU   rV   )rT   rU   rV   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   `         rv   )r   r   verifyr]   rT   rU   rV   r%   r0   r0   r1   r      s    /   c                 C   s   |  ddrtjS tjS rr   )rs   r   rl   r   rk   rt   r0   r0   r1   ru     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:
    rw   rx   ry   t   rn   i  r}   rv   )r   r   r~   r]   r   r0   r0   r1   r     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:
    rw   rx   ry      i`  i  r}   rv   )r   r   r~   r]   r   r0   r0   r1   r    8  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:
    rw   rx   ry      i  i  i   rv   )r   r   r~   r]   r   r0   r0   r1   r!   e  s    '   )
_ModelURLs)
model_urls)zshufflenetv2_x0.5_fbgemmzshufflenetv2_x1.0_fbgemm)5	functoolsr   typingr   r   r   r   r>   Ztorch.nnr*   r   Ztorchvision.modelsr   Ztransforms._presetsr
   Z_apir   r   Z_metar   _utilsr   r   r   r   r   r   utilsr   r   r   __all__ZInvertedResidualr"   ZShuffleNetV2r   intrQ   r]   rj   r   r   r   r   r   r   r    r!   r   r   rl   ri   Zquant_model_urlsr0   r0   r0   r1   <module>   s   '
-
//.