



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






					'''simple docstring'''





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






					def        lowerCAmelCase_    (	self:							List[str]						)       ->     Any:
											snake_case_			:List[str]						=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

											snake_case_			:Union[str, Any]						=      self.get_config()

											return config, pixel_values






					def        lowerCAmelCase_    (	self:							Optional[int]						)       ->     List[str]:
											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:							Optional[int]						,					snake_case:							Tuple						,					snake_case:							Union[str, Any]						)       ->     Optional[int]:
											snake_case_			:Union[str, Any]						=      FlaxResNetModel(config=_lowerCamelCase						)
											snake_case_			:int						=      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:							Any						,					snake_case:							Optional[int]						,					snake_case:							int						)       ->     Tuple:
											snake_case_			:Tuple						=      self.num_labels
											snake_case_			:List[Any]						=      FlaxResNetForImageClassification(config=_lowerCamelCase						)
											snake_case_			:int						=      model(_lowerCamelCase						)
											self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.num_labels)						)






					def        lowerCAmelCase_    (	self:							List[Any]						)       ->     Optional[Any]:
											snake_case_			:Tuple						=      self.prepare_config_and_inputs()
											snake_case_,	snake_case_			:Tuple						=      config_and_inputs
											snake_case_			:Tuple						=      {"""pixel_values""": pixel_values}
											return config, inputs_dict

@require_flax
class     lowerCamelCase  (      _lowerCAmelCase   ,						unittest.TestCase    ):






					'''simple docstring'''





					_A		:      List[str]						=					(FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

					_A		:      str						=					False
					_A		:      Dict						=					False
					_A		:      Tuple						=					False






					def        lowerCAmelCase_    (	self:							int						)       ->     Optional[Any]:
											snake_case_			:List[str]						=      FlaxResNetModelTester(self						)
											snake_case_			:Any						=      ConfigTester(self						,					config_class=_lowerCamelCase						,					has_text_modality=_lowerCamelCase						)






					def        lowerCAmelCase_    (	self:							Union[str, Any]						)       ->     Tuple:
											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:							Optional[int]						)       ->     Dict:
											return






					def        lowerCAmelCase_    (	self:							int						)       ->     Any:
											snake_case_			:Optional[int]						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_lowerCamelCase						)






					def        lowerCAmelCase_    (	self:							List[Any]						)       ->     Any:
											snake_case_			:str						=      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:							str						)       ->     int:
											pass






					@unittest.skip(reason="""ResNet does not support input and output embeddings"""						)
					def        lowerCAmelCase_    (	self:							Optional[int]						)       ->     Optional[int]:
											pass






					def        lowerCAmelCase_    (	self:							Optional[int]						)       ->     Tuple:
											snake_case_,	snake_case_			:Optional[int]						=      self.model_tester.prepare_config_and_inputs_for_common()

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

																	snake_case_			:Dict						=      ["""pixel_values"""]
																	self.assertListEqual(arg_names[:1]						,					_lowerCamelCase						)






					def        lowerCAmelCase_    (	self:							Union[str, Any]						)       ->     List[Any]:
											def check_hidden_states_output(snake_case:							Dict						,					snake_case:							List[str]						,					snake_case:							Any						):
																	snake_case_			:Tuple						=      model_class(_lowerCamelCase						)

																	snake_case_			:Any						=      model(**self._prepare_for_class(_lowerCamelCase						,					_lowerCamelCase						)						)

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

																	snake_case_			:List[Any]						=      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:							List[Any]						)       ->     List[str]:
											pass






					def        lowerCAmelCase_    (	self:							str						)       ->     List[Any]:
											snake_case_,	snake_case_			:Tuple						=      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_			:Union[str, Any]						=      self._prepare_for_class(_lowerCamelCase						,					_lowerCamelCase						)
																							snake_case_			:Any						=      model_class(_lowerCamelCase						)

																							@jax.jit
																							def model_jitted(snake_case:							Optional[int]						,					**snake_case:							Tuple						):
																													return model(pixel_values=_lowerCamelCase						,					**_lowerCamelCase						)

																							with self.subTest("""JIT Enabled"""						):
																													snake_case_			:Union[str, Any]						=      model_jitted(**_lowerCamelCase						).to_tuple()

																							with self.subTest("""JIT Disabled"""						):
																													with jax.disable_jit():
																																			snake_case_			:Union[str, Any]						=      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_  (      ) ->    Union[str, Any]:







						'''simple docstring'''


						snake_case_			:List[str]						=      Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
						return image

