





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






        '''simple docstring'''






        __snake_case :					List[str]          =							parent
        __snake_case :					int          =							batch_size
        __snake_case :					int          =							image_size
        __snake_case :					List[Any]          =							patch_size
        __snake_case :					Tuple          =							num_channels
        __snake_case :					Tuple          =							is_training
        __snake_case :					List[Any]          =							use_labels
        __snake_case :					Dict          =							hidden_size
        __snake_case :					Union[str, Any]          =							num_hidden_layers
        __snake_case :					List[str]          =							num_attention_heads
        __snake_case :					str          =							intermediate_size
        __snake_case :					Union[str, Any]          =							hidden_act
        __snake_case :					List[str]          =							hidden_dropout_prob
        __snake_case :					Optional[int]          =							attention_probs_dropout_prob
        __snake_case :					Dict          =							type_sequence_label_size
        __snake_case :					List[Any]          =							initializer_range
        __snake_case :					Union[str, Any]          =							num_labels
        __snake_case :					Union[str, Any]          =							scope
        __snake_case :					Any          =							n_targets
        __snake_case :					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
        __snake_case :					List[Any]          =							(image_size[1] // patch_size) * (image_size[0] // patch_size)
        __snake_case :					Optional[int]          =							num_patches + 1 + self.num_detection_tokens







      def     UpperCAmelCase		(			self )							-> Dict:






        '''simple docstring'''






        __snake_case :					Union[str, Any]          =							floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] )

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

        __snake_case :					Optional[int]          =							self.get_config()

        return config, pixel_values, labels







      def     UpperCAmelCase		(			self )							-> Dict:






        '''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     UpperCAmelCase		(			self					,    UpperCAmelCase					,    UpperCAmelCase					,    UpperCAmelCase )							-> str:






        '''simple docstring'''






        __snake_case :					Tuple          =							YolosModel(config=__lowercase )
        model.to(__lowercase )
        model.eval()
        __snake_case :					Tuple          =							model(__lowercase )
        self.parent.assertEqual(
            result.last_hidden_state.shape					,    (self.batch_size, self.expected_seq_len, self.hidden_size) )







      def     UpperCAmelCase		(			self					,    UpperCAmelCase					,    UpperCAmelCase					,    UpperCAmelCase )							-> List[Any]:






        '''simple docstring'''






        __snake_case :					List[str]          =							YolosForObjectDetection(__lowercase )
        model.to(__lowercase )
        model.eval()

        __snake_case :					List[str]          =							model(pixel_values=__lowercase )
        __snake_case :					Optional[int]          =							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) )

        __snake_case :					int          =							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     UpperCAmelCase		(			self )							-> Union[str, Any]:






        '''simple docstring'''






        __snake_case :					int          =							self.prepare_config_and_inputs()
        __snake_case :					List[str]          =							config_and_inputs
        __snake_case :					Dict          =							{'pixel_values': pixel_values}
        return config, inputs_dict




@require_torch
class 					_lowerCamelCase			(     snake_case_			, snake_case_			, unittest.TestCase     ):



      """simple docstring"""






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

      UpperCAmelCase_    :   int      =False
      UpperCAmelCase_    :   Union[str, Any]      =False
      UpperCAmelCase_    :   int      =False
      UpperCAmelCase_    :   Union[str, Any]      =False







      def     UpperCAmelCase		(			self					,    UpperCAmelCase					,    UpperCAmelCase					,    UpperCAmelCase=False )							-> Tuple:






        '''simple docstring'''






        __snake_case :					List[Any]          =							super()._prepare_for_class(__lowercase					,    __lowercase					,    return_labels=__lowercase )

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

        return inputs_dict







      def     UpperCAmelCase		(			self )							-> Dict:






        '''simple docstring'''






        __snake_case :					Any          =							YolosModelTester(self )
        __snake_case :					str          =							ConfigTester(self					,    config_class=__lowercase					,    has_text_modality=__lowercase					,    hidden_size=37 )







      def     UpperCAmelCase		(			self )							-> Dict:






        '''simple docstring'''






        self.config_tester.run_common_tests()







      def     UpperCAmelCase		(			self )							-> Dict:






        '''simple docstring'''






        pass







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






        '''simple docstring'''






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

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







      def     UpperCAmelCase		(			self )							-> Tuple:






        '''simple docstring'''






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

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

          __snake_case :					Union[str, Any]          =							['pixel_values']
          self.assertListEqual(arg_names[:1]					,    __lowercase )







      def     UpperCAmelCase		(			self )							-> Dict:






        '''simple docstring'''






        __snake_case :					str          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__lowercase )







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






        '''simple docstring'''






        __snake_case :					Any          =							self.model_tester.prepare_config_and_inputs_for_common()
        __snake_case :					Dict          =							True

        # in YOLOS, the seq_len is different
        __snake_case :					Optional[int]          =							self.model_tester.expected_seq_len
        for model_class in self.all_model_classes:
          __snake_case :					int          =							True
          __snake_case :					str          =							False
          __snake_case :					Optional[Any]          =							True
          __snake_case :					Optional[int]          =							model_class(__lowercase )
          model.to(__lowercase )
          model.eval()
          with torch.no_grad():
            __snake_case :					int          =							model(**self._prepare_for_class(__lowercase					,    __lowercase ) )
          __snake_case :					List[Any]          =							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"]
          __snake_case :					str          =							True
          __snake_case :					Union[str, Any]          =							model_class(__lowercase )
          model.to(__lowercase )
          model.eval()
          with torch.no_grad():
            __snake_case :					Tuple          =							model(**self._prepare_for_class(__lowercase					,    __lowercase ) )
          __snake_case :					Union[str, Any]          =							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]					,    )
          __snake_case :					Dict          =							len(__lowercase )

          # Check attention is always last and order is fine
          __snake_case :					Optional[int]          =							True
          __snake_case :					List[str]          =							True
          __snake_case :					Any          =							model_class(__lowercase )
          model.to(__lowercase )
          model.eval()
          with torch.no_grad():
            __snake_case :					str          =							model(**self._prepare_for_class(__lowercase					,    __lowercase ) )

          __snake_case :					List[str]          =							1
          self.assertEqual(out_len + added_hidden_states					,    len(__lowercase ) )

          __snake_case :					List[Any]          =							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     UpperCAmelCase		(			self )							-> List[str]:






        '''simple docstring'''






        def check_hidden_states_output(UpperCAmelCase					,    UpperCAmelCase					,    UpperCAmelCase ):
          __snake_case :					List[Any]          =							model_class(__lowercase )
          model.to(__lowercase )
          model.eval()

          with torch.no_grad():
            __snake_case :					Optional[int]          =							model(**self._prepare_for_class(__lowercase					,    __lowercase ) )

          __snake_case :					List[Any]          =							outputs.hidden_states

          __snake_case :					List[Any]          =							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
          __snake_case :					Dict          =							self.model_tester.expected_seq_len

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

        __snake_case :					Optional[int]          =							self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          __snake_case :					Tuple          =							True
          check_hidden_states_output(__lowercase					,    __lowercase					,    __lowercase )

          # check that output_hidden_states also work using config
          del inputs_dict["output_hidden_states"]
          __snake_case :					str          =							True

          check_hidden_states_output(__lowercase					,    __lowercase					,    __lowercase )







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






        '''simple docstring'''






        __snake_case :					Any          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_object_detection(*__lowercase )







      @slow
      def     UpperCAmelCase		(			self )							-> Any:






        '''simple docstring'''






        for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          __snake_case :					List[str]          =							YolosModel.from_pretrained(__lowercase )
          self.assertIsNotNone(__lowercase )







def    lowerCAmelCase__(						)			->     List[str]:
  __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     UpperCAmelCase		(			self )							-> Any:






        '''simple docstring'''






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







      @slow
      def     UpperCAmelCase		(			self )							-> Optional[int]:






        '''simple docstring'''






        __snake_case :					List[str]          =							YolosForObjectDetection.from_pretrained("hustvl/yolos-small" ).to(__lowercase )

        __snake_case :					Dict          =							self.default_image_processor
        __snake_case :					List[str]          =							prepare_img()
        __snake_case :					List[Any]          =							image_processor(images=__lowercase					,    return_tensors="pt" ).to(__lowercase )

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

        # verify outputs
        __snake_case :					Dict          =							torch.Size((1, 100, 92) )
        self.assertEqual(outputs.logits.shape					,    __lowercase )

        __snake_case :					Optional[Any]          =							torch.tensor(
            [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]					,    device=__lowercase					,    )
        __snake_case :					List[str]          =							torch.tensor(
            [[0.2_559, 0.5_455, 0.4_706], [0.2_989, 0.7_279, 0.1_875], [0.7_732, 0.4_017, 0.4_462]]					,    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
        __snake_case :					int          =							image_processor.post_process_object_detection(
            __lowercase					,    threshold=0.3					,    target_sizes=[image.size[::-1]] )[0]
        __snake_case :					List[Any]          =							torch.tensor([0.9_994, 0.9_790, 0.9_964, 0.9_972, 0.9_861] ).to(__lowercase )
        __snake_case :					Union[str, Any]          =							[75, 75, 17, 63, 17]
        __snake_case :					Any          =							torch.tensor([335.0609, 79.3848, 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'''
# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

import torch

from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
 from PIL import Image

class     _lowerCamelCase       (     snake_case_					):




 '''simple docstring'''
 __lowercase					:		int     				=    '''dandelin/vilt-b32-finetuned-vqa'''
 __lowercase					:		int     				=    (
     '''This is a tool that answers a question about an image. It takes an input named `image` which should be the '''
     '''image containing the information, as well as a `question` which should be the question in English. It '''
     '''returns a text that is the answer to the question.'''
 )
 __lowercase					:		Union[str, Any]     				=    '''image_qa'''
 __lowercase					:		Optional[Any]     				=    AutoProcessor
 __lowercase					:		Tuple     				=    AutoModelForVisualQuestionAnswering

 __lowercase					:		Tuple     				=    ['''image''', '''text''']
 __lowercase					:		List[str]     				=    ['''text''']


 def __init__(   self   ,   *__lowercase   ,   **__lowercase    ):

  """simple docstring"""







  requires_backends(self   ,   ['vision']    )
  super().__init__(*__lowercase   ,   **__lowercase    )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  return self.pre_processor(__lowercase   ,   __lowercase   ,   return_tensors='pt'    )


 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  with torch.no_grad():
   return self.model(**__lowercase    ).logits







 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  __A	:		List[str]	  =      outputs.argmax(-1    ).item()
  return self.model.config.idalabel[idx]






