
'''simple docstring'''





import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

import evaluate
import numpy as np
import torch
from datasets import load_dataset
from PIL import Image
from torchvision.transforms import (
    CenterCrop,
    Compose,
    Normalize,
    RandomHorizontalFlip,
    RandomResizedCrop,
    Resize,
    ToTensor,
)

import transformers
from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    AutoModelForImageClassification,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    set_seed,
)
from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, send_example_telemetry
from transformers.utils.versions import require_version
SCREAMING_SNAKE_CASE     =       logging.getLogger(__name__)

# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
check_min_version("""4.31.0""")

require_version("""datasets>=1.8.0""", """To fix: pip install -r examples/pytorch/image-classification/requirements.txt""")

SCREAMING_SNAKE_CASE     =       list(MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING.keys())
SCREAMING_SNAKE_CASE     =       tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)







def snake_case_     (					lowercase__		):
  with open(lowercase__   ,       "rb"		) as f:
    UpperCAmelCase__      :					Optional[Any]				       =							Image.open(lowercase__		)
    return im.convert("RGB"		)






@dataclass
class 							UpperCAmelCase_							:

 '''simple docstring'''
 lowercase_					:    Optional[str]				=							field(
     default=A					,				metadata={
         "help": "Name of a dataset from the hub (could be your own, possibly private dataset hosted on the hub)."
     }					,				)
 lowercase_					:    Optional[str]				=							field(
     default=A					,				metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}				)
 lowercase_					:    Optional[str]				=							field(default=A					,				metadata={"help": "A folder containing the training data."}				)
 lowercase_					:    Optional[str]				=							field(default=A					,				metadata={"help": "A folder containing the validation data."}				)
 lowercase_					:    Optional[float]				=							field(
     default=0.15					,				metadata={"help": "Percent to split off of train for validation."}				)
 lowercase_					:    Optional[int]				=							field(
     default=A					,				metadata={
         "help": (
             "For debugging purposes or quicker training, truncate the number of training examples to this "
             "value if set."
         )
     }					,				)
 lowercase_					:    Optional[int]				=							field(
     default=A					,				metadata={
         "help": (
             "For debugging purposes or quicker training, truncate the number of evaluation examples to this "
             "value if set."
         )
     }					,				)







 def 							UpperCamelCase  (					self		:  Dict ):



   '''simple docstring'''




   if self.dataset_name is None and (self.train_dir is None and self.validation_dir is None):
     raise ValueError(
         "You must specify either a dataset name from the hub or a train and/or validation directory." )