@require_flax
class     lowerCamelCase  (      unittest.TestCase    ):






					'''simple docstring'''





					@cached_property
					def        lowerCAmelCase_    (	self:							List[str]						)       ->     List[Any]:
											return AutoImageProcessor.from_pretrained("""microsoft/resnet-50"""						) if is_vision_available() else None






					@slow
					def        lowerCAmelCase_    (	self:							Tuple						)       ->     Optional[int]:
											snake_case_			:Union[str, Any]						=      FlaxResNetForImageClassification.from_pretrained("""microsoft/resnet-50"""						)

											snake_case_			:Union[str, Any]						=      self.default_image_processor
											snake_case_			:Union[str, Any]						=      prepare_img()
											snake_case_			:str						=      image_processor(images=_lowerCamelCase						,					return_tensors="""np"""						)

											snake_case_			:Union[str, Any]						=      model(**_lowerCamelCase						)

											# verify the logits
											snake_case_			:Optional[Any]						=      (1, 1_000)
											self.assertEqual(outputs.logits.shape						,					_lowerCamelCase						)

											snake_case_			:str						=      jnp.array([-1_1.1_0_6_9, -9.7_8_7_7, -8.3_7_7_7]						)

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





"""simple docstring"""
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

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






					'''simple docstring'''





					def __init__(	self:							int						,					snake_case:							List[str]						,					snake_case:							List[str]=7						,					snake_case:							Union[str, Any]=3						,					snake_case:							List[str]=18						,					snake_case:							Any=30						,					snake_case:							Any=400						,					snake_case:							Any=True						,					snake_case:							List[str]=None						,					snake_case:							Union[str, Any]=True						,					snake_case:							int=[0.5, 0.5, 0.5]						,					snake_case:							Tuple=[0.5, 0.5, 0.5]						,					)       ->     int:
											snake_case_			:Dict						=      size if size is not None else {"""height""": 18, """width""": 18}
											snake_case_			:Optional[Any]						=      parent
											snake_case_			:Tuple						=      batch_size
											snake_case_			:Tuple						=      num_channels
											snake_case_			:Optional[int]						=      image_size
											snake_case_			:Tuple						=      min_resolution
											snake_case_			:str						=      max_resolution
											snake_case_			:Any						=      do_resize
											snake_case_			:Union[str, Any]						=      size
											snake_case_			:Tuple						=      do_normalize
											snake_case_			:int						=      image_mean
											snake_case_			:List[Any]						=      image_std






					def        lowerCAmelCase_    (	self:							List[Any]						)       ->     List[Any]:
											return {
											    "image_mean": self.image_mean,
											    "image_std": self.image_std,
											    "do_normalize": self.do_normalize,
											    "do_resize": self.do_resize,
											    "size": self.size,
											}

