





"""simple docstring"""





import inspect
import unittest

from transformers import YolosConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch
							from torch import nn

							from transformers import YolosForObjectDetection, YolosModel
							from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor
class 		SCREAMING_SNAKE_CASE_							:
				"""simple docstring"""




				def __init__(     self  :Union[str, Any]						,    __lowercase  :Optional[int]						,    __lowercase  :Any=13						,    __lowercase  :Optional[Any]=[30, 30]						,    __lowercase  :Union[str, Any]=2						,    __lowercase  :Union[str, Any]=3						,    __lowercase  :str=True						,    __lowercase  :Tuple=True						,    __lowercase  :Any=32						,    __lowercase  :int=5						,    __lowercase  :Optional[Any]=4						,    __lowercase  :List[str]=37						,    __lowercase  :Optional[Any]="gelu"						,    __lowercase  :List[Any]=0.1						,    __lowercase  :Tuple=0.1						,    __lowercase  :Any=10						,    __lowercase  :Union[str, Any]=0.02						,    __lowercase  :int=3						,    __lowercase  :Any=None						,    __lowercase  :List[Any]=8						,    __lowercase  :int=10						,    ):
											__lowerCamelCase      :      Tuple         =parent
											__lowerCamelCase      :      Any         =batch_size
											__lowerCamelCase      :      int         =image_size
											__lowerCamelCase      :      Dict         =patch_size
											__lowerCamelCase      :      Union[str, Any]         =num_channels
											__lowerCamelCase      :      Tuple         =is_training
											__lowerCamelCase      :      Any         =use_labels
											__lowerCamelCase      :      Optional[Any]         =hidden_size
											__lowerCamelCase      :      List[str]         =num_hidden_layers
											__lowerCamelCase      :      Optional[Any]         =num_attention_heads
											__lowerCamelCase      :      int         =intermediate_size
											__lowerCamelCase      :      Dict         =hidden_act
											__lowerCamelCase      :      List[Any]         =hidden_dropout_prob
											__lowerCamelCase      :      List[Any]         =attention_probs_dropout_prob
											__lowerCamelCase      :      Tuple         =type_sequence_label_size
											__lowerCamelCase      :      Dict         =initializer_range
											__lowerCamelCase      :      List[str]         =num_labels
											__lowerCamelCase      :      Tuple         =scope
											__lowerCamelCase      :      Optional[int]         =n_targets
											__lowerCamelCase      :      Tuple         =num_detection_tokens
											# we set the expected sequence length (which is used in several tests)
											# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
											__lowerCamelCase      :      List[Any]         =(image_size[1] // patch_size) * (image_size[0] // patch_size)
											__lowerCamelCase      :      List[Any]         =num_patches + 1 + self.num_detection_tokens




				def 					__lowercase  (     self  :Any			):
											__lowerCamelCase      :      Tuple         =floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]			)

											__lowerCamelCase      :      Union[str, Any]         =None
											if self.use_labels:
																		# labels is a list of Dict (each Dict being the labels for a given example in the batch)
																		__lowerCamelCase      :      Optional[int]         =[]
																		for i in range(self.batch_size			):
																									__lowerCamelCase      :      int         ={}
																									__lowerCamelCase      :      Dict         =torch.randint(
																									    high=self.num_labels						,    size=(self.n_targets,)						,    device=__lowercase			)
																									__lowerCamelCase      :      Any         =torch.rand(self.n_targets						,    4						,    device=__lowercase			)
																									labels.append(__lowercase			)

											__lowerCamelCase      :      Dict         =self.get_config()

											return config, pixel_values, labels




				def 					__lowercase  (     self  :List[Any]			):
											return YolosConfig(
											    image_size=self.image_size						,    patch_size=self.patch_size						,    num_channels=self.num_channels						,    hidden_size=self.hidden_size						,    num_hidden_layers=self.num_hidden_layers						,    num_attention_heads=self.num_attention_heads						,    intermediate_size=self.intermediate_size						,    hidden_act=self.hidden_act						,    hidden_dropout_prob=self.hidden_dropout_prob						,    attention_probs_dropout_prob=self.attention_probs_dropout_prob						,    is_decoder=__lowercase						,    initializer_range=self.initializer_range						,    num_detection_tokens=self.num_detection_tokens						,    num_labels=self.num_labels						,    )




				def 					__lowercase  (     self  :Optional[int]						,    __lowercase  :Any						,    __lowercase  :Union[str, Any]						,    __lowercase  :List[str]			):
											__lowerCamelCase      :      Optional[int]         =YolosModel(config=__lowercase			)
											model.to(__lowercase			)
											model.eval()
											__lowerCamelCase      :      Optional[Any]         =model(__lowercase			)
											self.parent.assertEqual(
											    result.last_hidden_state.shape						,    (self.batch_size, self.expected_seq_len, self.hidden_size)			)




				def 					__lowercase  (     self  :Union[str, Any]						,    __lowercase  :Optional[int]						,    __lowercase  :Any						,    __lowercase  :str			):
											__lowerCamelCase      :      Union[str, Any]         =YolosForObjectDetection(__lowercase			)
											model.to(__lowercase			)
											model.eval()

											__lowerCamelCase      :      Union[str, Any]         =model(pixel_values=__lowercase			)
											__lowerCamelCase      :      int         =model(__lowercase			)

											self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_detection_tokens, self.num_labels + 1)			)
											self.parent.assertEqual(result.pred_boxes.shape						,    (self.batch_size, self.num_detection_tokens, 4)			)

											__lowerCamelCase      :      int         =model(pixel_values=__lowercase						,    labels=__lowercase			)

											self.parent.assertEqual(result.loss.shape						,    ()			)
											self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_detection_tokens, self.num_labels + 1)			)
											self.parent.assertEqual(result.pred_boxes.shape						,    (self.batch_size, self.num_detection_tokens, 4)			)




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      Dict         =self.prepare_config_and_inputs()
											__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase      :      List[Any]         =config_and_inputs
											__lowerCamelCase      :      Tuple         ={'''pixel_values''': pixel_values}
											return config, inputs_dict

