
    YhN<                        d dl mZ d dl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 g dZde	dede	fdZ G d dej                  Z G d dej                  Zdee         dedededef
dZdeddZ G d de          Z  G d d e          Z! G d! d"e          Z" G d# d$e          Z# e             ed%e j$        f&          dd'd(dee          dededefd)                        Z% e             ed%e!j$        f&          dd'd(dee!         dededefd*                        Z& e             ed%e"j$        f&          dd'd(dee"         dededefd+                        Z' e             ed%e#j$        f&          dd'd(dee#         dededefd,                        Z(dS )-    )partial)AnyCallableOptionalN)Tensor   )ImageClassification)_log_api_usage_once   )register_modelWeightsWeightsEnum)_IMAGENET_CATEGORIES)_ovewrite_named_paramhandle_legacy_interface)	ShuffleNetV2ShuffleNet_V2_X0_5_WeightsShuffleNet_V2_X1_0_WeightsShuffleNet_V2_X1_5_WeightsShuffleNet_V2_X2_0_Weightsshufflenet_v2_x0_5shufflenet_v2_x1_0shufflenet_v2_x1_5shufflenet_v2_x2_0xgroupsreturnc                     |                                  \  }}}}||z  }|                     |||||          } t          j        | dd                                          } |                     ||||          } | S )Nr   r   )sizeviewtorch	transpose
contiguous)r   r   	batchsizenum_channelsheightwidthchannels_per_groups          q/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torchvision/models/shufflenetv2.pychannel_shuffler*      s|    -.VVXX*I|VU%/ 	
y&"4feDDA1a  ++--A 	
y,66AH    c                        e Zd Zdedededdf fdZe	 dd
ededededededej        fd            Z	de
de
fdZ xZS )InvertedResidualinpoupstrider   Nc                    t                                                       d|cxk    rdk    sn t          d          || _        |dz  }| j        dk    r"||dz  k    rt          d| d| d| d          | j        dk    rt	          j        |                     ||d| j        d	          t	          j        |          t	          j        ||ddd
d          t	          j        |          t	          j	        d                    | _
        nt	          j                    | _
        t	          j        t	          j        | j        dk    r|n||ddd
d          t	          j        |          t	          j	        d          |                     ||d| j        d	          t	          j        |          t	          j        ||ddd
d          t	          j        |          t	          j	        d                    | _        d S )Nr      zillegal stride valuer   zInvalid combination of stride z, inp z	 and oup zB values. If stride == 1 then inp should be equal to oup // 2 << 1.kernel_sizer0   paddingr   F)r4   r0   r5   biasTinplace)super__init__
ValueErrorr0   nn
Sequentialdepthwise_convBatchNorm2dConv2dReLUbranch1branch2)selfr.   r/   r0   branch_features	__class__s        r)   r:   zInvertedResidual.__init__,   s"   V    q    3444(K13/Q*>#>#> U  U  Us  U  US  U  U  U   ;??=##C!DKYZ#[[s##	#AaQRY^___//%%% DLL =??DL}Iao   N?++GD!!!aX\XcmnooN?++IoAaYZafgggN?++GD!!!
 
r+   r   r   Fior4   r5   r6   c           	      8    t          j        | ||||||           S )N)r6   r   )r<   r@   )rG   rH   r4   r0   r5   r6   s         r)   r>   zInvertedResidual.depthwise_convV   s#     yA{FG$qQQQQr+   r   c                 D   | j         dk    rF|                    dd          \  }}t          j        ||                     |          fd          }n>t          j        |                     |          |                     |          fd          }t          |d          }|S )Nr   r   )dim)r0   chunkr!   catrC   rB   r*   )rD   r   x1x2outs        r)   forwardzInvertedResidual.forward\   s    ;!WWQAW&&FB)Rb!1!12:::CC)T\\!__dll1oo>AFFFCc1%%
r+   )r   r   F)__name__
__module____qualname__intr:   staticmethodboolr<   r@   r>   r   rQ   __classcell__rF   s   @r)   r-   r-   +   s        (
C (
c (
3 (
4 (
 (
 (
 (
 (
 (
T Z_R RRR%(R25RDGRSWR	R R R \R
	 	F 	 	 	 	 	 	 	 	r+   r-   c                        e Zd Zdefdee         dee         dededej        f         ddf
 fd	Z	d
e
de
fdZd
e
de
fdZ xZS )r   i  stages_repeatsstages_out_channelsnum_classesinverted_residual.r   Nc           
      6   t                                                       t          |            t          |          dk    rt	          d          t          |          dk    rt	          d          || _        d}| j        d         }t          j        t          j        ||dddd	          t          j	        |          t          j
        d
                    | _        |}t          j        ddd          | _        |  |  |  d dD             }t          ||| j        dd                    D ]h\  }}	} |||d          g}
t          |	dz
            D ]"}|
                     |||d                     #t#          | |t          j        |
            |}i| j        d         }t          j        t          j        ||dddd	          t          j	        |          t          j
        d
                    | _        t          j        ||          | _        d S )Nr2   z2expected stages_repeats as list of 3 positive ints   z7expected stages_out_channels as list of 5 positive intsr   r   r   F)r6   Tr7   r3   c                     g | ]}d | S )stage ).0rG   s     r)   
