


"""simple docstring"""







import inspect
import unittest

from transformers import ResNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

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


if is_flax_available():
						import jax
						import jax.numpy as jnp

						from transformers.models.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor
class 							lowerCamelCase       (			unittest.TestCase ):






							def __init__(						self :     Dict		, lowerCamelCase_ :     Dict		, lowerCamelCase_ :     Dict=3		, lowerCamelCase_ :     Optional[Any]=32		, lowerCamelCase_ :     Optional[int]=3		, lowerCamelCase_ :     Union[str, Any]=10		, lowerCamelCase_ :     Union[str, Any]=[10, 20, 30, 40]		, lowerCamelCase_ :     List[str]=[1, 1, 2, 1]		, lowerCamelCase_ :     List[str]=True		, lowerCamelCase_ :     List[str]=True		, lowerCamelCase_ :     List[str]="relu"		, lowerCamelCase_ :     int=3		, lowerCamelCase_ :     Union[str, Any]=None		, ):
													UpperCAmelCase__		=    parent
													UpperCAmelCase__		=    batch_size
													UpperCAmelCase__		=    image_size
													UpperCAmelCase__		=    num_channels
													UpperCAmelCase__		=    embeddings_size
													UpperCAmelCase__		=    hidden_sizes
													UpperCAmelCase__		=    depths
													UpperCAmelCase__		=    is_training
													UpperCAmelCase__		=    use_labels
													UpperCAmelCase__		=    hidden_act
													UpperCAmelCase__		=    num_labels
													UpperCAmelCase__		=    scope
													UpperCAmelCase__		=    len(lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     Optional[Any]				):
													UpperCAmelCase__		=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

													UpperCAmelCase__		=    self.get_config()

													return config, pixel_values






							def 					lowerCAmelCase     (						self :     Optional[Any]				):
													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 					lowerCAmelCase     (						self :     str		, lowerCamelCase_ :     Optional[int]		, lowerCamelCase_ :     Union[str, Any]				):
													UpperCAmelCase__		=    FlaxResNetModel(config=lowerCamelCase_				)
													UpperCAmelCase__		=    model(lowerCamelCase_				)

													# Output shape (b, c, h, w)
													self.parent.assertEqual(
													    result.last_hidden_state.shape		, (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)		, )






							def 					lowerCAmelCase     (						self :     List[Any]		, lowerCamelCase_ :     str		, lowerCamelCase_ :     str				):
													UpperCAmelCase__		=    self.num_labels
													UpperCAmelCase__		=    FlaxResNetForImageClassification(config=lowerCamelCase_				)
													UpperCAmelCase__		=    model(lowerCamelCase_				)
													self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.num_labels)				)






							def 					lowerCAmelCase     (						self :     Dict				):
													UpperCAmelCase__		=    self.prepare_config_and_inputs()
													UpperCAmelCase__	,				UpperCAmelCase__		=    config_and_inputs
													UpperCAmelCase__		=    {'''pixel_values''': pixel_values}
													return config, inputs_dict




@require_flax
class 							lowerCamelCase       (			__SCREAMING_SNAKE_CASE				,					unittest.TestCase ):
							SCREAMING_SNAKE_CASE										=      (FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

							SCREAMING_SNAKE_CASE										=      False
							SCREAMING_SNAKE_CASE										=      False
							SCREAMING_SNAKE_CASE										=      False






							def 					lowerCAmelCase     (						self :     Tuple				):
													UpperCAmelCase__		=    FlaxResNetModelTester(self				)
													UpperCAmelCase__		=    ConfigTester(self		, config_class=lowerCamelCase_		, has_text_modality=lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     List[str]				):
													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 					lowerCAmelCase     (						self :     List[str]				):
													return






							def 					lowerCAmelCase     (						self :     Dict				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     int				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_				)






							@unittest.skip(reason='''ResNet does not use inputs_embeds'''				)
							def 					lowerCAmelCase     (						self :     List[Any]				):
													pass






							@unittest.skip(reason='''ResNet does not support input and output embeddings'''				)
							def 					lowerCAmelCase     (						self :     Tuple				):
													pass






							def 					lowerCAmelCase     (						self :     Tuple				):
													UpperCAmelCase__	,				UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			UpperCAmelCase__		=    model_class(lowerCamelCase_				)
																			UpperCAmelCase__		=    inspect.signature(model.__call__				)
																			# signature.parameters is an OrderedDict => so arg_names order is deterministic
																			UpperCAmelCase__		=    [*signature.parameters.keys()]

																			UpperCAmelCase__		=    ['''pixel_values''']
																			self.assertListEqual(arg_names[:1]		, lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     Optional[Any]				):
													def check_hidden_states_output(lowerCamelCase_ :     Optional[int]		, lowerCamelCase_ :     str		, lowerCamelCase_ :     List[str]				):
																			UpperCAmelCase__		=    model_class(lowerCamelCase_				)

																			UpperCAmelCase__		=    model(**self._prepare_for_class(lowerCamelCase_		, lowerCamelCase_				)				)

																			UpperCAmelCase__		=    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																			UpperCAmelCase__		=    self.model_tester.num_stages
																			self.assertEqual(len(lowerCamelCase_				)		, expected_num_stages + 1				)






							@unittest.skip(reason='''ResNet does not use feedforward chunking'''				)
							def 					lowerCAmelCase     (						self :     int				):
													pass






							def 					lowerCAmelCase     (						self :     Dict				):
													UpperCAmelCase__	,				UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			with self.subTest(model_class.__name__				):
																									UpperCAmelCase__		=    self._prepare_for_class(lowerCamelCase_		, lowerCamelCase_				)
																									UpperCAmelCase__		=    model_class(lowerCamelCase_				)

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

																									with self.subTest('''JIT Enabled'''				):
																															UpperCAmelCase__		=    model_jitted(**lowerCamelCase_				).to_tuple()

																									with self.subTest('''JIT Disabled'''				):
																															with jax.disable_jit():
																																					UpperCAmelCase__		=    model_jitted(**lowerCamelCase_				).to_tuple()

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






def 					a__ (       ):





						"""simple docstring"""

						UpperCAmelCase__		=    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
						return image


@require_flax
class 							lowerCamelCase       (			unittest.TestCase ):






							@cached_property
							def 					lowerCAmelCase     (						self :     int				):
													return AutoImageProcessor.from_pretrained('''microsoft/resnet-50'''				) if is_vision_available() else None






							@slow
							def 					lowerCAmelCase     (						self :     Union[str, Any]				):
													UpperCAmelCase__		=    FlaxResNetForImageClassification.from_pretrained('''microsoft/resnet-50'''				)

													UpperCAmelCase__		=    self.default_image_processor
													UpperCAmelCase__		=    prepare_img()
													UpperCAmelCase__		=    image_processor(images=lowerCamelCase_		, return_tensors='''np'''				)

													UpperCAmelCase__		=    model(**lowerCamelCase_				)

													# verify the logits
													UpperCAmelCase__		=    (1, 1000)
													self.assertEqual(outputs.logits.shape		, lowerCamelCase_				)

													UpperCAmelCase__		=    jnp.array([-11.1069, -9.7877, -8.3777]				)

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



"""simple docstring"""







import safs
class 							lowerCamelCase       (			safs.SaFileSystem ):


							"""simple docstring"""