
    kh;                        d dl mZ d dlmZmZmZmZmZ d dl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 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  G d de	jB                        Z" G d de	jF                        Z$ G d d      Z% G d de	jF                        Z&dee%   de'dee   de(dede&fdZ)ded 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/              Z0 e        ed+e,j^                  f,      dd-d.dee,   de(dede&fd0              Z1 e        ed+e-j^                  f,      dd-d.dee-   de(dede&fd1              Z2 e        ed+e.j^                  f,      dd-d.dee.   de(dede&fd2              Z3y)3    )partial)AnyCallableListOptionalSequenceN)nnTensor)
functional   )Conv2dNormActivationPermute)StochasticDepth)ImageClassification)_log_api_usage_once   )register_modelWeightsWeightsEnum)_IMAGENET_CATEGORIES)_ovewrite_named_paramhandle_legacy_interface)	ConvNeXtConvNeXt_Tiny_WeightsConvNeXt_Small_WeightsConvNeXt_Base_WeightsConvNeXt_Large_Weightsconvnext_tinyconvnext_smallconvnext_baseconvnext_largec                       e Zd ZdedefdZy)LayerNorm2dxreturnc                     |j                  dddd      }t        j                  || j                  | j                  | j
                  | j                        }|j                  dddd      }|S )Nr   r      r   )permuteF
layer_normnormalized_shapeweightbiasepsselfr$   s     W/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/models/convnext.pyforwardzLayerNorm2d.forward   sW    IIaAq!LLD114;;		488TIIaAq!    N)__name__
__module____qualname__r
   r2    r3   r1   r#   r#      s     F r3   r#   c            
       h     e Zd Z	 d
dededeedej                  f      ddf fdZde	de	fd	Z
 xZS )CNBlockNlayer_scalestochastic_depth_prob
norm_layer.r%   c                    t         |           |t        t        j                  d      }t        j
                  t        j                  ||dd|d      t        g d       ||      t        j                  |d|z  d	      t        j                         t        j                  d|z  |d	      t        g d
            | _
        t        j                  t        j                  |dd      |z        | _        t        |d      | _        y )Nư>r.      r'   T)kernel_sizepaddinggroupsr-   )r   r   r'   r      )in_featuresout_featuresr-   )r   r'   r   r   r   row)super__init__r   r	   	LayerNorm
SequentialConv2dr   LinearGELUblock	Parametertorchonesr:   r   stochastic_depth)r0   dimr:   r;   r<   	__class__s        r1   rI   zCNBlock.__init__'   s     	 48J]]IIc3Aq4PL!sOII#AG$GGGIII!c'$GL!

 <<

31(=(KL /0Eu Mr3   inputc                 n    | j                   | j                  |      z  }| j                  |      }||z  }|S N)r:   rO   rS   )r0   rV   results      r1   r2   zCNBlock.forward>   s9    !!DJJu$55&&v.%r3   rX   )r4   r5   r6   floatr   r   r	   ModulerI   r
   r2   __classcell__rU   s   @r1   r9   r9   &   s_     :>N N  %	N
 Xc299n56N 
N.V  r3   r9   c                   6    e Zd Zdedee   deddfdZdefdZy)CNBlockConfiginput_channelsout_channels
num_layersr%   Nc                 .    || _         || _        || _        y rX   )r`   ra   rb   )r0   r`   ra   rb   s       r1   rI   zCNBlockConfig.__init__G   s     -($r3   c                     | j                   j                  dz   }|dz  }|dz  }|dz  }|dz  } |j                  di | j                  S )N(zinput_channels={input_channels}z, out_channels={out_channels}z, num_layers={num_layers})r7   )rU   r4   format__dict__)r0   ss     r1   __repr__zCNBlockConfig.__repr__Q   sX    NN##c)	..	,,	((	Sqxx($--((r3   )r4   r5   r6   intr   rI   strrj   r7   r3   r1   r_   r_   E   s=    %% sm% 	%
 
%)# )r3   r_   c                        e Zd Z	 	 	 	 	 ddee   dedededeede	j                  f      deede	j                  f      d	ed
df fdZded
efdZded
efdZ xZS )r   Nblock_settingr;   r:   num_classesrO   .r<   kwargsr%   c                    t         |           t        |        |st        d      t	        |t
              r't        |D cg c]  }t	        |t               c}      st        d      |t        }|t        t        d      }g }	|d   j                  }
