
    6bi
d                        S r SSKrSSKJs  Jr  SSKJr  SSKJ	r	  SSKJ
r
  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SrSSSSSS.r/ SQ/ SQSS./ SQ/ SQSS./ SQ/ SQSS./ SQ/ SQSS./ SQ/ SQSS.S.rSr " S S\
R0                  5      r " S S\
R0                  5      r S=S  jrS>S! jrS?S" jr            S@S# jr\" S$S%5               SAS& j5       r\" S'S(5               SBS) j5       r \" S*S+5               SCS, j5       r!\" S-S.5               SDS/ j5       r"\" S0S15               SES2 j5       r#\RI                  S3S49\l         \RI                  S5S49\ l         \RI                  S6S49\!l         \RI                  S7S49\"l         \RI                  S8S49\#l         \" S95      S>S: j5       r%\" S;5      SFS< j5       r&\RL                  R                   \&l         g)GzxConvNeXt models for TF-Keras.

References:

- [A ConvNet for the 2020s](https://arxiv.org/abs/2201.03545)
  (CVPR 2022)
    N)backend)initializers)layers)utils)imagenet_utils)
sequential)training)keras_exportzFhttps://storage.googleapis.com/tensorflow/keras-applications/convnext/)@8ae6e78ce2933352b1ef4008e6dd2f17bc40771563877d156bc6426c7cf503ff@d547c096cabd03329d7be5562c5e14798aa39ed24b474157cef5e85ab9e49ef1)@ce1277d8f1ee5a0ef0e171469089c18f5233860ceaf9b168049cb9263fd7483c@6fc8009faa2f00c1c1dfce59feea9b0745eb260a7dd11bee65c8e20843da6eab)@52cbb006d3dadd03f6e095a8ca1aca47aecdd75acb4bc74bce1f5c695d0086e6@40a20c5548a5e9202f69735ecc06c990e6b7c9d2de39f0361e27baeb24cb7c45)@070c5ed9ed289581e477741d3b34beffa920db8cf590899d6d2c67fba2a198a6@96f02b6f0753d4f543261bc9d09bed650f24dd6bc02ddde3066135b63d23a1cd)@c1f5ccab661354fc3a79a10fa99af82f0fbf10ec65cb894a3ae0815f17a889ee@de3f8a54174130e0cecdc71583354753d557fcf1f4487331558e2a16ba0cfe05)convnext_tinyconvnext_smallconvnext_baseconvnext_largeconvnext_xlarge)   r   	   r   )`              )depthsprojection_dimsdefault_size)r   r      r   )            )r   r   r   i   )r&   r'   r(   i   )tinysmallbaselargexlargea  Instantiates the {name} architecture.

  References:
    - [A ConvNet for the 2020s](https://arxiv.org/abs/2201.03545)
    (CVPR 2022)

  For image classification use cases, see
  [this page for detailed examples](
  https://keras.io/api/applications/#usage-examples-for-image-classification-models).
  For transfer learning use cases, make sure to read the
  [guide to transfer learning & fine-tuning](
    https://keras.io/guides/transfer_learning/).

  The `base`, `large`, and `xlarge` models were first pre-trained on the
  ImageNet-21k dataset and then fine-tuned on the ImageNet-1k dataset. The
  pre-trained parameters of the models were assembled from the
  [official repository](https://github.com/facebookresearch/ConvNeXt). To get a
  sense of how these parameters were converted to TF-Keras compatible
  parameters, please refer to
  [this repository](https://github.com/sayakpaul/keras-convnext-conversion).

  Note: Each TF-Keras Application expects a specific kind of input
  preprocessing. For ConvNeXt, preprocessing is included in the model using a
  `Normalization` layer. ConvNeXt models expect their inputs to be float or
  uint8 tensors of pixels with values in the [0-255] range.

  When calling the `summary()` method after instantiating a ConvNeXt model,
  prefer setting the `expand_nested` argument `summary()` to `True` to better
  investigate the instantiated model.

  Args:
    include_top: Whether to include the fully-connected
      layer at the top of the network. Defaults to `True`.
    weights: One of `None` (random initialization),
      `"imagenet"` (pre-training on ImageNet-1k), or the path to the weights
      file to be loaded. Defaults to `"imagenet"`.
    input_tensor: Optional TF-Keras tensor
      (i.e. output of `layers.Input()`)
      to use as image input for the model.
    input_shape: Optional shape tuple, only to be specified
      if `include_top` is False.
      It should have exactly 3 inputs channels.
    pooling: Optional pooling mode for feature extraction
      when `include_top` is `False`.
      - `None` means that the output of the model will be
        the 4D tensor output of the last convolutional layer.
      - `avg` means that global average pooling
        will be applied to the output of the
        last convolutional layer, and thus
        the output of the model will be a 2D tensor.
      - `max` means that global max pooling will
        be applied.
      Defaults to `None`.
    classes: Optional number of classes to classify images
      into, only to be specified if `include_top` is True, and
      if no `weights` argument is specified. 1000 is how many
      ImageNet classes there are. Defaults to `1000`.
    classifier_activation: A `str` or callable. The activation function to use
      on the "top" layer. Ignored unless `include_top=True`. Set
      `classifier_activation=None` to return the logits of the "top" layer.
      When loading pretrained weights, `classifier_activation` can only
      be `None` or `"softmax"`. Defaults to `"softmax"`.

  Returns:
    A `keras.Model` instance.
c                   B   ^  \ rS rSrSrU 4S jrSS jrU 4S jrSrU =r	$ )StochasticDepth   a  Stochastic Depth module.

It performs batch-wise dropping rather than sample-wise. In libraries like
`timm`, it's similar to `DropPath` layers that drops residual paths
sample-wise.

References:
  - https://github.com/rwightman/pytorch-image-models

Args:
  drop_path_rate (float): Probability of dropping paths. Should be within
    [0, 1].

Returns:
  Tensor either with the residual path dropped or kept.
c                 2   > [         TU ]  " S0 UD6  Xl        g N )super__init__drop_path_rate)selfr6   kwargs	__class__s      \/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/applications/convnext.pyr5   StochasticDepth.__init__   s    "6",    c                 4   U(       a  SU R                   -
  n[        R                  " U5      S   4S[        [        R                  " U5      5      S-
  -  -   nU[        R                  R                  USS5      -   n[        R                  " U5      nX-  U-  $ U$ )N   r   )r>   )r6   tfshapelenrandomuniformfloor)r7   xr	   	keep_probr@   random_tensors         r:   callStochasticDepth.call   s    D///IXXa[^%BHHQK0@10D(EEE%		(9(9%A(FFMHH]3MM]22r<   c                 ^   > [         TU ]  5       nUR                  SU R                  05        U$ )Nr6   )r4   
get_configupdater6   r7   configr9   s     r:   rK   StochasticDepth.get_config   s-    #%')<)<=>r<   )r6   N)
__name__
__module____qualname____firstlineno____doc__r5   rH   rK   __static_attributes____classcell__r9   s   @r:   r/   r/      s    "- r<   r/   c                   D   ^  \ rS rSrSrU 4S jrS rS rU 4S jrSr	U =r
$ )
LayerScale   zLayer scale module.

References:
  - https://arxiv.org/abs/2103.17239

Args:
  init_values (float): Initial value for layer scale. Should be within
    [0, 1].
  projection_dim (int): Projection dimensionality.

Returns:
  Tensor multiplied to the scale.
c                 >   > [         TU ]  " S0 UD6  Xl        X l        g r2   )r4   r5   init_valuesprojection_dim)r7   r]   r^   r8   r9   s       r:   r5   LayerScale.__init__   s    "6"&,r<   c                     U R                  SU R                  4[        R                  " U R                  5      SS9U l        g )NgammaT)namer@   initializer	trainable)
add_weightr^   r   Constantr]   ra   )r7   input_shapes     r:   buildLayerScale.build   s=    __&&($--d.>.>?	 % 

r<   c                     XR                   -  $ rP   )ra   )r7   rE   s     r:   rH   LayerScale.call   s    ::~r<   c                 t   > [         TU ]  5       nUR                  U R                  U R                  S.5        U$ )N)r]   r^   )r4   rK   rL   r]   r^   rM   s     r:   rK   LayerScale.get_config   s:    #%#//"&"5"5	
 r<   )ra   r]   r^   )rQ   rR   rS   rT   rU   r5   rh   rH   rK   rV   rW   rX   s   @r:   rZ   rZ      s!    -

 r<   rZ   c                 j   ^ ^^^ Tc"  S[        [        R                  " S5      5      -   mUUUU 4S jnU$ )a  ConvNeXt block.

