




"""simple docstring"""






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






			'''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     :			List[str]    =    parent
							_lowerCamelCase     :			Dict    =    batch_size
							_lowerCamelCase     :			Optional[Any]    =    image_size
							_lowerCamelCase     :			int    =    num_channels
							_lowerCamelCase     :			int    =    embeddings_size
							_lowerCamelCase     :			Optional[Any]    =    hidden_sizes
							_lowerCamelCase     :			Tuple    =    depths
							_lowerCamelCase     :			Optional[Any]    =    is_training
							_lowerCamelCase     :			Optional[Any]    =    use_labels
							_lowerCamelCase     :			Tuple    =    hidden_act
							_lowerCamelCase     :			Tuple    =    num_labels
							_lowerCamelCase     :			List[str]    =    scope
							_lowerCamelCase     :			Union[str, Any]    =    len(lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			int    =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

							_lowerCamelCase     :			Any    =    self.get_config()
							return config, pixel_values, labels
			def 					A_    (  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  ,      )
			def 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			Optional[int]    =    TFRegNetModel(config=lowercase  )
							_lowerCamelCase     :			Any    =    model(lowercase  ,      training=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  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			str    =    self.num_labels
							_lowerCamelCase     :			int    =    TFRegNetForImageClassification(lowercase  )
							_lowerCamelCase     :			List[str]    =    model(lowercase  ,      labels=lowercase  ,      training=lowercase  )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.num_labels)  )






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






@require_tf
class lowerCAmelCase__	(     lowercase,	lowercase,	unittest.TestCase						):






			'''simple docstring'''



			lowerCamelCase__      =			(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
			lowerCamelCase__      =			(
			    {"""feature-extraction""": TFRegNetModel, """image-classification""": TFRegNetForImageClassification}
			    if is_tf_available()
			    else {}
			)

			lowerCamelCase__      =			False
			lowerCamelCase__      =			False
			lowerCamelCase__      =			False
			lowerCamelCase__      =			False
			lowerCamelCase__      =			False
			def 					A_    (  self  ):
							_lowerCamelCase     :			Union[str, Any]    =    TFRegNetModelTester(self  )
							_lowerCamelCase     :			Union[str, Any]    =    ConfigTester(self  ,      config_class=lowercase  ,      has_text_modality=lowercase  )
			def 					A_    (  self  ):
							return
			@unittest.skip(reason='RegNet does not use inputs_embeds'  )
			def 					A_    (  self  ):
							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 					A_    (  self  ):
							super().test_keras_fit()
			@unittest.skip(reason='RegNet does not support input and output embeddings'  )
			def 					A_    (  self  ):
							pass
			def 					A_    (  self  ):
							_lowerCamelCase, _lowerCamelCase     :			Tuple    =    self.model_tester.prepare_config_and_inputs_for_common()

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

											_lowerCamelCase     :			int    =    ['pixel_values']
											self.assertListEqual(arg_names[:1]  ,      lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			Dict    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowercase  )
			def 					A_    (  self  ):
							def check_hidden_states_output(lowercase  ,      lowercase  ,      lowercase  ):
											_lowerCamelCase     :			Optional[Any]    =    model_class(lowercase  )
											_lowerCamelCase     :			List[Any]    =    model(**self._prepare_for_class(lowercase  ,      lowercase  )  ,      training=lowercase  )

											_lowerCamelCase     :			Tuple    =    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

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

															check_hidden_states_output(lowercase  ,      lowercase  ,      lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase, _lowerCamelCase     :			int    =    self.model_tester.prepare_config_and_inputs_for_common()

							def check_equivalence(lowercase  ,      lowercase  ,      lowercase  ,      lowercase={}  ):
											_lowerCamelCase     :			Optional[Any]    =    model(lowercase  ,      return_dict=lowercase  ,      **lowercase  )
											_lowerCamelCase     :			int    =    model(lowercase  ,      return_dict=lowercase  ,      **lowercase  ).to_tuple()

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

															recursive_check(lowercase  ,      lowercase  )

							for model_class in self.all_model_classes:
											_lowerCamelCase     :			Dict    =    model_class(lowercase  )

											_lowerCamelCase     :			Any    =    self._prepare_for_class(lowercase  ,      lowercase  )
											_lowerCamelCase     :			List[Any]    =    self._prepare_for_class(lowercase  ,      lowercase  )
											check_equivalence(lowercase  ,      lowercase  ,      lowercase  )

											_lowerCamelCase     :			Any    =    self._prepare_for_class(lowercase  ,      lowercase  ,      return_labels=lowercase  )
											_lowerCamelCase     :			int    =    self._prepare_for_class(lowercase  ,      lowercase  ,      return_labels=lowercase  )
											check_equivalence(lowercase  ,      lowercase  ,      lowercase  )

											_lowerCamelCase     :			int    =    self._prepare_for_class(lowercase  ,      lowercase  )
											_lowerCamelCase     :			Union[str, Any]    =    self._prepare_for_class(lowercase  ,      lowercase  )
											check_equivalence(lowercase  ,      lowercase  ,      lowercase  ,      {'output_hidden_states': True}  )

											_lowerCamelCase     :			Optional[Any]    =    self._prepare_for_class(lowercase  ,      lowercase  ,      return_labels=lowercase  )
											_lowerCamelCase     :			List[str]    =    self._prepare_for_class(lowercase  ,      lowercase  ,      return_labels=lowercase  )
											check_equivalence(lowercase  ,      lowercase  ,      lowercase  ,      {'output_hidden_states': True}  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			Tuple    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*lowercase  )






			@slow
			def 					A_    (  self  ):
							for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											_lowerCamelCase     :			Dict    =    TFRegNetModel.from_pretrained(lowercase  )
											self.assertIsNotNone(lowercase  )







def      _snake_case			(				):
				_lowerCamelCase     :			Optional[Any]    =    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'		)
				return image






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






			'''simple docstring'''



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






			@slow
			def 					A_    (  self  ):
							_lowerCamelCase     :			Optional[Any]    =    TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]  )

							_lowerCamelCase     :			Optional[int]    =    self.default_image_processor
							_lowerCamelCase     :			Optional[int]    =    prepare_img()
							_lowerCamelCase     :			Optional[Any]    =    image_processor(images=lowercase  ,      return_tensors='tf'  )

							# forward pass
							_lowerCamelCase     :			Dict    =    model(**lowercase  ,      training=lowercase  )

							# verify the logits
							_lowerCamelCase     :			Any    =    tf.TensorShape((1, 1000)  )
							self.assertEqual(outputs.logits.shape  ,      lowercase  )

							_lowerCamelCase     :			str    =    tf.constant([-0.41_80, -1.50_51, -3.48_36]  )

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





"""simple docstring"""






# Imports
import numpy as np






class lowerCAmelCase__	:






			'''simple docstring'''



			def __init__(  self  ,      lowercase=None  ,      lowercase=None  ,      lowercase=None  ,      lowercase=None  ,      lowercase=None  ):
							self.set_matricies(red=lowercase  ,      green=lowercase  ,      blue=lowercase  ,      red_edge=lowercase  ,      nir=lowercase  )
			def 					A_    (  self  ,      lowercase=None  ,      lowercase=None  ,      lowercase=None  ,      lowercase=None  ,      lowercase=None  ):
							if red is not None:
											_lowerCamelCase     :			Optional[int]    =    red
							if green is not None:
											_lowerCamelCase     :			Optional[Any]    =    green
							if blue is not None:
											_lowerCamelCase     :			Tuple    =    blue
							if red_edge is not None:
											_lowerCamelCase     :			Optional[Any]    =    red_edge
							if nir is not None:
											_lowerCamelCase     :			Union[str, Any]    =    nir
							return True
			def 					A_    (  self  ,      lowercase=""  ,      lowercase=None  ,      lowercase=None  ,      lowercase=None  ,      lowercase=None  ,      lowercase=None  ):
							self.set_matricies(red=lowercase  ,      green=lowercase  ,      blue=lowercase  ,      red_edge=lowercase  ,      nir=lowercase  )
							_lowerCamelCase     :			str    =    {
							    'ARVI2': self.arvaa,
							    'CCCI': self.ccci,
							    'CVI': self.cvi,
							    'GLI': self.gli,
							    'NDVI': self.ndvi,
							    'BNDVI': self.bndvi,
							    'redEdgeNDVI': self.red_edge_ndvi,
							    'GNDVI': self.gndvi,
							    'GBNDVI': self.gbndvi,
							    'GRNDVI': self.grndvi,
							    'RBNDVI': self.rbndvi,
							    'PNDVI': self.pndvi,
							    'ATSAVI': self.atsavi,
							    'BWDRVI': self.bwdrvi,
							    'CIgreen': self.ci_green,
							    'CIrededge': self.ci_rededge,
							    'CI': self.ci,
							    'CTVI': self.ctvi,
							    'GDVI': self.gdvi,
							    'EVI': self.evi,
							    'GEMI': self.gemi,
							    'GOSAVI': self.gosavi,
							    'GSAVI': self.gsavi,
							    'Hue': self.hue,
							    'IVI': self.ivi,
							    'IPVI': self.ipvi,
							    'I': self.i,
							    'RVI': self.rvi,
							    'MRVI': self.mrvi,
							    'MSAVI': self.m_savi,
							    'NormG': self.norm_g,
							    'NormNIR': self.norm_nir,
							    'NormR': self.norm_r,
							    'NGRDI': self.ngrdi,
							    'RI': self.ri,
							    'S': self.s,
							    'IF': self._if,
							    'DVI': self.dvi,
							    'TVI': self.tvi,
							    'NDRE': self.ndre,
							}

							try:
											return funcs[index]()
							except KeyError:
											print('Index not in the list!'  )
											return False
			def 					A_    (  self  ):
							return -0.18 + (1.17 * ((self.nir - self.red) / (self.nir + self.red)))
			def 					A_    (  self  ):
							return ((self.nir - self.redEdge) / (self.nir + self.redEdge)) / (
							    (self.nir - self.red) / (self.nir + self.red)
							)
			def 					A_    (  self  ):
							return self.nir * (self.red / (self.green**2))
			def 					A_    (  self  ):
							return (2 * self.green - self.red - self.blue) / (
							    2 * self.green + self.red + self.blue
							)
			def 					A_    (  self  ):
							return (self.nir - self.red) / (self.nir + self.red)
			def 					A_    (  self  ):
							return (self.nir - self.blue) / (self.nir + self.blue)
			def 					A_    (  self  ):
							return (self.redEdge - self.red) / (self.redEdge + self.red)
			def 					A_    (  self  ):
							return (self.nir - self.green) / (self.nir + self.green)
			def 					A_    (  self  ):
							return (self.nir - (self.green + self.blue)) / (
							    self.nir + (self.green + self.blue)
							)
			def 					A_    (  self  ):
							return (self.nir - (self.green + self.red)) / (
							    self.nir + (self.green + self.red)
							)
			def 					A_    (  self  ):
							return (self.nir - (self.blue + self.red)) / (self.nir + (self.blue + self.red))
			def 					A_    (  self  ):
							return (self.nir - (self.green + self.red + self.blue)) / (
							    self.nir + (self.green + self.red + self.blue)
							)
			def 					A_    (  self  ,      lowercase=0.08  ,      lowercase=1.22  ,      lowercase=0.03  ):
							return a * (
							    (self.nir - a * self.red - b)
							    / (a * self.nir + self.red - a * b + x * (1 + a**2))
							)
			def 					A_    (  self  ):
							return (0.1 * self.nir - self.blue) / (0.1 * self.nir + self.blue)
			def 					A_    (  self  ):
							return (self.nir / self.green) - 1
			def 					A_    (  self  ):
							return (self.nir / self.redEdge) - 1
			def 					A_    (  self  ):
							return (self.red - self.blue) / self.red
			def 					A_    (  self  ):
							_lowerCamelCase     :			Any    =    self.ndvi()
							return ((ndvi + 0.5) / (abs(ndvi + 0.5  ))) * (abs(ndvi + 0.5  ) ** (1 / 2))
			def 					A_    (  self  ):
							return self.nir - self.green
			def 					A_    (  self  ):
							return 2.5 * (
							    (self.nir - self.red) / (self.nir + 6 * self.red - 7.5 * self.blue + 1)
							)
			def 					A_    (  self  ):
							_lowerCamelCase     :			Any    =    (2 * (self.nir**2 - self.red**2) + 1.5 * self.nir + 0.5 * self.red) / (
							    self.nir + self.red + 0.5
							)
							return n * (1 - 0.25 * n) - (self.red - 0.1_25) / (1 - self.red)
			def 					A_    (  self  ,      lowercase=0.16  ):
							return (self.nir - self.green) / (self.nir + self.green + y)
			def 					A_    (  self  ,      lowercase=0.5  ):
							return ((self.nir - self.green) / (self.nir + self.green + n)) * (1 + n)
			def 					A_    (  self  ):
							return np.arctan(
							    ((2 * self.red - self.green - self.blue) / 30.5) * (self.green - self.blue)  )
			def 					A_    (  self  ,      lowercase=None  ,      lowercase=None  ):
							return (self.nir - b) / (a * self.red)
			def 					A_    (  self  ):
							return (self.nir / ((self.nir + self.red) / 2)) * (self.ndvi() + 1)
			def 					A_    (  self  ):
							return (self.red + self.green + self.blue) / 30.5
			def 					A_    (  self  ):
							return self.nir / self.red
			def 					A_    (  self  ):
							return (self.rvi() - 1) / (self.rvi() + 1)
			def 					A_    (  self  ):
							return (
							    (2 * self.nir + 1)
							    - ((2 * self.nir + 1) ** 2 - 8 * (self.nir - self.red)) ** (1 / 2)
							) / 2
			def 					A_    (  self  ):
							return self.green / (self.nir + self.red + self.green)
			def 					A_    (  self  ):
							return self.nir / (self.nir + self.red + self.green)
			def 					A_    (  self  ):
							return self.red / (self.nir + self.red + self.green)
			def 					A_    (  self  ):
							return (self.green - self.red) / (self.green + self.red)
			def 					A_    (  self  ):
							return (self.red - self.green) / (self.red + self.green)
			def 					A_    (  self  ):
							_lowerCamelCase     :			Union[str, Any]    =    np.max([np.max(self.red  ), np.max(self.green  ), np.max(self.blue  )]  )
							_lowerCamelCase     :			Dict    =    np.min([np.min(self.red  ), np.min(self.green  ), np.min(self.blue  )]  )
							return (max_value - min_value) / max_value
			def 					A_    (  self  ):
							return (2 * self.red - self.green - self.blue) / (self.green - self.blue)
			def 					A_    (  self  ):
							return self.nir / self.red
			def 					A_    (  self  ):
							return (self.ndvi() + 0.5) ** (1 / 2)






			def 					A_    (  self  ):
							return (self.nir - self.redEdge) / (self.nir + self.redEdge)