|	j                  t        d|
ddd|d d	             t        d
 |D              }d}|D ]  }g }t!        |j"                        D ]5  }||z  |dz
  z  }|j                   ||j                  ||             |dz  }7 |	j                  t%        j&                  |        |j(                  |	j                  t%        j&                   ||j                        t%        j*                  |j                  |j(                  dd                    t%        j&                  |	 | _        t%        j.                  d      | _        |d   }|j(                  |j(                  n|j                  }t%        j&                   ||      t%        j2                  d      t%        j4                  ||            | _        | j9                         D ]  }t	        |t$        j*                  t$        j4                  f      s.t$        j:                  j=                  |j>                  d       |j@                  ft$        j:                  jC                  |j@                          y c c}w )Nz%The block_setting should not be emptyz/The block_setting should be List[CNBlockConfig]r>   r?   r   r'   rD   T)rA   striderB   r<   activation_layerr-   c              3   4   K   | ]  }|j                     y wrX   )rb   ).0cnfs     r1   	<genexpr>z$ConvNeXt.__init__.<locals>.<genexpr>   s      IC Is   g      ?r   r   )rA   rr   g{Gz?)std)"rH   rI   r   
ValueError
isinstancer   allr_   	TypeErrorr9   r   r#   r`   appendr   sumrangerb   r	   rK   ra   rL   featuresAdaptiveAvgPool2davgpoolFlattenrM   
classifiermodulesinittrunc_normal_r,   r-   zeros_)r0   rn   r;   r:   ro   rO   r<   rp   ri   layersfirstconv_output_channelstotal_stage_blocksstage_block_idrv   stage_sd_prob	lastblocklastconv_output_channelsmrU   s                       r1   rI   zConvNeXt.__init__[   s    	D!DEE]H5#er>s`az!]?[>s:tMNN=E $7J"$ %2!$4$C$C! )%!%		
 ! I= II  	C%'E3>>* $/.@DVY\D\]U3#5#5{GLM!#	$
 MM"--/0+MM"3#5#56		#"4"4c6F6FTU^_`	$ v.++A.!"%	&/&<&<&HI""iNfNf 	! --/0"**Q-KcepAq
  	+A!bii34%%ahhD%966%GGNN166*		+s ?ts   K5r$   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rX   )r   r   r   r/   s     r1   _forward_implzConvNeXt._forward_impl   s0    MM!LLOOOAr3   c                 $    | j                  |      S rX   )r   r/   s     r1   r2   zConvNeXt.forward   s    !!!$$r3   )g        r>   i  NN)r4   r5   r6   r   r_   rZ   rk   r   r   r	   r[   r   rI   r
   r   r2   r\   r]   s   @r1   r   r   Z   s     (+!489=L+M*L+  %L+ 	L+
 L+ bii01L+ Xc299n56L+ L+ 
L+\v & % %F %r3   r   rn   r;   weightsprogressrp   r%   c                     |#t        |dt        |j                  d                t        | fd|i|}|"|j	                  |j                  |d             |S )Nro   
categoriesr;   T)r   
check_hash)r   lenmetar   load_state_dictget_state_dict)rn   r;   r   r   rp   models         r1   	_convnextr      sd     fmSl9S5TU]Z:OZSYZEg44hSW4XYLr3   )    r   zNhttps://github.com/pytorch/vision/tree/main/references/classification#convnexta  
        These weights improve upon the results of the original paper by using a modified version of TorchVision's
        `new training recipe
        <https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/>`_.
    )min_sizer   recipe_docsc            
       T    e Zd Z ed eedd      i eddddd	id
dd      ZeZy)r   z>https://download.pytorch.org/models/convnext_tiny-983f1562.pth      	crop_sizeresize_sizeiH<ImageNet-1KgzGT@gMbX	X@zacc@1zacc@5gm@gV-G[@
num_params_metrics_ops
_file_sizeurl
transformsr   N	r4   r5   r6   r   r   r   _COMMON_METAIMAGENET1K_V1DEFAULTr7   r3   r1   r   r      sS    L.#3O