@require_torch
@require_vision
class     lowerCamelCase  (      _lowerCAmelCase   ,						unittest.TestCase    ):






					'''simple docstring'''





					_A		:      Tuple						=					ViTImageProcessor if is_vision_available() else None






					def        lowerCAmelCase_    (	self:							str						)       ->     Optional[Any]:
											snake_case_			:Dict						=      ViTImageProcessingTester(self						)






					@property
					def        lowerCAmelCase_    (	self:							Tuple						)       ->     Optional[Any]:
											return self.image_processor_tester.prepare_image_processor_dict()






					def        lowerCAmelCase_    (	self:							str						)       ->     List[str]:
											snake_case_			:List[str]						=      self.image_processing_class(**self.image_processor_dict						)
											self.assertTrue(hasattr(snake_case						,					"""image_mean"""						)						)
											self.assertTrue(hasattr(snake_case						,					"""image_std"""						)						)
											self.assertTrue(hasattr(snake_case						,					"""do_normalize"""						)						)
											self.assertTrue(hasattr(snake_case						,					"""do_resize"""						)						)
											self.assertTrue(hasattr(snake_case						,					"""size"""						)						)






					def        lowerCAmelCase_    (	self:							Dict						)       ->     Union[str, Any]:
											snake_case_			:int						=      self.image_processing_class.from_dict(self.image_processor_dict						)
											self.assertEqual(image_processor.size						,					{"""height""": 18, """width""": 18}						)

											snake_case_			:int						=      self.image_processing_class.from_dict(self.image_processor_dict						,					size=42						)
											self.assertEqual(image_processor.size						,					{"""height""": 42, """width""": 42}						)






					def        lowerCAmelCase_    (	self:							Tuple						)       ->     int:
											pass






					def        lowerCAmelCase_    (	self:							Dict						)       ->     Union[str, Any]:
											# Initialize image_processing
											snake_case_			:Any						=      self.image_processing_class(**self.image_processor_dict						)
											# create random PIL images
											snake_case_			:str						=      prepare_image_inputs(self.image_processor_tester						,					equal_resolution=snake_case						)
											for image in image_inputs:
																	self.assertIsInstance(snake_case						,					Image.Image						)

											# Test not batched input
											snake_case_			:List[str]						=      image_processing(image_inputs[0]						,					return_tensors="""pt"""						).pixel_values
											self.assertEqual(
											    encoded_images.shape						,					(
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.size["""height"""],
											        self.image_processor_tester.size["""width"""],
											    )						,					)

											# Test batched
											snake_case_			:Dict						=      image_processing(snake_case						,					return_tensors="""pt"""						).pixel_values
											self.assertEqual(
											    encoded_images.shape						,					(
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.size["""height"""],
											        self.image_processor_tester.size["""width"""],
											    )						,					)






					def        lowerCAmelCase_    (	self:							Optional[Any]						)       ->     Tuple:
											# Initialize image_processing
											snake_case_			:int						=      self.image_processing_class(**self.image_processor_dict						)
											# create random numpy tensors
											snake_case_			:str						=      prepare_image_inputs(self.image_processor_tester						,					equal_resolution=snake_case						,					numpify=snake_case						)
											for image in image_inputs:
																	self.assertIsInstance(snake_case						,					np.ndarray						)

											# Test not batched input
											snake_case_			:Any						=      image_processing(image_inputs[0]						,					return_tensors="""pt"""						).pixel_values
											self.assertEqual(
											    encoded_images.shape						,					(
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.size["""height"""],
											        self.image_processor_tester.size["""width"""],
											    )						,					)

											# Test batched
											snake_case_			:Any						=      image_processing(snake_case						,					return_tensors="""pt"""						).pixel_values
											self.assertEqual(
											    encoded_images.shape						,					(
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.size["""height"""],
											        self.image_processor_tester.size["""width"""],
											    )						,					)






					def        lowerCAmelCase_    (	self:							List[Any]						)       ->     Optional[int]:
											# Initialize image_processing
											snake_case_			:Union[str, Any]						=      self.image_processing_class(**self.image_processor_dict						)
											# create random PyTorch tensors
											snake_case_			:List[Any]						=      prepare_image_inputs(self.image_processor_tester						,					equal_resolution=snake_case						,					torchify=snake_case						)
											for image in image_inputs:
																	self.assertIsInstance(snake_case						,					torch.Tensor						)

											# Test not batched input
											snake_case_			:Optional[Any]						=      image_processing(image_inputs[0]						,					return_tensors="""pt"""						).pixel_values
											self.assertEqual(
											    encoded_images.shape						,					(
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.size["""height"""],
											        self.image_processor_tester.size["""width"""],
											    )						,					)

											# Test batched
											snake_case_			:List[Any]						=      image_processing(snake_case						,					return_tensors="""pt"""						).pixel_values
											self.assertEqual(
											    encoded_images.shape						,					(
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.size["""height"""],
											        self.image_processor_tester.size["""width"""],
											    )						,					)
