





import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
				from PIL import Image
else:

				class 	A_       :
					@staticmethod
					def      _lowercase				(    *_A							,					**_A		):






								'''simple docstring'''







								pass

@is_pipeline_test
@require_torch
@require_vision
class 	A_       (unittest.TestCase			):
	UpperCAmelCase__					=			MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING
	def      _lowercase				(    self							,					_A							,					_A							,					_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				pipeline('''visual-question-answering'''							,					model='''hf-internal-testing/tiny-vilt-random-vqa'''		)
				UpperCAmelCase   		=				[
				    {
				        """image""": Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		),
				        """question""": """How many cats are there?""",
				    },
				    {
				        """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
				        """question""": """How many cats are there?""",
				    },
				]
				return vqa_pipeline, examples
	def      _lowercase				(    self							,					_A							,					_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				vqa_pipeline(_A							,					top_k=1		)
				self.assertEqual(
				    _A							,					[
				        [{'''score''': ANY(_A		), '''answer''': ANY(_A		)}],
				        [{'''score''': ANY(_A		), '''answer''': ANY(_A		)}],
				    ]							,					)
	@require_torch
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				pipeline('''visual-question-answering'''							,					model='''hf-internal-testing/tiny-vilt-random-vqa'''		)
				UpperCAmelCase   		=				"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
				UpperCAmelCase   		=				"""How many cats are there?"""

				UpperCAmelCase   		=				vqa_pipeline(image=_A							,					question='''How many cats are there?'''							,					top_k=2		)
				self.assertEqual(
				    _A							,					[{'''score''': ANY(_A		), '''answer''': ANY(_A		)}, {'''score''': ANY(_A		), '''answer''': ANY(_A		)}]		)

				UpperCAmelCase   		=				vqa_pipeline({'''image''': image, '''question''': question}							,					top_k=2		)
				self.assertEqual(
				    _A							,					[{'''score''': ANY(_A		), '''answer''': ANY(_A		)}, {'''score''': ANY(_A		), '''answer''': ANY(_A		)}]		)
	@slow
	@require_torch
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				pipeline('''visual-question-answering'''							,					model='''dandelin/vilt-b32-finetuned-vqa'''		)
				UpperCAmelCase   		=				"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
				UpperCAmelCase   		=				"""How many cats are there?"""

				UpperCAmelCase   		=				vqa_pipeline(image=_A							,					question=_A							,					top_k=2		)
				self.assertEqual(
				    nested_simplify(_A							,					decimals=4		)							,					[{'''score''': 0.87_99, '''answer''': '''2'''}, {'''score''': 0.2_96, '''answer''': '''1'''}]		)

				UpperCAmelCase   		=				vqa_pipeline({'''image''': image, '''question''': question}							,					top_k=2		)
				self.assertEqual(
				    nested_simplify(_A							,					decimals=4		)							,					[{'''score''': 0.87_99, '''answer''': '''2'''}, {'''score''': 0.2_96, '''answer''': '''1'''}]		)

				UpperCAmelCase   		=				vqa_pipeline(
				    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]							,					top_k=2		)
				self.assertEqual(
				    nested_simplify(_A							,					decimals=4		)							,					[[{'''score''': 0.87_99, '''answer''': '''2'''}, {'''score''': 0.2_96, '''answer''': '''1'''}]] * 2							,					)







	@require_tf
	@unittest.skip('''Visual question answering not implemented in TF'''		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				pass





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_image_inputs


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import MobileNetVaImageProcessor

class __snake_case		(      unittest.TestCase     ):
    def __init__(  self		,snake_case		,snake_case=7		,snake_case=3		,snake_case=18		,snake_case=30		,snake_case=400		,snake_case=True		,snake_case=None		,snake_case=True		,snake_case=None		,):



           '''simple docstring'''







           lowercase							:     Dict       	=      size if size is not None else {"""shortest_edge""": 20}
           lowercase							:     Union[str, Any]       	=      crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
           lowercase							:     str       	=      parent
           lowercase							:     int       	=      batch_size
           lowercase							:     str       	=      num_channels
           lowercase							:     int       	=      image_size
           lowercase							:     List[str]       	=      min_resolution
           lowercase							:     str       	=      max_resolution
           lowercase							:     Dict       	=      do_resize
           lowercase							:     Dict       	=      size
           lowercase							:     Dict       	=      do_center_crop
           lowercase							:     str       	=      crop_size
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           return {
               "do_resize": self.do_resize,
               "size": self.size,
               "do_center_crop": self.do_center_crop,
               "crop_size": self.crop_size,
           }






@require_torch
@require_vision
class __snake_case		(      lowerCAmelCase					,	unittest.TestCase     ):
    _a		:						Any=				MobileNetVaImageProcessor if is_vision_available() else None
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     List[Any]       	=      MobileNetVaImageProcessingTester(self      )
    @property
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           return self.image_processor_tester.prepare_image_processor_dict()
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Optional[int]       	=      self.image_processing_class(**self.image_processor_dict      )
           self.assertTrue(hasattr(snake_case		,"""do_resize"""      )      )
           self.assertTrue(hasattr(snake_case		,"""size"""      )      )
           self.assertTrue(hasattr(snake_case		,"""do_center_crop"""      )      )
           self.assertTrue(hasattr(snake_case		,"""crop_size"""      )      )
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     int       	=      self.image_processing_class.from_dict(self.image_processor_dict      )
           self.assertEqual(image_processor.size		,{"""shortest_edge""": 20}      )
           self.assertEqual(image_processor.crop_size		,{"""height""": 18, """width""": 18}      )

           lowercase							:     int       	=      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       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           pass
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Optional[Any]       	=      self.image_processing_class(**self.image_processor_dict      )
           # create random PIL images
           lowercase							:     str       	=      prepare_image_inputs(self.image_processor_tester		,equal_resolution=snake_case      )
           for image in image_inputs:
                  self.assertIsInstance(snake_case		,Image.Image      )

           # Test not batched input
           lowercase							:     Dict       	=      image_processing(image_inputs[0]		,return_tensors="""pt"""      ).pixel_values
           self.assertEqual(
               encoded_images.shape		,(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )		,)

           # Test batched
           lowercase							:     Tuple       	=      image_processing(snake_case		,return_tensors="""pt"""      ).pixel_values
           self.assertEqual(
               encoded_images.shape		,(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )		,)
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     List[Any]       	=      self.image_processing_class(**self.image_processor_dict      )
           # create random numpy tensors
           lowercase							:     Union[str, Any]       	=      prepare_image_inputs(self.image_processor_tester		,equal_resolution=snake_case		,numpify=snake_case      )
           for image in image_inputs:
                  self.assertIsInstance(snake_case		,np.ndarray      )

           # Test not batched input
           lowercase							:     Optional[Any]       	=      image_processing(image_inputs[0]		,return_tensors="""pt"""      ).pixel_values
           self.assertEqual(
               encoded_images.shape		,(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )		,)

           # Test batched
           lowercase							:     List[str]       	=      image_processing(snake_case		,return_tensors="""pt"""      ).pixel_values
           self.assertEqual(
               encoded_images.shape		,(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )		,)
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Optional[Any]       	=      self.image_processing_class(**self.image_processor_dict      )
           # create random PyTorch tensors
           lowercase							:     Tuple       	=      prepare_image_inputs(self.image_processor_tester		,equal_resolution=snake_case		,torchify=snake_case      )
           for image in image_inputs:
                  self.assertIsInstance(snake_case		,torch.Tensor      )

           # Test not batched input
           lowercase							:     Optional[int]       	=      image_processing(image_inputs[0]		,return_tensors="""pt"""      ).pixel_values
           self.assertEqual(
               encoded_images.shape		,(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )		,)

           # Test batched
           lowercase							:     List[str]       	=      image_processing(snake_case		,return_tensors="""pt"""      ).pixel_values
           self.assertEqual(
               encoded_images.shape		,(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )		,)
