
    khT<                     .   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 g dZde
dede
fdZ G d de	j6                        Z G d de	j6                        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!jJ                  f&      dd'd(dee!   dededefd)              Z& e        ed%e"jJ                  f&      dd'd(dee"   dededefd*              Z' e        ed%e#jJ                  f&      dd'd(dee#   dededefd+              Z( e        ed%e$jJ                  f&      dd'd(dee$   dededefd,              Z)y)-    )partial)AnyCallableList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                     | j                         \  }}}}||z  }| j                  |||||      } t        j                  | dd      j	                         } | j                  ||||      } | S )Nr   r	   )sizeviewtorch	transpose
contiguous)r   r   	batchsizenum_channelsheightwidthchannels_per_groups          [/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/models/shufflenetv2.pychannel_shuffler+      sp    -.VVX*I|VU%/ 	
y&"4feDA1a ++-A 	
y,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t        d       t        d      || _        |dz  }| j                  dk(  r||dz  k7  rt        d| d| d| d      | j                  dkD  rt	        j
                  | 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kD  r|n||ddd
d      t	        j                  |      t	        j                  d      | j                  ||d| j                  d	      t	        j                  |      t	        j                  ||ddd
d      t	        j                  |      t	        j                  d            | _        y )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_sizer1   paddingr   F)r5   r1   r6   biasTinplace)super__init__
ValueErrorr1   nn
Sequentialdepthwise_convBatchNorm2dConv2dReLUbranch1branch2)selfr/   r0   r1   branch_features	__class__s        r*   r;   zInvertedResidual.__init__,   s   V q 344 !344(KK13/Q*>#>0se9SE  RT  U  ;;?==##C!DKKYZ#[s#		#AaQRY^_/%DL ==?DL}}IIao NN?+GGD!aX\XcXcmnoNN?+IIoAaYZafgNN?+GGD!
r,   ior5   r6   r7   c           	      :    t        j                  | ||||||       S )N)r7   r   )r=   rA   )rH   rI   r5   r1   r6   r7   s         r*   r?   zInvertedResidual.depthwise_convV   s     yyA{FG$qQQr,   r   c                 (   | j                   dk(  r?|j                  dd      \  }}t        j                  || j	                  |      fd      }n7t        j                  | j                  |      | j	                  |      fd      }t        |d      }|S )Nr   r	   )dim)r1   chunkr"   catrD   rC   r+   )rE   r   x1x2outs        r*   forwardzInvertedResidual.forward\   sx    ;;!WWQAW&FB))Rb!12:C))T\\!_dll1o>AFCc1%
r,   )r   r   F)__name__
__module____qualname__intr;   staticmethodboolr=   rA   r?   r   rR   __classcell__rG   s   @r*   r.   r.   +   s    (
C (
c (
3 (
4 (
T Z_RRR%(R25RDGRSW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           
         t         |           t        |        t        |      dk7  rt	        d      t        |      dk7  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 cg c]  }d| 	 }}t        ||| j
                  dd        D ]\  \  }	}
} |||d      g}t        |
dz
        D ]  }|j!                   |||d              t#        | |	t        j                  |        |}^ | j
                  d   }t        j                  t        j                  ||dddd	      t        j                  |      t        j                  d
            | _        t        j&                  ||      | _        y c c}w )Nr3   z2expected stages_repeats as list of 3 positive ints   z7expected stages_out_channels as list of 5 positive intsr   r	   r   F)r7   Tr8   r4   )r	   r3      stage)r:   r;   r   lenr<   _stage_out_channelsr=   r>   rA   r@   rB   conv1	MaxPool2dmaxpoolziprangeappendsetattrconv5Linearfc)rE   r\   r]   r^   r_   input_channelsoutput_channelsrH   stage_namesnamerepeatsseqrG   s               r*   r;   zShuffleNetV2.__init__i   s    	D!~!#QRR"#q(VWW#6 2215]]IInoq!QUKNN?+GGD!


 )||!QG 	,56qqc{66.1+~tOgOghihjOk.l 	-*D'?$^_aHIC7Q;' S

,_oqQRSD$s 34,N	- 2226]]IInoq!QUKNN?+GGD!

 ))O[9 7s   *G8r   c                    | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }| j                  |      }|j                  ddg      }| j                  |      }|S )Nr	   r3   )rg   ri   stage2stage3stage4rn   meanrp   rE   r   s     r*   _forward_implzShuffleNetV2._forward_impl   ss    JJqMLLOKKNKKNKKNJJqMFFAq6NGGAJr,   c                 $    | j                  |      S )N)r}   r|   s     r*   rR   zShuffleNetV2.forward   s    !!!$$r,   )rS   rT   rU   r.   r   rV   r   r=   Moduler;   r   r}   rR   rY   rZ   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 |}| "|j	                  | j                  |d             |S )Nr^   
categoriesT)r   
check_hash)r   re   metar   load_state_dictget_state_dict)r   r   r   r   models        r*   _shufflenetv2r      s]     fmSl9S5TU$)&)Eg44hSW4XYLr,   )r   r   z2https://github.com/ericsun99/Shufflenet-v2-Pytorch)min_sizer   recipec                   T    e Zd Z ed eed      i edddddid	d
dd      ZeZy)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	rS   rT   rU   r   r   r
   _COMMON_METAIMAGENET1K_V1DEFAULT r,   r*   r   r      sT    R.#>

!##  q
	M$ Gr,   r   c                   T    e Zd Z ed eed      i edddddid	d
dd      ZeZy)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   r   r,   r*   r   r      sT    P.#>

!##  q
	M$ Gr,   r   c                   X    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y)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   r   r,   r*   r   r      [    P.#3O

C!##   
M* Gr,   r   c                   X    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y)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   r   r,   r*   r   r     r   r,   r   
pretrained)r   T)r   r   c                 R    t         j                  |       } 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:
    rb      rb   )   0   `         )r   verifyr   r   r   r   s      r*   r   r     s,    4 )//8G(I7NYRXYYr,   c                 R    t         j                  |       } 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 )//8G(I7P[TZ[[r,   c                 R    t         j                  |       } 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                 R    t         j                  |       } 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   r"   torch.nnr=   r   transforms._presetsr
   utilsr   _apir   r   r   _metar   _utilsr   r   __all__rV   r+   r   r.   r   rX   r   r   r   r   r   r   r   r   r   r   r   r   r,   r*   <module>r      sn    0 0    5 ' 6 6 ' B
v s v :ryy :z>%299 >%Bk"  	
 $ &B , , 2 2 ,0J0X0X!YZ7;dZ34ZGKZ^aZZ [ Z: ,0J0X0X!YZ7;d\34\GK\^a\\ [ \: ,0J0X0X!YZ7;d\34\GK\^a\\ [ \: ,0J0X0X!YZ7;d\34\GK\^a\\ [ \r,   