






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

							'''simple docstring'''





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

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

											snake_case__									=     self.get_config()

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


							def lowerCAmelCase_ (     self:      List[str]						)		->		Dict:
											snake_case__									=     self.prepare_config_and_inputs()
											snake_case__							,						snake_case__							,						snake_case__									=     config_and_inputs
											snake_case__									=     {'pixel_values': pixel_values}
											return config, inputs_dict


@require_tf
class   __SCREAMING_SNAKE_CASE(	a_    ,  a_    ,  unittest.TestCase	):

							'''simple docstring'''





							_UpperCAmelCase     =       (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
							_UpperCAmelCase     =       (
							    {"""feature-extraction""": TFResNetModel, """image-classification""": TFResNetForImageClassification}
							    if is_tf_available()
							    else {}
							)

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

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

															snake_case__									=     ['pixel_values']
															self.assertListEqual(arg_names[:1]       ,	UpperCamelCase						)
							def lowerCAmelCase_ (     self:      List[Any]						)		->		Union[str, Any]:
											snake_case__									=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*UpperCamelCase						)
							def lowerCAmelCase_ (     self:      List[str]						)		->		Dict:

											def check_hidden_states_output(UpperCamelCase:      List[Any]       ,	UpperCamelCase:      Optional[int]       ,	UpperCamelCase:      Optional[Any]						):
															snake_case__									=     model_class(UpperCamelCase						)
															snake_case__									=     model(**self._prepare_for_class(UpperCamelCase       ,	UpperCamelCase						)						)

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

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

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

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

																			check_hidden_states_output(UpperCamelCase       ,	UpperCamelCase       ,	UpperCamelCase						)
							def lowerCAmelCase_ (     self:      List[str]						)		->		Tuple:
											snake_case__									=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*UpperCamelCase						)


							@slow
							def lowerCAmelCase_ (     self:      Optional[int]						)		->		Dict:

											for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case__									=     TFResNetModel.from_pretrained(UpperCamelCase						)
															self.assertIsNotNone(UpperCamelCase						)


def      a_ (      )			->			Union[str, Any]:







				"""simple docstring"""
				snake_case__									=     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
				return image


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

							'''simple docstring'''





							@cached_property
							def lowerCAmelCase_ (     self:      str						)		->		Union[str, Any]:
											return (
											    AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)
											    if is_vision_available()
											    else None
											)


							@slow
							def lowerCAmelCase_ (     self:      str						)		->		Union[str, Any]:
											snake_case__									=     TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)

											snake_case__									=     self.default_image_processor
											snake_case__									=     prepare_img()
											snake_case__									=     image_processor(images=UpperCamelCase       ,	return_tensors='tf'						)

											# forward pass
											snake_case__									=     model(**UpperCamelCase						)

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

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

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












def      a_ (      _A      ,   _A  )			->			str:







				"""simple docstring"""
				if number < 0 or shift_amount < 0:
								raise ValueError('both inputs must be positive integers'  )

				snake_case__									=     str(bin(_A  )  )
				binary_number += "0" * shift_amount
				return binary_number
def      a_ (      _A      ,   _A  )			->			str:







				"""simple docstring"""
				if number < 0 or shift_amount < 0:
								raise ValueError('both inputs must be positive integers'  )

				snake_case__									=     str(bin(_A  )  )[2:]
				if shift_amount >= len(_A  ):
								return "0b0"
				snake_case__									=     binary_number[: len(_A  ) - shift_amount]
				return "0b" + shifted_binary_number
def      a_ (      _A      ,   _A  )			->			str:







				"""simple docstring"""
				if number >= 0:  # Get binary representation of positive number
								snake_case__									=     '0' + str(bin(_A  )  ).strip('-'  )[2:]
				else:  # Get binary (2's complement) representation of negative number
								snake_case__									=     len(bin(_A  )[3:]  )  # Find 2's complement of number
								snake_case__									=     bin(abs(_A  ) - (1 << binary_number_length)  )[3:]
								snake_case__									=     (
								    '1' + '0' * (binary_number_length - len(_A  )) + binary_number
								)

				if shift_amount >= len(_A  ):
								return "0b" + binary_number[0] * len(_A  )
				return (
				    "0b"
				    + binary_number[0] * shift_amount
				    + binary_number[: len(_A  ) - shift_amount]
				)


if __name__ == "__main__":
							import doctest

							doctest.testmod()




