

"""simple docstring"""




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      _UpperCAmelCase   :



	def __init__(  self     :       List[Any]			,    _lowercase     :       List[Any]			,    _lowercase     :       Optional[int]=3			,    _lowercase     :       str=32			,    _lowercase     :       List[str]=3			,    _lowercase     :       str=10			,    _lowercase     :       Optional[int]=[10, 20, 30, 40]			,    _lowercase     :       Optional[Any]=[1, 1, 2, 1]			,    _lowercase     :       Optional[Any]=True			,    _lowercase     :       List[str]=True			,    _lowercase     :       Union[str, Any]="relu"			,    _lowercase     :       List[Any]=3			,    _lowercase     :       Tuple=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(_lowercase						)



	def        a  (  self     :       Any						):
		__UpperCAmelCase      		= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

		__UpperCAmelCase      		= None
		if self.use_labels:
			__UpperCAmelCase      		= ids_tensor([self.batch_size]			,    self.num_labels						)

		__UpperCAmelCase      		= self.get_config()

		return config, pixel_values, labels



	def        a  (  self     :       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        a  (  self     :       Optional[int]			,    _lowercase     :       Any			,    _lowercase     :       Optional[int]			,    _lowercase     :       List[Any]						):
		__UpperCAmelCase      		= TFResNetModel(config=_lowercase						)
		__UpperCAmelCase      		= model(_lowercase						)
		# 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        a  (  self     :       Any			,    _lowercase     :       List[str]			,    _lowercase     :       Optional[Any]			,    _lowercase     :       Tuple						):
		__UpperCAmelCase      		= self.num_labels
		__UpperCAmelCase      		= TFResNetForImageClassification(_lowercase						)
		__UpperCAmelCase      		= model(_lowercase			,    labels=_lowercase						)
		self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_labels)						)



	def        a  (  self     :       int						):
		__UpperCAmelCase      		= self.prepare_config_and_inputs()
		__UpperCAmelCase		,							__UpperCAmelCase		,							__UpperCAmelCase      		= config_and_inputs
		__UpperCAmelCase      		= {'''pixel_values''': pixel_values}
		return config, inputs_dict






@require_tf
class      _UpperCAmelCase   (							_lowerCAmelCase		,    _lowerCAmelCase		,    unittest.TestCase       ):
	a__		: str					=					(TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
	a__		: Optional[Any]					=					(
	    {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification}
	    if is_tf_available()
	    else {}
	)

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



	def        a  (  self     :       str						):
		__UpperCAmelCase      		= TFResNetModelTester(self						)
		__UpperCAmelCase      		= ConfigTester(self			,    config_class=_lowercase			,    has_text_modality=_lowercase						)



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



	@unittest.skip(reason='''ResNet does not use inputs_embeds'''						)
	def        a  (  self     :       Union[str, Any]						):
		pass



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



	def        a  (  self     :       Any						):
		__UpperCAmelCase		,							__UpperCAmelCase      		= self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			__UpperCAmelCase      		= model_class(_lowercase						)
			__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]			,    _lowercase						)



	def        a  (  self     :       Union[str, Any]						):
		__UpperCAmelCase      		= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*_lowercase						)



	def        a  (  self     :       Optional[Any]						):
		def check_hidden_states_output(_lowercase     :       str			,    _lowercase     :       Optional[Any]			,    _lowercase     :       int						):
			__UpperCAmelCase      		= model_class(_lowercase						)
			__UpperCAmelCase      		= model(**self._prepare_for_class(_lowercase			,    _lowercase						)						)

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

			__UpperCAmelCase      		= self.model_tester.num_stages
			self.assertEqual(len(_lowercase						)			,    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]			,    )

		__UpperCAmelCase		,							__UpperCAmelCase      		= self.model_tester.prepare_config_and_inputs_for_common()
		__UpperCAmelCase      		= ['''basic''', '''bottleneck''']
		for model_class in self.all_model_classes:
			for layer_type in layers_type:
				__UpperCAmelCase      		= layer_type
				__UpperCAmelCase      		= True
				check_hidden_states_output(_lowercase			,    _lowercase			,    _lowercase						)

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

				check_hidden_states_output(_lowercase			,    _lowercase			,    _lowercase						)



	def        a  (  self     :       int						):
		__UpperCAmelCase      		= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_image_classification(*_lowercase						)



	@slow
	def        a  (  self     :       Optional[Any]						):
		for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			__UpperCAmelCase      		= TFResNetModel.from_pretrained(_lowercase						)
			self.assertIsNotNone(_lowercase						)






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




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



	@cached_property
	def        a  (  self     :       Optional[Any]						):
		return (
		    AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)
		    if is_vision_available()
		    else None
		)



	@slow
	def        a  (  self     :       Tuple						):
		__UpperCAmelCase      		= TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)

		__UpperCAmelCase      		= self.default_image_processor
		__UpperCAmelCase      		= prepare_img()
		__UpperCAmelCase      		= image_processor(images=_lowercase			,    return_tensors='''tf'''						)

		# forward pass
		__UpperCAmelCase      		= model(**_lowercase						)

		# verify the logits
		__UpperCAmelCase      		= tf.TensorShape((1, 10_00)						)
		self.assertEqual(outputs.logits.shape			,    _lowercase						)

		__UpperCAmelCase      		= tf.constant([-11.1_069, -9.7_877, -8.3_777]						)

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







def       SCREAMING_SNAKE_CASE__  (       _lowercase     : int   ,    _lowercase     : int			)   ->							Optional[int]:




		'''simple docstring'''
		if b == 0:
				return 1
		if (b % 2) == 0:
				return actual_power(_lowercase   ,    int(b / 2			)			) * actual_power(_lowercase   ,    int(b / 2			)			)
		else:
				return a * actual_power(_lowercase   ,    int(b / 2			)			) * actual_power(_lowercase   ,    int(b / 2			)			)


def       SCREAMING_SNAKE_CASE__  (       _lowercase     : int   ,    _lowercase     : int			)   ->							float:




		'''simple docstring'''
		if b < 0:
				return 1 / actual_power(_lowercase   ,    _lowercase			)
		return actual_power(_lowercase   ,    _lowercase			)


if __name__ == "__main__":
			print(power(-2, -3))


