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:			Optional[Any]				,				__lowerCAmelCase:			str				,				__lowerCAmelCase:			Any=13				,				__lowerCAmelCase:			str=[30, 30]				,				__lowerCAmelCase:			int=2				,				__lowerCAmelCase:			Tuple=3				,				__lowerCAmelCase:			Optional[int]=True				,				__lowerCAmelCase:			Optional[Any]=True				,				__lowerCAmelCase:			Dict=32				,				__lowerCAmelCase:			List[str]=5				,				__lowerCAmelCase:			Dict=4				,				__lowerCAmelCase:			str=37				,				__lowerCAmelCase:			Any="gelu"				,				__lowerCAmelCase:			List[Any]=0.1				,				__lowerCAmelCase:			int=0.1				,				__lowerCAmelCase:			int=10				,				__lowerCAmelCase:			Optional[Any]=0.02				,				__lowerCAmelCase:			Optional[Any]=3				,				__lowerCAmelCase:			Union[str, Any]=None				,				__lowerCAmelCase:			List[str]=8				,				__lowerCAmelCase:			Union[str, Any]=10				,				) -> List[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       parent
    __UpperCAmelCase						    =       batch_size
    __UpperCAmelCase						    =       image_size
    __UpperCAmelCase						    =       patch_size
    __UpperCAmelCase						    =       num_channels
    __UpperCAmelCase						    =       is_training
    __UpperCAmelCase						    =       use_labels
    __UpperCAmelCase						    =       hidden_size
    __UpperCAmelCase						    =       num_hidden_layers
    __UpperCAmelCase						    =       num_attention_heads
    __UpperCAmelCase						    =       intermediate_size
    __UpperCAmelCase						    =       hidden_act
    __UpperCAmelCase						    =       hidden_dropout_prob
    __UpperCAmelCase						    =       attention_probs_dropout_prob
    __UpperCAmelCase						    =       type_sequence_label_size
    __UpperCAmelCase						    =       initializer_range
    __UpperCAmelCase						    =       num_labels
    __UpperCAmelCase						    =       scope
    __UpperCAmelCase						    =       n_targets
    __UpperCAmelCase						    =       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
    __UpperCAmelCase						    =       (image_size[1] // patch_size) * (image_size[0] // patch_size)
    __UpperCAmelCase						    =       num_patches + 1 + self.num_detection_tokens






  def 	_UpperCAmelCase				(				self:			Tuple   ) -> Dict:




    '''simple docstring'''




    __UpperCAmelCase						    =       floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]   )

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

    __UpperCAmelCase						    =       self.get_config()

    return config, pixel_values, labels






  def 	_UpperCAmelCase				(				self:			Any   ) -> int:




    '''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=UpperCamelCase__				,				initializer_range=self.initializer_range				,				num_detection_tokens=self.num_detection_tokens				,				num_labels=self.num_labels				,				)






  def 	_UpperCAmelCase				(				self:			Optional[int]				,				__lowerCAmelCase:			Any				,				__lowerCAmelCase:			Tuple				,				__lowerCAmelCase:			int   ) -> Optional[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       YolosModel(config=UpperCamelCase__   )
    model.to(UpperCamelCase__   )
    model.eval()
    __UpperCAmelCase						    =       model(UpperCamelCase__   )
    self.parent.assertEqual(
        result.last_hidden_state.shape				,				(self.batch_size, self.expected_seq_len, self.hidden_size)   )






  def 	_UpperCAmelCase				(				self:			Dict				,				__lowerCAmelCase:			Union[str, Any]				,				__lowerCAmelCase:			Union[str, Any]				,				__lowerCAmelCase:			str   ) -> Any:




    '''simple docstring'''




    __UpperCAmelCase						    =       YolosForObjectDetection(UpperCamelCase__   )
    model.to(UpperCamelCase__   )
    model.eval()

    __UpperCAmelCase						    =       model(pixel_values=UpperCamelCase__   )
    __UpperCAmelCase						    =       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)   )

    __UpperCAmelCase						    =       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:			Tuple   ) -> Optional[int]:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.prepare_config_and_inputs()
    __UpperCAmelCase						    =       config_and_inputs
    __UpperCAmelCase						    =       {'''pixel_values''': pixel_values}
    return config, inputs_dict






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



  """simple docstring"""






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

  lowerCAmelCase__		:	str    		= False
  lowerCAmelCase__		:	Any    		= False
  lowerCAmelCase__		:	Tuple    		= False
  lowerCAmelCase__		:	int    		= False






  def 	_UpperCAmelCase				(				self:			Union[str, Any]				,				__lowerCAmelCase:			List[Any]				,				__lowerCAmelCase:			int				,				__lowerCAmelCase:			Tuple=False   ) -> Union[str, Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       super()._prepare_for_class(UpperCamelCase__				,				UpperCamelCase__				,				return_labels=UpperCamelCase__   )

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

    return inputs_dict






  def 	_UpperCAmelCase				(				self:			Union[str, Any]   ) -> str:




    '''simple docstring'''




    __UpperCAmelCase						    =       YolosModelTester(self   )
    __UpperCAmelCase						    =       ConfigTester(self				,				config_class=UpperCamelCase__				,				has_text_modality=UpperCamelCase__				,				hidden_size=37   )






  def 	_UpperCAmelCase				(				self:			Union[str, Any]   ) -> str:




    '''simple docstring'''




    self.config_tester.run_common_tests()






  def 	_UpperCAmelCase				(				self:			List[str]   ) -> List[str]:




    '''simple docstring'''




    pass






  def 	_UpperCAmelCase				(				self:			int   ) -> List[str]:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

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






  def 	_UpperCAmelCase				(				self:			Tuple   ) -> Optional[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

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

      __UpperCAmelCase						    =       ['''pixel_values''']
      self.assertListEqual(arg_names[:1]				,				UpperCamelCase__   )






  def 	_UpperCAmelCase				(				self:			Tuple   ) -> List[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*UpperCamelCase__   )






  def 	_UpperCAmelCase				(				self:			str   ) -> str:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()
    __UpperCAmelCase						    =       True

    # in YOLOS, the seq_len is different
    __UpperCAmelCase						    =       self.model_tester.expected_seq_len
    for model_class in self.all_model_classes:
      __UpperCAmelCase						    =       True
      __UpperCAmelCase						    =       False
      __UpperCAmelCase						    =       True
      __UpperCAmelCase						    =       model_class(UpperCamelCase__   )
      model.to(UpperCamelCase__   )
      model.eval()
      with torch.no_grad():
        __UpperCAmelCase						    =       model(**self._prepare_for_class(UpperCamelCase__				,				UpperCamelCase__   )   )
      __UpperCAmelCase						    =       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"]
      __UpperCAmelCase						    =       True
      __UpperCAmelCase						    =       model_class(UpperCamelCase__   )
      model.to(UpperCamelCase__   )
      model.eval()
      with torch.no_grad():
        __UpperCAmelCase						    =       model(**self._prepare_for_class(UpperCamelCase__				,				UpperCamelCase__   )   )
      __UpperCAmelCase						    =       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]				,				)
      __UpperCAmelCase						    =       len(UpperCamelCase__   )

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

      __UpperCAmelCase						    =       1
      self.assertEqual(out_len + added_hidden_states				,				len(UpperCamelCase__   )   )

      __UpperCAmelCase						    =       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:			List[Any]   ) -> Any:




    '''simple docstring'''




    def check_hidden_states_output(__lowerCAmelCase:			Union[str, Any]				,				__lowerCAmelCase:			Optional[Any]				,				__lowerCAmelCase:			str   ):
      __UpperCAmelCase						    =       model_class(UpperCamelCase__   )
      model.to(UpperCamelCase__   )
      model.eval()

      with torch.no_grad():
        __UpperCAmelCase						    =       model(**self._prepare_for_class(UpperCamelCase__				,				UpperCamelCase__   )   )

      __UpperCAmelCase						    =       outputs.hidden_states

      __UpperCAmelCase						    =       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
      __UpperCAmelCase						    =       self.model_tester.expected_seq_len

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

    __UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __UpperCAmelCase						    =       True
      check_hidden_states_output(UpperCamelCase__				,				UpperCamelCase__				,				UpperCamelCase__   )

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

      check_hidden_states_output(UpperCamelCase__				,				UpperCamelCase__				,				UpperCamelCase__   )






  def 	_UpperCAmelCase				(				self:			Optional[Any]   ) -> List[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_object_detection(*UpperCamelCase__   )






  @slow
  def 	_UpperCAmelCase				(				self:			Dict   ) -> Tuple:




    '''simple docstring'''




    for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      __UpperCAmelCase						    =       YolosModel.from_pretrained(UpperCamelCase__   )
      self.assertIsNotNone(UpperCamelCase__   )






def       __lowerCAmelCase      (						)    ->  List[str]:
  __UpperCAmelCase						    =       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:			Union[str, Any]   ) -> Optional[Any]:




    '''simple docstring'''




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






  @slow
  def 	_UpperCAmelCase				(				self:			Any   ) -> Dict:




    '''simple docstring'''




    __UpperCAmelCase						    =       YolosForObjectDetection.from_pretrained("hustvl/yolos-small"   ).to(UpperCamelCase__   )

    __UpperCAmelCase						    =       self.default_image_processor
    __UpperCAmelCase						    =       prepare_img()
    __UpperCAmelCase						    =       image_processor(images=UpperCamelCase__				,				return_tensors="pt"   ).to(UpperCamelCase__   )

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

    # verify outputs
    __UpperCAmelCase						    =       torch.Size((1, 100, 92)   )
    self.assertEqual(outputs.logits.shape				,				UpperCamelCase__   )

    __UpperCAmelCase						    =       torch.tensor(
        [[-24.0_248, -10.3_024, -14.8_290], [-42.0_392, -16.8_200, -27.4_334], [-27.2_743, -11.8_154, -18.7_148]]				,				device=UpperCamelCase__				,				)
    __UpperCAmelCase						    =       torch.tensor(
        [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]]				,				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
    __UpperCAmelCase						    =       image_processor.post_process_object_detection(
        UpperCamelCase__				,				threshold=0.3				,				target_sizes=[image.size[::-1]]   )[0]
    __UpperCAmelCase						    =       torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861]   ).to(UpperCamelCase__   )
    __UpperCAmelCase						    =       [75, 75, 17, 63, 17]
    __UpperCAmelCase						    =       torch.tensor([335.0_609, 79.3_848, 375.4_216, 187.2_495]   ).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 warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding


class 			UpperCAmelCase__    (		snake_case					):



  """simple docstring"""






  lowerCAmelCase__		:	Optional[int]    		= ['image_processor', 'tokenizer']
  lowerCAmelCase__		:	Any    		= 'CLIPImageProcessor'
  lowerCAmelCase__		:	Any    		= ('CLIPTokenizer', 'CLIPTokenizerFast')






  def __init__(				self:			str				,				__lowerCAmelCase:			List[Any]=None				,				__lowerCAmelCase:			Any=None				,				**__lowerCAmelCase:			str   ) -> Optional[int]:




    '''simple docstring'''




    __UpperCAmelCase						    =       None
    if "feature_extractor" in kwargs:
      warnings.warn(
          "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
          " instead."				,				__lowerCAmelCase				,				)
      __UpperCAmelCase						    =       kwargs.pop("feature_extractor"   )

    __UpperCAmelCase						    =       image_processor if image_processor is not None else feature_extractor
    if image_processor is None:
      raise ValueError("You need to specify an `image_processor`."   )
    if tokenizer is None:
      raise ValueError("You need to specify a `tokenizer`."   )

    super().__init__(__lowerCAmelCase				,				__lowerCAmelCase   )






  def __call__(				self:			Optional[int]				,				__lowerCAmelCase:			Union[str, Any]=None				,				__lowerCAmelCase:			Dict=None				,				__lowerCAmelCase:			Optional[int]=None				,				**__lowerCAmelCase:			Optional[int]   ) -> Dict:




    '''simple docstring'''




    if text is None and images is None:
      raise ValueError("You have to specify either text or images. Both cannot be none."   )

    if text is not None:
      __UpperCAmelCase						    =       self.tokenizer(__lowerCAmelCase				,				return_tensors=__lowerCAmelCase				,				**__lowerCAmelCase   )

    if images is not None:
      __UpperCAmelCase						    =       self.image_processor(__lowerCAmelCase				,				return_tensors=__lowerCAmelCase				,				**__lowerCAmelCase   )

    if text is not None and images is not None:
      __UpperCAmelCase						    =       image_features.pixel_values
      return encoding
    elif text is not None:
      return encoding
    else:
      return BatchEncoding(data=dict(**__lowerCAmelCase   )				,				tensor_type=__lowerCAmelCase   )






  def 	_UpperCAmelCase				(				self:			str				,				*__lowerCAmelCase:			str				,				**__lowerCAmelCase:			List[Any]   ) -> Optional[Any]:




    '''simple docstring'''




    return self.tokenizer.batch_decode(*__lowerCAmelCase				,				**__lowerCAmelCase   )






  def 	_UpperCAmelCase				(				self:			Any				,				*__lowerCAmelCase:			List[Any]				,				**__lowerCAmelCase:			Dict   ) -> Union[str, Any]:




    '''simple docstring'''




    return self.tokenizer.decode(*__lowerCAmelCase				,				**__lowerCAmelCase   )






  @property
  def 	_UpperCAmelCase				(				self:			Tuple   ) -> List[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.tokenizer.model_input_names
    __UpperCAmelCase						    =       self.image_processor.model_input_names
    return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names   )   )






  @property
  def 	_UpperCAmelCase				(				self:			Optional[int]   ) -> str:




    '''simple docstring'''




    warnings.warn(
        "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."				,				__lowerCAmelCase				,				)
    return self.image_processor_class






  @property
  def 	_UpperCAmelCase				(				self:			List[Any]   ) -> List[str]:




    '''simple docstring'''




    warnings.warn(
        "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."				,				__lowerCAmelCase				,				)
    return self.image_processor