References:
- https://arxiv.org/abs/2201.03545
- https://github.com/facebookresearch/ConvNeXt/blob/main/models/convnext.py

Notes:
  In the original ConvNeXt implementation (linked above), the authors use
  `Dense` layers for pointwise convolutions for increased efficiency.
  Following that, this implementation also uses the same.

Args:
  projection_dim (int): Number of filters for convolution layers. In the
    ConvNeXt paper, this is referred to as projection dimension.
  drop_path_rate (float): Probability of dropping paths. Should be within
    [0, 1].
  layer_scale_init_value (float): Layer scale value. Should be a small float
    number.
  name: name to path to the keras layer.

Returns:
  A function representing a ConvNeXtBlock block.
prestemc                   > U n[         R                  " TSSTTS-   S9" U5      n[         R                  " STS-   S9" U5      n[         R                  " ST-  TS	-   S
9" U5      n[         R                  " STS-   S
9" U5      n[         R                  " TTS-   S
9" U5      nTb  [        TTTS-   S
9" U5      nT(       a  [        TTS-   S
9nO[         R                  " STS-   S
9nX" U5      -   $ )N   same_depthwise_conv)filterskernel_sizepaddinggroupsrb   ư>
_layernormepsilonrb      _pointwise_conv_1rb   gelu_gelu_pointwise_conv_2_layer_scale_stochastic_depthlinear	_identity)r   Conv2DLayerNormalizationDense
ActivationrZ   r/   )inputsrE   layerr6   layer_scale_init_valuerb   r^   s      r:   applyConvNeXtBlock.<locals>.apply  s   MM"!))
  %%d9LMaPLL^+$9L2LMaPf4'>:1=LLd5H.HI!L!-&N* 	A
 #T,?%?E %%hTK5GHEa  r<   strr   get_uid)r^   r6   r   rb   r   s   ```` r:   ConvNeXtBlockr      s3    4 |3wy9::! !< Lr<   c                 ^   ^  T c"  S[        [        R                  " S5      5      -   m U 4S jnU$ )zsNormalizes inputs with ImageNet-1k mean and std.

Args:
  name (str): Name prefix.

Returns:
  A presemt function.
ro   c                 N   > [         R                  " / SQ/ SQTS-   S9" U 5      n U $ )N)g33333^@gR]@gRY@)g)~@gec}@g    ȷ@_prestem_normalization)meanvariancerb   )r   Normalization)rE   rb   s    r:   r   PreStem.<locals>.apply<  s7      8
 00
  r<   r   )rb   r   s   ` r:   PreStemr   0  s.     |3wy9::
 Lr<   c                 `   ^ ^^ Tc  [        [        R                  " S5      5      mUUU 4S jnU$ )zImplementation of classification head of ConvNeXt.

Args:
  num_classes: number of classes for Dense layer
  classifier_activation: activation function for the Dense layer
  name: name prefix

Returns:
  Classification head function.
headc                    > [         R                  " TS-   S9" U 5      n [         R                  " STS-   S9" U 5      n [         R                  " TTTS-   S9" U 5      n U $ )N	_head_gapr~   rx   _head_layernormrz   _head_dense)
activationrb   )r   GlobalAveragePooling2Dr   r   )rE   classifier_activationrb   num_classess    r:   r   Head.<locals>.applyY  so    ))tk/AB1E%%t&77

 LL,%
 	
 r<   r   )r   r   rb   r   s   ``` r:   Headr   K  s)     |7??6*+
 Lr<   c                    US;   d9  [         R                  R                  R                  U5      (       d  [	        S5      eUS:X  a  U(       a  US:w  a  [	        S5      e[
        R                  " U
US[        R                  " 5       UUS9n
U	c  [        R                  " U
S9nO2[        R                  " U	5      (       d  [        R                  " XS	9nOU	nU	b#  [        R                  R                  U	5      S
   nOUnUnU(       a:  [        R                  " 5       S:X  a  SOSnU
US-
     nUS:X  a  [        US9" U5      n[         R"                  " [        R$                  " US
   SSUS-   S9[        R&                  " SUS-   S9/US-   S9n/ nUR)                  U5        Sn[+        U5       H  n[         R"                  " [        R&                  " SUS-   [-        U5      -   S9[        R$                  " UUS-      SSUS-   [-        U5      -   S9/US-   [-        U5      -   S9nUR)                  U5        M     [.        R0                  " SU[3        U 5      5       Vs/ s H  n[5        U5      PM     nnS
nSn[+        U5       HO  nUU   " W5      n[+        U U   5       H'  n[7        UU   UUU-      UUSU SU 3-   S9" U5      nM)     UU U   -  nMQ     U(       a(  [
        R8                  " X5        [;        UUUS9" W5      nO]US:X  a  [        R<                  " 5       " W5      nO!US :X  a  [        R>                  " 5       " W5      n[        R&                  " SS!9" W5      n[@        RB                  " UUUS"9nUS:X  ac  U(       a  S#n[D        U   S
   nOS$n[D        U   S   nUU-   n[        RF                  RI                  U[J        U-   S%US&9n URM                  U 5        U$ Ub  URM                  U5        U$ s  snf )'a	  Instantiates ConvNeXt architecture given specific configuration.

Args:
  depths: An iterable containing depths for each individual stages.
  projection_dims: An iterable containing output number of channels of
  each individual stages.
  drop_path_rate: Stochastic depth probability. If 0.0, then stochastic
    depth won't be used.
  layer_scale_init_value: Layer scale coefficient. If 0.0, layer scaling
    won't be used.
  default_size: Default input image size.
  model_name: An optional name for the model.
  include_preprocessing: boolean denoting whther to include preprocessing in
    the model. When `weights="imagenet"` this should be always set to True.
    But for other models (e.g., randomly initialized) users should set it
    to False and apply preprocessing to data accordingly.
  include_top: Boolean denoting whether to include classification head to
    the model.
  weights: one of `None` (random initialization), `"imagenet"` (pre-training
    on ImageNet-1k), or the path to the weights file to be loaded.
  input_tensor: optional TF-Keras tensor (i.e. output of `layers.Input()`)
    to use as image input for the model.
  input_shape: optional shape tuple, only to be specified if `include_top`
    is False. It should have exactly 3 inputs channels.
  pooling: optional pooling mode for feature extraction when `include_top`
    is `False`.
    - `None` means that the output of the model will be the 4D tensor output
      of the last convolutional layer.
    - `avg` means that global average pooling will be applied to the output
      of the last convolutional layer, and thus the output of the model will
      be a 2D tensor.
    - `max` means that global max pooling will be applied.
  classes: optional number of classes to classify images into, only to be
    specified if `include_top` is True, and if no `weights` argument is
    specified.
  classifier_activation: A `str` or callable. The activation function to use
    on the "top" layer. Ignored unless `include_top=True`. Set
    `classifier_activation=None` to return the logits of the "top" layer.

Returns:
  A `keras.Model` instance.

Raises:
    ValueError: in case of invalid argument for `weights`,
      or invalid input shape.
    ValueError: if `classifier_activation` is not `softmax`, or `None`
      when using a pretrained top layer.
    ValueError: if `include_top` is True but `num_classes` is not 1000
      when using ImageNet.
>   NimagenetzThe `weights` argument should be either `None` (random initialization), `imagenet` (pre-training on ImageNet), or the path to the weights file to be loaded.r     zWIf using `weights` as `'imagenet'` with `include_top` as true, `classes` should be 1000    )r#   min_sizedata_formatrequire_flattenweights)r@   )tensorr@   r   channels_lastr   r>   r~   r|   
_stem_conv)ru   stridesrb   rx   _stem_layernormrz   _stem_downsampling_layernorm_   _downsampling_conv__downsampling_block_        _stage__block_)r^   r6   r   rb   )r   r   rb   avgmax)r{   )r   outputsrb   z.h5z	_notop.h5models)cache_subdir	file_hash)'r?   iogfileexists
ValueErrorr   obtain_input_shaper   image_data_formatr   Inputis_keras_tensorr   layer_utilsget_source_inputsr   r   
Sequentialr   r   appendranger   nplinspacesumfloatr   validate_activationr   r   GlobalMaxPooling2Dtraining_libModelWEIGHTS_HASHES
data_utilsget_fileBASE_WEIGHTS_PATHload_weights)!r!   r"   r6   r   r#   
model_nameinclude_preprocessinginclude_topr   input_tensorrg   poolingclassesr   	img_inputr   rE   channel_axisnum_channelsstemdownsample_layersnum_downsample_layersidownsample_layerdepth_drop_ratescurnum_convnext_blocksjmodelfile_suffixr   	file_nameweights_paths!                                    r:   ConvNeXtr   h  sE   D ))RUU[[-?-?-H-H<
 	
 *D1
 	
 !33!--/#K LL{3	&&|44LLI$I""44\B1EA**,?AQ 	 #<!#341Z(+A   MM",.	 %%:0A#A
	
 '!D  T"()%00)) #&@@3q6I #AE* !#&;;c!fD	 44s1v=
 	  !12! *, ++c>3v;GGQaG  
 C&'a #vay!A.q1/a8'=GA3gaS"99	
 A " 	vay ( **+@J"7
 	 e--/2A))+A.A%%d3A6fajIE *K&z215I%K&z215I,	''00	)!	 1 
 	<( L 
	7#Los   O1z(keras.applications.convnext.ConvNeXtTinyzkeras.applications.ConvNeXtTinyc	                 j    [        [        S   S   [        S   S   SS[        S   S   U UUUUUUUUS9$ )Nr)   r!   r"   r   rx   r#   r!   r"   r6   r   r#   r   r   r   r   r   rg   r   r   r   r   MODEL_CONFIGS	r   r   r   r   r   rg   r   r   r   s	            r:   ConvNeXtTinyr   ?  Z     V$X.%f-.?@#"6*>:3!3 r<   z)keras.applications.convnext.ConvNeXtSmallz keras.applications.ConvNeXtSmallc	                 j    [        [        S   S   [        S   S   SS[        S   S   U UUUUUUUUS9$ )Nr*   r!   r"   r   rx   r#   r   r   r   s	            r:   ConvNeXtSmallr   `  Z     W%h/%g./@A#"7+N;3!3 r<   z(keras.applications.convnext.ConvNeXtBasezkeras.applications.ConvNeXtBasec	                 j    [        [        S   S   [        S   S   SS[        S   S   U UUUUUUUUS9$ )Nr+   r!   r"   r   rx   r#   r   r   r   s	            r:   ConvNeXtBaser     r   r<   z)keras.applications.convnext.ConvNeXtLargez keras.applications.ConvNeXtLargec	                 j    [        [        S   S   [        S   S   SS[        S   S   U UUUUUUUUS9$ )Nr,   r!   r"   r   rx   r#   r   r   r   s	            r:   ConvNeXtLarger     r   r<   z*keras.applications.convnext.ConvNeXtXLargez!keras.applications.ConvNeXtXLargec	                 j    [        [        S   S   [        S   S   SS[        S   S   U UUUUUUUUS9$ )Nr-   r!   r"   r   rx   r#   r   r   r   s	            r:   ConvNeXtXLarger    sZ     X&x0%h/0AB#"8,^<3!3 r<   r   r~   r   r   r   r  z,keras.applications.convnext.preprocess_inputc                     U $ )a  A placeholder method for backward compatibility.

