





from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import ResNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
						import tensorflow as tf

						from transformers import TFResNetForImageClassification, TFResNetModel
						from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor
class SCREAMING_SNAKE_CASE_						:






						def __init__(      self      :      List[str]		,   _A      :      str		,   _A      :      List[Any]=3		,   _A      :      Optional[Any]=32		,   _A      :      int=3		,   _A      :      Union[str, Any]=10		,   _A      :      str=[10, 20, 30, 40]		,   _A      :      Optional[Any]=[1, 1, 2, 1]		,   _A      :      int=True		,   _A      :      Tuple=True		,   _A      :      Any="relu"		,   _A      :      Optional[int]=3		,   _A      :      int=None		,   )							->    List[str]:







							"""simple docstring"""


							snake_case_		:			Union[str, Any]											=	parent
							snake_case_		:			Union[str, Any]											=	batch_size
							snake_case_		:			Optional[int]											=	image_size
							snake_case_		:			Union[str, Any]											=	num_channels
							snake_case_		:			Optional[Any]											=	embeddings_size
							snake_case_		:			str											=	hidden_sizes
							snake_case_		:			Dict											=	depths
							snake_case_		:			Dict											=	is_training
							snake_case_		:			Dict											=	use_labels
							snake_case_		:			Optional[int]											=	hidden_act
							snake_case_		:			List[str]											=	num_labels
							snake_case_		:			List[Any]											=	scope
							snake_case_		:			Tuple											=	len(A__	)






						def UpperCAmelCase_				(      self      :      List[Any]	)							->    Tuple:







							"""simple docstring"""


							snake_case_		:			int											=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

							snake_case_		:			Optional[Any]											=	None
							if self.use_labels:
								snake_case_		:			List[Any]											=	ids_tensor([self.batch_size]		,   self.num_labels	)

							snake_case_		:			List[Any]											=	self.get_config()

							return config, pixel_values, labels






						def UpperCAmelCase_				(      self      :      Optional[int]	)							->    Optional[int]:







							"""simple docstring"""


							return ResNetConfig(
							    num_channels=self.num_channels		,   embeddings_size=self.embeddings_size		,   hidden_sizes=self.hidden_sizes		,   depths=self.depths		,   hidden_act=self.hidden_act		,   num_labels=self.num_labels		,   image_size=self.image_size		,   )






						def UpperCAmelCase_				(      self      :      List[Any]		,   _A      :      Dict		,   _A      :      Tuple		,   _A      :      Any	)							->    int:







							"""simple docstring"""


							snake_case_		:			int											=	TFResNetModel(config=A__	)
							snake_case_		:			Optional[Any]											=	model(A__	)
							# expected last hidden states: B, C, H // 32, W // 32
							self.parent.assertEqual(
							    result.last_hidden_state.shape		,   (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)		,   )






						def UpperCAmelCase_				(      self      :      List[str]		,   _A      :      str		,   _A      :      Optional[Any]		,   _A      :      Optional[Any]	)							->    Tuple:







							"""simple docstring"""


							snake_case_		:			str											=	self.num_labels
							snake_case_		:			Union[str, Any]											=	TFResNetForImageClassification(A__	)
							snake_case_		:			List[Any]											=	model(A__		,   labels=A__	)
							self.parent.assertEqual(result.logits.shape		,   (self.batch_size, self.num_labels)	)






						def UpperCAmelCase_				(      self      :      int	)							->    List[str]:







							"""simple docstring"""


							snake_case_		:			int											=	self.prepare_config_and_inputs()
							snake_case_						,snake_case_						,snake_case_		:			Optional[int]											=	config_and_inputs
							snake_case_		:			Any											=	{'pixel_values': pixel_values}
							return config, inputs_dict