@require_torch
class 		SCREAMING_SNAKE_CASE_							(    snake_case__       ,  snake_case__       ,  unittest.TestCase   ):
				"""simple docstring"""
				__snake_case   :       List[str]	 =		(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
				__snake_case   :       Any	 =		(
				    {"""feature-extraction""": YolosModel, """object-detection""": YolosForObjectDetection} if is_torch_available() else {}
				)

				__snake_case   :       Optional[int]	 =		False
				__snake_case   :       List[str]	 =		False
				__snake_case   :       Union[str, Any]	 =		False
				__snake_case   :       Optional[int]	 =		False




				def 					__lowercase  (     self  :Optional[int]						,    __lowercase  :Union[str, Any]						,    __lowercase  :Any						,    __lowercase  :Tuple=False			):
											__lowerCamelCase      :      str         =super()._prepare_for_class(__lowercase						,    __lowercase						,    return_labels=__lowercase			)

											if return_labels:
																		if model_class.__name__ == "YolosForObjectDetection":
																									__lowerCamelCase      :      str         =[]
																									for i in range(self.model_tester.batch_size			):
																																__lowerCamelCase      :      List[str]         ={}
																																__lowerCamelCase      :      int         =torch.ones(
																																    size=(self.model_tester.n_targets,)						,    device=__lowercase						,    dtype=torch.long			)
																																__lowerCamelCase      :      Optional[int]         =torch.ones(
																																    self.model_tester.n_targets						,    4						,    device=__lowercase						,    dtype=torch.float			)
																																labels.append(__lowercase			)
																									__lowerCamelCase      :      int         =labels

											return inputs_dict




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      Tuple         =YolosModelTester(self			)
											__lowerCamelCase      :      int         =ConfigTester(self						,    config_class=__lowercase						,    has_text_modality=__lowercase						,    hidden_size=37			)




				def 					__lowercase  (     self  :Tuple			):
											self.config_tester.run_common_tests()




				def 					__lowercase  (     self  :List[Any]			):
											# YOLOS does not use inputs_embeds
											pass




				def 					__lowercase  (     self  :Any			):
											__lowerCamelCase    ,						__lowerCamelCase      :      List[str]         =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowerCamelCase      :      Union[str, Any]         =model_class(__lowercase			)
																		self.assertIsInstance(model.get_input_embeddings()						,    (nn.Module)			)
																		__lowerCamelCase      :      Dict         =model.get_output_embeddings()
																		self.assertTrue(x is None or isinstance(__lowercase						,    nn.Linear			)			)




				def 					__lowercase  (     self  :List[str]			):
											__lowerCamelCase    ,						__lowerCamelCase      :      Union[str, Any]         =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowerCamelCase      :      Union[str, Any]         =model_class(__lowercase			)
																		__lowerCamelCase      :      Any         =inspect.signature(model.forward			)
																		# signature.parameters is an OrderedDict => so arg_names order is deterministic
																		__lowerCamelCase      :      Optional[Any]         =[*signature.parameters.keys()]

																		__lowerCamelCase      :      Optional[Any]         =['''pixel_values''']
																		self.assertListEqual(arg_names[:1]						,    __lowercase			)




				def 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      Any         =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__lowercase			)




				def 					__lowercase  (     self  :Optional[int]			):
											__lowerCamelCase    ,						__lowerCamelCase      :      Dict         =self.model_tester.prepare_config_and_inputs_for_common()
											__lowerCamelCase      :      Union[str, Any]         =True

											# in YOLOS, the seq_len is different
											__lowerCamelCase      :      List[str]         =self.model_tester.expected_seq_len
											for model_class in self.all_model_classes:
																		__lowerCamelCase      :      int         =True
																		__lowerCamelCase      :      Tuple         =False
																		__lowerCamelCase      :      Any         =True
																		__lowerCamelCase      :      List[Any]         =model_class(__lowercase			)
																		model.to(__lowercase			)
																		model.eval()
																		with torch.no_grad():
																									__lowerCamelCase      :      str         =model(**self._prepare_for_class(__lowercase						,    __lowercase			)			)
																		__lowerCamelCase      :      str         =outputs.attentions
																		self.assertEqual(len(__lowercase			)						,    self.model_tester.num_hidden_layers			)

																		# check that output_attentions also work using config
																		del inputs_dict["output_attentions"]
																		__lowerCamelCase      :      int         =True
																		__lowerCamelCase      :      int         =model_class(__lowercase			)
																		model.to(__lowercase			)
																		model.eval()
																		with torch.no_grad():
																									__lowerCamelCase      :      Any         =model(**self._prepare_for_class(__lowercase						,    __lowercase			)			)
																		__lowerCamelCase      :      Optional[int]         =outputs.attentions
																		self.assertEqual(len(__lowercase			)						,    self.model_tester.num_hidden_layers			)

																		self.assertListEqual(
																		    list(attentions[0].shape[-3:]			)						,    [self.model_tester.num_attention_heads, seq_len, seq_len]						,    )
																		__lowerCamelCase      :      Optional[Any]         =len(__lowercase			)

																		# Check attention is always last and order is fine
																		__lowerCamelCase      :      List[str]         =True
																		__lowerCamelCase      :      Union[str, Any]         =True
																		__lowerCamelCase      :      Union[str, Any]         =model_class(__lowercase			)
																		model.to(__lowercase			)
																		model.eval()
																		with torch.no_grad():
																									__lowerCamelCase      :      Optional[int]         =model(**self._prepare_for_class(__lowercase						,    __lowercase			)			)

																		__lowerCamelCase      :      Union[str, Any]         =1
																		self.assertEqual(out_len + added_hidden_states						,    len(__lowercase			)			)

																		__lowerCamelCase      :      Union[str, Any]         =outputs.attentions

																		self.assertEqual(len(__lowercase			)						,    self.model_tester.num_hidden_layers			)
																		self.assertListEqual(
																		    list(self_attentions[0].shape[-3:]			)						,    [self.model_tester.num_attention_heads, seq_len, seq_len]						,    )




				def 					__lowercase  (     self  :Tuple			):
											def check_hidden_states_output(__lowercase  :Optional[Any]						,    __lowercase  :Union[str, Any]						,    __lowercase  :Union[str, Any]			):
																		__lowerCamelCase      :      Tuple         =model_class(__lowercase			)
																		model.to(__lowercase			)
																		model.eval()

																		with torch.no_grad():
																									__lowerCamelCase      :      Any         =model(**self._prepare_for_class(__lowercase						,    __lowercase			)			)

																		__lowerCamelCase      :      List[str]         =outputs.hidden_states

																		__lowerCamelCase      :      List[str]         =getattr(
																		    self.model_tester						,    '''expected_num_hidden_layers'''						,    self.model_tester.num_hidden_layers + 1			)
																		self.assertEqual(len(__lowercase			)						,    __lowercase			)

																		# YOLOS has a different seq_length
																		__lowerCamelCase      :      List[Any]         =self.model_tester.expected_seq_len

																		self.assertListEqual(
																		    list(hidden_states[0].shape[-2:]			)						,    [seq_length, self.model_tester.hidden_size]						,    )

											__lowerCamelCase    ,						__lowerCamelCase      :      Optional[Any]         =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowerCamelCase      :      Optional[int]         =True
																		check_hidden_states_output(__lowercase						,    __lowercase						,    __lowercase			)

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		__lowerCamelCase      :      Optional[Any]         =True

																		check_hidden_states_output(__lowercase						,    __lowercase						,    __lowercase			)




				def 					__lowercase  (     self  :Any			):
											__lowerCamelCase      :      List[str]         =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_object_detection(*__lowercase			)




				@slow
				def 					__lowercase  (     self  :Optional[Any]			):
											for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowerCamelCase      :      List[str]         =YolosModel.from_pretrained(__lowercase			)
																		self.assertIsNotNone(__lowercase			)



