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


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import BlipImageProcessor






class        _lowercase   (				unittest.TestCase		):




	def __init__( self		, UpperCAmelCase_		, UpperCAmelCase_=7		, UpperCAmelCase_=3		, UpperCAmelCase_=18		, UpperCAmelCase_=30		, UpperCAmelCase_=400		, UpperCAmelCase_=True		, UpperCAmelCase_=None		, UpperCAmelCase_=True		, UpperCAmelCase_=False		, UpperCAmelCase_=[0.48145466, 0.4578275, 0.40821073]		, UpperCAmelCase_=[0.26862954, 0.26130258, 0.27577711]		, UpperCAmelCase_=True		, )		->							Tuple:
			lowerCamelCase     :			Tuple		=    size if size is not None else {"height": 20, "width": 20}
			lowerCamelCase     :			List[str]		=    parent
			lowerCamelCase     :			Optional[Any]		=    batch_size
			lowerCamelCase     :			int		=    num_channels
			lowerCamelCase     :			List[str]		=    image_size
			lowerCamelCase     :			str		=    min_resolution
			lowerCamelCase     :			Any		=    max_resolution
			lowerCamelCase     :			List[Any]		=    do_resize
			lowerCamelCase     :			List[Any]		=    size
			lowerCamelCase     :			Union[str, Any]		=    do_normalize
			lowerCamelCase     :			Union[str, Any]		=    image_mean
			lowerCamelCase     :			Union[str, Any]		=    image_std
			lowerCamelCase     :			Any		=    do_pad
			lowerCamelCase     :			Any		=    do_convert_rgb




	def 			_UpperCamelCase    ( self					)		->							Union[str, Any]:
			return {
			    "do_resize": self.do_resize,
			    "size": self.size,
			    "do_normalize": self.do_normalize,
			    "image_mean": self.image_mean,
			    "image_std": self.image_std,
			    "do_convert_rgb": self.do_convert_rgb,
			    "do_pad": self.do_pad,
			}




	def 			_UpperCamelCase    ( self		, UpperCAmelCase_=False		, UpperCAmelCase_=False		, UpperCAmelCase_=False					)		->							Dict:
			assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

			if equal_resolution:
					lowerCamelCase     :			str		=    []
					for i in range(self.batch_size					):
							image_inputs.append(
							    np.random.randint(
							        255		, size=(self.num_channels, self.max_resolution, self.max_resolution)		, dtype=np.uinta					)					)
			else:
					lowerCamelCase     :			Optional[int]		=    []
					for i in range(self.batch_size					):
							lowerCamelCase     :			int		=    np.random.choice(np.arange(self.min_resolution		, self.max_resolution					)		, 2					)
							image_inputs.append(np.random.randint(255		, size=(self.num_channels, width, height)		, dtype=np.uinta					)					)

			if not numpify and not torchify:
					# PIL expects the channel dimension as last dimension
					lowerCamelCase     :			Tuple		=    [Image.fromarray(np.moveaxis(__lowerCamelCase		, 0		, -1					)					) for x in image_inputs]

			if torchify:
					lowerCamelCase     :			Dict		=    [torch.from_numpy(__lowerCamelCase					) for x in image_inputs]

			return image_inputs






