





'''simple docstring'''






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 GLPNImageProcessor




class 			UpperCAmelCase					(      unittest.TestCase       ):




      def __init__(      self      :			Dict      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			Optional[Any]=7      ,      __snake_case      :			List[str]=3      ,      __snake_case      :			List[str]=18      ,      __snake_case      :			Any=30      ,      __snake_case      :			Any=4_00      ,      __snake_case      :			Union[str, Any]=True      ,      __snake_case      :			List[Any]=32      ,      __snake_case      :			Union[str, Any]=True      ,      )    ->				Tuple:
            _lowerCAmelCase												=							parent
            _lowerCAmelCase												=							batch_size
            _lowerCAmelCase												=							num_channels
            _lowerCAmelCase												=							image_size
            _lowerCAmelCase												=							min_resolution
            _lowerCAmelCase												=							max_resolution
            _lowerCAmelCase												=							do_resize
            _lowerCAmelCase												=							size_divisor
            _lowerCAmelCase												=							do_rescale




      def     lowercase__   (      self      :			Tuple  )    ->				str:
            return {
                "do_resize": self.do_resize,
                "size_divisor": self.size_divisor,
                "do_rescale": self.do_rescale,
            }




@require_torch
@require_vision
class 			UpperCAmelCase					(      snake_case_							,		unittest.TestCase       ):
      _lowercase:      Union[str, Any]											=       GLPNImageProcessor if is_vision_available() else None




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				Tuple:
            _lowerCAmelCase												=							GLPNImageProcessingTester(self  )




      @property
      def     lowercase__   (      self      :			Any  )    ->				int:
            return self.image_processor_tester.prepare_image_processor_dict()




      def     lowercase__   (      self      :			List[str]  )    ->				Optional[int]:
            _lowerCAmelCase												=							self.image_processing_class(**self.image_processor_dict  )
            self.assertTrue(hasattr(__snake_case      ,      """do_resize"""  )  )
            self.assertTrue(hasattr(__snake_case      ,      """size_divisor"""  )  )
            self.assertTrue(hasattr(__snake_case      ,      """resample"""  )  )
            self.assertTrue(hasattr(__snake_case      ,      """do_rescale"""  )  )




      def     lowercase__   (      self      :			List[str]  )    ->				Dict:
            pass




      def     lowercase__   (      self      :			Optional[int]  )    ->				str:
            # Initialize image_processing
            _lowerCAmelCase												=							self.image_processing_class(**self.image_processor_dict  )
            # create random PIL images
            _lowerCAmelCase												=							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 (GLPNImageProcessor doesn't support batching)
            _lowerCAmelCase												=							image_processing(image_inputs[0]      ,      return_tensors="""pt"""  ).pixel_values
            self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0  )
            self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0  )




      def     lowercase__   (      self      :			Optional[Any]  )    ->				Any:
            # Initialize image_processing
            _lowerCAmelCase												=							self.image_processing_class(**self.image_processor_dict  )
            # create random numpy tensors
            _lowerCAmelCase												=							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 (GLPNImageProcessor doesn't support batching)
            _lowerCAmelCase												=							image_processing(image_inputs[0]      ,      return_tensors="""pt"""  ).pixel_values
            self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0  )
            self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0  )




      def     lowercase__   (      self      :			Optional[int]  )    ->				List[str]:
            # Initialize image_processing
            _lowerCAmelCase												=							self.image_processing_class(**self.image_processor_dict  )
            # create random PyTorch tensors
            _lowerCAmelCase												=							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 (GLPNImageProcessor doesn't support batching)
            _lowerCAmelCase												=							image_processing(image_inputs[0]      ,      return_tensors="""pt"""  ).pixel_values
            self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0  )
            self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0  )













'''simple docstring'''






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




            @staticmethod
            def     lowercase__   (      *__snake_case      :			Optional[Any]      ,      **__snake_case      :			Any  )    ->				Tuple:
                  pass




@is_pipeline_test
@require_torch
@require_vision
class 			UpperCAmelCase					(      unittest.TestCase       ):
      _lowercase:      Union[str, Any]											=       MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING




      def     lowercase__   (      self      :			List[str]      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			str      ,      __snake_case      :			List[str]  )    ->				int:
            _lowerCAmelCase												=							pipeline("""visual-question-answering"""      ,      model="""hf-internal-testing/tiny-vilt-random-vqa"""  )
            _lowerCAmelCase												=							[
                {
                    """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      :			Any      ,      __snake_case      :			List[Any]      ,      __snake_case      :			List[Any]  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							vqa_pipeline(__snake_case      ,      top_k=1  )
            self.assertEqual(
                __snake_case      ,      [
                    [{"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}],
                    [{"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}],
                ]      ,      )




      @require_torch
      def     lowercase__   (      self      :			str  )    ->				int:
            _lowerCAmelCase												=							pipeline("""visual-question-answering"""      ,      model="""hf-internal-testing/tiny-vilt-random-vqa"""  )
            _lowerCAmelCase												=							"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
            _lowerCAmelCase												=							"""How many cats are there?"""

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

            _lowerCAmelCase												=							vqa_pipeline({"""image""": image, """question""": question}      ,      top_k=2  )
            self.assertEqual(
                __snake_case      ,      [{"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}, {"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}]  )




      @slow
      @require_torch
      def     lowercase__   (      self      :			List[Any]  )    ->				List[str]:
            _lowerCAmelCase												=							pipeline("""visual-question-answering"""      ,      model="""dandelin/vilt-b32-finetuned-vqa"""  )
            _lowerCAmelCase												=							"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
            _lowerCAmelCase												=							"""How many cats are there?"""

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

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

            _lowerCAmelCase												=							vqa_pipeline(
                [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]      ,      top_k=2  )
            self.assertEqual(
                nested_simplify(__snake_case      ,      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      :			List[str]  )    ->				Union[str, Any]:
            pass






