



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

 """simple docstring"""



 def __init__(	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=1_3		,				SCREAMING_SNAKE_CASE=[3_0, 3_0]		,				SCREAMING_SNAKE_CASE=2		,				SCREAMING_SNAKE_CASE=3		,				SCREAMING_SNAKE_CASE=True		,				SCREAMING_SNAKE_CASE=True		,				SCREAMING_SNAKE_CASE=3_2		,				SCREAMING_SNAKE_CASE=5		,				SCREAMING_SNAKE_CASE=4		,				SCREAMING_SNAKE_CASE=3_7		,				SCREAMING_SNAKE_CASE="gelu"		,				SCREAMING_SNAKE_CASE=0.1		,				SCREAMING_SNAKE_CASE=0.1		,				SCREAMING_SNAKE_CASE=1_0		,				SCREAMING_SNAKE_CASE=0.02		,				SCREAMING_SNAKE_CASE=3		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=8		,				SCREAMING_SNAKE_CASE=1_0		,				) ->			Any:
  a__												= parent
  a__												= batch_size
  a__												= image_size
  a__												= patch_size
  a__												= num_channels
  a__												= is_training
  a__												= use_labels
  a__												= hidden_size
  a__												= num_hidden_layers
  a__												= num_attention_heads
  a__												= intermediate_size
  a__												= hidden_act
  a__												= hidden_dropout_prob
  a__												= attention_probs_dropout_prob
  a__												= type_sequence_label_size
  a__												= initializer_range
  a__												= num_labels
  a__												= scope
  a__												= n_targets
  a__												= 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
  a__												= (image_size[1] // patch_size) * (image_size[0] // patch_size)
  a__												= num_patches + 1 + self.num_detection_tokens

 def 						_UpperCAmelCase       (	self      ) ->			List[str]:
  a__												= floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]      )

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

  a__												= self.get_config()

  return config, pixel_values, labels

 def 						_UpperCAmelCase       (	self      ) ->			Union[str, Any]:
  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=__UpperCamelCase		,				initializer_range=self.initializer_range		,				num_detection_tokens=self.num_detection_tokens		,				num_labels=self.num_labels		,				)

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ) ->			Union[str, Any]:
  a__												= YolosModel(config=__UpperCamelCase      )
  model.to(__UpperCamelCase      )
  model.eval()
  a__												= model(__UpperCamelCase      )
  self.parent.assertEqual(
      result.last_hidden_state.shape		,				(self.batch_size, self.expected_seq_len, self.hidden_size)      )

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ) ->			Optional[Any]:
  a__												= YolosForObjectDetection(__UpperCamelCase      )
  model.to(__UpperCamelCase      )
  model.eval()

  a__												= model(pixel_values=__UpperCamelCase      )
  a__												= model(__UpperCamelCase      )

  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)      )

  a__												= model(pixel_values=__UpperCamelCase		,				labels=__UpperCamelCase      )

  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 						_UpperCAmelCase       (	self      ) ->			Optional[int]:
  a__												= self.prepare_config_and_inputs()
  a__							,			a__							,			a__												= config_and_inputs
  a__												= {'''pixel_values''': pixel_values}
  return config, inputs_dict