"##  !
M  Gr3   r   c            
       T    e Zd Z ed eedd      i eddddd	id
dd      ZeZy)r   z?https://download.pytorch.org/models/convnext_small-0c510722.pthr      r   iHZr   gClT@g)X@r   g|?5^!@g"~g@r   r   Nr   r7   r3   r1   r   r      sS    M.#3O

"##  !
M  Gr3   r   c            
       T    e Zd Z ed eedd      i eddddd	id
dd      ZeZy)r   z>https://download.pytorch.org/models/convnext_base-6075fbad.pthr      r   ihGr   gU@gHz7X@r   g(\µ.@g/$!u@r   r   Nr   r7   r3   r1   r   r      sS    L.#3O

"##  !
M  Gr3   r   c            
       T    e Zd Z ed eedd      i eddddd	id
dd      ZeZy)r   z?https://download.pytorch.org/models/convnext_large-ea097f82.pthr   r   r   ir   g"~U@gX9v>X@r   g|?5.A@gK@r   r   Nr   r7   r3   r1   r   r     sS    M.#3O

###  !
M  Gr3   r   
pretrained)r   T)r   r   c                     t         j                  |       } t        ddd      t        ddd      t        ddd      t        ddd      g}|j                  dd	      }t	        ||| |fi |S )
a  ConvNeXt Tiny model architecture from the
    `A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`_ paper.

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

    .. autoclass:: torchvision.models.ConvNeXt_Tiny_Weights
        :members:
    `      r'        	   Nr;   g?)r   verifyr_   popr   r   r   rp   rn   r;   s        r1   r   r   !  sy    & $**73G 	b#q!c3"c3"c4#	M #JJ'>D]$97HWPVWWr3   c                     t         j                  |       } t        ddd      t        ddd      t        ddd      t        ddd      g}|j                  dd	      }t	        ||| |fi |S )
a  ConvNeXt Small model architecture from the
    `A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`_ paper.

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

    .. autoclass:: torchvision.models.ConvNeXt_Small_Weights
        :members:
    r   r   r'   r   r      Nr;   g?)r   r   r_   r   r   r   s        r1   r   r   @  sy    * %++G4G 	b#q!c3"c3#c4#	M #JJ'>D]$97HWPVWWr3   c                     t         j                  |       } t        ddd      t        ddd      t        ddd      t        ddd      g}|j                  dd	      }t	        ||| |fi |S )
a  ConvNeXt Base model architecture from the
    `A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`_ paper.

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

    .. autoclass:: torchvision.models.ConvNeXt_Base_Weights
        :members:
          r'   i   i   r   Nr;         ?)r   r   r_   r   r   r   s        r1   r    r    a  sy    & $**73G 	c3"c3"c4$dD!$	M #JJ'>D]$97HWPVWWr3   c                     t         j                  |       } t        ddd      t        ddd      t        ddd      t        ddd      g}|j                  dd	      }t	        ||| |fi |S )
a  ConvNeXt Large model architecture from the
    `A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`_ paper.

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

    .. autoclass:: torchvision.models.ConvNeXt_Large_Weights
        :members:
    r   r   r'   r   i   r   Nr;   r   )r   r   r_   r   r   r   s        r1   r!   r!     sy    * %++G4G 	c3"c3"c4$dD!$	M #JJ'>D]$97HWPVWWr3   )4	functoolsr   typingr   r   r   r   r   rQ   r	   r
   torch.nnr   r)   ops.miscr   r   ops.stochastic_depthr   transforms._presetsr   utilsr   _apir   r   r   _metar   _utilsr   r   __all__rJ   r#   r[   r9   r_   r   rZ   boolr   r   r   r   r   r   r   r   r   r    r!   r7   r3   r1   <module>r      s    : :   $ 4 2 5 ' 6 6 ' B
",, bii >) )*V%ryy V%r&  k" 	
  & &^		K ([ (K ([ ( ,0E0S0S!TU@DW[ Xh'<= XPT Xgj Xow X V X: ,0F0T0T!UV37$X/0XCGXZ]XX W X> ,0E0S0S!TU@DW[ Xh'<= XPT Xgj Xow X V X: ,0F0T0T!UV37$X/0XCGXZ]XX W Xr3   