@require_tf
class SCREAMING_SNAKE_CASE_						(					_lowerCamelCase       ,				_lowerCamelCase       ,				unittest.TestCase					):
						__magic_name__:			Union[str, Any]       =       (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
						__magic_name__:			int       =       (
						    {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification}
						    if is_tf_available()
						    else {}
						)

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






						def UpperCAmelCase_				(      self      :      int	)							->    Union[str, Any]:







							"""simple docstring"""


							snake_case_		:			Any											=	TFResNetModelTester(self	)
							snake_case_		:			int											=	ConfigTester(self		,   config_class=A__		,   has_text_modality=A__	)






						def UpperCAmelCase_				(      self      :      str	)							->    int:







							"""simple docstring"""


							self.create_and_test_config_common_properties()
							self.config_tester.create_and_test_config_to_json_string()
							self.config_tester.create_and_test_config_to_json_file()
							self.config_tester.create_and_test_config_from_and_save_pretrained()
							self.config_tester.create_and_test_config_with_num_labels()
							self.config_tester.check_config_can_be_init_without_params()
							self.config_tester.check_config_arguments_init()






						def UpperCAmelCase_				(      self      :      Optional[Any]	)							->    str:







							"""simple docstring"""


							return






						@unittest.skip(reason='ResNet does not use inputs_embeds'	)
						def UpperCAmelCase_				(      self      :      Optional[Any]	)							->    Tuple:







							"""simple docstring"""


							pass






						@unittest.skip(reason='ResNet does not support input and output embeddings'	)
						def UpperCAmelCase_				(      self      :      List[Any]	)							->    Union[str, Any]:







							"""simple docstring"""


							pass






						def UpperCAmelCase_				(      self      :      List[Any]	)							->    str:







							"""simple docstring"""


							snake_case_						,snake_case_		:			List[str]											=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_		:			List[str]											=	model_class(A__	)
								snake_case_		:			Optional[Any]											=	inspect.signature(model.call	)
								# signature.parameters is an OrderedDict => so arg_names order is deterministic
								snake_case_		:			str											=	[*signature.parameters.keys()]

								snake_case_		:			Union[str, Any]											=	['pixel_values']
								self.assertListEqual(arg_names[:1]		,   A__	)






						def UpperCAmelCase_				(      self      :      Dict	)							->    List[str]:







							"""simple docstring"""


							snake_case_		:			Tuple											=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*A__	)






						def UpperCAmelCase_				(      self      :      Tuple	)							->    Optional[Any]:







							"""simple docstring"""


							def check_hidden_states_output(_A      :      Dict		,   _A      :      Any		,   _A      :      str	):
								snake_case_		:			Any											=	model_class(A__	)
								snake_case_		:			int											=	model(**self._prepare_for_class(A__		,   A__	)	)

								snake_case_		:			str											=	outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

								snake_case_		:			Optional[int]											=	self.model_tester.num_stages
								self.assertEqual(len(A__	)		,   expected_num_stages + 1	)

								# ResNet's feature maps are of shape (batch_size, num_channels, height, width)
								self.assertListEqual(
								    list(hidden_states[0].shape[-2:]	)		,   [self.model_tester.image_size // 4, self.model_tester.image_size // 4]		,   )

							snake_case_						,snake_case_		:			Optional[Any]											=	self.model_tester.prepare_config_and_inputs_for_common()
							snake_case_		:			Optional[int]											=	['basic', 'bottleneck']
							for model_class in self.all_model_classes:
								for layer_type in layers_type:
									snake_case_		:			Union[str, Any]											=	layer_type
									snake_case_		:			Tuple											=	True
									check_hidden_states_output(A__		,   A__		,   A__	)

									# check that output_hidden_states also work using config
									del inputs_dict["output_hidden_states"]
									snake_case_		:			int											=	True

									check_hidden_states_output(A__		,   A__		,   A__	)






						def UpperCAmelCase_				(      self      :      Optional[Any]	)							->    int:







							"""simple docstring"""


							snake_case_		:			List[str]											=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*A__	)






						@slow
						def UpperCAmelCase_				(      self      :      Dict	)							->    List[Any]:







							"""simple docstring"""


							for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								snake_case_		:			List[Any]											=	TFResNetModel.from_pretrained(A__	)
								self.assertIsNotNone(A__	)


def 							SCREAMING_SNAKE_CASE__						(    ):
	snake_case_		:			Optional[Any]											=	Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'   )
	return image


@require_tf
@require_vision
class SCREAMING_SNAKE_CASE_						(					unittest.TestCase					):






						@cached_property
						def UpperCAmelCase_				(      self      :      List[str]	)							->    Any:







							"""simple docstring"""


							return (
							    AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)
							    if is_vision_available()
							    else None
							)






						@slow
						def UpperCAmelCase_				(      self      :      Optional[int]	)							->    str:







							"""simple docstring"""


							snake_case_		:			str											=	TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)

							snake_case_		:			Tuple											=	self.default_image_processor
							snake_case_		:			Optional[int]											=	prepare_img()
							snake_case_		:			Any											=	image_processor(images=A__		,   return_tensors='tf'	)

							# forward pass
							snake_case_		:			Optional[Any]											=	model(**A__	)

							# verify the logits
							snake_case_		:			List[str]											=	tf.TensorShape((1, 1000)	)
							self.assertEqual(outputs.logits.shape		,   A__	)

							snake_case_		:			Tuple											=	tf.constant([-1_1.1_0_6_9, -9.7_8_7_7, -8.3_7_7_7]	)

							self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()		,   A__		,   atol=1E-4	)	)








