
    h;                        d dl mZ d dlmZ d dl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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              Z3 e        ed,e/j`                  f-      dd.d/dee/   de)dede&fd3              Z4y)4    )Sequence)partial)AnyCallableOptionalN)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     Y/var/www/html/eduruby.in/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	   r1    r2   r0   r"   r"      s     F r2   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onesr9   r   stochastic_depth)r/   dimr9   r:   r;   	__class__s        r0   rH   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r2   inputc                 n    | j                   | j                  |      z  }| j                  |      }||z  }|S N)r9   rN   rR   )r/   rU   results      r0   r1   zCNBlock.forward?   s9    !!DJJu$55&&v.%r2   rW   )r3   r4   r5   floatr   r   r   ModulerH   r	   r1   __classcell__rT   s   @r0   r8   r8   '   s_     :>N N  %	N
 Xc299n56N 
N.V  r2   r8   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 rW   )r_   r`   ra   )r/   r_   r`   ra   s       r0   rH   zCNBlockConfig.__init__H   s     -($r2   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})r6   )rT   r3   format__dict__)r/   ss     r0   __repr__zCNBlockConfig.__repr__R   sX    NN##c)	..	,,	((	Sqxx($--((r2   )r3   r4   r5   intr   rH   strri   r6   r2   r0   r^   r^   F   s=    %% sm% 	%
 
%)# )r2   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:   r9   num_classesrN   .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&   rC   T)r@   striderA   r;   activation_layerr,   c              3   4   K   | ]  }|j                     y wrW   )ra   ).0cnfs     r0   	<genexpr>z$ConvNeXt.__init__.<locals>.<genexpr>   s      IC Is   g      ?r   r   )r@   rq   g{Gz?)std)"rG   rH   r   
ValueError
isinstancer   allr^   	TypeErrorr8   r   r"   r_   appendr   sumrangera   r   rJ   r`   rK   featuresAdaptiveAvgPool2davgpoolFlattenrL   
classifiermodulesinittrunc_normal_r+   r,   zeros_)r/   rm   r:   r9   rn   rN   r;   ro   rh   layersfirstconv_output_channelstotal_stage_blocksstage_block_idru   stage_sd_prob	lastblocklastconv_output_channelsmrT   s                       r0   rH   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 rW   )r   r   r   r.   s     r0   _forward_implzConvNeXt._forward_impl   s0    MM!LLOOOAr2   c                 $    | j                  |      S rW   )r   r.   s     r0   r1   zConvNeXt.forward   s    !!!$$r2   )g        r=   i  NN)r3   r4   r5   listr^   rY   rj   r   r   r   rZ   r   rH   r	   r   r1   r[   r\   s   @r0   r   r   [   s     (+!489=L+M*L+  %L+ 	L+
 L+ bii01L+ Xc299n56L+ L+ 
L+\v & % %F %r2   r   rm   r:   weightsprogressro   r$   c                     |#t        |dt        |j                  d                t        | fd|i|}|"|j	                  |j                  |d             |S )Nrn   
categoriesr:   T)r   
check_hash)r   lenmetar   load_state_dictget_state_dict)rm   r:   r   r   ro   models         r0   	_convnextr      sd     fmSl9S5TU]Z:OZSYZEg44hSW4XYLr2   )    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	r3   r4   r5   r   r   r   _COMMON_METAIMAGENET1K_V1DEFAULTr6   r2   r0   r   r      sS    L.#3O

"##  !
M  Gr2   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   r6   r2   r0   r   r      sS    M.#3O

"##  !
M  Gr2   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   r6   r2   r0   r   r      sS    L.#3O

"##  !
M  Gr2   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   r6   r2   r0   r   r     sS    M.#3O

###  !
M  Gr2   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   ro   rm   r:   s        r0   r   r   "  sy    & $**73G 	b#q!c3"c3"c4#	M #JJ'>D]$97HWPVWWr2   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        r0   r   r   A  sy    * %++G4G 	b#q!c3"c3#c4#	M #JJ'>D]$97HWPVWWr2   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        r0   r   r   b  sy    & $**73G 	c3"c3"c4$dD!$	M #JJ'>D]$97HWPVWWr2   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        r0   r    r      sy    * %++G4G 	c3"c3"c4$dD!$	M #JJ'>D]$97HWPVWWr2   )5collections.abcr   	functoolsr   typingr   r   r   rP   r   r	   torch.nnr
   r(   ops.miscr   r   ops.stochastic_depthr   transforms._presetsr   utilsr   _apir   r   r   _metar   _utilsr   r   __all__rI   r"   rZ   r8   r^   r   r   rY   boolr   r   r   r   r   r   r   r   r   r   r    r6   r2   r0   <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r2   