






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

class _lowercase      (							unittest.TestCase						):




							def __init__(				self:							List[str]     ,				UpperCamelCase__:							List[Any]     ,				UpperCamelCase__:							int=13     ,				UpperCamelCase__:							Tuple=3     ,				UpperCamelCase__:							Dict=18     ,				UpperCamelCase__:							Any=30     ,				UpperCamelCase__:							List[Any]=400     ,				UpperCamelCase__:							List[str]=True     ,				UpperCamelCase__:							Dict=None     ,				UpperCamelCase__:							Any=True     ,				UpperCamelCase__:							Optional[Any]=[0.5, 0.5, 0.5]     ,				UpperCamelCase__:							List[str]=[0.5, 0.5, 0.5]     ,				):
														lowerCamelCase__   :      Union[str, Any]			    =			size if size is not None else {"""height""": 18, """width""": 18}
														lowerCamelCase__   :      Optional[Any]			    =			parent
														lowerCamelCase__   :      List[str]			    =			batch_size
														lowerCamelCase__   :      List[Any]			    =			num_channels
														lowerCamelCase__   :      Tuple			    =			image_size
														lowerCamelCase__   :      Optional[Any]			    =			min_resolution
														lowerCamelCase__   :      Tuple			    =			max_resolution
														lowerCamelCase__   :      Union[str, Any]			    =			do_resize
														lowerCamelCase__   :      Tuple			    =			size
														lowerCamelCase__   :      Dict			    =			do_normalize
														lowerCamelCase__   :      Union[str, Any]			    =			image_mean
														lowerCamelCase__   :      Tuple			    =			image_std




							def 						lowerCamelCase_							(				self:							str	):
														return {
														    "image_mean": self.image_mean,
														    "image_std": self.image_std,
														    "do_normalize": self.do_normalize,
														    "do_resize": self.do_resize,
														    "size": self.size,
														}



@require_torch
@require_vision
class _lowercase      (							_lowercase							, unittest.TestCase						):
							a							 =			EfficientNetImageProcessor if is_vision_available() else None




							def 						lowerCamelCase_							(				self:							Optional[Any]	):
														lowerCamelCase__   :      int			    =			EfficientNetImageProcessorTester(self	)




							@property
							def 						lowerCamelCase_							(				self:							Optional[Any]	):
														return self.image_processor_tester.prepare_image_processor_dict()




							def 						lowerCamelCase_							(				self:							Dict	):
														lowerCamelCase__   :      Any			    =			self.image_processing_class(**self.image_processor_dict	)
														self.assertTrue(hasattr(UpperCamelCase__     ,				"""image_mean"""	)	)
														self.assertTrue(hasattr(UpperCamelCase__     ,				"""image_std"""	)	)
														self.assertTrue(hasattr(UpperCamelCase__     ,				"""do_normalize"""	)	)
														self.assertTrue(hasattr(UpperCamelCase__     ,				"""do_resize"""	)	)
														self.assertTrue(hasattr(UpperCamelCase__     ,				"""size"""	)	)




							def 						lowerCamelCase_							(				self:							Any	):
														lowerCamelCase__   :      Dict			    =			self.image_processing_class.from_dict(self.image_processor_dict	)
														self.assertEqual(image_processor.size     ,				{"""height""": 18, """width""": 18}	)

														lowerCamelCase__   :      str			    =			self.image_processing_class.from_dict(self.image_processor_dict     ,				size=42	)
														self.assertEqual(image_processor.size     ,				{"""height""": 42, """width""": 42}	)




							def 						lowerCamelCase_							(				self:							Dict	):
														# Initialize image_processing
														lowerCamelCase__   :      List[str]			    =			self.image_processing_class(**self.image_processor_dict	)
														# create random PIL images
														lowerCamelCase__   :      Tuple			    =			prepare_image_inputs(self.image_processor_tester     ,				equal_resolution=UpperCamelCase__	)
														for image in image_inputs:
																					self.assertIsInstance(UpperCamelCase__     ,				Image.Image	)

														# Test not batched input
														lowerCamelCase__   :      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.size["""height"""],
														        self.image_processor_tester.size["""width"""],
														    )     ,				)

														# Test batched
														lowerCamelCase__   :      List[Any]			    =			image_processing(UpperCamelCase__     ,				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.size["""height"""],
														        self.image_processor_tester.size["""width"""],
														    )     ,				)




							def 						lowerCamelCase_							(				self:							Optional[Any]	):
														# Initialize image_processing
														lowerCamelCase__   :      Union[str, Any]			    =			self.image_processing_class(**self.image_processor_dict	)
														# create random numpy tensors
														lowerCamelCase__   :      List[str]			    =			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
														lowerCamelCase__   :      List[str]			    =			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.size["""height"""],
														        self.image_processor_tester.size["""width"""],
														    )     ,				)

														# Test batched
														lowerCamelCase__   :      Union[str, Any]			    =			image_processing(UpperCamelCase__     ,				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.size["""height"""],
														        self.image_processor_tester.size["""width"""],
														    )     ,				)




							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														# Initialize image_processing
														lowerCamelCase__   :      Optional[int]			    =			self.image_processing_class(**self.image_processor_dict	)
														# create random PyTorch tensors
														lowerCamelCase__   :      Tuple			    =			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
														lowerCamelCase__   :      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.size["""height"""],
														        self.image_processor_tester.size["""width"""],
														    )     ,				)

														# Test batched
														lowerCamelCase__   :      str			    =			image_processing(UpperCamelCase__     ,				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.size["""height"""],
														        self.image_processor_tester.size["""width"""],
														    )     ,				)









'''simple docstring'''







import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
			from PIL import Image

class _lowercase      (							unittest.TestCase							, _lowercase						):




							def 						lowerCamelCase_							(				self:							Tuple	):
														lowerCamelCase__   :      Optional[Any]			    =			load_tool("""image-captioning"""	)
														self.tool.setup()
														lowerCamelCase__   :      Optional[Any]			    =			load_tool("""image-captioning"""     ,				remote=UpperCamelCase__	)




							def 						lowerCamelCase_							(				self:							int	):
														lowerCamelCase__   :      List[Any]			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      Optional[Any]			    =			self.tool(UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)




							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														lowerCamelCase__   :      Tuple			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      Dict			    =			self.remote_tool(UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)




							def 						lowerCamelCase_							(				self:							str	):
														lowerCamelCase__   :      Optional[int]			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      str			    =			self.tool(image=UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)




							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														lowerCamelCase__   :      Optional[int]			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      Optional[int]			    =			self.remote_tool(image=UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)