<listcomp>z)ShuffleNetV2.__init__.<locals>.<listcomp>   s    666q{q{{666r+   )r   r2      )r9   r:   r
   lenr;   _stage_out_channelsr<   r=   r@   r?   rA   conv1	MaxPool2dmaxpoolziprangeappendsetattrconv5Linearfc)rD   r[   r\   r]   r^   input_channelsoutput_channelsstage_namesnamerepeatsseqrG   rF   s               r)   r:   zShuffleNetV2.__init__i   s2    	D!!!~!##QRRR"##q((VWWW#6 215]Inoq!QUKKKN?++GD!!!
 


 )|!QGGG 	"""66I666.1+~tOghihjhjOk.l.l 	- 	-*D'?$$^_aHHIC7Q;'' S S

,,_oqQQRRRRD$s 3444,NN226]Inoq!QUKKKN?++GD!!!
 

 )O[99r+   r   c                 Z   |                      |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }|                    ddg          }|                     |          }|S )Nr   r2   )rj   rl   stage2stage3stage4rq   meanrs   rD   r   s     r)   _forward_implzShuffleNetV2._forward_impl   s    JJqMMLLOOKKNNKKNNKKNNJJqMMFFAq6NNGGAJJr+   c                 ,    |                      |          S )N)r   r   s     r)   rQ   zShuffleNetV2.forward   s    !!!$$$r+   )rR   rS   rT   r-   listrU   r   r<   Moduler:   r   r   rQ   rX   rY   s   @r)   r   r   h   s        
  6F.: .:S	.: "#Y.: 	.:
 $CN3.: 
.: .: .: .: .: .:`
v 
& 
 
 
 
% %F % % % % % % % %r+   r   weightsprogressargskwargsc                     | )t          |dt          | j        d                              t          |i |}| *|                    |                     |d                     |S )Nr]   
categoriesT)r   
check_hash)r   rh   metar   load_state_dictget_state_dict)r   r   r   r   models        r)   _shufflenetv2r      sp     fmSl9S5T5TUUU$)&))Eg44hSW4XXYYYLr+   )r   r   z2https://github.com/ericsun99/Shufflenet-v2-Pytorch)min_sizer   recipec                   b    e Zd Z ed eed          i edddddid	d
dd          ZeZdS )r   zDhttps://download.pytorch.org/models/shufflenetv2_x0.5-f707e7126e.pth   	crop_sizei ImageNet-1Kg-FN@g9voT@zacc@1zacc@5g{Gz?gT㥛 @VThese weights were trained from scratch to reproduce closely the results of the paper.
num_params_metrics_ops
_file_size_docsurl
transformsr   N	rR   rS   rT   r   r   r	   _COMMON_METAIMAGENET1K_V1DEFAULTrc   r+   r)   r   r      s        GR7.#>>>

!##    q
 
 
	  M$ GGGr+   r   c                   b    e Zd Z ed eed          i edddddid	d
dd          ZeZdS )r   zBhttps://download.pytorch.org/models/shufflenetv2_x1-5666bf0f80.pthr   r   i" r   gI+WQ@gNbX9V@r   g(\?gE!@r   r   r   Nr   rc   r+   r)   r   r      s        GP7.#>>>

!##    q
 
 
	  M$ GGGr+   r   c                   f    e Zd Z ed eedd          i eddddd	d
idddd          ZeZdS )r   zBhttps://download.pytorch.org/models/shufflenetv2_x1_5-3c479a10.pthr      r   resize_size+https://github.com/pytorch/vision/pull/5906iv5 r   g9v?R@g/$V@r   gl?gw/+@
                These weights were trained from scratch by using TorchVision's `new training recipe
                <https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/>`_.
            r   r   r   r   r   r   r   Nr   rc   r+   r)   r   r              GP7.#3OOO

C!##     
 
 
  M* GGGr+   r   c                   f    e Zd Z ed eedd          i eddddd	d
idddd          ZeZdS )r   zBhttps://download.pytorch.org/models/shufflenetv2_x2_0-8be3c8ee.pthr   r   r   r   ip r   gQS@gMb@W@r   g-?g+n<@r   r   r   Nr   rc   r+   r)   r   r     r   r+   r   
pretrained)r   T)r   r   c                 ^    t                               |           } t          | |g dg dfi |S )a  
    Constructs a ShuffleNetV2 architecture with 0.5x output channels, as described in
    `ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design
    <https://arxiv.org/abs/1807.11164>`__.

    Args:
        weights (:class:`~torchvision.models.ShuffleNet_V2_X0_5_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ShuffleNet_V2_X0_5_Weights` 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.
        **kwargs: parameters passed to the ``torchvision.models.shufflenetv2.ShuffleNetV2``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/shufflenetv2.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ShuffleNet_V2_X0_5_Weights
        :members:
    rf      rf   )   0   `         )r   verifyr   r   r   r   s      r)   r   r     s?    4 )//88G(III7N7N7NYYRXYYYr+   c                 ^    t                               |           } t          | |g dg dfi |S )a  
    Constructs a ShuffleNetV2 architecture with 1.0x output channels, as described in
    `ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design
    <https://arxiv.org/abs/1807.11164>`__.

    Args:
        weights (:class:`~torchvision.models.ShuffleNet_V2_X1_0_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ShuffleNet_V2_X1_0_Weights` 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.
        **kwargs: parameters passed to the ``torchvision.models.shufflenetv2.ShuffleNetV2``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/shufflenetv2.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ShuffleNet_V2_X1_0_Weights
        :members:
    r   )r   t   r   i  r   )r   r   r   r   s      r)   r   r   >  ?    4 )//88G(III7P7P7P[[TZ[[[r+   c                 ^    t                               |           } t          | |g dg dfi |S )a  
    Constructs a ShuffleNetV2 architecture with 1.5x output channels, as described in
    `ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design
    <https://arxiv.org/abs/1807.11164>`__.

    Args:
        weights (:class:`~torchvision.models.ShuffleNet_V2_X1_5_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ShuffleNet_V2_X1_5_Weights` 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.
        **kwargs: parameters passed to the ``torchvision.models.shufflenetv2.ShuffleNetV2``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/shufflenetv2.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ShuffleNet_V2_X1_5_Weights
        :members:
    r   )r      i`  i  r   )r   r   r   r   s      r)   r   r   ]  r   r+   c                 ^    t                               |           } t          | |g dg dfi |S )a  
    Constructs a ShuffleNetV2 architecture with 2.0x output channels, as described in
    `ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design
    <https://arxiv.org/abs/1807.11164>`__.

    Args:
        weights (:class:`~torchvision.models.ShuffleNet_V2_X2_0_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ShuffleNet_V2_X2_0_Weights` 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.
        **kwargs: parameters passed to the ``torchvision.models.shufflenetv2.ShuffleNetV2``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/shufflenetv2.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ShuffleNet_V2_X2_0_Weights
        :members:
    r   )r      i  i  i   )r   r   r   r   s      r)   r   r   |  r   r+   ))	functoolsr   typingr   r   r   r!   torch.nnr<   r   transforms._presetsr	   utilsr
   _apir   r   r   _metar   _utilsr   r   __all__rU   r*   r   r-   r   rW   r   r   r   r   r   r   r   r   r   r   r   rc   r+   r)   <module>r      s         * * * * * * * * * *              5 5 5 5 5 5 ' ' ' ' ' ' 6 6 6 6 6 6 6 6 6 6 ' ' ' ' ' ' B B B B B B B B
 
 
v s v    : : : : :ry : : :z>% >% >% >% >%29 >% >% >%Bk"  	
    $ &B        ,       ,       2       2 ,0J0X!YZZZ7;dZ Z Z34ZGKZ^aZZ Z Z [Z Z: ,0J0X!YZZZ7;d\ \ \34\GK\^a\\ \ \ [Z \: ,0J0X!YZZZ7;d\ \ \34\GK\^a\\ \ \ [Z \: ,0J0X!YZZZ7;d\ \ \34\GK\^a\\ \ \ [Z \ \ \r+   