The preprocessing logic has been included in the convnext model
implementation. Users are no longer required to call this method to
normalize the input data. This method does nothing and only kept as a
placeholder to align the API surface between old and new version of model.

Args:
  x: A floating point `numpy.array` or a `tf.Tensor`.
  data_format: Optional data format of the image tensor/array. `None` means
    the global setting `tf.keras.backend.image_data_format()` is used
    (unless you changed it, it uses "channels_last").
    Defaults to `None`.

Returns:
  Unchanged `numpy.array` or `tf.Tensor`.
r3   )rE   r   s     r:   preprocess_inputr    s	    & Hr<   z.keras.applications.convnext.decode_predictionsc                 *    [         R                  " XS9$ )N)top)r   decode_predictions)predsr  s     r:   r  r    s    ,,U<<r<   )r   rx   NrP   )r   NN)r   rx   r    convnextTTNNNNr   softmax)	r   TTr   NNNr   r
  )	r   TTr   NNNr   r
  )	r   TTr   NNNr   r
  )	r   TTr   NNNr   r
  )	r   TTr   NNNr   r
  )   )'rU   numpyr   tensorflow.compat.v2compatv2r?   tf_keras.srcr   r   r   r   tf_keras.src.applicationsr   tf_keras.src.enginer   r	   r    tensorflow.python.util.tf_exportr
   r   r   r   BASE_DOCSTRINGLayerr/   rZ   r   r   r   r   r   r   r   r   r  formatr  r  r3   r<   r:   <module>r     s
  "  ! !   %   4 * 8 : M 
#4 .  .  0  0  1+8AH"fll "J' 'V KO;|6@ #Qn .%
 #	: /&
  #	: .%
 #	: /&
  #	: 0'
 !#	: &,,.,A &--?-C %,,.,A &--?-C '..4D.E  <= >* >?= @= ,>>FF  r<   