


'''simple docstring'''

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       a__				( unittest.TestCase      ):







				"""simple docstring"""



				def __init__(self       ,			__lowercase       ,			__lowercase=3       ,			__lowercase=32       ,			__lowercase=3       ,			__lowercase=10       ,			__lowercase=[10, 20, 30, 40]       ,			__lowercase=[1, 1, 2, 1]       ,			__lowercase=True       ,			__lowercase=True       ,			__lowercase="relu"       ,			__lowercase=3       ,			__lowercase=None       ,			):
										__lowerCAmelCase							       =				parent
										__lowerCAmelCase							       =				batch_size
										__lowerCAmelCase							       =				image_size
										__lowerCAmelCase							       =				num_channels
										__lowerCAmelCase							       =				embeddings_size
										__lowerCAmelCase							       =				hidden_sizes
										__lowerCAmelCase							       =				depths
										__lowerCAmelCase							       =				is_training
										__lowerCAmelCase							       =				use_labels
										__lowerCAmelCase							       =				hidden_act
										__lowerCAmelCase							       =				num_labels
										__lowerCAmelCase							       =				scope
										__lowerCAmelCase							       =				len(__lowercase		)




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

										__lowerCAmelCase							       =				self.get_config()

										return config, pixel_values




				def 					_snake_case  (self		):
										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 					_snake_case  (self       ,			__lowercase       ,			__lowercase		):
										__lowerCAmelCase							       =				FlaxRegNetModel(config=__lowercase		)
										__lowerCAmelCase							       =				model(__lowercase		)

										# 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 					_snake_case  (self       ,			__lowercase       ,			__lowercase		):
										__lowerCAmelCase							       =				self.num_labels
										__lowerCAmelCase							       =				FlaxRegNetForImageClassification(config=__lowercase		)
										__lowerCAmelCase							       =				model(__lowercase		)
										self.parent.assertEqual(result.logits.shape       ,			(self.batch_size, self.num_labels)		)




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				self.prepare_config_and_inputs()
										__lowerCAmelCase       ,					__lowerCAmelCase							       =				config_and_inputs
										__lowerCAmelCase							       =				{'''pixel_values''': pixel_values}
										return config, inputs_dict


@require_flax
class       a__				( __A							,						unittest.TestCase      ):







				"""simple docstring"""



				__UpperCamelCase    :		Any	=	(FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

				__UpperCamelCase    :		Tuple	=	False
				__UpperCamelCase    :		Tuple	=	False
				__UpperCamelCase    :		Dict	=	False




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				FlaxRegNetModelTester(self		)
										__lowerCAmelCase							       =				ConfigTester(self       ,			config_class=__lowercase       ,			has_text_modality=__lowercase		)




				def 					_snake_case  (self		):
										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 					_snake_case  (self		):
										return




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__lowercase		)




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*__lowercase		)




				@unittest.skip(reason='''RegNet does not use inputs_embeds'''		)
				def 					_snake_case  (self		):
										pass




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




				def 					_snake_case  (self		):
										__lowerCAmelCase       ,					__lowerCAmelCase							       =				self.model_tester.prepare_config_and_inputs_for_common()

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

																__lowerCAmelCase							       =				['''pixel_values''']
																self.assertListEqual(arg_names[:1]       ,			__lowercase		)




				def 					_snake_case  (self		):
										def check_hidden_states_output(__lowercase       ,			__lowercase       ,			__lowercase		):
																__lowerCAmelCase							       =				model_class(__lowercase		)

																__lowerCAmelCase							       =				model(**self._prepare_for_class(__lowercase       ,			__lowercase		)		)

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

																__lowerCAmelCase							       =				self.model_tester.num_stages
																self.assertEqual(len(__lowercase		)       ,			expected_num_stages + 1		)

										__lowerCAmelCase       ,					__lowerCAmelCase							       =				self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__lowerCAmelCase							       =				True
																check_hidden_states_output(__lowercase       ,			__lowercase       ,			__lowercase		)

																# check that output_hidden_states also work using config
																del inputs_dict["output_hidden_states"]
																__lowerCAmelCase							       =				True

																check_hidden_states_output(__lowercase       ,			__lowercase       ,			__lowercase		)




				def 					_snake_case  (self		):
										__lowerCAmelCase       ,					__lowerCAmelCase							       =				self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																with self.subTest(model_class.__name__		):
																						__lowerCAmelCase							       =				self._prepare_for_class(__lowercase       ,			__lowercase		)
																						__lowerCAmelCase							       =				model_class(__lowercase		)

																						@jax.jit
																						def model_jitted(__lowercase       ,			**__lowercase		):
																												return model(pixel_values=__lowercase       ,			**__lowercase		)

																						with self.subTest('''JIT Enabled'''		):
																												__lowerCAmelCase							       =				model_jitted(**__lowercase		).to_tuple()

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

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

def 							__magic_name__(						):
						__lowerCAmelCase							       =				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
						return image




@require_flax
class       a__				( unittest.TestCase      ):







				"""simple docstring"""



				@cached_property
				def 					_snake_case  (self		):
										return AutoImageProcessor.from_pretrained('''facebook/regnet-y-040'''		) if is_vision_available() else None




				@slow
				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				FlaxRegNetForImageClassification.from_pretrained('''facebook/regnet-y-040'''		)

										__lowerCAmelCase							       =				self.default_image_processor
										__lowerCAmelCase							       =				prepare_img()
										__lowerCAmelCase							       =				image_processor(images=__lowercase       ,			return_tensors='''np'''		)

										__lowerCAmelCase							       =				model(**__lowercase		)

										# verify the logits
										__lowerCAmelCase							       =				(1, 10_00)
										self.assertEqual(outputs.logits.shape       ,			__lowercase		)

										__lowerCAmelCase							       =				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]       ,			__lowercase       ,			atol=1e-4		)		)





'''simple docstring'''

import numpy as np

from transformers import Pipeline

def 							__magic_name__(						lowerCamelCase):
						__lowerCAmelCase							       =				np.max(lowerCamelCase,				axis=-1,				keepdims=lowerCamelCase)
						__lowerCAmelCase							       =				np.exp(outputs - maxes)
						return shifted_exp / shifted_exp.sum(axis=-1,				keepdims=lowerCamelCase)




class       a__				( __A      ):







				"""simple docstring"""



				def 					_snake_case  (self       ,			**__lowercase		):
										__lowerCAmelCase							       =				{}
										if "second_text" in kwargs:
																__lowerCAmelCase							       =				kwargs['''second_text''']
										return preprocess_kwargs, {}, {}




				def 					_snake_case  (self       ,			__lowercase       ,			__lowercase=None		):
										return self.tokenizer(__lowercase       ,			text_pair=__lowercase       ,			return_tensors=self.framework		)




				def 					_snake_case  (self       ,			__lowercase		):
										return self.model(**__lowercase		)




				def 					_snake_case  (self       ,			__lowercase		):
										__lowerCAmelCase							       =				model_outputs.logits[0].numpy()
										__lowerCAmelCase							       =				softmax(__lowercase		)

										__lowerCAmelCase							       =				np.argmax(__lowercase		)
										__lowerCAmelCase							       =				self.model.config.idalabel[best_class]
										__lowerCAmelCase							       =				probabilities[best_class].item()
										__lowerCAmelCase							       =				logits.tolist()
										return {"label": label, "score": score, "logits": logits}

