






"""simple docstring"""






import inspect
import unittest

from transformers import YolosConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import YolosForObjectDetection, YolosModel
  from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor

class 							lowerCAmelCase__       :






      '''simple docstring'''

      def __init__(		self  :						Dict		,			lowercase_  :						str		,			lowercase_  :						Dict=13		,			lowercase_  :						Any=[30, 30]		,			lowercase_  :						List[Any]=2		,			lowercase_  :						List[Any]=3		,			lowercase_  :						int=True		,			lowercase_  :						Tuple=True		,			lowercase_  :						str=32		,			lowercase_  :						Dict=5		,			lowercase_  :						Dict=4		,			lowercase_  :						Optional[Any]=37		,			lowercase_  :						List[str]="gelu"		,			lowercase_  :						Optional[Any]=0.1		,			lowercase_  :						Dict=0.1		,			lowercase_  :						List[str]=10		,			lowercase_  :						Union[str, Any]=0.02		,			lowercase_  :						Optional[Any]=3		,			lowercase_  :						Tuple=None		,			lowercase_  :						Union[str, Any]=8		,			lowercase_  :						Any=10		,			):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						List[str]							=	parent
          SCREAMING_SNAKE_CASE_   :						int							=	batch_size
          SCREAMING_SNAKE_CASE_   :						int							=	image_size
          SCREAMING_SNAKE_CASE_   :						Dict							=	patch_size
          SCREAMING_SNAKE_CASE_   :						List[str]							=	num_channels
          SCREAMING_SNAKE_CASE_   :						Any							=	is_training
          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	use_labels
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	hidden_size
          SCREAMING_SNAKE_CASE_   :						Dict							=	num_hidden_layers
          SCREAMING_SNAKE_CASE_   :						str							=	num_attention_heads
          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	intermediate_size
          SCREAMING_SNAKE_CASE_   :						List[str]							=	hidden_act
          SCREAMING_SNAKE_CASE_   :						Tuple							=	hidden_dropout_prob
          SCREAMING_SNAKE_CASE_   :						List[str]							=	attention_probs_dropout_prob
          SCREAMING_SNAKE_CASE_   :						List[str]							=	type_sequence_label_size
          SCREAMING_SNAKE_CASE_   :						str							=	initializer_range
          SCREAMING_SNAKE_CASE_   :						List[str]							=	num_labels
          SCREAMING_SNAKE_CASE_   :						str							=	scope
          SCREAMING_SNAKE_CASE_   :						List[str]							=	n_targets
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	num_detection_tokens
          # we set the expected sequence length (which is used in several tests)
          # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
          SCREAMING_SNAKE_CASE_   :						Tuple							=	(image_size[1] // patch_size) * (image_size[0] // patch_size)
          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	num_patches + 1 + self.num_detection_tokens
      def _SCREAMING_SNAKE_CASE				(		self  :						Optional[int]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						List[Any]							=	floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]])

          SCREAMING_SNAKE_CASE_   :						List[str]							=	None
          if self.use_labels:
              # labels is a list of Dict (each Dict being the labels for a given example in the batch)
              SCREAMING_SNAKE_CASE_   :						List[str]							=	[]
              for i in range(self.batch_size):
                  SCREAMING_SNAKE_CASE_   :						Dict							=	{}
                  SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	torch.randint(
                      high=self.num_labels		,			size=(self.n_targets,)		,			device=lowercase_)
                  SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	torch.rand(self.n_targets		,			4		,			device=lowercase_)
                  labels.append(lowercase_)

          SCREAMING_SNAKE_CASE_   :						List[Any]							=	self.get_config()

          return config, pixel_values, labels
      def _SCREAMING_SNAKE_CASE				(		self  :						Optional[Any]):




          '''simple docstring'''


          return YolosConfig(
              image_size=self.image_size		,			patch_size=self.patch_size		,			num_channels=self.num_channels		,			hidden_size=self.hidden_size		,			num_hidden_layers=self.num_hidden_layers		,			num_attention_heads=self.num_attention_heads		,			intermediate_size=self.intermediate_size		,			hidden_act=self.hidden_act		,			hidden_dropout_prob=self.hidden_dropout_prob		,			attention_probs_dropout_prob=self.attention_probs_dropout_prob		,			is_decoder=lowercase_		,			initializer_range=self.initializer_range		,			num_detection_tokens=self.num_detection_tokens		,			num_labels=self.num_labels		,			)
      def _SCREAMING_SNAKE_CASE				(		self  :						Tuple		,			lowercase_  :						List[Any]		,			lowercase_  :						Optional[int]		,			lowercase_  :						int):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						List[Any]							=	YolosModel(config=lowercase_)
          model.to(lowercase_)
          model.eval()
          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	model(lowercase_)
          self.parent.assertEqual(
              result.last_hidden_state.shape		,			(self.batch_size, self.expected_seq_len, self.hidden_size))
      def _SCREAMING_SNAKE_CASE				(		self  :						List[Any]		,			lowercase_  :						Dict		,			lowercase_  :						List[Any]		,			lowercase_  :						Optional[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						List[str]							=	YolosForObjectDetection(lowercase_)
          model.to(lowercase_)
          model.eval()

          SCREAMING_SNAKE_CASE_   :						List[str]							=	model(pixel_values=lowercase_)
          SCREAMING_SNAKE_CASE_   :						List[str]							=	model(lowercase_)

          self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.num_detection_tokens, self.num_labels + 1))
          self.parent.assertEqual(result.pred_boxes.shape		,			(self.batch_size, self.num_detection_tokens, 4))

          SCREAMING_SNAKE_CASE_   :						List[Any]							=	model(pixel_values=lowercase_		,			labels=lowercase_)

          self.parent.assertEqual(result.loss.shape		,			())
          self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.num_detection_tokens, self.num_labels + 1))
          self.parent.assertEqual(result.pred_boxes.shape		,			(self.batch_size, self.num_detection_tokens, 4))



      def _SCREAMING_SNAKE_CASE				(		self  :						List[Any]):




          '''simple docstring'''


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