@require_torch
class    __UpperCamelCase			(       _a	,	_a	,	unittest.TestCase				):

 """simple docstring"""



 _lowercase   :    List[str]      						=					(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
 _lowercase   :    Optional[Any]      						=					(
     {'''feature-extraction''': YolosModel, '''object-detection''': YolosForObjectDetection} if is_torch_available() else {}
 )

 _lowercase   :    int      						=					False
 _lowercase   :    List[str]      						=					False
 _lowercase   :    Any      						=					False
 _lowercase   :    Tuple      						=					False

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=False      ) ->			Tuple:
  a__												= super()._prepare_for_class(__UpperCamelCase		,				__UpperCamelCase		,				return_labels=__UpperCamelCase      )

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

  return inputs_dict

 def 						_UpperCAmelCase       (	self      ) ->			Dict:
  a__												= YolosModelTester(self      )
  a__												= ConfigTester(self		,				config_class=__UpperCamelCase		,				has_text_modality=__UpperCamelCase		,				hidden_size=3_7      )

 def 						_UpperCAmelCase       (	self      ) ->			Optional[int]:
  self.config_tester.run_common_tests()

 def 						_UpperCAmelCase       (	self      ) ->			Optional[int]:
  # YOLOS does not use inputs_embeds
  pass

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

  for model_class in self.all_model_classes:
   a__												= model_class(__UpperCamelCase      )
   self.assertIsInstance(model.get_input_embeddings()		,				(nn.Module)      )
   a__												= model.get_output_embeddings()
   self.assertTrue(x is None or isinstance(__UpperCamelCase		,				nn.Linear      )      )

 def 						_UpperCAmelCase       (	self      ) ->			Dict:
  a__							,			a__												= self.model_tester.prepare_config_and_inputs_for_common()

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

   a__												= ['''pixel_values''']
   self.assertListEqual(arg_names[:1]		,				__UpperCamelCase      )

 def 						_UpperCAmelCase       (	self      ) ->			Dict:
  a__												= self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_model(*__UpperCamelCase      )

 def 						_UpperCAmelCase       (	self      ) ->			Any:
  a__							,			a__												= self.model_tester.prepare_config_and_inputs_for_common()
  a__												= True

  # in YOLOS, the seq_len is different
  a__												= self.model_tester.expected_seq_len
  for model_class in self.all_model_classes:
   a__												= True
   a__												= False
   a__												= True
   a__												= model_class(__UpperCamelCase      )
   model.to(__UpperCamelCase      )
   model.eval()
   with torch.no_grad():
    a__												= model(**self._prepare_for_class(__UpperCamelCase		,				__UpperCamelCase      )      )
   a__												= outputs.attentions
   self.assertEqual(len(__UpperCamelCase      )		,				self.model_tester.num_hidden_layers      )

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

   self.assertListEqual(
       list(attentions[0].shape[-3:]      )		,				[self.model_tester.num_attention_heads, seq_len, seq_len]		,				)
   a__												= len(__UpperCamelCase      )

   # Check attention is always last and order is fine
   a__												= True
   a__												= True
   a__												= model_class(__UpperCamelCase      )
   model.to(__UpperCamelCase      )
   model.eval()
   with torch.no_grad():
    a__												= model(**self._prepare_for_class(__UpperCamelCase		,				__UpperCamelCase      )      )

   a__												= 1
   self.assertEqual(out_len + added_hidden_states		,				len(__UpperCamelCase      )      )

   a__												= outputs.attentions

   self.assertEqual(len(__UpperCamelCase      )		,				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 						_UpperCAmelCase       (	self      ) ->			Dict:
  def check_hidden_states_output(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ):
   a__												= model_class(__UpperCamelCase      )
   model.to(__UpperCamelCase      )
   model.eval()

   with torch.no_grad():
    a__												= model(**self._prepare_for_class(__UpperCamelCase		,				__UpperCamelCase      )      )

   a__												= outputs.hidden_states

   a__												= getattr(
       self.model_tester		,				'''expected_num_hidden_layers'''		,				self.model_tester.num_hidden_layers + 1      )
   self.assertEqual(len(__UpperCamelCase      )		,				__UpperCamelCase      )

   # YOLOS has a different seq_length
   a__												= self.model_tester.expected_seq_len

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

  a__							,			a__												= self.model_tester.prepare_config_and_inputs_for_common()

  for model_class in self.all_model_classes:
   a__												= True
   check_hidden_states_output(__UpperCamelCase		,				__UpperCamelCase		,				__UpperCamelCase      )

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

   check_hidden_states_output(__UpperCamelCase		,				__UpperCamelCase		,				__UpperCamelCase      )

 def 						_UpperCAmelCase       (	self      ) ->			List[str]:
  a__												= self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_object_detection(*__UpperCamelCase      )



 @slow
 def 						_UpperCAmelCase       (	self      ) ->			Any:
  for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   a__												= YolosModel.from_pretrained(__UpperCamelCase      )
   self.assertIsNotNone(__UpperCamelCase      )






def     __a					(		):
 a__												= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
 return image

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

 """simple docstring"""



 @cached_property
 def 						_UpperCAmelCase       (	self      ) ->			Any:
  return AutoImageProcessor.from_pretrained('''hustvl/yolos-small'''      ) if is_vision_available() else None



 @slow
 def 						_UpperCAmelCase       (	self      ) ->			Dict:
  a__												= YolosForObjectDetection.from_pretrained('''hustvl/yolos-small'''      ).to(__UpperCamelCase      )

  a__												= self.default_image_processor
  a__												= prepare_img()
  a__												= image_processor(images=__UpperCamelCase		,				return_tensors='''pt'''      ).to(__UpperCamelCase      )

  # forward pass
  with torch.no_grad():
   a__												= model(inputs.pixel_values      )

  # verify outputs
  a__												= torch.Size((1, 1_0_0, 9_2)      )
  self.assertEqual(outputs.logits.shape		,				__UpperCamelCase      )

  a__												= 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=__UpperCamelCase		,				)
  a__												= 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=__UpperCamelCase      )
  self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]		,				__UpperCamelCase		,				atol=1e-4      )      )
  self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]		,				__UpperCamelCase		,				atol=1e-4      )      )

  # verify postprocessing
  a__												= image_processor.post_process_object_detection(
      __UpperCamelCase		,				threshold=0.3		,				target_sizes=[image.size[::-1]]      )[0]
  a__												= torch.tensor([0.99_94, 0.97_90, 0.99_64, 0.99_72, 0.98_61]      ).to(__UpperCamelCase      )
  a__												= [7_5, 7_5, 1_7, 6_3, 1_7]
  a__												= torch.tensor([3_3_5.0_6_0_9, 79.38_48, 3_7_5.4_2_1_6, 1_8_7.2_4_9_5]      ).to(__UpperCamelCase      )

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