def 				lowerCAmelCase_			(      ):


							'''simple docstring'''







							__lowerCamelCase      :      Dict         =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
							return image
@require_torch
@require_vision
class 		SCREAMING_SNAKE_CASE_							(    unittest.TestCase   ):
				"""simple docstring"""




				@cached_property
				def 					__lowercase  (     self  :Optional[Any]			):
											return AutoImageProcessor.from_pretrained('''hustvl/yolos-small'''			) if is_vision_available() else None




				@slow
				def 					__lowercase  (     self  :List[Any]			):
											__lowerCamelCase      :      int         =YolosForObjectDetection.from_pretrained('''hustvl/yolos-small'''			).to(__lowercase			)

											__lowerCamelCase      :      int         =self.default_image_processor
											__lowerCamelCase      :      Optional[int]         =prepare_img()
											__lowerCamelCase      :      Any         =image_processor(images=__lowercase						,    return_tensors='''pt'''			).to(__lowercase			)

											# forward pass
											with torch.no_grad():
																		__lowerCamelCase      :      Optional[int]         =model(inputs.pixel_values			)

											# verify outputs
											__lowerCamelCase      :      int         =torch.Size((1, 100, 92)			)
											self.assertEqual(outputs.logits.shape						,    __lowercase			)

											__lowerCamelCase      :      Optional[Any]         =torch.tensor(
											    [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]						,    device=__lowercase						,    )
											__lowerCamelCase      :      List[str]         =torch.tensor(
											    [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]]						,    device=__lowercase			)
											self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]						,    __lowercase						,    atol=1e-4			)			)
											self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]						,    __lowercase						,    atol=1e-4			)			)

											# verify postprocessing
											__lowerCamelCase      :      Optional[Any]         =image_processor.post_process_object_detection(
											    __lowercase						,    threshold=0.3						,    target_sizes=[image.size[::-1]]			)[0]
											__lowerCamelCase      :      Dict         =torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861]			).to(__lowercase			)
											__lowerCamelCase      :      Optional[Any]         =[75, 75, 17, 63, 17]
											__lowerCamelCase      :      Optional[Any]         =torch.tensor([335.0609, 79.3848, 375.4216, 187.2495]			).to(__lowercase			)

											self.assertEqual(len(results['''scores''']			)						,    5			)
											self.assertTrue(torch.allclose(results['''scores''']						,    __lowercase						,    atol=1e-4			)			)
											self.assertSequenceEqual(results['''labels'''].tolist()						,    __lowercase			)
											self.assertTrue(torch.allclose(results['''boxes'''][0, :]						,    __lowercase			)			)