import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
						import jax

						from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel
class SCREAMING_SNAKE_CASE_						(					unittest.TestCase					):






						def __init__(      self      :      Dict		,   _A      :      List[str]		,   _A      :      Dict=13		,   _A      :      str=30		,   _A      :      Any=2		,   _A      :      Dict=3		,   _A      :      Optional[Any]=True		,   _A      :      Tuple=True		,   _A      :      List[str]=32		,   _A      :      int=5		,   _A      :      Optional[int]=4		,   _A      :      Dict=37		,   _A      :      Tuple="gelu"		,   _A      :      Dict=0.1		,   _A      :      str=0.1		,   _A      :      int=10		,   _A      :      Union[str, Any]=0.0_2		,   )							->    int:







							"""simple docstring"""


							snake_case_		:			int											=	parent
							snake_case_		:			str											=	batch_size
							snake_case_		:			List[str]											=	image_size
							snake_case_		:			Tuple											=	patch_size
							snake_case_		:			str											=	num_channels
							snake_case_		:			List[str]											=	is_training
							snake_case_		:			List[str]											=	use_labels
							snake_case_		:			str											=	hidden_size
							snake_case_		:			Union[str, Any]											=	num_hidden_layers
							snake_case_		:			Any											=	num_attention_heads
							snake_case_		:			List[Any]											=	intermediate_size
							snake_case_		:			Any											=	hidden_act
							snake_case_		:			List[Any]											=	hidden_dropout_prob
							snake_case_		:			List[Any]											=	attention_probs_dropout_prob
							snake_case_		:			Tuple											=	type_sequence_label_size
							snake_case_		:			Any											=	initializer_range

							# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
							snake_case_		:			Tuple											=	(image_size // patch_size) ** 2
							snake_case_		:			List[Any]											=	num_patches + 1






						def UpperCAmelCase_				(      self      :      Dict	)							->    List[str]:







							"""simple docstring"""


							snake_case_		:			int											=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

							snake_case_		:			Optional[Any]											=	ViTConfig(
							    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=_A		,   initializer_range=self.initializer_range		,   )

							return config, pixel_values






						def UpperCAmelCase_				(      self      :      int		,   _A      :      Union[str, Any]		,   _A      :      List[Any]	)							->    Dict:







							"""simple docstring"""


							snake_case_		:			Union[str, Any]											=	FlaxViTModel(config=_A	)
							snake_case_		:			Optional[Any]											=	model(_A	)
							# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
							snake_case_		:			Union[str, Any]											=	(self.image_size, self.image_size)
							snake_case_		:			int											=	(self.patch_size, self.patch_size)
							snake_case_		:			Optional[Any]											=	(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
							self.parent.assertEqual(result.last_hidden_state.shape		,   (self.batch_size, num_patches + 1, self.hidden_size)	)






						def UpperCAmelCase_				(      self      :      str		,   _A      :      Any		,   _A      :      List[str]	)							->    Dict:







							"""simple docstring"""


							snake_case_		:			Dict											=	self.type_sequence_label_size
							snake_case_		:			Any											=	FlaxViTForImageClassification(config=_A	)
							snake_case_		:			Optional[int]											=	model(_A	)
							self.parent.assertEqual(result.logits.shape		,   (self.batch_size, self.type_sequence_label_size)	)

							# test greyscale images
							snake_case_		:			List[str]											=	1
							snake_case_		:			Dict											=	FlaxViTForImageClassification(_A	)

							snake_case_		:			Optional[int]											=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
							snake_case_		:			Any											=	model(_A	)






						def UpperCAmelCase_				(      self      :      Any	)							->    Any:







							"""simple docstring"""


							snake_case_		:			Tuple											=	self.prepare_config_and_inputs()
							(
							    (
							    snake_case_
							)						,(
							    snake_case_
							)						,
							)		:			Any											=	config_and_inputs
							snake_case_		:			Tuple											=	{'pixel_values': pixel_values}
							return config, inputs_dict
@require_flax
class SCREAMING_SNAKE_CASE_						(					snake_case_       ,				unittest.TestCase					):
						__magic_name__:			Tuple       =       (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()






						def UpperCAmelCase_				(      self      :      List[str]	)							->    None:







							"""simple docstring"""


							snake_case_		:			Union[str, Any]											=	FlaxViTModelTester(self	)
							snake_case_		:			Any											=	ConfigTester(self		,   config_class=_A		,   has_text_modality=_A		,   hidden_size=37	)






						def UpperCAmelCase_				(      self      :      Any	)							->    Dict:







							"""simple docstring"""


							self.config_tester.run_common_tests()






						def UpperCAmelCase_				(      self      :      Any	)							->    Optional[Any]:







							"""simple docstring"""


							snake_case_		:			str											=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_A	)






						def UpperCAmelCase_				(      self      :      List[Any]	)							->    Union[str, Any]:







							"""simple docstring"""


							snake_case_		:			Tuple											=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*_A	)






						def UpperCAmelCase_				(      self      :      int	)							->    List[str]:







							"""simple docstring"""


							snake_case_						,snake_case_		:			Tuple											=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_		:			str											=	model_class(_A	)
								snake_case_		:			Dict											=	inspect.signature(model.__call__	)
								# signature.parameters is an OrderedDict => so arg_names order is deterministic
								snake_case_		:			Optional[Any]											=	[*signature.parameters.keys()]

								snake_case_		:			Dict											=	['pixel_values']
								self.assertListEqual(arg_names[:1]		,   _A	)






						def UpperCAmelCase_				(      self      :      Optional[Any]	)							->    str:







							"""simple docstring"""


							snake_case_						,snake_case_		:			Optional[int]											=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								with self.subTest(model_class.__name__	):
									snake_case_		:			Dict											=	self._prepare_for_class(_A		,   _A	)
									snake_case_		:			Tuple											=	model_class(_A	)

									@jax.jit
									def model_jitted(_A      :      Optional[int]		,   **_A      :      Any	):
										return model(pixel_values=_A		,   **_A	)

									with self.subTest('JIT Enabled'	):
										snake_case_		:			Optional[Any]											=	model_jitted(**_A	).to_tuple()

									with self.subTest('JIT Disabled'	):
										with jax.disable_jit():
											snake_case_		:			Optional[Any]											=	model_jitted(**_A	).to_tuple()

									self.assertEqual(len(_A	)		,   len(_A	)	)
									for jitted_output, output in zip(_A		,   _A	):
										self.assertEqual(jitted_output.shape		,   output.shape	)






						@slow
						def UpperCAmelCase_				(      self      :      List[str]	)							->    Any:







							"""simple docstring"""


							for model_class_name in self.all_model_classes:
								snake_case_		:			List[Any]											=	model_class_name.from_pretrained('google/vit-base-patch16-224'	)
								snake_case_		:			Optional[Any]											=	model(np.ones((1, 3, 224, 224)	)	)
								self.assertIsNotNone(_A	)

