





'''simple docstring'''






import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
      from PIL import Image

      from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
      import torch

if is_tf_available():
      import tensorflow as tf




@require_vision
@require_torchvision
class 			UpperCAmelCase					(      unittest.TestCase       ):




      def     lowercase__   (      self      :			int  )    ->				Tuple:
            _lowerCAmelCase												=							tempfile.mkdtemp()
            _lowerCAmelCase												=							SamImageProcessor()
            _lowerCAmelCase												=							SamProcessor(UpperCAmelCase_  )
            processor.save_pretrained(self.tmpdirname  )




      def     lowercase__   (      self      :			Dict      ,      **__snake_case      :			Any  )    ->				Optional[int]:
            return AutoProcessor.from_pretrained(self.tmpdirname      ,      **UpperCAmelCase_  ).image_processor




      def     lowercase__   (      self      :			Tuple  )    ->				int:
            shutil.rmtree(self.tmpdirname  )




      def     lowercase__   (      self      :			Tuple  )    ->				str:
            _lowerCAmelCase												=							[np.random.randint(2_55      ,      size=(3, 30, 4_00)      ,      dtype=np.uinta  )]

            _lowerCAmelCase												=							[Image.fromarray(np.moveaxis(UpperCAmelCase_      ,      0      ,      -1  )  ) for x in image_inputs]

            return image_inputs




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				Optional[int]:
            _lowerCAmelCase												=							SamProcessor(image_processor=self.get_image_processor()  )
            processor.save_pretrained(self.tmpdirname  )

            _lowerCAmelCase												=							self.get_image_processor(do_normalize=UpperCAmelCase_      ,      padding_value=1.0  )

            _lowerCAmelCase												=							SamProcessor.from_pretrained(self.tmpdirname      ,      do_normalize=UpperCAmelCase_      ,      padding_value=1.0  )

            self.assertEqual(processor.image_processor.to_json_string()      ,      image_processor_add_kwargs.to_json_string()  )
            self.assertIsInstance(processor.image_processor      ,      UpperCAmelCase_  )




      def     lowercase__   (      self      :			Dict  )    ->				Any:
            _lowerCAmelCase												=							self.get_image_processor()

            _lowerCAmelCase												=							SamProcessor(image_processor=UpperCAmelCase_  )

            _lowerCAmelCase												=							self.prepare_image_inputs()

            _lowerCAmelCase												=							image_processor(UpperCAmelCase_      ,      return_tensors="""np"""  )
            _lowerCAmelCase												=							processor(images=UpperCAmelCase_      ,      return_tensors="""np"""  )

            input_feat_extract.pop("""original_sizes"""  )  # pop original_sizes as it is popped in the processor
            input_feat_extract.pop("""reshaped_input_sizes"""  )  # pop original_sizes as it is popped in the processor

            for key in input_feat_extract.keys():
                  self.assertAlmostEqual(input_feat_extract[key].sum()      ,      input_processor[key].sum()      ,      delta=1E-2  )




      @require_torch
      def     lowercase__   (      self      :			Optional[int]  )    ->				int:
            _lowerCAmelCase												=							self.get_image_processor()

            _lowerCAmelCase												=							SamProcessor(image_processor=UpperCAmelCase_  )
            _lowerCAmelCase												=							[torch.ones((1, 3, 5, 5)  )]

            _lowerCAmelCase												=							[[17_64, 26_46]]

            _lowerCAmelCase												=							[[6_83, 10_24]]
            _lowerCAmelCase												=							processor.post_process_masks(UpperCAmelCase_      ,      UpperCAmelCase_      ,      UpperCAmelCase_  )
            self.assertEqual(masks[0].shape      ,      (1, 3, 17_64, 26_46)  )

            _lowerCAmelCase												=							processor.post_process_masks(
                UpperCAmelCase_      ,      torch.tensor(UpperCAmelCase_  )      ,      torch.tensor(UpperCAmelCase_  )  )
            self.assertEqual(masks[0].shape      ,      (1, 3, 17_64, 26_46)  )

            # should also work with np
            _lowerCAmelCase												=							[np.ones((1, 3, 5, 5)  )]
            _lowerCAmelCase												=							processor.post_process_masks(UpperCAmelCase_      ,      np.array(UpperCAmelCase_  )      ,      np.array(UpperCAmelCase_  )  )

            self.assertEqual(masks[0].shape      ,      (1, 3, 17_64, 26_46)  )

            _lowerCAmelCase												=							[[1, 0], [0, 1]]
            with self.assertRaises(UpperCAmelCase_  ):
                  _lowerCAmelCase												=							processor.post_process_masks(UpperCAmelCase_      ,      np.array(UpperCAmelCase_  )      ,      np.array(UpperCAmelCase_  )  )