@require_torch
class 							lowerCAmelCase__       (    UpperCAmelCase__       ,	UpperCAmelCase__       ,	unittest.TestCase				):






      '''simple docstring'''

      __UpperCamelCase							=						(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
      __UpperCamelCase							=						(
          {"feature-extraction": YolosModel, "object-detection": YolosForObjectDetection} if is_torch_available() else {}
      )

      __UpperCamelCase							=						False
      __UpperCamelCase							=						False
      __UpperCamelCase							=						False
      __UpperCamelCase							=						False
      def _SCREAMING_SNAKE_CASE				(		self  :						Any		,			lowercase_  :						Dict		,			lowercase_  :						int		,			lowercase_  :						int=False):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						List[str]							=	super()._prepare_for_class(lowercase_		,			lowercase_		,			return_labels=lowercase_)

          if return_labels:
              if model_class.__name__ == "YolosForObjectDetection":
                  SCREAMING_SNAKE_CASE_   :						Optional[int]							=	[]
                  for i in range(self.model_tester.batch_size):
                      SCREAMING_SNAKE_CASE_   :						Optional[int]							=	{}
                      SCREAMING_SNAKE_CASE_   :						str							=	torch.ones(
                          size=(self.model_tester.n_targets,)		,			device=lowercase_		,			dtype=torch.long)
                      SCREAMING_SNAKE_CASE_   :						Optional[int]							=	torch.ones(
                          self.model_tester.n_targets		,			4		,			device=lowercase_		,			dtype=torch.float)
                      labels.append(lowercase_)
                  SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	labels

          return inputs_dict
      def _SCREAMING_SNAKE_CASE				(		self  :						Any):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						List[Any]							=	YolosModelTester(self)
          SCREAMING_SNAKE_CASE_   :						Any							=	ConfigTester(self		,			config_class=lowercase_		,			has_text_modality=lowercase_		,			hidden_size=37)
      def _SCREAMING_SNAKE_CASE				(		self  :						str):




          '''simple docstring'''


          self.config_tester.run_common_tests()
      def _SCREAMING_SNAKE_CASE				(		self  :						Tuple):




          '''simple docstring'''


          pass
      def _SCREAMING_SNAKE_CASE				(		self  :						Optional[int]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	model_class(lowercase_)
              self.assertIsInstance(model.get_input_embeddings()		,			(nn.Module))
              SCREAMING_SNAKE_CASE_   :						List[str]							=	model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(lowercase_		,			nn.Linear))
      def _SCREAMING_SNAKE_CASE				(		self  :						int):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Any							=	self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE_   :						Tuple							=	model_class(lowercase_)
              SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	inspect.signature(model.forward)
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              SCREAMING_SNAKE_CASE_   :						str							=	[*signature.parameters.keys()]

              SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	['''pixel_values''']
              self.assertListEqual(arg_names[:1]		,			lowercase_)
      def _SCREAMING_SNAKE_CASE				(		self  :						List[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						str							=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowercase_)
      def _SCREAMING_SNAKE_CASE				(		self  :						Any):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Any							=	self.model_tester.prepare_config_and_inputs_for_common()
          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	True

          # in YOLOS, the seq_len is different
          SCREAMING_SNAKE_CASE_   :						int							=	self.model_tester.expected_seq_len
          for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE_   :						List[str]							=	True
              SCREAMING_SNAKE_CASE_   :						Tuple							=	False
              SCREAMING_SNAKE_CASE_   :						int							=	True
              SCREAMING_SNAKE_CASE_   :						List[str]							=	model_class(lowercase_)
              model.to(lowercase_)
              model.eval()
              with torch.no_grad():
                  SCREAMING_SNAKE_CASE_   :						Optional[int]							=	model(**self._prepare_for_class(lowercase_		,			lowercase_))
              SCREAMING_SNAKE_CASE_   :						List[str]							=	outputs.attentions
              self.assertEqual(len(lowercase_)		,			self.model_tester.num_hidden_layers)

              # check that output_attentions also work using config
              del inputs_dict["output_attentions"]
              SCREAMING_SNAKE_CASE_   :						Dict							=	True
              SCREAMING_SNAKE_CASE_   :						Tuple							=	model_class(lowercase_)
              model.to(lowercase_)
              model.eval()
              with torch.no_grad():
                  SCREAMING_SNAKE_CASE_   :						Any							=	model(**self._prepare_for_class(lowercase_		,			lowercase_))
              SCREAMING_SNAKE_CASE_   :						List[str]							=	outputs.attentions
              self.assertEqual(len(lowercase_)		,			self.model_tester.num_hidden_layers)

              self.assertListEqual(
                  list(attentions[0].shape[-3:])		,			[self.model_tester.num_attention_heads, seq_len, seq_len]		,			)
              SCREAMING_SNAKE_CASE_   :						List[str]							=	len(lowercase_)

              # Check attention is always last and order is fine
              SCREAMING_SNAKE_CASE_   :						int							=	True
              SCREAMING_SNAKE_CASE_   :						Dict							=	True
              SCREAMING_SNAKE_CASE_   :						Tuple							=	model_class(lowercase_)
              model.to(lowercase_)
              model.eval()
              with torch.no_grad():
                  SCREAMING_SNAKE_CASE_   :						List[str]							=	model(**self._prepare_for_class(lowercase_		,			lowercase_))

              SCREAMING_SNAKE_CASE_   :						Any							=	1
              self.assertEqual(out_len + added_hidden_states		,			len(lowercase_))

              SCREAMING_SNAKE_CASE_   :						Tuple							=	outputs.attentions

              self.assertEqual(len(lowercase_)		,			self.model_tester.num_hidden_layers)
              self.assertListEqual(
                  list(self_attentions[0].shape[-3:])		,			[self.model_tester.num_attention_heads, seq_len, seq_len]		,			)
      def _SCREAMING_SNAKE_CASE				(		self  :						Optional[int]):




          '''simple docstring'''


          def check_hidden_states_output(lowercase_  :						int		,			lowercase_  :						str		,			lowercase_  :						Dict):
              SCREAMING_SNAKE_CASE_   :						Dict							=	model_class(lowercase_)
              model.to(lowercase_)
              model.eval()

              with torch.no_grad():
                  SCREAMING_SNAKE_CASE_   :						Tuple							=	model(**self._prepare_for_class(lowercase_		,			lowercase_))

              SCREAMING_SNAKE_CASE_   :						Any							=	outputs.hidden_states

              SCREAMING_SNAKE_CASE_   :						str							=	getattr(
                  self.model_tester		,			'''expected_num_hidden_layers'''		,			self.model_tester.num_hidden_layers + 1)
              self.assertEqual(len(lowercase_)		,			lowercase_)

              # YOLOS has a different seq_length
              SCREAMING_SNAKE_CASE_   :						str							=	self.model_tester.expected_seq_len

              self.assertListEqual(
                  list(hidden_states[0].shape[-2:])		,			[seq_length, self.model_tester.hidden_size]		,			)

          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE_   :						List[str]							=	True
              check_hidden_states_output(lowercase_		,			lowercase_		,			lowercase_)

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

              check_hidden_states_output(lowercase_		,			lowercase_		,			lowercase_)
      def _SCREAMING_SNAKE_CASE				(		self  :						List[str]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Any							=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_object_detection(*lowercase_)



      @slow
      def _SCREAMING_SNAKE_CASE				(		self  :						Any):




          '''simple docstring'''


          for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE_   :						List[Any]							=	YolosModel.from_pretrained(lowercase_)
              self.assertIsNotNone(lowercase_)



def       _A    ()				-> List[str]:
    """simple docstring"""






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





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






      '''simple docstring'''

      @cached_property
      def _SCREAMING_SNAKE_CASE				(		self  :						Optional[int]):




          '''simple docstring'''


          return AutoImageProcessor.from_pretrained('''hustvl/yolos-small''') if is_vision_available() else None



      @slow
      def _SCREAMING_SNAKE_CASE				(		self  :						str):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	YolosForObjectDetection.from_pretrained('''hustvl/yolos-small''').to(lowercase_)

          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	self.default_image_processor
          SCREAMING_SNAKE_CASE_   :						List[str]							=	prepare_img()
          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	image_processor(images=lowercase_		,			return_tensors='''pt''').to(lowercase_)

          # forward pass
          with torch.no_grad():
              SCREAMING_SNAKE_CASE_   :						List[Any]							=	model(inputs.pixel_values)

          # verify outputs
          SCREAMING_SNAKE_CASE_   :						int							=	torch.Size((1, 100, 92))
          self.assertEqual(outputs.logits.shape		,			lowercase_)

          SCREAMING_SNAKE_CASE_   :						List[str]							=	torch.tensor(
              [[-24.02_48, -10.30_24, -14.82_90], [-42.03_92, -16.82_00, -27.43_34], [-27.27_43, -11.81_54, -18.71_48]]		,			device=lowercase_		,			)
          SCREAMING_SNAKE_CASE_   :						Any							=	torch.tensor(
              [[0.25_59, 0.54_55, 0.47_06], [0.29_89, 0.72_79, 0.18_75], [0.77_32, 0.40_17, 0.44_62]]		,			device=lowercase_)
          self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]		,			lowercase_		,			atol=1e-4))
          self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]		,			lowercase_		,			atol=1e-4))

          # verify postprocessing
          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	image_processor.post_process_object_detection(
              lowercase_		,			threshold=0.3		,			target_sizes=[image.size[::-1]])[0]
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	torch.tensor([0.99_94, 0.97_90, 0.99_64, 0.99_72, 0.98_61]).to(lowercase_)
          SCREAMING_SNAKE_CASE_   :						Dict							=	[75, 75, 17, 63, 17]
          SCREAMING_SNAKE_CASE_   :						int							=	torch.tensor([335.0609, 79.38_48, 375.4216, 187.2495]).to(lowercase_)

          self.assertEqual(len(results['''scores'''])		,			5)
          self.assertTrue(torch.allclose(results['''scores''']		,			lowercase_		,			atol=1e-4))
          self.assertSequenceEqual(results['''labels'''].tolist()		,			lowercase_)
          self.assertTrue(torch.allclose(results['''boxes'''][0, :]		,			lowercase_))










