





import inspect
import unittest

from transformers import RegNetConfig, 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.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
						from PIL import Image

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






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







							"""simple docstring"""


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






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







							"""simple docstring"""


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

							snake_case_						:	Any      =		self.get_config()

							return config, pixel_values






						def UpperCAmelCase_ (					self					:					int		)							->			Tuple:







							"""simple docstring"""


							return RegNetConfig(
							    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[str]						,      _A					:					Union[str, Any]						,      _A					:					Any		)							->			Dict:







							"""simple docstring"""


							snake_case_						:	Optional[Any]      =		FlaxRegNetModel(config=__lowerCamelCase		)
							snake_case_						:	Any      =		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 UpperCAmelCase_ (					self					:					Tuple						,      _A					:					Tuple						,      _A					:					List[Any]		)							->			Optional[int]:







							"""simple docstring"""


							snake_case_						:	Optional[Any]      =		self.num_labels
							snake_case_						:	List[str]      =		FlaxRegNetForImageClassification(config=__lowerCamelCase		)
							snake_case_						:	Tuple      =		model(__lowerCamelCase		)
							self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.num_labels)		)






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







							"""simple docstring"""


							snake_case_						:	Union[str, Any]      =		self.prepare_config_and_inputs()
							snake_case_  ,snake_case_						:	Optional[int]      =		config_and_inputs
							snake_case_						:	List[Any]      =		{'pixel_values': pixel_values}
							return config, inputs_dict
@require_flax
class SCREAMING_SNAKE_CASE_  (    _UpperCamelCase				, unittest.TestCase    ):
						__magic_name__:	Tuple       					=				(FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

						__magic_name__:	str       					=				False
						__magic_name__:	Optional[Any]       					=				False
						__magic_name__:	List[str]       					=				False






						def UpperCAmelCase_ (					self					:					Any		)							->			None:







							"""simple docstring"""


							snake_case_						:	Optional[Any]      =		FlaxRegNetModelTester(self		)
							snake_case_						:	Optional[int]      =		ConfigTester(self						,      config_class=__lowerCamelCase						,      has_text_modality=__lowerCamelCase		)






						def UpperCAmelCase_ (					self					:					int		)							->			Dict:







							"""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					:					str		)							->			Optional[Any]:







							"""simple docstring"""


							return






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







							"""simple docstring"""


							snake_case_						:	Optional[int]      =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowerCamelCase		)






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







							"""simple docstring"""


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






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







							"""simple docstring"""


							pass






						@unittest.skip(reason='RegNet does not support input and output embeddings'		)
						def UpperCAmelCase_ (					self					:					Tuple		)							->			Dict:







							"""simple docstring"""


							pass






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







							"""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(__lowerCamelCase		)
								snake_case_						:	Any      =		inspect.signature(model.__call__		)
								# signature.parameters is an OrderedDict => so arg_names order is deterministic
								snake_case_						:	int      =		[*signature.parameters.keys()]

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






						def UpperCAmelCase_ (					self					:					Dict		)							->			Tuple:







							"""simple docstring"""


							def check_hidden_states_output(_A					:					Dict						,      _A					:					List[str]						,      _A					:					List[str]		):
								snake_case_						:	List[str]      =		model_class(__lowerCamelCase		)

								snake_case_						:	int      =		model(**self._prepare_for_class(__lowerCamelCase						,      __lowerCamelCase		)		)

								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(__lowerCamelCase		)						,      expected_num_stages + 1		)

							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_						:	Optional[int]      =		True
								check_hidden_states_output(__lowerCamelCase						,      __lowerCamelCase						,      __lowerCamelCase		)

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

								check_hidden_states_output(__lowerCamelCase						,      __lowerCamelCase						,      __lowerCamelCase		)






						def UpperCAmelCase_ (					self					:					Dict		)							->			str:







							"""simple docstring"""


							snake_case_  ,snake_case_						:	str      =		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(__lowerCamelCase						,      __lowerCamelCase		)
									snake_case_						:	Union[str, Any]      =		model_class(__lowerCamelCase		)

									@jax.jit
									def model_jitted(_A					:					List[str]						,      **_A					:					List[Any]		):
										return model(pixel_values=__lowerCamelCase						,      **__lowerCamelCase		)

									with self.subTest('JIT Enabled'		):
										snake_case_						:	Any      =		model_jitted(**__lowerCamelCase		).to_tuple()

									with self.subTest('JIT Disabled'		):
										with jax.disable_jit():
											snake_case_						:	Dict      =		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 							SCREAMING_SNAKE_CASE__  (			):
	snake_case_						:	Optional[int]      =		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      )
	return image


@require_flax
class SCREAMING_SNAKE_CASE_  (    unittest.TestCase    ):






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







							"""simple docstring"""


							return AutoImageProcessor.from_pretrained('facebook/regnet-y-040'		) if is_vision_available() else None






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







							"""simple docstring"""


							snake_case_						:	Optional[Any]      =		FlaxRegNetForImageClassification.from_pretrained('facebook/regnet-y-040'		)

							snake_case_						:	List[Any]      =		self.default_image_processor
							snake_case_						:	List[str]      =		prepare_img()
							snake_case_						:	Dict      =		image_processor(images=__lowerCamelCase						,      return_tensors='np'		)

							snake_case_						:	Dict      =		model(**__lowerCamelCase		)

							# verify the logits
							snake_case_						:	Optional[int]      =		(1, 1000)
							self.assertEqual(outputs.logits.shape						,      __lowerCamelCase		)

							snake_case_						:	List[str]      =		jnp.array([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6]		)

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







import warnings

from diffusers import StableDiffusionImgaImgPipeline  # noqa F401


warnings.warn(
    'The `image_to_image.py` script is outdated. Please use directly `from diffusers import'
    ' StableDiffusionImg2ImgPipeline` instead.'
)