@dataclass
class 							UpperCAmelCase_							:

 '''simple docstring'''
 lowercase_					:    str				=							field(
     default="google/vit-base-patch16-224-in21k"					,				metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}					,				)
 lowercase_					:    Optional[str]				=							field(
     default=A					,				metadata={"help": "If training from scratch, pass a model type from the list: " + ", ".join(A				)}					,				)
 lowercase_					:    Optional[str]				=							field(
     default=A					,				metadata={"help": "Pretrained config name or path if not the same as model_name"}				)
 lowercase_					:    Optional[str]				=							field(
     default=A					,				metadata={"help": "Where do you want to store the pretrained models downloaded from s3"}				)
 lowercase_					:    str				=							field(
     default="main"					,				metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."}					,				)
 lowercase_					:    str				=							field(default=A					,				metadata={"help": "Name or path of preprocessor config."}				)
 lowercase_					:    bool				=							field(
     default=A					,				metadata={
         "help": (
             "Will use the token generated when running `huggingface-cli login` (necessary to use this script "
             "with private models)."
         )
     }					,				)
 lowercase_					:    bool				=							field(
     default=A					,				metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."}					,				)







def snake_case_     (					lowercase__		):
  UpperCAmelCase__      :					Any				       =							torch.stack([example["pixel_values"] for example in examples]		)
  UpperCAmelCase__      :					int				       =							torch.tensor([example["labels"] for example in examples]		)
  return {"pixel_values": pixel_values, "labels": labels}







def snake_case_     (					):
  # See all possible arguments in src/transformers/training_args.py
  # or by passing the --help flag to this script.
  # We now keep distinct sets of args, for a cleaner separation of concerns.

  UpperCAmelCase__      :					Optional[int]				       =							HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)		)
  if len(sys.argv		) == 2 and sys.argv[1].endswith(".json"		):
    # If we pass only one argument to the script and it's the path to a json file,
    # let's parse it to get our arguments.
    UpperCAmelCase__       ,			UpperCAmelCase__       ,			UpperCAmelCase__      :					Optional[int]				       =							parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]		)		)
  else:
    UpperCAmelCase__       ,			UpperCAmelCase__       ,			UpperCAmelCase__      :					Union[str, Any]				       =							parser.parse_args_into_dataclasses()

  # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The
  # information sent is the one passed as arguments along with your Python/PyTorch versions.
  send_example_telemetry("run_image_classification"   ,       lowercase__   ,       lowercase__		)

  # Setup logging
  logging.basicConfig(
      format="%(asctime)s - %(levelname)s - %(name)s - %(message)s"   ,       datefmt="%m/%d/%Y %H:%M:%S"   ,       handlers=[logging.StreamHandler(sys.stdout		)]   ,       )

  if training_args.should_log:
    # The default of training_args.log_level is passive, so we set log level at info here to have that default.
    transformers.utils.logging.set_verbosity_info()

  UpperCAmelCase__      :					int				       =							training_args.get_process_log_level()
  logger.setLevel(lowercase__		)
  transformers.utils.logging.set_verbosity(lowercase__		)
  transformers.utils.logging.enable_default_handler()
  transformers.utils.logging.enable_explicit_format()

  # Log on each process the small summary:
  logger.warning(
      F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}"""
      + F"""distributed training: {bool(training_args.local_rank != -1		)}, 16-bits training: {training_args.fpaa}"""		)
  logger.info(F"""Training/evaluation parameters {training_args}"""		)

  # Detecting last checkpoint.
  UpperCAmelCase__      :					List[str]				       =							None
  if os.path.isdir(training_args.output_dir		) and training_args.do_train and not training_args.overwrite_output_dir:
    UpperCAmelCase__      :					List[Any]				       =							get_last_checkpoint(training_args.output_dir		)
    if last_checkpoint is None and len(os.listdir(training_args.output_dir		)		) > 0:
      raise ValueError(
          F"""Output directory ({training_args.output_dir}) already exists and is not empty. """
          "Use --overwrite_output_dir to overcome."		)
    elif last_checkpoint is not None and training_args.resume_from_checkpoint is None:
      logger.info(
          F"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """
          "the `--output_dir` or add `--overwrite_output_dir` to train from scratch."		)

    # Set seed before initializing model.
  set_seed(training_args.seed		)

  # Initialize our dataset and prepare it for the 'image-classification' task.
  if data_args.dataset_name is not None:
    UpperCAmelCase__      :					Tuple				       =							load_dataset(
        data_args.dataset_name   ,       data_args.dataset_config_name   ,       cache_dir=model_args.cache_dir   ,       task="image-classification"   ,       use_auth_token=True if model_args.use_auth_token else None   ,       )
  else:
    UpperCAmelCase__      :					str				       =							{}
    if data_args.train_dir is not None:
      UpperCAmelCase__      :					List[Any]				       =							os.path.join(data_args.train_dir   ,       "**"		)
    if data_args.validation_dir is not None:
      UpperCAmelCase__      :					Optional[Any]				       =							os.path.join(data_args.validation_dir   ,       "**"		)
    UpperCAmelCase__      :					Union[str, Any]				       =							load_dataset(
        "imagefolder"   ,       data_files=lowercase__   ,       cache_dir=model_args.cache_dir   ,       task="image-classification"   ,       )

  # If we don't have a validation split, split off a percentage of train as validation.
  UpperCAmelCase__      :					int				       =							None if "validation" in dataset.keys() else data_args.train_val_split
  if isinstance(data_args.train_val_split   ,       lowercase__		) and data_args.train_val_split > 0.0:
    UpperCAmelCase__      :					List[Any]				       =							dataset["train"].train_test_split(data_args.train_val_split		)
    UpperCAmelCase__      :					Dict				       =							split["train"]
    UpperCAmelCase__      :					List[str]				       =							split["test"]

  # Prepare label mappings.
  # We'll include these in the model's config to get human readable labels in the Inference API.
  UpperCAmelCase__      :					List[str]				       =							dataset["train"].features["labels"].names
  UpperCAmelCase__       ,			UpperCAmelCase__      :					Optional[int]				       =							{}, {}
  for i, label in enumerate(lowercase__		):
    UpperCAmelCase__      :					int				       =							str(lowercase__		)
    UpperCAmelCase__      :					List[Any]				       =							label

  # Load the accuracy metric from the datasets package
  UpperCAmelCase__      :					List[str]				       =							evaluate.load("accuracy"		)

  # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a
  # predictions and label_ids field) and has to return a dictionary string to float.
  def compute_metrics(lowercase__		):
    return metric.compute(predictions=np.argmax(p.predictions   ,       axis=1		)   ,       references=p.label_ids		)

  UpperCAmelCase__      :					Union[str, Any]				       =							AutoConfig.from_pretrained(
      model_args.config_name or model_args.model_name_or_path   ,       num_labels=len(lowercase__		)   ,       labelaid=lowercase__   ,       idalabel=lowercase__   ,       finetuning_task="image-classification"   ,       cache_dir=model_args.cache_dir   ,       revision=model_args.model_revision   ,       use_auth_token=True if model_args.use_auth_token else None   ,       )
  UpperCAmelCase__      :					Optional[Any]				       =							AutoModelForImageClassification.from_pretrained(
      model_args.model_name_or_path   ,       from_tf=bool(".ckpt" in model_args.model_name_or_path		)   ,       config=lowercase__   ,       cache_dir=model_args.cache_dir   ,       revision=model_args.model_revision   ,       use_auth_token=True if model_args.use_auth_token else None   ,       ignore_mismatched_sizes=model_args.ignore_mismatched_sizes   ,       )
  UpperCAmelCase__      :					List[str]				       =							AutoImageProcessor.from_pretrained(
      model_args.image_processor_name or model_args.model_name_or_path   ,       cache_dir=model_args.cache_dir   ,       revision=model_args.model_revision   ,       use_auth_token=True if model_args.use_auth_token else None   ,       )

  # Define torchvision transforms to be applied to each image.
  if "shortest_edge" in image_processor.size:
    UpperCAmelCase__      :					List[str]				       =							image_processor.size["shortest_edge"]
  else:
    UpperCAmelCase__      :					Union[str, Any]				       =							(image_processor.size["height"], image_processor.size["width"])
  UpperCAmelCase__      :					Tuple				       =							Normalize(mean=image_processor.image_mean   ,       std=image_processor.image_std		)
  UpperCAmelCase__      :					List[str]				       =							Compose(
      [
          RandomResizedCrop(lowercase__		),
          RandomHorizontalFlip(),
          ToTensor(),
          normalize,
      ]		)
  UpperCAmelCase__      :					Any				       =							Compose(
      [
          Resize(lowercase__		),
          CenterCrop(lowercase__		),
          ToTensor(),
          normalize,
      ]		)

  def train_transforms(lowercase__		):
    UpperCAmelCase__      :					int				       =							[
        _train_transforms(pil_img.convert("RGB"		)		) for pil_img in example_batch["image"]
    ]
    return example_batch

  def val_transforms(lowercase__		):
    UpperCAmelCase__      :					Tuple				       =							[_val_transforms(pil_img.convert("RGB"		)		) for pil_img in example_batch["image"]]
    return example_batch

  if training_args.do_train:
    if "train" not in dataset:
      raise ValueError("--do_train requires a train dataset"		)
    if data_args.max_train_samples is not None:
      UpperCAmelCase__      :					Optional[int]				       =							(
          dataset["train"].shuffle(seed=training_args.seed		).select(range(data_args.max_train_samples		)		)
      )
    # Set the training transforms
    dataset["train"].set_transform(lowercase__		)

  if training_args.do_eval:
    if "validation" not in dataset:
      raise ValueError("--do_eval requires a validation dataset"		)
    if data_args.max_eval_samples is not None:
      UpperCAmelCase__      :					List[Any]				       =							(
          dataset["validation"].shuffle(seed=training_args.seed		).select(range(data_args.max_eval_samples		)		)
      )
    # Set the validation transforms
    dataset["validation"].set_transform(lowercase__		)

  # Initalize our trainer
  UpperCAmelCase__      :					int				       =							Trainer(
      model=lowercase__   ,       args=lowercase__   ,       train_dataset=dataset["train"] if training_args.do_train else None   ,       eval_dataset=dataset["validation"] if training_args.do_eval else None   ,       compute_metrics=lowercase__   ,       tokenizer=lowercase__   ,       data_collator=lowercase__   ,       )

  # Training
  if training_args.do_train:
    UpperCAmelCase__      :					List[Any]				       =							None
    if training_args.resume_from_checkpoint is not None:
      UpperCAmelCase__      :					List[Any]				       =							training_args.resume_from_checkpoint
    elif last_checkpoint is not None:
      UpperCAmelCase__      :					List[str]				       =							last_checkpoint
    UpperCAmelCase__      :					str				       =							trainer.train(resume_from_checkpoint=lowercase__		)
    trainer.save_model()
    trainer.log_metrics("train"   ,       train_result.metrics		)
    trainer.save_metrics("train"   ,       train_result.metrics		)
    trainer.save_state()

  # Evaluation
  if training_args.do_eval:
    UpperCAmelCase__      :					List[Any]				       =							trainer.evaluate()
    trainer.log_metrics("eval"   ,       lowercase__		)
    trainer.save_metrics("eval"   ,       lowercase__		)

  # Write model card and (optionally) push to hub
  UpperCAmelCase__      :					Dict				       =							{
      "finetuned_from": model_args.model_name_or_path,
      "tasks": "image-classification",
      "dataset": data_args.dataset_name,
      "tags": ["image-classification", "vision"],
  }
  if training_args.push_to_hub:
    trainer.push_to_hub(**lowercase__		)
  else:
    trainer.create_model_card(**lowercase__		)


