






from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import RegNetConfig
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 TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel


if is_vision_available():
			from PIL import Image

			from transformers import AutoImageProcessor
class  lowerCAmelCase_       :




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




							def 						__snake_case					(   self     :   int	):
											lowerCAmelCase__      			=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

											lowerCAmelCase__      			=						self.get_config()
											return config, pixel_values, labels




							def 						__snake_case					(   self     :   Union[str, Any]	):
											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 ,							)




							def 						__snake_case					(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Dict	):
											lowerCAmelCase__      			=						TFRegNetModel(config=SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_ ,							training=SCREAMING_SNAKE_CASE_	)
											# 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 						__snake_case					(   self     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   Any	):
											lowerCAmelCase__      			=						self.num_labels
											lowerCAmelCase__      			=						TFRegNetForImageClassification(SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_ ,							labels=SCREAMING_SNAKE_CASE_ ,							training=SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.num_labels)	)




							def 						__snake_case					(   self     :   Optional[Any]	):
											lowerCAmelCase__      			=						self.prepare_config_and_inputs()
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						config_and_inputs
											lowerCAmelCase__      			=						{'''pixel_values''': pixel_values}
											return config, inputs_dict



@require_tf
class  lowerCAmelCase_       (				snake_case__     ,    snake_case__     ,    unittest.TestCase					):
							UpperCamelCase_		:Optional[Any]		      =    (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
							UpperCamelCase_		:List[Any]		      =    (
							    {'feature-extraction': TFRegNetModel, 'image-classification': TFRegNetForImageClassification}
							    if is_tf_available()
							    else {}
							)

							UpperCamelCase_		:Any		      =    False
							UpperCamelCase_		:List[Any]		      =    False
							UpperCamelCase_		:List[Any]		      =    False
							UpperCamelCase_		:Optional[Any]		      =    False
							UpperCamelCase_		:Optional[int]		      =    False




							def 						__snake_case					(   self     :   Optional[int]	):
											lowerCAmelCase__      			=						TFRegNetModelTester(self	)
											lowerCAmelCase__      			=						ConfigTester(self ,							config_class=SCREAMING_SNAKE_CASE_ ,							has_text_modality=SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Tuple	):
											return




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




							@unittest.skipIf(
							    not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''	)	) == 0 ,							reason='''TF does not support backprop for grouped convolutions on CPU.''' ,							)
							@slow
							def 						__snake_case					(   self     :   int	):
											super().test_keras_fit()




							@unittest.skip(reason='''RegNet does not support input and output embeddings'''	)
							def 						__snake_case					(   self     :   List[Any]	):
											pass




							def 						__snake_case					(   self     :   str	):
											lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_common()

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




							def 						__snake_case					(   self     :   Dict	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Union[str, Any]	):
											def check_hidden_states_output(SCREAMING_SNAKE_CASE_     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[int]	):
															lowerCAmelCase__      			=						model_class(SCREAMING_SNAKE_CASE_	)
															lowerCAmelCase__      			=						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	) ,							training=SCREAMING_SNAKE_CASE_	)

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

															lowerCAmelCase__      			=						self.model_tester.num_stages
															self.assertEqual(len(SCREAMING_SNAKE_CASE_	) ,							expected_num_stages + 1	)

															# RegNet'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 // 2, self.model_tester.image_size // 2] ,							)

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

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

																			check_hidden_states_output(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Optional[int]	):
											lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_common()

											def check_equivalence(SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   Optional[int]={}	):
															lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_ ,							return_dict=SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)
															lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_ ,							return_dict=SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	).to_tuple()

															def recursive_check(SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   int	):
																			if isinstance(SCREAMING_SNAKE_CASE_ ,							(List, Tuple)	):
																							for tuple_iterable_value, dict_iterable_value in zip(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	):
																											recursive_check(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)
																			elif tuple_object is None:
																							return
																			else:
																							self.assertTrue(
																							    all(tf.equal(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)	) ,							msg=(
																							        '''Tuple and dict output are not equal. Difference:'''
																							        f' {tf.math.reduce_max(tf.abs(tuple_object - dict_object	)	)}'
																							    ) ,							)

																			recursive_check(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)

											for model_class in self.all_model_classes:
															lowerCAmelCase__      			=						model_class(SCREAMING_SNAKE_CASE_	)

															lowerCAmelCase__      			=						self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)
															lowerCAmelCase__      			=						self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)
															check_equivalence(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)

															lowerCAmelCase__      			=						self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							return_labels=SCREAMING_SNAKE_CASE_	)
															lowerCAmelCase__      			=						self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							return_labels=SCREAMING_SNAKE_CASE_	)
															check_equivalence(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)

															lowerCAmelCase__      			=						self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)
															lowerCAmelCase__      			=						self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)
															check_equivalence(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							{'''output_hidden_states''': True}	)

															lowerCAmelCase__      			=						self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							return_labels=SCREAMING_SNAKE_CASE_	)
															lowerCAmelCase__      			=						self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							return_labels=SCREAMING_SNAKE_CASE_	)
															check_equivalence(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							{'''output_hidden_states''': True}	)




							def 						__snake_case					(   self     :   List[Any]	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_	)




							@slow
							def 						__snake_case					(   self     :   Union[str, Any]	):
											for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowerCAmelCase__      			=						TFRegNetModel.from_pretrained(SCREAMING_SNAKE_CASE_	)
															self.assertIsNotNone(SCREAMING_SNAKE_CASE_	)