import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import VivitImageProcessor



class    a_ (			unittest.TestCase    ):






			def __init__(      self				,			__UpperCamelCase				,			__UpperCamelCase=7				,			__UpperCamelCase=3				,			__UpperCamelCase=10				,			__UpperCamelCase=18				,			__UpperCamelCase=30				,			__UpperCamelCase=400				,			__UpperCamelCase=True				,			__UpperCamelCase=None				,			__UpperCamelCase=True				,			__UpperCamelCase=[0.5, 0.5, 0.5]				,			__UpperCamelCase=[0.5, 0.5, 0.5]				,			__UpperCamelCase=None				,			):
									_lowercase					       =					size if size is not None else {"""shortest_edge""": 18}
									_lowercase					       =					crop_size if crop_size is not None else {"""height""": 18, """width""": 18}

									_lowercase					       =					parent
									_lowercase					       =					batch_size
									_lowercase					       =					num_channels
									_lowercase					       =					num_frames
									_lowercase					       =					image_size
									_lowercase					       =					min_resolution
									_lowercase					       =					max_resolution
									_lowercase					       =					do_resize
									_lowercase					       =					size
									_lowercase					       =					do_normalize
									_lowercase					       =					image_mean
									_lowercase					       =					image_std
									_lowercase					       =					crop_size






			def    UpperCamelCase_				(      self  ):
									return {
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									    "do_normalize": self.do_normalize,
									    "do_resize": self.do_resize,
									    "size": self.size,
									    "crop_size": self.crop_size,
									}