"""simple docstring"""






from __future__ import annotations

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



def       _A    (__a					,       __a					,       __a = False							)				-> list[float]:
    """simple docstring"""






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



def       _A    (__a					,       __a					,       __a = 10**-1							)				-> bool:
    """simple docstring"""






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


if __name__ == "__main__":
  # Test to check if it works
  UpperCAmelCase_     :				Union[str, Any]         =				array(
      [
          polar_force(7_1_8.4, 180 - 30),
          polar_force(8_7_9.5_4, 45),
          polar_force(100, -90),
      ]
  )

  UpperCAmelCase_     :				NDArray[floataa]         =				array([[0, 0], [0, 0], [0, 0]])

  assert in_static_equilibrium(forces, location)

  # Problem 1 in image_data/2D_problems.jpg
  UpperCAmelCase_     :				Union[str, Any]         =				array(
      [
          polar_force(30 * 9.8_1, 15),
          polar_force(215, 180 - 45),
          polar_force(264, 90 - 30),
      ]
  )

  UpperCAmelCase_     :				Optional[int]         =				array([[0, 0], [0, 0], [0, 0]])

  assert in_static_equilibrium(forces, location)

  # Problem in image_data/2D_problems_1.jpg
  UpperCAmelCase_     :				List[str]         =				array([[0, -2000], [0, -1200], [0, 15600], [0, -12400]])

  UpperCAmelCase_     :				Any         =				array([[0, 0], [6, 0], [10, 0], [12, 0]])

  assert in_static_equilibrium(forces, location)

  import doctest

  doctest.testmod()


