


"""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):

	"""simple docstring"""





	def __init__(				self    :       str    ,    UpperCAmelCase__    :       Optional[Any]    ,    UpperCAmelCase__    :       Any=7    ,    UpperCAmelCase__    :       Union[str, Any]=3    ,    UpperCAmelCase__    :       Any=1_8    ,    UpperCAmelCase__    :       Tuple=3_0    ,    UpperCAmelCase__    :       int=4_0_0    ,    UpperCAmelCase__    :       Union[str, Any]=True    ,    UpperCAmelCase__    :       Optional[Any]=3_2    ,    UpperCAmelCase__    :       List[str]=True    ,    ) ->			Optional[Any]:
						__SCREAMING_SNAKE_CASE		     =       parent
						__SCREAMING_SNAKE_CASE		     =       batch_size
						__SCREAMING_SNAKE_CASE		     =       num_channels
						__SCREAMING_SNAKE_CASE		     =       image_size
						__SCREAMING_SNAKE_CASE		     =       min_resolution
						__SCREAMING_SNAKE_CASE		     =       max_resolution
						__SCREAMING_SNAKE_CASE		     =       do_resize
						__SCREAMING_SNAKE_CASE		     =       size_divisor
						__SCREAMING_SNAKE_CASE		     =       do_rescale




	def 						UpperCAmelCase_   (				self    :       Tuple     ) ->			List[str]:
						return {
						    "do_resize": self.do_resize,
						    "size_divisor": self.size_divisor,
						    "do_rescale": self.do_rescale,
						}




@require_torch
@require_vision
class 				UpperCamelCase_			(				UpperCamelCase    ,						unittest.TestCase):

	"""simple docstring"""





	snake_case__       :						Optional[int]					      =    GLPNImageProcessor if is_vision_available() else None




	def 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			Any:
						__SCREAMING_SNAKE_CASE		     =       GLPNImageProcessingTester(self     )




	@property
	def 						UpperCAmelCase_   (				self    :       Tuple     ) ->			Union[str, Any]:
						return self.image_processor_tester.prepare_image_processor_dict()




	def 						UpperCAmelCase_   (				self    :       Dict     ) ->			Optional[Any]:
						__SCREAMING_SNAKE_CASE		     =       self.image_processing_class(**self.image_processor_dict     )
						self.assertTrue(hasattr(UpperCAmelCase__    ,    "do_resize"     )     )
						self.assertTrue(hasattr(UpperCAmelCase__    ,    "size_divisor"     )     )
						self.assertTrue(hasattr(UpperCAmelCase__    ,    "resample"     )     )
						self.assertTrue(hasattr(UpperCAmelCase__    ,    "do_rescale"     )     )




	def 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			Optional[Any]:
						pass




	def 						UpperCAmelCase_   (				self    :       Union[str, Any]     ) ->			Optional[Any]:
						# Initialize image_processing
						__SCREAMING_SNAKE_CASE		     =       self.image_processing_class(**self.image_processor_dict     )
						# create random PIL images
						__SCREAMING_SNAKE_CASE		     =       prepare_image_inputs(self.image_processor_tester    ,    equal_resolution=UpperCAmelCase__     )
						for image in image_inputs:
											self.assertIsInstance(UpperCAmelCase__    ,    Image.Image     )

						# Test not batched input (GLPNImageProcessor doesn't support batching)
						__SCREAMING_SNAKE_CASE		     =       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 						UpperCAmelCase_   (				self    :       Any     ) ->			Union[str, Any]:
						# Initialize image_processing
						__SCREAMING_SNAKE_CASE		     =       self.image_processing_class(**self.image_processor_dict     )
						# create random numpy tensors
						__SCREAMING_SNAKE_CASE		     =       prepare_image_inputs(self.image_processor_tester    ,    equal_resolution=UpperCAmelCase__    ,    numpify=UpperCAmelCase__     )
						for image in image_inputs:
											self.assertIsInstance(UpperCAmelCase__    ,    np.ndarray     )

						# Test not batched input (GLPNImageProcessor doesn't support batching)
						__SCREAMING_SNAKE_CASE		     =       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 						UpperCAmelCase_   (				self    :       Optional[int]     ) ->			Any:
						# Initialize image_processing
						__SCREAMING_SNAKE_CASE		     =       self.image_processing_class(**self.image_processor_dict     )
						# create random PyTorch tensors
						__SCREAMING_SNAKE_CASE		     =       prepare_image_inputs(self.image_processor_tester    ,    equal_resolution=UpperCAmelCase__    ,    torchify=UpperCAmelCase__     )
						for image in image_inputs:
											self.assertIsInstance(UpperCAmelCase__    ,    torch.Tensor     )

						# Test not batched input (GLPNImageProcessor doesn't support batching)
						__SCREAMING_SNAKE_CASE		     =       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_			:

			"""simple docstring"""





			@staticmethod
			def 						UpperCAmelCase_   (				*UpperCAmelCase__    :       List[Any]    ,    **UpperCAmelCase__    :       Optional[Any]     ) ->			Optional[int]:
								pass




@is_pipeline_test
@require_torch
@require_vision
class 				UpperCamelCase_			(				unittest.TestCase):

	"""simple docstring"""





	snake_case__       :						List[Any]					      =    MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING




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




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

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

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




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

						__SCREAMING_SNAKE_CASE		     =       vqa_pipeline(image=UpperCAmelCase__    ,    question=UpperCAmelCase__    ,    top_k=2     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__    ,    decimals=4     )    ,    [{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]     )

						__SCREAMING_SNAKE_CASE		     =       vqa_pipeline({"image": image, "question": question}    ,    top_k=2     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__    ,    decimals=4     )    ,    [{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]     )

						__SCREAMING_SNAKE_CASE		     =       vqa_pipeline(
						    [{"image": image, "question": question}, {"image": image, "question": question}]    ,    top_k=2     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__    ,    decimals=4     )    ,    [[{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]] * 2    ,    )




	@require_tf
	@unittest.skip("Visual question answering not implemented in TF"     )
	def 						UpperCAmelCase_   (				self    :       str     ) ->			Optional[Any]:
						pass