@require_torch
@require_vision
class    a_ (			_a					,			unittest.TestCase    ):
			a	:			str					   =							VivitImageProcessor if is_vision_available() else None






			def    UpperCamelCase_				(      self  ):
									_lowercase					       =					VivitImageProcessingTester(self  )






			@property
			def    UpperCamelCase_				(      self  ):
									return self.image_processor_tester.prepare_image_processor_dict()






			def    UpperCamelCase_				(      self  ):
									_lowercase					       =					self.image_processing_class(**self.image_processor_dict  )
									self.assertTrue(hasattr(__UpperCamelCase				,			"""image_mean"""  )  )
									self.assertTrue(hasattr(__UpperCamelCase				,			"""image_std"""  )  )
									self.assertTrue(hasattr(__UpperCamelCase				,			"""do_normalize"""  )  )
									self.assertTrue(hasattr(__UpperCamelCase				,			"""do_resize"""  )  )
									self.assertTrue(hasattr(__UpperCamelCase				,			"""do_center_crop"""  )  )
									self.assertTrue(hasattr(__UpperCamelCase				,			"""size"""  )  )






			def    UpperCamelCase_				(      self  ):
									_lowercase					       =					self.image_processing_class.from_dict(self.image_processor_dict  )
									self.assertEqual(image_processor.size				,			{"""shortest_edge""": 18}  )
									self.assertEqual(image_processor.crop_size				,			{"""height""": 18, """width""": 18}  )

									_lowercase					       =					self.image_processing_class.from_dict(self.image_processor_dict				,			size=42				,			crop_size=84  )
									self.assertEqual(image_processor.size				,			{"""shortest_edge""": 42}  )
									self.assertEqual(image_processor.crop_size				,			{"""height""": 84, """width""": 84}  )






			def    UpperCamelCase_				(      self  ):
									# Initialize image_processing
									_lowercase					       =					self.image_processing_class(**self.image_processor_dict  )
									# create random PIL videos
									_lowercase					       =					prepare_video_inputs(self.image_processor_tester				,			equal_resolution=__UpperCamelCase  )
									for video in video_inputs:
															self.assertIsInstance(__UpperCamelCase				,			__UpperCamelCase  )
															self.assertIsInstance(video[0]				,			Image.Image  )

									# Test not batched input
									_lowercase					       =					image_processing(video_inputs[0]				,			return_tensors="""pt"""  ).pixel_values
									self.assertEqual(
									    encoded_videos.shape				,			(
									        1,
									        self.image_processor_tester.num_frames,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    )				,			)

									# Test batched
									_lowercase					       =					image_processing(__UpperCamelCase				,			return_tensors="""pt"""  ).pixel_values
									self.assertEqual(
									    encoded_videos.shape				,			(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_frames,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    )				,			)






			def    UpperCamelCase_				(      self  ):
									# Initialize image_processing
									_lowercase					       =					self.image_processing_class(**self.image_processor_dict  )
									# create random numpy tensors
									_lowercase					       =					prepare_video_inputs(self.image_processor_tester				,			equal_resolution=__UpperCamelCase				,			numpify=__UpperCamelCase  )
									for video in video_inputs:
															self.assertIsInstance(__UpperCamelCase				,			__UpperCamelCase  )
															self.assertIsInstance(video[0]				,			np.ndarray  )

									# Test not batched input
									_lowercase					       =					image_processing(video_inputs[0]				,			return_tensors="""pt"""  ).pixel_values
									self.assertEqual(
									    encoded_videos.shape				,			(
									        1,
									        self.image_processor_tester.num_frames,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    )				,			)

									# Test batched
									_lowercase					       =					image_processing(__UpperCamelCase				,			return_tensors="""pt"""  ).pixel_values
									self.assertEqual(
									    encoded_videos.shape				,			(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_frames,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    )				,			)






			def    UpperCamelCase_				(      self  ):
									# Initialize image_processing
									_lowercase					       =					self.image_processing_class(**self.image_processor_dict  )
									# create random PyTorch tensors
									_lowercase					       =					prepare_video_inputs(self.image_processor_tester				,			equal_resolution=__UpperCamelCase				,			torchify=__UpperCamelCase  )
									for video in video_inputs:
															self.assertIsInstance(__UpperCamelCase				,			__UpperCamelCase  )
															self.assertIsInstance(video[0]				,			torch.Tensor  )

									# Test not batched input
									_lowercase					       =					image_processing(video_inputs[0]				,			return_tensors="""pt"""  ).pixel_values
									self.assertEqual(
									    encoded_videos.shape				,			(
									        1,
									        self.image_processor_tester.num_frames,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    )				,			)

									# Test batched
									_lowercase					       =					image_processing(__UpperCamelCase				,			return_tensors="""pt"""  ).pixel_values
									self.assertEqual(
									    encoded_videos.shape				,			(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_frames,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    )				,			)