


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





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





     def      __A					(self       )     ->   str:
      _lowercase							      =floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]       )

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

      _lowercase							      =self.get_config()

      return config, pixel_values, labels





     def      __A					(self       )     ->   Dict:
      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      __A					(self		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase       )     ->   Dict:
      _lowercase							      =YolosModel(config=UpperCAmelCase       )
      model.to(UpperCAmelCase       )
      model.eval()
      _lowercase							      =model(UpperCAmelCase       )
      self.parent.assertEqual(
          result.last_hidden_state.shape		,     (self.batch_size, self.expected_seq_len, self.hidden_size)       )





     def      __A					(self		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase       )     ->   List[str]:
      _lowercase							      =YolosForObjectDetection(UpperCAmelCase       )
      model.to(UpperCAmelCase       )
      model.eval()

      _lowercase							      =model(pixel_values=UpperCAmelCase       )
      _lowercase							      =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)       )

      _lowercase							      =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      __A					(self       )     ->   List[Any]:
      _lowercase							      =self.prepare_config_and_inputs()
      _lowercase      ,    _lowercase      ,    _lowercase							      =config_and_inputs
      _lowercase							      ={'''pixel_values''': pixel_values}
      return config, inputs_dict







@require_torch
class       lowerCamelCase__   (       lowerCAmelCase   ,							lowerCAmelCase   ,							unittest.TestCase):
     SCREAMING_SNAKE_CASE__      			=			(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
     SCREAMING_SNAKE_CASE__      			=			(
         {'''feature-extraction''': YolosModel, '''object-detection''': YolosForObjectDetection} if is_torch_available() else {}
     )

     SCREAMING_SNAKE_CASE__      			=			False
     SCREAMING_SNAKE_CASE__      			=			False
     SCREAMING_SNAKE_CASE__      			=			False
     SCREAMING_SNAKE_CASE__      			=			False





     def      __A					(self		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase=False       )     ->   Optional[int]:
      _lowercase							      =super()._prepare_for_class(UpperCAmelCase		,     UpperCAmelCase		,     return_labels=UpperCAmelCase       )

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

      return inputs_dict





     def      __A					(self       )     ->   List[Any]:
      _lowercase							      =YolosModelTester(self       )
      _lowercase							      =ConfigTester(self		,     config_class=UpperCAmelCase		,     has_text_modality=UpperCAmelCase		,     hidden_size=3_7       )





     def      __A					(self       )     ->   List[str]:
      self.config_tester.run_common_tests()





     def      __A					(self       )     ->   Dict:
      # YOLOS does not use inputs_embeds
      pass





     def      __A					(self       )     ->   Any:
      _lowercase      ,    _lowercase							      =self.model_tester.prepare_config_and_inputs_for_common()

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





     def      __A					(self       )     ->   int:
      _lowercase      ,    _lowercase							      =self.model_tester.prepare_config_and_inputs_for_common()

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

       _lowercase							      =['''pixel_values''']
       self.assertListEqual(arg_names[:1]		,     UpperCAmelCase       )





     def      __A					(self       )     ->   Tuple:
      _lowercase							      =self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*UpperCAmelCase       )





     def      __A					(self       )     ->   Tuple:
      _lowercase      ,    _lowercase							      =self.model_tester.prepare_config_and_inputs_for_common()
      _lowercase							      =True

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

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

       _lowercase							      =1
       self.assertEqual(out_len + added_hidden_states		,     len(UpperCAmelCase       )       )

       _lowercase							      =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      __A					(self       )     ->   Any:
      def check_hidden_states_output(UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase       ):
       _lowercase							      =model_class(UpperCAmelCase       )
       model.to(UpperCAmelCase       )
       model.eval()

       with torch.no_grad():
        _lowercase							      =model(**self._prepare_for_class(UpperCAmelCase		,     UpperCAmelCase       )       )

       _lowercase							      =outputs.hidden_states

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

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

      _lowercase      ,    _lowercase							      =self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
       _lowercase							      =True
       check_hidden_states_output(UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase       )

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

       check_hidden_states_output(UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase       )





     def      __A					(self       )     ->   List[Any]:
      _lowercase							      =self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_object_detection(*UpperCAmelCase       )





     @slow
     def      __A					(self       )     ->   int:
      for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _lowercase							      =YolosModel.from_pretrained(UpperCAmelCase       )
       self.assertIsNotNone(UpperCAmelCase       )



def 				UpperCAmelCase_					(							)     ->			List[str]:







 """simple docstring"""


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







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





     @cached_property
     def      __A					(self       )     ->   Optional[int]:
      return AutoImageProcessor.from_pretrained('''hustvl/yolos-small'''       ) if is_vision_available() else None





     @slow
     def      __A					(self       )     ->   Optional[int]:
      _lowercase							      =YolosForObjectDetection.from_pretrained('''hustvl/yolos-small'''       ).to(UpperCAmelCase       )

      _lowercase							      =self.default_image_processor
      _lowercase							      =prepare_img()
      _lowercase							      =image_processor(images=UpperCAmelCase		,     return_tensors='''pt'''       ).to(UpperCAmelCase       )

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

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

      _lowercase							      =torch.tensor(
          [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]		,     device=UpperCAmelCase		,     )
      _lowercase							      =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
      _lowercase							      =image_processor.post_process_object_detection(
          UpperCAmelCase		,     threshold=0.3		,     target_sizes=[image.size[::-1]]       )[0]
      _lowercase							      =torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861]       ).to(UpperCAmelCase       )
      _lowercase							      =[7_5, 7_5, 1_7, 6_3, 1_7]
      _lowercase							      =torch.tensor([335.0609, 79.3848, 375.4216, 187.2495]       ).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       )       )





'''simple docstring'''
import re

from filelock import FileLock


try:
		import nltk

		__snake_case							      =     True
except (ImportError, ModuleNotFoundError):
		__snake_case							      =     False

if NLTK_AVAILABLE:
		with FileLock('''.lock''') as lock:
				nltk.download('''punkt''', quiet=True)


def 			a      (			__a   )  ->	str:



					'''simple docstring'''






					re.sub('''<n>''' ,					'''''' ,					__a   )  # remove pegasus newline char
					assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)"
					return "\n".join(nltk.sent_tokenize(__a   )   )