






'''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     A    :






	"""simple docstring"""





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

							UpperCamelCase_				:   Optional[Any]					    =							None
							if self.use_labels:
													UpperCamelCase_				:   List[Any]					    =							ids_tensor([self.batch_size]			,     self.num_labels )

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

							return config, pixel_values, labels
	def   _UpperCAmelCase       (							self ):
							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   _UpperCAmelCase       (							self			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase ):
							UpperCamelCase_				:   Optional[Any]					    =							TFResNetModel(config=_A )
							UpperCamelCase_				:   List[str]					    =							model(_A )
							# 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   _UpperCAmelCase       (							self			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase ):
							UpperCamelCase_				:   Any					    =							self.num_labels
							UpperCamelCase_				:   Dict					    =							TFResNetForImageClassification(_A )
							UpperCamelCase_				:   int					    =							model(_A			,     labels=_A )
							self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_labels) )





	def   _UpperCAmelCase       (							self ):
							UpperCamelCase_				:   Union[str, Any]					    =							self.prepare_config_and_inputs()
							UpperCamelCase_				:   Tuple					    =							config_and_inputs
							UpperCamelCase_				:   Any					    =							{'pixel_values': pixel_values}
							return config, inputs_dict






@require_tf
class     A    (      a__,   a__,   unittest.TestCase						):






	"""simple docstring"""





	__a			:						Optional[int]      =						(TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
	__a			:						Dict      =						(
	    {'''feature-extraction''': TFResNetModel, '''image-classification''': TFResNetForImageClassification}
	    if is_tf_available()
	    else {}
	)

	__a			:						Optional[Any]      =						False
	__a			:						Union[str, Any]      =						False
	__a			:						Any      =						False
	__a			:						List[Any]      =						False
	__a			:						Optional[Any]      =						False
	def   _UpperCAmelCase       (							self ):
							UpperCamelCase_				:   List[str]					    =							TFResNetModelTester(self )
							UpperCamelCase_				:   List[str]					    =							ConfigTester(self			,     config_class=_A			,     has_text_modality=_A )
	def   _UpperCAmelCase       (							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   _UpperCAmelCase       (							self ):
							return
	@unittest.skip(reason="""ResNet does not use inputs_embeds""" )
	def   _UpperCAmelCase       (							self ):
							pass
	@unittest.skip(reason="""ResNet does not support input and output embeddings""" )
	def   _UpperCAmelCase       (							self ):
							pass
	def   _UpperCAmelCase       (							self ):
							UpperCamelCase_				:   Optional[int]					    =							self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													UpperCamelCase_				:   int					    =							model_class(_A )
													UpperCamelCase_				:   Union[str, Any]					    =							inspect.signature(model.call )
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													UpperCamelCase_				:   Any					    =							[*signature.parameters.keys()]

													UpperCamelCase_				:   str					    =							['pixel_values']
													self.assertListEqual(arg_names[:1]			,     _A )
	def   _UpperCAmelCase       (							self ):
							UpperCamelCase_				:   Dict					    =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_A )
	def   _UpperCAmelCase       (							self ):

							def check_hidden_states_output(__lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase ):
													UpperCamelCase_				:   int					    =							model_class(_A )
													UpperCamelCase_				:   int					    =							model(**self._prepare_for_class(_A			,     _A ) )

													UpperCamelCase_				:   Dict					    =							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

													UpperCamelCase_				:   int					    =							self.model_tester.num_stages
													self.assertEqual(len(_A )			,     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_				:   List[str]					    =							self.model_tester.prepare_config_and_inputs_for_common()
							UpperCamelCase_				:   Any					    =							['basic', 'bottleneck']
							for model_class in self.all_model_classes:
													for layer_type in layers_type:
																			UpperCamelCase_				:   Optional[int]					    =							layer_type
																			UpperCamelCase_				:   Tuple					    =							True
																			check_hidden_states_output(_A			,     _A			,     _A )

																			# check that output_hidden_states also work using config
																			del inputs_dict["output_hidden_states"]
																			UpperCamelCase_				:   Any					    =							True

																			check_hidden_states_output(_A			,     _A			,     _A )
	def   _UpperCAmelCase       (							self ):
							UpperCamelCase_				:   str					    =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*_A )





	@slow
	def   _UpperCAmelCase       (							self ):

							for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													UpperCamelCase_				:   Optional[Any]					    =							TFResNetModel.from_pretrained(_A )
													self.assertIsNotNone(_A )






def 							snake_case					(     )       ->				List[str]:





						"""simple docstring"""

						UpperCamelCase_				:   Tuple					    =							Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""	)
						return image




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






	"""simple docstring"""





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





	@slow
	def   _UpperCAmelCase       (							self ):
							UpperCamelCase_				:   str					    =							TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] )

							UpperCamelCase_				:   Tuple					    =							self.default_image_processor
							UpperCamelCase_				:   Optional[Any]					    =							prepare_img()
							UpperCamelCase_				:   int					    =							image_processor(images=_A			,     return_tensors="""tf""" )

							# forward pass
							UpperCamelCase_				:   int					    =							model(**_A )

							# verify the logits
							UpperCamelCase_				:   Optional[Any]					    =							tf.TensorShape((1, 10_00) )
							self.assertEqual(outputs.logits.shape			,     _A )

							UpperCamelCase_				:   Any					    =							tf.constant([-11.10_69, -9.78_77, -8.37_77] )

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














'''simple docstring'''

def 							snake_case					(     a_							: int	)       ->				int:





      """simple docstring"""

      assert (
          isinstance(a_    ,   a_	) and number_of_steps > 0
      ), f"number_of_steps needs to be positive integer, your input {number_of_steps}"
      if number_of_steps == 1:
            return 1
      UpperCamelCase_    ,			UpperCamelCase_				:   str					    =							1, 1
      for _ in range(number_of_steps - 1	):
            UpperCamelCase_    ,			UpperCamelCase_				:   Tuple					    =							current + previous, current
      return current


if __name__ == "__main__":
      import doctest

      doctest.testmod()