def 	lowerCAmelCase_		()		->    Optional[int]:


				'''simple docstring'''




				lowerCAmelCase__      			=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
				return image




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




							@cached_property
							def 						__snake_case					(   self     :   List[str]	):
											return (
											    AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)
											    if is_vision_available()
											    else None
											)




							@slow
							def 						__snake_case					(   self     :   Optional[int]	):
											lowerCAmelCase__      			=						TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)

											lowerCAmelCase__      			=						self.default_image_processor
											lowerCAmelCase__      			=						prepare_img()
											lowerCAmelCase__      			=						image_processor(images=SCREAMING_SNAKE_CASE_ ,							return_tensors='''tf'''	)

											# forward pass
											lowerCAmelCase__      			=						model(**SCREAMING_SNAKE_CASE_ ,							training=SCREAMING_SNAKE_CASE_	)

											# verify the logits
											lowerCAmelCase__      			=						tf.TensorShape((1, 1_000)	)
											self.assertEqual(outputs.logits.shape ,							SCREAMING_SNAKE_CASE_	)

											lowerCAmelCase__      			=						tf.constant([-0.4_180, -1.5_051, -3.4_836]	)

											tf.debugging.assert_near(outputs.logits[0, :3] ,							SCREAMING_SNAKE_CASE_ ,							atol=1e-4	)













import numpy as np
from cva import destroyAllWindows, imread, imshow, waitKey
class  lowerCAmelCase_       :




							def __init__(   self     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   int	):
											if dst_width < 0 or dst_height < 0:
															raise ValueError('''Destination width/height should be > 0'''	)

											lowerCAmelCase__      			=						img
											lowerCAmelCase__      			=						img.shape[1]
											lowerCAmelCase__      			=						img.shape[0]
											lowerCAmelCase__      			=						dst_width
											lowerCAmelCase__      			=						dst_height

											lowerCAmelCase__      			=						self.src_w / self.dst_w
											lowerCAmelCase__      			=						self.src_h / self.dst_h

											lowerCAmelCase__      			=						lowerCAmelCase__      			=						(
											    np.ones((self.dst_h, self.dst_w, 3) ,							np.uinta	) * 255
											)




							def 						__snake_case					(   self     :   List[str]	):
											for i in range(self.dst_h	):
															for j in range(self.dst_w	):
																			lowerCAmelCase__      			=						self.img[self.get_y(SCREAMING_SNAKE_CASE_	)][self.get_x(SCREAMING_SNAKE_CASE_	)]




							def 						__snake_case					(   self     :   Any ,							SCREAMING_SNAKE_CASE_     :   int	):
											return int(self.ratio_x * x	)




							def 						__snake_case					(   self     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   int	):
											return int(self.ratio_y * y	)


if __name__ == "__main__":
			_UpperCAmelCase				,   _UpperCAmelCase			:		List[str]   				=    800, 600
			_UpperCAmelCase			:		Tuple   				=    imread("image_data/lena.jpg", 1)
			_UpperCAmelCase			:		str   				=    NearestNeighbour(im, dst_w, dst_h)
			n.process()

			imshow(
			    F'''Image resized from: {im.shape[1]}x{im.shape[0]} to {dst_w}x{dst_h}''', n.output
			)
			waitKey(0)
			destroyAllWindows()