"""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


_UpperCamelCase							=      logging.get_logger(__name__)

# General docstring
_UpperCamelCase							=      'RegNetConfig'

# Base docstring
_UpperCamelCase							=      'facebook/regnet-y-040'
_UpperCamelCase							=      [1, 1088, 7, 7]

# Image classification docstring
_UpperCamelCase							=      'facebook/regnet-y-040'
_UpperCamelCase							=      'tabby, tabby cat'

_UpperCamelCase							=      [
    'facebook/regnet-y-040',
    # See all regnet models at https://huggingface.co/models?filter=regnet
]
class 		SCREAMING_SNAKE_CASE_							(    tf.keras.layers.Layer   ):
				"""simple docstring"""




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




				def 					__lowercase  (     self  :Optional[int]						,    __lowercase  :Any			):
											__lowerCamelCase      :      str         =self.convolution(self.padding(__lowercase			)			)
											__lowerCamelCase      :      Optional[int]         =self.normalization(__lowercase			)
											__lowerCamelCase      :      Any         =self.activation(__lowercase			)
											return hidden_state

class 		SCREAMING_SNAKE_CASE_							(    tf.keras.layers.Layer   ):
				"""simple docstring"""




				def __init__(     self  :Union[str, Any]						,    __lowercase  :RegNetConfig						,    **__lowercase  :Any			):
											super().__init__(**__lowercase			)
											__lowerCamelCase      :      Tuple         =config.num_channels
											__lowerCamelCase      :      Union[str, Any]         =TFRegNetConvLayer(
											    out_channels=config.embedding_size						,    kernel_size=3						,    stride=2						,    activation=config.hidden_act						,    name='''embedder'''						,    )




				def 					__lowercase  (     self  :int						,    __lowercase  :List[str]			):
											__lowerCamelCase      :      int         =shape_list(__lowercase			)[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)
											__lowerCamelCase      :      Union[str, Any]         =tf.transpose(__lowercase						,    perm=(0, 2, 3, 1)			)
											__lowerCamelCase      :      Optional[int]         =self.embedder(__lowercase			)
											return hidden_state

class 		SCREAMING_SNAKE_CASE_							(    tf.keras.layers.Layer   ):
				"""simple docstring"""




				def __init__(     self  :List[Any]						,    __lowercase  :int						,    __lowercase  :int = 2						,    **__lowercase  :Optional[int]			):
											super().__init__(**__lowercase			)
											__lowerCamelCase      :      int         =tf.keras.layers.ConvaD(
											    filters=__lowercase						,    kernel_size=1						,    strides=__lowercase						,    use_bias=__lowercase						,    name='''convolution'''			)
											__lowerCamelCase      :      List[str]         =tf.keras.layers.BatchNormalization(epsilon=1e-5						,    momentum=0.9						,    name='''normalization'''			)




				def 					__lowercase  (     self  :Optional[Any]						,    __lowercase  :tf.Tensor						,    __lowercase  :bool = False			):
											return self.normalization(self.convolution(__lowercase			)						,    training=__lowercase			)

class 		SCREAMING_SNAKE_CASE_							(    tf.keras.layers.Layer   ):
				"""simple docstring"""




				def __init__(     self  :Dict						,    __lowercase  :int						,    __lowercase  :int						,    **__lowercase  :List[str]			):
											super().__init__(**__lowercase			)
											__lowerCamelCase      :      int         =tf.keras.layers.GlobalAveragePoolingaD(keepdims=__lowercase						,    name='''pooler'''			)
											__lowerCamelCase      :      int         =[
											    tf.keras.layers.ConvaD(filters=__lowercase						,    kernel_size=1						,    activation='''relu'''						,    name='''attention.0'''			),
											    tf.keras.layers.ConvaD(filters=__lowercase						,    kernel_size=1						,    activation='''sigmoid'''						,    name='''attention.2'''			),
											]




				def 					__lowercase  (     self  :Dict						,    __lowercase  :Union[str, Any]			):
											# [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels]
											__lowerCamelCase      :      Any         =self.pooler(__lowercase			)
											for layer_module in self.attention:
																		__lowerCamelCase      :      Any         =layer_module(__lowercase			)
											__lowerCamelCase      :      Dict         =hidden_state * pooled
											return hidden_state

class 		SCREAMING_SNAKE_CASE_							(    tf.keras.layers.Layer   ):
				"""simple docstring"""




				def __init__(     self  :Optional[int]						,    __lowercase  :RegNetConfig						,    __lowercase  :int						,    __lowercase  :int						,    __lowercase  :int = 1						,    **__lowercase  :str			):
											super().__init__(**__lowercase			)
											__lowerCamelCase      :      Dict         =in_channels != out_channels or stride != 1
											__lowerCamelCase      :      int         =max(1						,    out_channels // config.groups_width			)
											__lowerCamelCase      :      List[str]         =(
											    TFRegNetShortCut(__lowercase						,    stride=__lowercase						,    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.
											__lowerCamelCase      :      str         =[
											    TFRegNetConvLayer(__lowercase						,    kernel_size=1						,    activation=config.hidden_act						,    name='''layer.0'''			),
											    TFRegNetConvLayer(
											        __lowercase						,    stride=__lowercase						,    groups=__lowercase						,    activation=config.hidden_act						,    name='''layer.1'''			),
											    TFRegNetConvLayer(__lowercase						,    kernel_size=1						,    activation=__lowercase						,    name='''layer.2'''			),
											]
											__lowerCamelCase      :      Optional[int]         =ACTaFN[config.hidden_act]




				def 					__lowercase  (     self  :int						,    __lowercase  :Optional[int]			):
											__lowerCamelCase      :      List[Any]         =hidden_state
											for layer_module in self.layers:
																		__lowerCamelCase      :      str         =layer_module(__lowercase			)
											__lowerCamelCase      :      List[Any]         =self.shortcut(__lowercase			)
											hidden_state += residual
											__lowerCamelCase      :      Optional[int]         =self.activation(__lowercase			)
											return hidden_state

class 		SCREAMING_SNAKE_CASE_							(    tf.keras.layers.Layer   ):
				"""simple docstring"""




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




				def 					__lowercase  (     self  :Tuple						,    __lowercase  :Tuple			):
											__lowerCamelCase      :      List[Any]         =hidden_state
											for layer_module in self.layers:
																		__lowerCamelCase      :      int         =layer_module(__lowercase			)
											__lowerCamelCase      :      List[str]         =self.shortcut(__lowercase			)
											hidden_state += residual
											__lowerCamelCase      :      List[str]         =self.activation(__lowercase			)
											return hidden_state

class 		SCREAMING_SNAKE_CASE_							(    tf.keras.layers.Layer   ):
				"""simple docstring"""




				def __init__(     self  :int						,    __lowercase  :RegNetConfig						,    __lowercase  :int						,    __lowercase  :int						,    __lowercase  :int = 2						,    __lowercase  :int = 2						,    **__lowercase  :Union[str, Any]			):
											super().__init__(**__lowercase			)

											__lowerCamelCase      :      List[str]         =TFRegNetXLayer if config.layer_type == '''x''' else TFRegNetYLayer
											__lowerCamelCase      :      List[Any]         =[
											    # downsampling is done in the first layer with stride of 2
											    layer(__lowercase						,    __lowercase						,    __lowercase						,    stride=__lowercase						,    name='''layers.0'''			),
											    *[layer(__lowercase						,    __lowercase						,    __lowercase						,    name=f'layers.{i+1}'			) for i in range(depth - 1			)],
											]




				def 					__lowercase  (     self  :int						,    __lowercase  :List[str]			):
											for layer_module in self.layers:
																		__lowerCamelCase      :      int         =layer_module(__lowercase			)
											return hidden_state

class 		SCREAMING_SNAKE_CASE_							(    tf.keras.layers.Layer   ):
				"""simple docstring"""




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




				def 					__lowercase  (     self  :str						,    __lowercase  :tf.Tensor						,    __lowercase  :bool = False						,    __lowercase  :bool = True			):
											__lowerCamelCase      :      Optional[Any]         =() if output_hidden_states else None

											for stage_module in self.stages:
																		if output_hidden_states:
																									__lowerCamelCase      :      Dict         =hidden_states + (hidden_state,)

																		__lowerCamelCase      :      List[Any]         =stage_module(__lowercase			)

											if output_hidden_states:
																		__lowerCamelCase      :      Union[str, 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=__lowercase						,    hidden_states=__lowercase			)

@keras_serializable
class 		SCREAMING_SNAKE_CASE_							(    tf.keras.layers.Layer   ):
				"""simple docstring"""
				__snake_case   :       Optional[int]	 =		RegNetConfig




				def __init__(     self  :List[Any]						,    __lowercase  :Dict						,    **__lowercase  :Union[str, Any]			):
											super().__init__(**__lowercase			)
											__lowerCamelCase      :      int         =config
											__lowerCamelCase      :      List[str]         =TFRegNetEmbeddings(__lowercase						,    name='''embedder'''			)
											__lowerCamelCase      :      List[str]         =TFRegNetEncoder(__lowercase						,    name='''encoder'''			)
											__lowerCamelCase      :      List[Any]         =tf.keras.layers.GlobalAveragePoolingaD(keepdims=__lowercase						,    name='''pooler'''			)




				@unpack_inputs
				def 					__lowercase  (     self  :List[Any]						,    __lowercase  :tf.Tensor						,    __lowercase  :Optional[bool] = None						,    __lowercase  :Optional[bool] = None						,    __lowercase  :bool = False						,    ):
											__lowerCamelCase      :      Union[str, Any]         =(
											    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
											)
											__lowerCamelCase      :      Tuple         =return_dict if return_dict is not None else self.config.use_return_dict

											__lowerCamelCase      :      Tuple         =self.embedder(__lowercase						,    training=__lowercase			)

											__lowerCamelCase      :      Optional[Any]         =self.encoder(
											    __lowercase						,    output_hidden_states=__lowercase						,    return_dict=__lowercase						,    training=__lowercase			)

											__lowerCamelCase      :      str         =encoder_outputs[0]
											__lowerCamelCase      :      Tuple         =self.pooler(__lowercase			)

											# Change to NCHW output format have uniformity in the modules
											__lowerCamelCase      :      int         =tf.transpose(__lowercase						,    perm=(0, 3, 1, 2)			)
											__lowerCamelCase      :      Any         =tf.transpose(__lowercase						,    perm=(0, 3, 1, 2)			)

											# Change the other hidden state outputs to NCHW as well
											if output_hidden_states:
																		__lowerCamelCase      :      str         =tuple([tf.transpose(__lowercase						,    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=__lowercase						,    pooler_output=__lowercase						,    hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states						,    )

class 		SCREAMING_SNAKE_CASE_							(    snake_case__   ):
				"""simple docstring"""
				__snake_case   :       Optional[int]	 =		RegNetConfig
				__snake_case   :       int	 =		"""regnet"""
				__snake_case   :       int	 =		"""pixel_values"""




				@property
				def 					__lowercase  (     self  :List[str]			):
											return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224)						,    dtype=tf.floataa			)}

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

_UpperCamelCase							=      r'\n    Args:\n        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConveNextImageProcessor.__call__`] for details.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n'
@add_start_docstrings(
    """The bare RegNet model outputting raw features without any specific head on top."""       ,  snake_case__       ,  )
class 		SCREAMING_SNAKE_CASE_							(    snake_case__   ):
				"""simple docstring"""




				def __init__(     self  :Union[str, Any]						,    __lowercase  :RegNetConfig						,    *__lowercase  :List[str]						,    **__lowercase  :int			):
											super().__init__(__lowercase						,    *__lowercase						,    **__lowercase			)
											__lowerCamelCase      :      Tuple         =TFRegNetMainLayer(__lowercase						,    name='''regnet'''			)




				@unpack_inputs
				@add_start_docstrings_to_model_forward(__lowercase			)
				@add_code_sample_docstrings(
				    checkpoint=_CHECKPOINT_FOR_DOC						,    output_type=__lowercase						,    config_class=_CONFIG_FOR_DOC						,    modality='''vision'''						,    expected_output=_EXPECTED_OUTPUT_SHAPE						,    )
				def 					__lowercase  (     self  :Optional[Any]						,    __lowercase  :tf.Tensor						,    __lowercase  :Optional[bool] = None						,    __lowercase  :Optional[bool] = None						,    __lowercase  :Optional[int]=False						,    ):
											__lowerCamelCase      :      List[Any]         =(
											    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
											)
											__lowerCamelCase      :      Optional[int]         =return_dict if return_dict is not None else self.config.use_return_dict

											__lowerCamelCase      :      Dict         =self.regnet(
											    pixel_values=__lowercase						,    output_hidden_states=__lowercase						,    return_dict=__lowercase						,    training=__lowercase						,    )
											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(
    """
    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """       ,  snake_case__       ,  )
class 		SCREAMING_SNAKE_CASE_							(    snake_case__       ,  snake_case__   ):
				"""simple docstring"""




				def __init__(     self  :Union[str, Any]						,    __lowercase  :RegNetConfig						,    *__lowercase  :List[Any]						,    **__lowercase  :Dict			):
											super().__init__(__lowercase						,    *__lowercase						,    **__lowercase			)
											__lowerCamelCase      :      Optional[int]         =config.num_labels
											__lowerCamelCase      :      Optional[int]         =TFRegNetMainLayer(__lowercase						,    name='''regnet'''			)
											# classification head
											__lowerCamelCase      :      Union[str, Any]         =[
											    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(__lowercase			)
				@add_code_sample_docstrings(
				    checkpoint=_IMAGE_CLASS_CHECKPOINT						,    output_type=__lowercase						,    config_class=_CONFIG_FOR_DOC						,    expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT						,    )
				def 					__lowercase  (     self  :List[Any]						,    __lowercase  :tf.Tensor = None						,    __lowercase  :tf.Tensor = None						,    __lowercase  :bool = None						,    __lowercase  :bool = None						,    __lowercase  :int=False						,    ):
											__lowerCamelCase      :      str         =(
											    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
											)
											__lowerCamelCase      :      Optional[int]         =return_dict if return_dict is not None else self.config.use_return_dict

											__lowerCamelCase      :      str         =self.regnet(
											    __lowercase						,    output_hidden_states=__lowercase						,    return_dict=__lowercase						,    training=__lowercase			)

											__lowerCamelCase      :      Any         =outputs.pooler_output if return_dict else outputs[1]

											__lowerCamelCase      :      List[str]         =self.classifier[0](__lowercase			)
											__lowerCamelCase      :      str         =self.classifier[1](__lowercase			)

											__lowerCamelCase      :      str         =None if labels is None else self.hf_compute_loss(labels=__lowercase						,    logits=__lowercase			)

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

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