@require_vision
@require_tf
class 			UpperCAmelCase					(      unittest.TestCase       ):




      def     lowercase__   (      self      :			Any  )    ->				Optional[int]:
            _lowerCAmelCase												=							tempfile.mkdtemp()
            _lowerCAmelCase												=							SamImageProcessor()
            _lowerCAmelCase												=							SamProcessor(UpperCAmelCase_  )
            processor.save_pretrained(self.tmpdirname  )




      def     lowercase__   (      self      :			Tuple      ,      **__snake_case      :			Optional[int]  )    ->				Tuple:
            return AutoProcessor.from_pretrained(self.tmpdirname      ,      **UpperCAmelCase_  ).image_processor




      def     lowercase__   (      self      :			Dict  )    ->				Union[str, Any]:
            shutil.rmtree(self.tmpdirname  )




      def     lowercase__   (      self      :			List[str]  )    ->				Tuple:
            _lowerCAmelCase												=							[np.random.randint(2_55      ,      size=(3, 30, 4_00)      ,      dtype=np.uinta  )]

            _lowerCAmelCase												=							[Image.fromarray(np.moveaxis(UpperCAmelCase_      ,      0      ,      -1  )  ) for x in image_inputs]

            return image_inputs




      def     lowercase__   (      self      :			Dict  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							SamProcessor(image_processor=self.get_image_processor()  )
            processor.save_pretrained(self.tmpdirname  )

            _lowerCAmelCase												=							self.get_image_processor(do_normalize=UpperCAmelCase_      ,      padding_value=1.0  )

            _lowerCAmelCase												=							SamProcessor.from_pretrained(self.tmpdirname      ,      do_normalize=UpperCAmelCase_      ,      padding_value=1.0  )

            self.assertEqual(processor.image_processor.to_json_string()      ,      image_processor_add_kwargs.to_json_string()  )
            self.assertIsInstance(processor.image_processor      ,      UpperCAmelCase_  )




      def     lowercase__   (      self      :			Any  )    ->				Optional[Any]:
            _lowerCAmelCase												=							self.get_image_processor()

            _lowerCAmelCase												=							SamProcessor(image_processor=UpperCAmelCase_  )

            _lowerCAmelCase												=							self.prepare_image_inputs()

            _lowerCAmelCase												=							image_processor(UpperCAmelCase_      ,      return_tensors="""np"""  )
            _lowerCAmelCase												=							processor(images=UpperCAmelCase_      ,      return_tensors="""np"""  )

            input_feat_extract.pop("""original_sizes"""  )  # pop original_sizes as it is popped in the processor
            input_feat_extract.pop("""reshaped_input_sizes"""  )  # pop reshaped_input_sizes as it is popped in the processor

            for key in input_feat_extract.keys():
                  self.assertAlmostEqual(input_feat_extract[key].sum()      ,      input_processor[key].sum()      ,      delta=1E-2  )




      @require_tf
      def     lowercase__   (      self      :			Tuple  )    ->				Optional[int]:
            _lowerCAmelCase												=							self.get_image_processor()

            _lowerCAmelCase												=							SamProcessor(image_processor=UpperCAmelCase_  )
            _lowerCAmelCase												=							[tf.ones((1, 3, 5, 5)  )]

            _lowerCAmelCase												=							[[17_64, 26_46]]

            _lowerCAmelCase												=							[[6_83, 10_24]]
            _lowerCAmelCase												=							processor.post_process_masks(UpperCAmelCase_      ,      UpperCAmelCase_      ,      UpperCAmelCase_      ,      return_tensors="""tf"""  )
            self.assertEqual(masks[0].shape      ,      (1, 3, 17_64, 26_46)  )

            _lowerCAmelCase												=							processor.post_process_masks(
                UpperCAmelCase_      ,      tf.convert_to_tensor(UpperCAmelCase_  )      ,      tf.convert_to_tensor(UpperCAmelCase_  )      ,      return_tensors="""tf"""      ,      )
            self.assertEqual(masks[0].shape      ,      (1, 3, 17_64, 26_46)  )

            # should also work with np
            _lowerCAmelCase												=							[np.ones((1, 3, 5, 5)  )]
            _lowerCAmelCase												=							processor.post_process_masks(
                UpperCAmelCase_      ,      np.array(UpperCAmelCase_  )      ,      np.array(UpperCAmelCase_  )      ,      return_tensors="""tf"""  )

            self.assertEqual(masks[0].shape      ,      (1, 3, 17_64, 26_46)  )

            _lowerCAmelCase												=							[[1, 0], [0, 1]]
            with self.assertRaises(tf.errors.InvalidArgumentError  ):
                  _lowerCAmelCase												=							processor.post_process_masks(
                      UpperCAmelCase_      ,      np.array(UpperCAmelCase_  )      ,      np.array(UpperCAmelCase_  )      ,      return_tensors="""tf"""  )




@require_vision
@require_torchvision
class 			UpperCAmelCase					(      unittest.TestCase       ):




      def     lowercase__   (      self      :			str  )    ->				List[str]:
            _lowerCAmelCase												=							tempfile.mkdtemp()
            _lowerCAmelCase												=							SamImageProcessor()
            _lowerCAmelCase												=							SamProcessor(UpperCAmelCase_  )
            processor.save_pretrained(self.tmpdirname  )




      def     lowercase__   (      self      :			List[Any]      ,      **__snake_case      :			Union[str, Any]  )    ->				List[Any]:
            return AutoProcessor.from_pretrained(self.tmpdirname      ,      **UpperCAmelCase_  ).image_processor




      def     lowercase__   (      self      :			Dict  )    ->				Optional[Any]:
            shutil.rmtree(self.tmpdirname  )




      def     lowercase__   (      self      :			str  )    ->				Optional[Any]:
            _lowerCAmelCase												=							[np.random.randint(2_55      ,      size=(3, 30, 4_00)      ,      dtype=np.uinta  )]

            _lowerCAmelCase												=							[Image.fromarray(np.moveaxis(UpperCAmelCase_      ,      0      ,      -1  )  ) for x in image_inputs]

            return image_inputs




      @is_pt_tf_cross_test
      def     lowercase__   (      self      :			Union[str, Any]  )    ->				List[str]:
            _lowerCAmelCase												=							self.get_image_processor()

            _lowerCAmelCase												=							SamProcessor(image_processor=UpperCAmelCase_  )
            _lowerCAmelCase												=							np.random.randint(0      ,      2      ,      size=(1, 3, 5, 5)  ).astype(np.floataa  )
            _lowerCAmelCase												=							[tf.convert_to_tensor(UpperCAmelCase_  )]
            _lowerCAmelCase												=							[torch.tensor(UpperCAmelCase_  )]

            _lowerCAmelCase												=							[[17_64, 26_46]]

            _lowerCAmelCase												=							[[6_83, 10_24]]
            _lowerCAmelCase												=							processor.post_process_masks(
                UpperCAmelCase_      ,      UpperCAmelCase_      ,      UpperCAmelCase_      ,      return_tensors="""tf"""  )
            _lowerCAmelCase												=							processor.post_process_masks(
                UpperCAmelCase_      ,      UpperCAmelCase_      ,      UpperCAmelCase_      ,      return_tensors="""pt"""  )

            self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()  )  )




      @is_pt_tf_cross_test
      def     lowercase__   (      self      :			Tuple  )    ->				List[Any]:
            _lowerCAmelCase												=							self.get_image_processor()

            _lowerCAmelCase												=							SamProcessor(image_processor=UpperCAmelCase_  )

            _lowerCAmelCase												=							self.prepare_image_inputs()

            _lowerCAmelCase												=							image_processor(UpperCAmelCase_      ,      return_tensors="""pt"""  )["pixel_values"].numpy()
            _lowerCAmelCase												=							processor(images=UpperCAmelCase_      ,      return_tensors="""pt"""  )["pixel_values"].numpy()

            _lowerCAmelCase												=							image_processor(UpperCAmelCase_      ,      return_tensors="""tf"""  )["pixel_values"].numpy()
            _lowerCAmelCase												=							processor(images=UpperCAmelCase_      ,      return_tensors="""tf"""  )["pixel_values"].numpy()

            self.assertTrue(np.allclose(UpperCAmelCase_      ,      UpperCAmelCase_  )  )
            self.assertTrue(np.allclose(UpperCAmelCase_      ,      UpperCAmelCase_  )  )
            self.assertTrue(np.allclose(UpperCAmelCase_      ,      UpperCAmelCase_  )  )













'''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