@require_torch
@require_vision
class        _lowercase   (				__lowercase				,       unittest.TestCase		):
	lowercase_        =	BlipImageProcessor if is_vision_available() else None




	def 			_UpperCamelCase    ( self					)		->							str:
			lowerCamelCase     :			str		=    BlipImageProcessingTester(self					)




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




	def 			_UpperCamelCase    ( self					)		->							Union[str, Any]:
			lowerCamelCase     :			List[Any]		=    self.image_processing_class(**self.image_processor_dict					)
			self.assertTrue(hasattr(__lowerCamelCase		, 'do_resize'					)					)
			self.assertTrue(hasattr(__lowerCamelCase		, 'size'					)					)
			self.assertTrue(hasattr(__lowerCamelCase		, 'do_normalize'					)					)
			self.assertTrue(hasattr(__lowerCamelCase		, 'image_mean'					)					)
			self.assertTrue(hasattr(__lowerCamelCase		, 'image_std'					)					)
			self.assertTrue(hasattr(__lowerCamelCase		, 'do_convert_rgb'					)					)




	def 			_UpperCamelCase    ( self					)		->							Tuple:
			pass




	def 			_UpperCamelCase    ( self					)		->							Optional[Any]:
			# Initialize image_processor
			lowerCamelCase     :			Optional[Any]		=    self.image_processing_class(**self.image_processor_dict					)
			# create random PIL images
			lowerCamelCase     :			List[str]		=    self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase					)
			for image in image_inputs:
					self.assertIsInstance(__lowerCamelCase		, Image.Image					)

			# Test not batched input
			lowerCamelCase     :			Optional[Any]		=    image_processor(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     :			Optional[Any]		=    image_processor(__lowerCamelCase		, 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 			_UpperCamelCase    ( self					)		->							List[Any]:
			# Initialize image_processor
			lowerCamelCase     :			List[Any]		=    self.image_processing_class(**self.image_processor_dict					)
			# create random numpy tensors
			lowerCamelCase     :			str		=    self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase		, numpify=__lowerCamelCase					)
			for image in image_inputs:
					self.assertIsInstance(__lowerCamelCase		, np.ndarray					)

			# Test not batched input
			lowerCamelCase     :			Optional[Any]		=    image_processor(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     :			Optional[int]		=    image_processor(__lowerCamelCase		, 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 			_UpperCamelCase    ( self					)		->							List[Any]:
			# Initialize image_processor
			lowerCamelCase     :			int		=    self.image_processing_class(**self.image_processor_dict					)
			# create random PyTorch tensors
			lowerCamelCase     :			List[Any]		=    self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase		, torchify=__lowerCamelCase					)
			for image in image_inputs:
					self.assertIsInstance(__lowerCamelCase		, torch.Tensor					)

			# Test not batched input
			lowerCamelCase     :			Any		=    image_processor(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     :			Tuple		=    image_processor(__lowerCamelCase		, 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'],
			    )		, )






@require_torch
@require_vision
class        _lowercase   (				__lowercase				,       unittest.TestCase		):
	lowercase_        =	BlipImageProcessor if is_vision_available() else None




	def 			_UpperCamelCase    ( self					)		->							Optional[int]:
			lowerCamelCase     :			Any		=    BlipImageProcessingTester(self		, num_channels=4					)
			lowerCamelCase     :			Optional[int]		=    3




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




	def 			_UpperCamelCase    ( self					)		->							str:
			lowerCamelCase     :			Tuple		=    self.image_processing_class(**self.image_processor_dict					)
			self.assertTrue(hasattr(__lowerCamelCase		, 'do_resize'					)					)
			self.assertTrue(hasattr(__lowerCamelCase		, 'size'					)					)
			self.assertTrue(hasattr(__lowerCamelCase		, 'do_normalize'					)					)
			self.assertTrue(hasattr(__lowerCamelCase		, 'image_mean'					)					)
			self.assertTrue(hasattr(__lowerCamelCase		, 'image_std'					)					)
			self.assertTrue(hasattr(__lowerCamelCase		, 'do_convert_rgb'					)					)




	def 			_UpperCamelCase    ( self					)		->							Dict:
			pass




	def 			_UpperCamelCase    ( self					)		->							Dict:
			# Initialize image_processor
			lowerCamelCase     :			Optional[Any]		=    self.image_processing_class(**self.image_processor_dict					)
			# create random PIL images
			lowerCamelCase     :			Union[str, Any]		=    self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase					)
			for image in image_inputs:
					self.assertIsInstance(__lowerCamelCase		, Image.Image					)

			# Test not batched input
			lowerCamelCase     :			Union[str, Any]		=    image_processor(image_inputs[0]		, return_tensors='pt'					).pixel_values
			self.assertEqual(
			    encoded_images.shape		, (
			        1,
			        self.expected_encoded_image_num_channels,
			        self.image_processor_tester.size['height'],
			        self.image_processor_tester.size['width'],
			    )		, )

			# Test batched
			lowerCamelCase     :			Tuple		=    image_processor(__lowerCamelCase		, return_tensors='pt'					).pixel_values
			self.assertEqual(
			    encoded_images.shape		, (
			        self.image_processor_tester.batch_size,
			        self.expected_encoded_image_num_channels,
			        self.image_processor_tester.size['height'],
			        self.image_processor_tester.size['width'],
			    )		, )













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


if is_torch_available():
       import torch

if is_vision_available():
       from PIL import Image

       from transformers import CLIPImageProcessor





class 	lowerCAmelCase__(       unittest.TestCase			):






       '''simple docstring'''


       def __init__(					self			,  __lowerCamelCase			,  __lowerCamelCase=7			,  __lowerCamelCase=3			,  __lowerCamelCase=1_8			,  __lowerCamelCase=3_0			,  __lowerCamelCase=4_0_0			,  __lowerCamelCase=True			,  __lowerCamelCase=None			,  __lowerCamelCase=True			,  __lowerCamelCase=None			,  __lowerCamelCase=True			,  __lowerCamelCase=[0.4814_5466, 0.457_8275, 0.4082_1073]			,  __lowerCamelCase=[0.2686_2954, 0.2613_0258, 0.2757_7711]			,  __lowerCamelCase=True			,  )	->   List[Any]:
            _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							size if size is not None else {"shortest_edge": 2_0}
            _SCREAMING_SNAKE_CASE				:		int  			=							crop_size if crop_size is not None else {"height": 1_8, "width": 1_8}
            _SCREAMING_SNAKE_CASE				:		str  			=							parent
            _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							batch_size
            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							num_channels
            _SCREAMING_SNAKE_CASE				:		Any  			=							image_size
            _SCREAMING_SNAKE_CASE				:		Any  			=							min_resolution
            _SCREAMING_SNAKE_CASE				:		int  			=							max_resolution
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							do_resize
            _SCREAMING_SNAKE_CASE				:		List[Any]  			=							size
            _SCREAMING_SNAKE_CASE				:		int  			=							do_center_crop
            _SCREAMING_SNAKE_CASE				:		Dict  			=							crop_size
            _SCREAMING_SNAKE_CASE				:		int  			=							do_normalize
            _SCREAMING_SNAKE_CASE				:		List[Any]  			=							image_mean
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							image_std
            _SCREAMING_SNAKE_CASE				:		Tuple  			=							do_convert_rgb




       def 			UpperCamelCase_	(					self							)	->   List[str]:
            return {
                "do_resize": self.do_resize,
                "size": self.size,
                "do_center_crop": self.do_center_crop,
                "crop_size": self.crop_size,
                "do_normalize": self.do_normalize,
                "image_mean": self.image_mean,
                "image_std": self.image_std,
                "do_convert_rgb": self.do_convert_rgb,
            }







       def 			UpperCamelCase_	(					self			,  __lowerCamelCase=False			,  __lowerCamelCase=False			,  __lowerCamelCase=False							)	->   Tuple:
            assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

            if equal_resolution:
                 _SCREAMING_SNAKE_CASE				:		Tuple  			=							[]
                 for i in range(self.batch_size							):
                      image_inputs.append(
                          np.random.randint(
                              2_5_5			,  size=(self.num_channels, self.max_resolution, self.max_resolution)			,  dtype=np.uinta							)							)
            else:
                 _SCREAMING_SNAKE_CASE				:		Dict  			=							[]
                 for i in range(self.batch_size							):
                      _SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE				:		Tuple  			=							np.random.choice(np.arange(self.min_resolution			,  self.max_resolution							)			,  2							)
                      image_inputs.append(np.random.randint(2_5_5			,  size=(self.num_channels, width, height)			,  dtype=np.uinta							)							)

            if not numpify and not torchify:
                 # PIL expects the channel dimension as last dimension
                 _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							[Image.fromarray(np.moveaxis(__lowerCamelCase			,  0			,  -1							)							) for x in image_inputs]

            if torchify:
                 _SCREAMING_SNAKE_CASE				:		List[Any]  			=							[torch.from_numpy(__lowerCamelCase							) for x in image_inputs]

            return image_inputs





@require_torch
@require_vision
class 	lowerCAmelCase__(       __lowercase     ,   unittest.TestCase			):






       '''simple docstring'''


       __snake_case       	=			CLIPImageProcessor if is_vision_available() else None




       def 			UpperCamelCase_	(					self							)	->   Dict:
            _SCREAMING_SNAKE_CASE				:		str  			=							CLIPImageProcessingTester(self							)




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




       def 			UpperCamelCase_	(					self							)	->   int:
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							self.image_processing_class(**self.image_processor_dict							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_resize"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "size"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_center_crop"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "center_crop"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_normalize"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "image_mean"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "image_std"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_convert_rgb"							)							)




       def 			UpperCamelCase_	(					self							)	->   Optional[Any]:
            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							self.image_processing_class.from_dict(self.image_processor_dict							)
            self.assertEqual(image_processor.size			,  {"shortest_edge": 2_0}							)
            self.assertEqual(image_processor.crop_size			,  {"height": 1_8, "width": 1_8}							)

            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							self.image_processing_class.from_dict(self.image_processor_dict			,  size=4_2			,  crop_size=8_4							)
            self.assertEqual(image_processor.size			,  {"shortest_edge": 4_2}							)
            self.assertEqual(image_processor.crop_size			,  {"height": 8_4, "width": 8_4}							)




       def 			UpperCamelCase_	(					self							)	->   Tuple:
            pass




       def 			UpperCamelCase_	(					self							)	->   str:
            # Initialize image_processing
            _SCREAMING_SNAKE_CASE				:		str  			=							self.image_processing_class(**self.image_processor_dict							)
            # create random PIL images
            _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase							)
            for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase			,  Image.Image							)

            # Test not batched input
            _SCREAMING_SNAKE_CASE				:		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
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							image_processing(__lowerCamelCase			,  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 			UpperCamelCase_	(					self							)	->   Union[str, Any]:
            # Initialize image_processing
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							self.image_processing_class(**self.image_processor_dict							)
            # create random numpy tensors
            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase			,  numpify=__lowerCamelCase							)
            for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase			,  np.ndarray							)

            # Test not batched input
            _SCREAMING_SNAKE_CASE				:		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
            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							image_processing(__lowerCamelCase			,  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 			UpperCamelCase_	(					self							)	->   Any:
            # Initialize image_processing
            _SCREAMING_SNAKE_CASE				:		str  			=							self.image_processing_class(**self.image_processor_dict							)
            # create random PyTorch tensors
            _SCREAMING_SNAKE_CASE				:		int  			=							self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase			,  torchify=__lowerCamelCase							)
            for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase			,  torch.Tensor							)

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

            # Test batched
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							image_processing(__lowerCamelCase			,  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"],
                )			,  )





