


"""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 	lowercase__				:





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





   def 		UpperCamelCase_  (							self)  ->  List[Any]:
      _lowerCamelCase	:					Optional[int]   =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

      _lowerCamelCase	:					Tuple   =		self.get_config()
      return config, pixel_values, labels





   def 		UpperCamelCase_  (							self)  ->  Dict:
      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 		UpperCamelCase_  (							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)  ->  Any:
      _lowerCamelCase	:					Optional[int]   =		TFRegNetModel(config=SCREAMING_SNAKE_CASE)
      _lowerCamelCase	:					int   =		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 		UpperCamelCase_  (							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)  ->  int:
      _lowerCamelCase	:					Optional[Any]   =		self.num_labels
      _lowerCamelCase	:					List[str]   =		TFRegNetForImageClassification(SCREAMING_SNAKE_CASE)
      _lowerCamelCase	:					Tuple   =		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 		UpperCamelCase_  (							self)  ->  List[str]:
      _lowerCamelCase	:					Optional[int]   =		self.prepare_config_and_inputs()
      _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase	:					Optional[Any]   =		config_and_inputs
      _lowerCamelCase	:					Tuple   =		{"""pixel_values""": pixel_values}
      return config, inputs_dict







@require_tf
class 	lowercase__				(					A_ ,A_ ,unittest.TestCase				):
   __UpperCAmelCase							       =					(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
   __UpperCAmelCase							       =					(
       {'''feature-extraction''': TFRegNetModel, '''image-classification''': TFRegNetForImageClassification}
       if is_tf_available()
       else {}
   )

   __UpperCAmelCase							       =					False
   __UpperCAmelCase							       =					False
   __UpperCAmelCase							       =					False
   __UpperCAmelCase							       =					False
   __UpperCAmelCase							       =					False





   def 		UpperCamelCase_  (							self)  ->  List[str]:
      _lowerCamelCase	:					str   =		TFRegNetModelTester(self)
      _lowerCamelCase	:					Union[str, Any]   =		ConfigTester(self      ,   config_class=SCREAMING_SNAKE_CASE      ,   has_text_modality=SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Optional[Any]:
      return





   @unittest.skip(reason="""RegNet does not use inputs_embeds""")
   def 		UpperCamelCase_  (							self)  ->  List[str]:
      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 		UpperCamelCase_  (							self)  ->  List[str]:
      super().test_keras_fit()





   @unittest.skip(reason="""RegNet does not support input and output embeddings""")
   def 		UpperCamelCase_  (							self)  ->  Optional[Any]:
      pass





   def 		UpperCamelCase_  (							self)  ->  Dict:
      _lowerCamelCase   ,    _lowerCamelCase	:					int   =		self.model_tester.prepare_config_and_inputs_for_common()

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

         _lowerCamelCase	:					List[str]   =		["""pixel_values"""]
         self.assertListEqual(arg_names[:1]      ,   SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Union[str, Any]:
      _lowerCamelCase	:					Union[str, Any]   =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Union[str, Any]:
      def check_hidden_states_output(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE):
         _lowerCamelCase	:					int   =		model_class(SCREAMING_SNAKE_CASE)
         _lowerCamelCase	:					Tuple   =		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)      ,   training=SCREAMING_SNAKE_CASE)

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

         _lowerCamelCase	:					Union[str, Any]   =		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	:					List[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	:					Union[str, Any]   =		layer_type
            _lowerCamelCase	:					List[Any]   =		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	:					Tuple   =		True

            check_hidden_states_output(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Union[str, Any]:
      _lowerCamelCase   ,    _lowerCamelCase	:					str   =		self.model_tester.prepare_config_and_inputs_for_common()

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

         def recursive_check(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE):
            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	:					Tuple   =		model_class(SCREAMING_SNAKE_CASE)

         _lowerCamelCase	:					str   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)
         _lowerCamelCase	:					Optional[int]   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)
         check_equivalence(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)

         _lowerCamelCase	:					Optional[Any]   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   return_labels=SCREAMING_SNAKE_CASE)
         _lowerCamelCase	:					Optional[Any]   =		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	:					Any   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)
         _lowerCamelCase	:					Any   =		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	:					int   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   return_labels=SCREAMING_SNAKE_CASE)
         _lowerCamelCase	:					Any   =		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 		UpperCamelCase_  (							self)  ->  Optional[int]:
      _lowerCamelCase	:					Union[str, Any]   =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE)





   @slow
   def 		UpperCamelCase_  (							self)  ->  int:
      for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _lowerCamelCase	:					List[str]   =		TFRegNetModel.from_pretrained(SCREAMING_SNAKE_CASE)
         self.assertIsNotNone(SCREAMING_SNAKE_CASE)







def 				_snake_case      (   ):




   """simple docstring"""







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





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





   @cached_property
   def 		UpperCamelCase_  (							self)  ->  Any:
      return (
          AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0])
          if is_vision_available()
          else None
      )





   @slow
   def 		UpperCamelCase_  (							self)  ->  List[Any]:
      _lowerCamelCase	:					str   =		TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0])

      _lowerCamelCase	:					Optional[int]   =		self.default_image_processor
      _lowerCamelCase	:					int   =		prepare_img()
      _lowerCamelCase	:					List[Any]   =		image_processor(images=SCREAMING_SNAKE_CASE      ,   return_tensors="""tf""")

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

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

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

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





"""simple docstring"""
from __future__ import annotations

from numpy import array, cos, cross, floataa, radians, sin
from numpy.typing import NDArray

def 				_snake_case      (   __snake_case							:      float						, __snake_case							:      float						, __snake_case							:      bool = False   ):




   """simple docstring"""







   if radian_mode:
      return [magnitude * cos(__snake_case   ), magnitude * sin(__snake_case   )]
   return [magnitude * cos(radians(__snake_case   )   ), magnitude * sin(radians(__snake_case   )   )]

def 				_snake_case      (   __snake_case							:      NDArray[floataa]						, __snake_case							:      NDArray[floataa]						, __snake_case							:      float = 10**-1   ):




   """simple docstring"""







   _lowerCamelCase	:					NDArray[floataa]   =		cross(__snake_case						, __snake_case   )
   _lowerCamelCase	:					float   =		sum(__snake_case   )
   return abs(__snake_case   ) < eps


if __name__ == "__main__":
 # Test to check if it works
 UpperCAmelCase				=						array(
     [
         polar_force(718.4, 180 - 30),
         polar_force(879.54, 45),
         polar_force(100, -90),
     ]
 )

 UpperCAmelCase				=						array([[0, 0], [0, 0], [0, 0]])

 assert in_static_equilibrium(forces, location)

 # Problem 1 in image_data/2D_problems.jpg
 UpperCAmelCase				=						array(
     [
         polar_force(30 * 9.81, 15),
         polar_force(215, 180 - 45),
         polar_force(264, 90 - 30),
     ]
 )

 UpperCAmelCase				=						array([[0, 0], [0, 0], [0, 0]])

 assert in_static_equilibrium(forces, location)

 # Problem in image_data/2D_problems_1.jpg
 UpperCAmelCase				=						array([[0, -2000], [0, -1200], [0, 1_5600], [0, -1_2400]])

 UpperCAmelCase				=						array([[0, 0], [6, 0], [10, 0], [12, 0]])

 assert in_static_equilibrium(forces, location)

 import doctest

 doctest.testmod()