if __name__ == "__main__":
      main()





'''simple docstring'''





from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFSequenceClassifierOutput,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import logging
from .configuration_regnet import RegNetConfig


SCREAMING_SNAKE_CASE     =       logging.get_logger(__name__)

# General docstring
SCREAMING_SNAKE_CASE     =       """RegNetConfig"""

# Base docstring
SCREAMING_SNAKE_CASE     =       """facebook/regnet-y-040"""
SCREAMING_SNAKE_CASE     =       [1, 1_0_8_8, 7, 7]

# Image classification docstring
SCREAMING_SNAKE_CASE     =       """facebook/regnet-y-040"""
SCREAMING_SNAKE_CASE     =       """tabby, tabby cat"""

SCREAMING_SNAKE_CASE     =       [
    """facebook/regnet-y-040""",
    # See all regnet models at https://huggingface.co/models?filter=regnet
]






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  str			,				snake_case__		:  int			,				snake_case__		:  int = 3			,				snake_case__		:  int = 1			,				snake_case__		:  int = 1			,				snake_case__		:  Optional[str] = "relu"			,				**snake_case__		:  Dict			,				):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   # The padding and conv has been verified in
   # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb
   UpperCAmelCase__      :					Any				       =							tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 )
   UpperCAmelCase__      :					Union[str, Any]				       =							tf.keras.layers.ConvaD(
       filters=snake_case__			,				kernel_size=snake_case__			,				strides=snake_case__			,				padding="VALID"			,				groups=snake_case__			,				use_bias=snake_case__			,				name="convolution"			,				)
   UpperCAmelCase__      :					Any				       =							tf.keras.layers.BatchNormalization(epsilon=1e-5			,				momentum=0.9			,				name="normalization" )
   UpperCAmelCase__      :					Tuple				       =							ACTaFN[activation] if activation is not None else tf.identity







 def 							UpperCamelCase  (					self		:  str			,				snake_case__		:  str ):



   '''simple docstring'''




   UpperCAmelCase__      :					str				       =							self.convolution(self.padding(snake_case__ ) )
   UpperCAmelCase__      :					Dict				       =							self.normalization(snake_case__ )
   UpperCAmelCase__      :					Any				       =							self.activation(snake_case__ )
   return hidden_state






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  List[Any]			,				snake_case__		:  RegNetConfig			,				**snake_case__		:  Optional[int] ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					Any				       =							config.num_channels
   UpperCAmelCase__      :					Optional[Any]				       =							TFRegNetConvLayer(
       out_channels=config.embedding_size			,				kernel_size=3			,				stride=2			,				activation=config.hidden_act			,				name="embedder"			,				)







 def 							UpperCamelCase  (					self		:  int			,				snake_case__		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Tuple				       =							shape_list(snake_case__ )[1]
   if tf.executing_eagerly() and num_channels != self.num_channels:
     raise ValueError(
         "Make sure that the channel dimension of the pixel values match with the one set in the configuration." )

   # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format.
   # So change the input format from `NCHW` to `NHWC`.
   # shape = (batch_size, in_height, in_width, in_channels=num_channels)
   UpperCAmelCase__      :					List[str]				       =							tf.transpose(snake_case__			,				perm=(0, 2, 3, 1) )
   UpperCAmelCase__      :					Dict				       =							self.embedder(snake_case__ )
   return hidden_state






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  Optional[int]			,				snake_case__		:  int			,				snake_case__		:  int = 2			,				**snake_case__		:  Optional[int] ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					int				       =							tf.keras.layers.ConvaD(
       filters=snake_case__			,				kernel_size=1			,				strides=snake_case__			,				use_bias=snake_case__			,				name="convolution" )
   UpperCAmelCase__      :					str				       =							tf.keras.layers.BatchNormalization(epsilon=1e-5			,				momentum=0.9			,				name="normalization" )







 def 							UpperCamelCase  (					self		:  str			,				snake_case__		:  tf.Tensor			,				snake_case__		:  bool = False ):



   '''simple docstring'''




   return self.normalization(self.convolution(snake_case__ )			,				training=snake_case__ )






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  List[str]			,				snake_case__		:  int			,				snake_case__		:  int			,				**snake_case__		:  Union[str, Any] ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					Any				       =							tf.keras.layers.GlobalAveragePoolingaD(keepdims=snake_case__			,				name="pooler" )
   UpperCAmelCase__      :					Optional[Any]				       =							[
       tf.keras.layers.ConvaD(filters=snake_case__			,				kernel_size=1			,				activation="relu"			,				name="attention.0" ),
       tf.keras.layers.ConvaD(filters=snake_case__			,				kernel_size=1			,				activation="sigmoid"			,				name="attention.2" ),
   ]







 def 							UpperCamelCase  (					self		:  Optional[int]			,				snake_case__		:  Any ):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							self.pooler(snake_case__ )
   for layer_module in self.attention:
     UpperCAmelCase__      :					Dict				       =							layer_module(snake_case__ )
   UpperCAmelCase__      :					int				       =							hidden_state * pooled
   return hidden_state






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  List[str]			,				snake_case__		:  RegNetConfig			,				snake_case__		:  int			,				snake_case__		:  int			,				snake_case__		:  int = 1			,				**snake_case__		:  str ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					Optional[Any]				       =							in_channels != out_channels or stride != 1
   UpperCAmelCase__      :					Optional[int]				       =							max(1			,				out_channels // config.groups_width )
   UpperCAmelCase__      :					Tuple				       =							(
       TFRegNetShortCut(snake_case__			,				stride=snake_case__			,				name="shortcut" )
       if should_apply_shortcut
       else tf.keras.layers.Activation("linear"			,				name="shortcut" )
   )
   # `self.layers` instead of `self.layer` because that is a reserved argument.
   UpperCAmelCase__      :					int				       =							[
       TFRegNetConvLayer(snake_case__			,				kernel_size=1			,				activation=config.hidden_act			,				name="layer.0" ),
       TFRegNetConvLayer(
           snake_case__			,				stride=snake_case__			,				groups=snake_case__			,				activation=config.hidden_act			,				name="layer.1" ),
       TFRegNetConvLayer(snake_case__			,				kernel_size=1			,				activation=snake_case__			,				name="layer.2" ),
   ]
   UpperCAmelCase__      :					List[Any]				       =							ACTaFN[config.hidden_act]







 def 							UpperCamelCase  (					self		:  Tuple			,				snake_case__		:  Optional[int] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Any				       =							hidden_state
   for layer_module in self.layers:
     UpperCAmelCase__      :					List[str]				       =							layer_module(snake_case__ )
   UpperCAmelCase__      :					Optional[Any]				       =							self.shortcut(snake_case__ )
   hidden_state += residual
   UpperCAmelCase__      :					Union[str, Any]				       =							self.activation(snake_case__ )
   return hidden_state






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  List[Any]			,				snake_case__		:  RegNetConfig			,				snake_case__		:  int			,				snake_case__		:  int			,				snake_case__		:  int = 1			,				**snake_case__		:  List[Any] ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					int				       =							in_channels != out_channels or stride != 1
   UpperCAmelCase__      :					Union[str, Any]				       =							max(1			,				out_channels // config.groups_width )
   UpperCAmelCase__      :					Dict				       =							(
       TFRegNetShortCut(snake_case__			,				stride=snake_case__			,				name="shortcut" )
       if should_apply_shortcut
       else tf.keras.layers.Activation("linear"			,				name="shortcut" )
   )
   UpperCAmelCase__      :					Optional[Any]				       =							[
       TFRegNetConvLayer(snake_case__			,				kernel_size=1			,				activation=config.hidden_act			,				name="layer.0" ),
       TFRegNetConvLayer(
           snake_case__			,				stride=snake_case__			,				groups=snake_case__			,				activation=config.hidden_act			,				name="layer.1" ),
       TFRegNetSELayer(snake_case__			,				reduced_channels=int(round(in_channels / 4 ) )			,				name="layer.2" ),
       TFRegNetConvLayer(snake_case__			,				kernel_size=1			,				activation=snake_case__			,				name="layer.3" ),
   ]
   UpperCAmelCase__      :					Dict				       =							ACTaFN[config.hidden_act]







 def 							UpperCamelCase  (					self		:  Optional[int]			,				snake_case__		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							hidden_state
   for layer_module in self.layers:
     UpperCAmelCase__      :					Tuple				       =							layer_module(snake_case__ )
   UpperCAmelCase__      :					Dict				       =							self.shortcut(snake_case__ )
   hidden_state += residual
   UpperCAmelCase__      :					List[Any]				       =							self.activation(snake_case__ )
   return hidden_state






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  List[Any]			,				snake_case__		:  RegNetConfig			,				snake_case__		:  int			,				snake_case__		:  int			,				snake_case__		:  int = 2			,				snake_case__		:  int = 2			,				**snake_case__		:  Any ):



   '''simple docstring'''




   super().__init__(**snake_case__ )

   UpperCAmelCase__      :					Any				       =							TFRegNetXLayer if config.layer_type == "x" else TFRegNetYLayer
   UpperCAmelCase__      :					int				       =							[
       # downsampling is done in the first layer with stride of 2
       layer(snake_case__			,				snake_case__			,				snake_case__			,				stride=snake_case__			,				name="layers.0" ),
       *[layer(snake_case__			,				snake_case__			,				snake_case__			,				name=F"""layers.{i+1}""" ) for i in range(depth - 1 )],
   ]







 def 							UpperCamelCase  (					self		:  Dict			,				snake_case__		:  Tuple ):



   '''simple docstring'''




   for layer_module in self.layers:
     UpperCAmelCase__      :					Tuple				       =							layer_module(snake_case__ )
   return hidden_state






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  Dict			,				snake_case__		:  RegNetConfig			,				**snake_case__		:  Any ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					Union[str, Any]				       =							[]
   # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
   self.stages.append(
       TFRegNetStage(
           snake_case__			,				config.embedding_size			,				config.hidden_sizes[0]			,				stride=2 if config.downsample_in_first_stage else 1			,				depth=config.depths[0]			,				name="stages.0"			,				) )
   UpperCAmelCase__      :					List[Any]				       =							zip(config.hidden_sizes			,				config.hidden_sizes[1:] )
   for i, ((in_channels, out_channels), depth) in enumerate(zip(snake_case__			,				config.depths[1:] ) ):
     self.stages.append(TFRegNetStage(snake_case__			,				snake_case__			,				snake_case__			,				depth=snake_case__			,				name=F"""stages.{i+1}""" ) )







 def 							UpperCamelCase  (					self		:  Dict			,				snake_case__		:  tf.Tensor			,				snake_case__		:  bool = False			,				snake_case__		:  bool = True ):



   '''simple docstring'''




   UpperCAmelCase__      :					Tuple				       =							() if output_hidden_states else None

   for stage_module in self.stages:
     if output_hidden_states:
       UpperCAmelCase__      :					Optional[Any]				       =							hidden_states + (hidden_state,)

     UpperCAmelCase__      :					Dict				       =							stage_module(snake_case__ )

   if output_hidden_states:
     UpperCAmelCase__      :					Any				       =							hidden_states + (hidden_state,)

   if not return_dict:
     return tuple(v for v in [hidden_state, hidden_states] if v is not None )

   return TFBaseModelOutputWithNoAttention(last_hidden_state=snake_case__			,				hidden_states=snake_case__ )






@keras_serializable
class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''
 lowercase_					:    Dict				=							RegNetConfig







 def __init__(					self		:  int			,				snake_case__		:  str			,				**snake_case__		:  Union[str, Any] ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					List[Any]				       =							config
   UpperCAmelCase__      :					Dict				       =							TFRegNetEmbeddings(snake_case__			,				name="embedder" )
   UpperCAmelCase__      :					int				       =							TFRegNetEncoder(snake_case__			,				name="encoder" )
   UpperCAmelCase__      :					int				       =							tf.keras.layers.GlobalAveragePoolingaD(keepdims=snake_case__			,				name="pooler" )







 @unpack_inputs
 def 							UpperCamelCase  (					self		:  Dict			,				snake_case__		:  tf.Tensor			,				snake_case__		:  Optional[bool] = None			,				snake_case__		:  Optional[bool] = None			,				snake_case__		:  bool = False			,				):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[Any]				       =							(
       output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
   )
   UpperCAmelCase__      :					Tuple				       =							return_dict if return_dict is not None else self.config.use_return_dict

   UpperCAmelCase__      :					str				       =							self.embedder(snake_case__			,				training=snake_case__ )

   UpperCAmelCase__      :					Optional[Any]				       =							self.encoder(
       snake_case__			,				output_hidden_states=snake_case__			,				return_dict=snake_case__			,				training=snake_case__ )

   UpperCAmelCase__      :					Optional[int]				       =							encoder_outputs[0]
   UpperCAmelCase__      :					Optional[Any]				       =							self.pooler(snake_case__ )

   # Change to NCHW output format have uniformity in the modules
   UpperCAmelCase__      :					List[Any]				       =							tf.transpose(snake_case__			,				perm=(0, 3, 1, 2) )
   UpperCAmelCase__      :					Any				       =							tf.transpose(snake_case__			,				perm=(0, 3, 1, 2) )

   # Change the other hidden state outputs to NCHW as well
   if output_hidden_states:
     UpperCAmelCase__      :					Optional[int]				       =							tuple([tf.transpose(snake_case__			,				perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] )

   if not return_dict:
     return (last_hidden_state, pooled_output) + encoder_outputs[1:]

   return TFBaseModelOutputWithPoolingAndNoAttention(
       last_hidden_state=snake_case__			,				pooler_output=snake_case__			,				hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states			,				)






class 							UpperCAmelCase_							(    A				):

 '''simple docstring'''
 lowercase_					:    List[Any]				=							RegNetConfig
 lowercase_					:    Optional[int]				=							"regnet"
 lowercase_					:    Optional[Any]				=							"pixel_values"







 @property
 def 							UpperCamelCase  (					self		:  Any ):



   '''simple docstring'''




   return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_24, 2_24)			,				dtype=tf.floataa )}



SCREAMING_SNAKE_CASE     =       R"""
    Parameters:
    This model is a Tensorflow
    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a
    regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and
    behavior.
        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.
"""

SCREAMING_SNAKE_CASE     =       R"""
    Args:
        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConveNextImageProcessor.__call__`] for details.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
"""






@add_start_docstrings(
    "The bare RegNet model outputting raw features without any specific head on top."					,				A					,				)
class 							UpperCAmelCase_							(    A				):

 '''simple docstring'''







 def __init__(					self		:  Union[str, Any]			,				snake_case__		:  RegNetConfig			,				*snake_case__		:  Any			,				**snake_case__		:  List[str] ):



   '''simple docstring'''




   super().__init__(snake_case__			,				*snake_case__			,				**snake_case__ )
   UpperCAmelCase__      :					Any				       =							TFRegNetMainLayer(snake_case__			,				name="regnet" )







 @unpack_inputs
 @add_start_docstrings_to_model_forward(snake_case__ )
 @add_code_sample_docstrings(
     checkpoint=_CHECKPOINT_FOR_DOC			,				output_type=snake_case__			,				config_class=_CONFIG_FOR_DOC			,				modality="vision"			,				expected_output=_EXPECTED_OUTPUT_SHAPE			,				)
 def 							UpperCamelCase  (					self		:  Optional[int]			,				snake_case__		:  tf.Tensor			,				snake_case__		:  Optional[bool] = None			,				snake_case__		:  Optional[bool] = None			,				snake_case__		:  int=False			,				):



   '''simple docstring'''




   UpperCAmelCase__      :					Tuple				       =							(
       output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
   )
   UpperCAmelCase__      :					str				       =							return_dict if return_dict is not None else self.config.use_return_dict

   UpperCAmelCase__      :					List[str]				       =							self.regnet(
       pixel_values=snake_case__			,				output_hidden_states=snake_case__			,				return_dict=snake_case__			,				training=snake_case__			,				)
   if not return_dict:
     return (outputs[0],) + outputs[1:]

   return TFBaseModelOutputWithPoolingAndNoAttention(
       last_hidden_state=outputs.last_hidden_state			,				pooler_output=outputs.pooler_output			,				hidden_states=outputs.hidden_states			,				)






@add_start_docstrings(
    "\n    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "					,				A					,				)
class 							UpperCAmelCase_							(    A					,				A				):

 '''simple docstring'''







 def __init__(					self		:  List[str]			,				snake_case__		:  RegNetConfig			,				*snake_case__		:  Optional[Any]			,				**snake_case__		:  int ):



   '''simple docstring'''




   super().__init__(snake_case__			,				*snake_case__			,				**snake_case__ )
   UpperCAmelCase__      :					Optional[Any]				       =							config.num_labels
   UpperCAmelCase__      :					str				       =							TFRegNetMainLayer(snake_case__			,				name="regnet" )
   # classification head
   UpperCAmelCase__      :					Optional[int]				       =							[
       tf.keras.layers.Flatten(),
       tf.keras.layers.Dense(config.num_labels			,				name="classifier.1" ) if config.num_labels > 0 else tf.identity,
   ]







 @unpack_inputs
 @add_start_docstrings_to_model_forward(snake_case__ )
 @add_code_sample_docstrings(
     checkpoint=_IMAGE_CLASS_CHECKPOINT			,				output_type=snake_case__			,				config_class=_CONFIG_FOR_DOC			,				expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT			,				)
 def 							UpperCamelCase  (					self		:  List[str]			,				snake_case__		:  tf.Tensor = None			,				snake_case__		:  tf.Tensor = None			,				snake_case__		:  bool = None			,				snake_case__		:  bool = None			,				snake_case__		:  List[Any]=False			,				):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							(
       output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
   )
   UpperCAmelCase__      :					List[Any]				       =							return_dict if return_dict is not None else self.config.use_return_dict

   UpperCAmelCase__      :					Dict				       =							self.regnet(
       snake_case__			,				output_hidden_states=snake_case__			,				return_dict=snake_case__			,				training=snake_case__ )

   UpperCAmelCase__      :					Dict				       =							outputs.pooler_output if return_dict else outputs[1]

   UpperCAmelCase__      :					Union[str, Any]				       =							self.classifier[0](snake_case__ )
   UpperCAmelCase__      :					str				       =							self.classifier[1](snake_case__ )

   UpperCAmelCase__      :					Any				       =							None if labels is None else self.hf_compute_loss(labels=snake_case__			,				logits=snake_case__ )

   if not return_dict:
     UpperCAmelCase__      :					Tuple				       =							(logits,) + outputs[2:]
     return ((loss,) + output) if loss is not None else output

   return TFSequenceClassifierOutput(loss=snake_case__			,				logits=snake_case__			,				hidden_states=outputs.hidden_states )



