





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 	_UpperCAmelCase						( unittest.TestCase       ):




					def __init__(    self     ,   __lowerCAmelCase     ,   __lowerCAmelCase=7     ,   __lowerCAmelCase=3     ,   __lowerCAmelCase=18     ,   __lowerCAmelCase=30     ,   __lowerCAmelCase=400     ,   __lowerCAmelCase=True     ,   __lowerCAmelCase=None     ,   __lowerCAmelCase=True     ,   __lowerCAmelCase=False     ,   __lowerCAmelCase=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]     ,   __lowerCAmelCase=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]     ,   __lowerCAmelCase=True     ,   )    ->	List[str]:
							UpperCamelCase__								=						size if size is not None else {"""height""": 20, """width""": 20}
							UpperCamelCase__								=						parent
							UpperCamelCase__								=						batch_size
							UpperCamelCase__								=						num_channels
							UpperCamelCase__								=						image_size
							UpperCamelCase__								=						min_resolution
							UpperCamelCase__								=						max_resolution
							UpperCamelCase__								=						do_resize
							UpperCamelCase__								=						size
							UpperCamelCase__								=						do_normalize
							UpperCamelCase__								=						image_mean
							UpperCamelCase__								=						image_std
							UpperCamelCase__								=						do_pad
							UpperCamelCase__								=						do_convert_rgb




					def 						snake_case_		(    self							)    ->	str:
							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 						snake_case_		(    self     ,   __lowerCAmelCase=False     ,   __lowerCAmelCase=False     ,   __lowerCAmelCase=False							)    ->	Optional[int]:
							assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

							if equal_resolution:
									UpperCamelCase__								=						[]
									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:
									UpperCamelCase__								=						[]
									for i in range(self.batch_size							):
											UpperCamelCase__	,      UpperCamelCase__								=						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
									UpperCamelCase__								=						[Image.fromarray(np.moveaxis(_UpperCAmelCase     ,   0     ,   -1							)							) for x in image_inputs]

							if torchify:
									UpperCamelCase__								=						[torch.from_numpy(_UpperCAmelCase							) for x in image_inputs]

							return image_inputs




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




					def 						snake_case_		(    self							)    ->	Dict:
							UpperCamelCase__								=						BlipImageProcessingTester(self							)




					@property
					def 						snake_case_		(    self							)    ->	Optional[int]:
							return self.image_processor_tester.prepare_image_processor_dict()




					def 						snake_case_		(    self							)    ->	Union[str, Any]:
							UpperCamelCase__								=						self.image_processing_class(**self.image_processor_dict							)
							self.assertTrue(hasattr(_UpperCAmelCase     ,   """do_resize"""							)							)
							self.assertTrue(hasattr(_UpperCAmelCase     ,   """size"""							)							)
							self.assertTrue(hasattr(_UpperCAmelCase     ,   """do_normalize"""							)							)
							self.assertTrue(hasattr(_UpperCAmelCase     ,   """image_mean"""							)							)
							self.assertTrue(hasattr(_UpperCAmelCase     ,   """image_std"""							)							)
							self.assertTrue(hasattr(_UpperCAmelCase     ,   """do_convert_rgb"""							)							)




					def 						snake_case_		(    self							)    ->	Union[str, Any]:
							pass




					def 						snake_case_		(    self							)    ->	int:
							# Initialize image_processor
							UpperCamelCase__								=						self.image_processing_class(**self.image_processor_dict							)
							# create random PIL images
							UpperCamelCase__								=						self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCAmelCase							)
							for image in image_inputs:
									self.assertIsInstance(_UpperCAmelCase     ,   Image.Image							)

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

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

							# Test not batched input
							UpperCamelCase__								=						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
							UpperCamelCase__								=						image_processor(_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"""],
							    )     ,   )




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




					def 						snake_case_		(    self							)    ->	Optional[int]:
							UpperCamelCase__								=						BlipImageProcessingTester(self     ,   num_channels=4							)
							UpperCamelCase__								=						3




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




					def 						snake_case_		(    self							)    ->	List[str]:
							UpperCamelCase__								=						self.image_processing_class(**self.image_processor_dict							)
							self.assertTrue(hasattr(_UpperCAmelCase     ,   """do_resize"""							)							)
							self.assertTrue(hasattr(_UpperCAmelCase     ,   """size"""							)							)
							self.assertTrue(hasattr(_UpperCAmelCase     ,   """do_normalize"""							)							)
							self.assertTrue(hasattr(_UpperCAmelCase     ,   """image_mean"""							)							)
							self.assertTrue(hasattr(_UpperCAmelCase     ,   """image_std"""							)							)
							self.assertTrue(hasattr(_UpperCAmelCase     ,   """do_convert_rgb"""							)							)




					def 						snake_case_		(    self							)    ->	Optional[int]:
							pass




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

							# Test not batched input
							UpperCamelCase__								=						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
							UpperCamelCase__								=						image_processor(_UpperCAmelCase     ,   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 BlipImageProcessor


class 		lowerCAmelCase_				(  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							, )		->				List[str]:
        lowerCamelCase									=size if size is not None else {"""height""": 20, """width""": 20}
        lowerCamelCase									=parent
        lowerCamelCase									=batch_size
        lowerCamelCase									=num_channels
        lowerCamelCase									=image_size
        lowerCamelCase									=min_resolution
        lowerCamelCase									=max_resolution
        lowerCamelCase									=do_resize
        lowerCamelCase									=size
        lowerCamelCase									=do_normalize
        lowerCamelCase									=image_mean
        lowerCamelCase									=image_std
        lowerCamelCase									=do_pad
        lowerCamelCase									=do_convert_rgb





     def 						lowerCamelCase							(	self  )		->				str:
        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 						lowerCamelCase							(	self							, _UpperCAmelCase=False							, _UpperCAmelCase=False							, _UpperCAmelCase=False  )		->				Optional[int]:
        assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

        if equal_resolution:
           lowerCamelCase									=[]
           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									=[]
           for i in range(self.batch_size  ):
              lowerCamelCase				,       lowerCamelCase									=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									=[Image.fromarray(np.moveaxis(_UpperCAmelCase							, 0							, -1  )  ) for x in image_inputs]

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

        return image_inputs




@require_torch
@require_vision
class 		lowerCAmelCase_				(  UpperCamelCase				,     unittest.TestCase ):
     __lowerCamelCase       :    str 	=     BlipImageProcessor if is_vision_available() else None





     def 						lowerCamelCase							(	self  )		->				Dict:
        lowerCamelCase									=BlipImageProcessingTester(self  )





     @property
     def 						lowerCamelCase							(	self  )		->				Optional[int]:
        return self.image_processor_tester.prepare_image_processor_dict()





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





     def 						lowerCamelCase							(	self  )		->				Union[str, Any]:
        pass





     def 						lowerCamelCase							(	self  )		->				int:
        # Initialize image_processor
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        # create random PIL images
        lowerCamelCase									=self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCAmelCase  )
        for image in image_inputs:
           self.assertIsInstance(_UpperCAmelCase							, Image.Image  )

        # Test not batched input
        lowerCamelCase									=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									=image_processor(_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_processor
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        # create random numpy tensors
        lowerCamelCase									=self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCAmelCase							, numpify=_UpperCAmelCase  )
        for image in image_inputs:
           self.assertIsInstance(_UpperCAmelCase							, np.ndarray  )

        # Test not batched input
        lowerCamelCase									=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									=image_processor(_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_processor
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        # create random PyTorch tensors
        lowerCamelCase									=self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCAmelCase							, torchify=_UpperCAmelCase  )
        for image in image_inputs:
           self.assertIsInstance(_UpperCAmelCase							, torch.Tensor  )

        # Test not batched input
        lowerCamelCase									=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									=image_processor(_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"""],
            )							, )




@require_torch
@require_vision
class 		lowerCAmelCase_				(  UpperCamelCase				,     unittest.TestCase ):
     __lowerCamelCase       :    Any 	=     BlipImageProcessor if is_vision_available() else None





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





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





     def 						lowerCamelCase							(	self  )		->				List[str]:
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """do_resize"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """size"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """do_normalize"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """image_mean"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """image_std"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """do_convert_rgb"""  )  )





     def 						lowerCamelCase							(	self  )		->				Optional[int]:
        pass





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

        # Test not batched input
        lowerCamelCase									=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									=image_processor(_UpperCAmelCase							, 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"""],
            )							, )