o
    VhA                     @   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 g dZ G dd dej!Z"G dd dej#Z$dee% dee% dee de&de&dede$f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d0d1d2deee)ef  de&de&dede$f
d3d4Z-ed5d+ed,d6d. fd/dd0d1d2deee*ef  de&de&dede$f
d7d8Z.ed9d+ed,d:d. fd/dd0d1d2deee+ef  de&de&dede$f
d;d<Z/ed=d+ed,d>d. fd/dd0d1d2deee,ef  de&de&dede$f
d?d@Z0dS )A    )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                       s<   e Zd Zdededdf fddZdedefdd	Z  ZS )
QuantizableInvertedResidualargskwargsreturnNc                    s"   t  j|i | tj | _d S N)super__init__nn	quantizedFloatFunctionalcatselfr$   r%   	__class__ `/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/models/quantization/shufflenetv2.pyr)   $   s   z$QuantizableInvertedResidual.__init__xc                 C   sh   | j dkr|jddd\}}| jj|| |gdd}n| jj| || |gdd}t|d}|S )Nr   r   )dim)stridechunkr-   branch2branch1r   channel_shuffle)r/   r4   x1x2outr2   r2   r3   forward(   s   
 z#QuantizableInvertedResidual.forward)__name__
__module____qualname__r   r)   r   r>   __classcell__r2   r2   r0   r3   r#   #   s    r#   c                       sT   e Zd Zdededdf fddZdedefdd	Zdd
ee ddfddZ	  Z
S )r   r$   r%   r&   Nc                    s6   t  j|dti| tjj | _tjj | _	d S )Ninverted_residual)
r(   r)   r#   torchaoquantization	QuantStubquantDeQuantStubdequantr.   r0   r2   r3   r)   6   s   z QuantizableShuffleNetV2.__init__r4   c                 C   s"   |  |}| |}| |}|S r'   )rH   _forward_implrJ   )r/   r4   r2   r2   r3   r>   ;   s   


zQuantizableShuffleNetV2.forwardis_qatc                 C   s   | j  D ]\}}|dv r|durt|g dg|dd q|  D ]3}t|tu rTt|jj  dkrBt|jddgg d	g|dd t|jg dd
dgg dg|dd q!d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
        )conv1conv5N)012T)inplacer   rO   rP   )rQ   34rS   rT   )567)	_modulesitemsr   modulestyper#   lenr9   r8   )r/   rL   namemr2   r2   r3   
fuse_modelA   s    
z"QuantizableShuffleNetV2.fuse_modelr'   )r?   r@   rA   r   r)   r   r>   r   boolr_   rB   r2   r2   r0   r3   r   4   s     r   stages_repeatsstages_out_channels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| |r7t|| |d urE||j|dd |S )Nnum_classes
categoriesbackendfbgemmT)rd   
check_hash)	r   r\   metapopr   r   r   load_state_dictget_state_dict)ra   rb   rc   rd   re   r%   rh   modelr2   r2   r3   _shufflenetv2Z   s   	

rp   )r   r   ri   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_sizerg   rh   recipe_docsc                
   @   F   e Zd Zedeeddi 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#~jL@gRS@zacc@1zacc@5g{Gz?gjt?
num_paramsunquantized_metrics_ops
_file_sizeurl
transformsrk   N)r?   r@   rA   r   r   r
   _COMMON_METAr   IMAGENET1K_V1IMAGENET1K_FBGEMM_V1DEFAULTr2   r2   r2   r3   r      &    
r   c                
   @   rt   )r   zQhttps://download.pytorch.org/models/quantized/shufflenetv2_x1_fbgemm-1e62bb32.pthru   rv   i" rx   gףp=
Q@gh|?U@ry   g(\?gy&1@rz   r   N)r?   r@   rA   r   r   r
   r   r   r   r   r   r2   r2   r2   r3   r      r   r   c                   @   J   e Zd Zedeedddi 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.pthru      rw   resize_size+https://github.com/pytorch/vision/pull/5906iv5 rx   gSR@g̬V@ry   gl?gK7A`@rr   r{   r|   r}   r~   r   r   N)r?   r@   rA   r   r   r
   r   r   r   r   r   r2   r2   r2   r3   r      (    r   c                   @   r   )r   zShttps://download.pytorch.org/models/quantized/shufflenetv2_x2_0_fbgemm-5cac526c.pthru   r   r   r   ip rx   g-R@gZd;W@ry   g-?g|?5@r   r   N)r?   r@   rA   r   r   r
   r   r   r   r   r   r2   r2   r2   r3   r      r   r   quantized_shufflenet_v2_x0_5)r]   
pretrainedc                 C      |  ddr	tjS tjS Nre   F)getr   r   r   r   r%   r2   r2   r3   <lambda>      
r   )rc   TFrc   rd   re   c                 K   4   |rt nt| } tg dg d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:
          r   )   0   `         r   )r   r   verifyrp   rc   rd   re   r%   r2   r2   r3   r         0r   quantized_shufflenet_v2_x1_0c                 C   r   r   )r   r   r   r   r   r   r2   r2   r3   r     r   c                 K   r   )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   t   r   i  r   r   )r   r   r   rp   r   r2   r2   r3   r      r   r    quantized_shufflenet_v2_x1_5c                 C   r   r   )r   r   r   r   r   r   r2   r2   r3   r   F  r   c                 K   r   )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      i`  i  r   r   )r   r   r   rp   r   r2   r2   r3   r!   B  r   r!   quantized_shufflenet_v2_x2_0c                 C   r   r   )r   r   r   r   r   r   r2   r2   r3   r   |  r   c                 K   r   )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      i  i  i   r   )r   r   r   rp   r   r2   r2   r3   r"   x  r   r"   )1	functoolsr   typingr   r   r   r   rD   torch.nnr*   r   torchvision.modelsr   transforms._presetsr
   _apir   r   r   _metar   _utilsr   r   r   r   r   r   utilsr   r   r   __all__InvertedResidualr#   ShuffleNetV2r   intr`   rp   r   r   r   r   r   r   r    r!   r"   r2   r2   r2   r3   <module>   s    &

-
-
-