@require_torch
@require_vision
class 	lowerCAmelCase__(       __lowercase     ,   unittest.TestCase			):






       '''simple docstring'''


       __snake_case       	=			CLIPImageProcessor if is_vision_available() else None




       def 			UpperCamelCase_	(					self							)	->   int:
            _SCREAMING_SNAKE_CASE				:		Any  			=							CLIPImageProcessingTester(self			,  num_channels=4							)
            _SCREAMING_SNAKE_CASE				:		Any  			=							3




       @property
       def 			UpperCamelCase_	(					self							)	->   Dict:
            return self.image_processor_tester.prepare_image_processor_dict()




       def 			UpperCamelCase_	(					self							)	->   Union[str, Any]:
            _SCREAMING_SNAKE_CASE				:		str  			=							self.image_processing_class(**self.image_processor_dict							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_resize"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "size"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_center_crop"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "center_crop"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_normalize"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "image_mean"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "image_std"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_convert_rgb"							)							)




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







       def 			UpperCamelCase_	(					self							)	->   Union[str, Any]:
            # Initialize image_processing
            _SCREAMING_SNAKE_CASE				:		Dict  			=							self.image_processing_class(**self.image_processor_dict							)
            # create random PIL images
            _SCREAMING_SNAKE_CASE				:		Any  			=							self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase							)
            for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase			,  Image.Image							)

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

            # Test batched
            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							image_processing(__lowerCamelCase			,  return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape			,  (
                    self.image_processor_tester.batch_size,
                    self.expected_encoded_image_num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )			,  )