



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 ChineseCLIPImageProcessor

class     _UpperCamelCase			(   unittest.TestCase					):






			'''simple docstring'''

			def __init__(    self	:    List[Any]  ,		snake_case_	:    str  ,		snake_case_	:    List[str]=7  ,		snake_case_	:    List[str]=3  ,		snake_case_	:    Optional[int]=18  ,		snake_case_	:    List[Any]=30  ,		snake_case_	:    Tuple=400  ,		snake_case_	:    Tuple=True  ,		snake_case_	:    List[Any]=None  ,		snake_case_	:    str=True  ,		snake_case_	:    int=None  ,		snake_case_	:    Optional[Any]=True  ,		snake_case_	:    List[Any]=[0.4814_5466, 0.457_8275, 0.4082_1073]  ,		snake_case_	:    List[str]=[0.2686_2954, 0.2613_0258, 0.2757_7711]  ,		snake_case_	:    Tuple=True  ,		):
							UpperCamelCase_:							int		=  size if size is not None else {'''height''': 224, '''width''': 224}
							UpperCamelCase_:							Optional[Any]		=  crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
							UpperCamelCase_:							int		=  parent
							UpperCamelCase_:							Union[str, Any]		=  batch_size
							UpperCamelCase_:							List[Any]		=  num_channels
							UpperCamelCase_:							Dict		=  image_size
							UpperCamelCase_:							List[str]		=  min_resolution
							UpperCamelCase_:							Optional[Any]		=  max_resolution
							UpperCamelCase_:							Union[str, Any]		=  do_resize
							UpperCamelCase_:							Dict		=  size
							UpperCamelCase_:							Any		=  do_center_crop
							UpperCamelCase_:							Union[str, Any]		=  crop_size
							UpperCamelCase_:							List[str]		=  do_normalize
							UpperCamelCase_:							Optional[Any]		=  image_mean
							UpperCamelCase_:							Optional[Any]		=  image_std
							UpperCamelCase_:							List[Any]		=  do_convert_rgb

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

							if equal_resolution:
											UpperCamelCase_:							Dict		=  []
											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_:							Tuple		=  []
											for i in range(self.batch_size ):
															UpperCamelCase_:							Tuple		=  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_:							Optional[int]		=  [Image.fromarray(np.moveaxis(__A  ,		0  ,		-1 ) ) for x in image_inputs]

							if torchify:
											UpperCamelCase_:							str		=  [torch.from_numpy(__A ) for x in image_inputs]

							return image_inputs




@require_torch
@require_vision
class     _UpperCamelCase			(   lowerCamelCase__       ,    unittest.TestCase					):






			'''simple docstring'''

			__UpperCamelCase :  Tuple    =	ChineseCLIPImageProcessor if is_vision_available() else None

			def  lowerCAmelCase__		(    self	:    Dict ):
							UpperCamelCase_:							Optional[Any]		=  ChineseCLIPImageProcessingTester(self  ,		do_center_crop=__A )

			@property
			def  lowerCAmelCase__		(    self	:    Tuple ):
							return self.image_processor_tester.prepare_image_processor_dict()

			def  lowerCAmelCase__		(    self	:    Any ):
							UpperCamelCase_:							List[Any]		=  self.image_processing_class(**self.image_processor_dict )
							self.assertTrue(hasattr(__A  ,		"""do_resize""" ) )
							self.assertTrue(hasattr(__A  ,		"""size""" ) )
							self.assertTrue(hasattr(__A  ,		"""do_center_crop""" ) )
							self.assertTrue(hasattr(__A  ,		"""center_crop""" ) )
							self.assertTrue(hasattr(__A  ,		"""do_normalize""" ) )
							self.assertTrue(hasattr(__A  ,		"""image_mean""" ) )
							self.assertTrue(hasattr(__A  ,		"""image_std""" ) )
							self.assertTrue(hasattr(__A  ,		"""do_convert_rgb""" ) )

			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							UpperCamelCase_:							Union[str, Any]		=  self.image_processing_class.from_dict(self.image_processor_dict )
							self.assertEqual(image_processor.size  ,		{"""height""": 224, """width""": 224} )
							self.assertEqual(image_processor.crop_size  ,		{"""height""": 18, """width""": 18} )

							UpperCamelCase_:							int		=  self.image_processing_class.from_dict(self.image_processor_dict  ,		size=42  ,		crop_size=84 )
							self.assertEqual(image_processor.size  ,		{"""shortest_edge""": 42} )
							self.assertEqual(image_processor.crop_size  ,		{"""height""": 84, """width""": 84} )

			def  lowerCAmelCase__		(    self	:    Union[str, Any] ):
							pass

			def  lowerCAmelCase__		(    self	:    Tuple ):
							# Initialize image_processing
							UpperCamelCase_:							Dict		=  self.image_processing_class(**self.image_processor_dict )
							# create random PIL images
							UpperCamelCase_:							Optional[int]		=  self.image_processor_tester.prepare_inputs(equal_resolution=__A )
							for image in image_inputs:
											self.assertIsInstance(__A  ,		Image.Image )

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

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

							# Test not batched input
							UpperCamelCase_:							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
							UpperCamelCase_:							List[str]		=  image_processing(__A  ,		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     _UpperCamelCase			(   lowerCamelCase__       ,    unittest.TestCase					):






			'''simple docstring'''

			__UpperCamelCase :  Optional[Any]    =	ChineseCLIPImageProcessor if is_vision_available() else None

			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							UpperCamelCase_:							str		=  ChineseCLIPImageProcessingTester(self  ,		num_channels=4  ,		do_center_crop=__A )
							UpperCamelCase_:							Dict		=  3

			@property
			def  lowerCAmelCase__		(    self	:    List[Any] ):
							return self.image_processor_tester.prepare_image_processor_dict()

			def  lowerCAmelCase__		(    self	:    Dict ):
							UpperCamelCase_:							List[Any]		=  self.image_processing_class(**self.image_processor_dict )
							self.assertTrue(hasattr(__A  ,		"""do_resize""" ) )
							self.assertTrue(hasattr(__A  ,		"""size""" ) )
							self.assertTrue(hasattr(__A  ,		"""do_center_crop""" ) )
							self.assertTrue(hasattr(__A  ,		"""center_crop""" ) )
							self.assertTrue(hasattr(__A  ,		"""do_normalize""" ) )
							self.assertTrue(hasattr(__A  ,		"""image_mean""" ) )
							self.assertTrue(hasattr(__A  ,		"""image_std""" ) )
							self.assertTrue(hasattr(__A  ,		"""do_convert_rgb""" ) )

			def  lowerCAmelCase__		(    self	:    List[Any] ):
							pass




			def  lowerCAmelCase__		(    self	:    Tuple ):
							# Initialize image_processing
							UpperCamelCase_:							Optional[Any]		=  self.image_processing_class(**self.image_processor_dict )
							# create random PIL images
							UpperCamelCase_:							Dict		=  self.image_processor_tester.prepare_inputs(equal_resolution=__A )
							for image in image_inputs:
											self.assertIsInstance(__A  ,		Image.Image )

							# Test not batched input
							UpperCamelCase_:							Tuple		=  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
							UpperCamelCase_:							Tuple		=  image_processing(__A  ,		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"""],
							    )  ,		)








import json
import os
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision, slow
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 ImageGPTImageProcessor

class     _UpperCamelCase			(   unittest.TestCase					):






			'''simple docstring'''

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




			def  lowerCAmelCase__		(    self	:    str ):
							return {
							    # here we create 2 clusters for the sake of simplicity
							    "clusters": np.asarray(
							        [
							            [0.8866_4436_3403_3203, 0.6618_8293_6954_4983, 0.3891_7464_0178_6804],
							            [-0.6042_5591_4688_1104, -0.0_2295_0088_6052_8469, 0.5423_7973_6900_3296],
							        ] ),
							    "do_resize": self.do_resize,
							    "size": self.size,
							    "do_normalize": self.do_normalize,
							}




@require_torch
@require_vision
class     _UpperCamelCase			(   _A       ,    unittest.TestCase					):






			'''simple docstring'''

			__UpperCamelCase :  Optional[int]    =	ImageGPTImageProcessor if is_vision_available() else None

			def  lowerCAmelCase__		(    self	:    List[str] ):
							UpperCamelCase_:							Any		=  ImageGPTImageProcessingTester(self )

			@property
			def  lowerCAmelCase__		(    self	:    Optional[int] ):
							return self.image_processor_tester.prepare_image_processor_dict()

			def  lowerCAmelCase__		(    self	:    Optional[int] ):
							UpperCamelCase_:							List[Any]		=  self.image_processing_class(**self.image_processor_dict )
							self.assertTrue(hasattr(snake_case_  ,		"""clusters""" ) )
							self.assertTrue(hasattr(snake_case_  ,		"""do_resize""" ) )
							self.assertTrue(hasattr(snake_case_  ,		"""size""" ) )
							self.assertTrue(hasattr(snake_case_  ,		"""do_normalize""" ) )

			def  lowerCAmelCase__		(    self	:    int ):
							UpperCamelCase_:							List[Any]		=  self.image_processing_class.from_dict(self.image_processor_dict )
							self.assertEqual(image_processor.size  ,		{"""height""": 18, """width""": 18} )

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

			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							UpperCamelCase_:							int		=  self.image_processing_class(**self.image_processor_dict )
							UpperCamelCase_:							Optional[int]		=  json.loads(image_processor.to_json_string() )
							for key, value in self.image_processor_dict.items():
											if key == "clusters":
															self.assertTrue(np.array_equal(snake_case_  ,		obj[key] ) )
											else:
															self.assertEqual(obj[key]  ,		snake_case_ )

			def  lowerCAmelCase__		(    self	:    List[Any] ):
							UpperCamelCase_:							Dict		=  self.image_processing_class(**self.image_processor_dict )

							with tempfile.TemporaryDirectory() as tmpdirname:
											UpperCamelCase_:							int		=  os.path.join(snake_case_  ,		"""image_processor.json""" )
											image_processor_first.to_json_file(snake_case_ )
											UpperCamelCase_:							Any		=  self.image_processing_class.from_json_file(snake_case_ ).to_dict()

							UpperCamelCase_:							str		=  image_processor_first.to_dict()
							for key, value in image_processor_first.items():
											if key == "clusters":
															self.assertTrue(np.array_equal(snake_case_  ,		image_processor_second[key] ) )
											else:
															self.assertEqual(image_processor_first[key]  ,		snake_case_ )

			def  lowerCAmelCase__		(    self	:    Union[str, Any] ):
							UpperCamelCase_:							Optional[Any]		=  self.image_processing_class(**self.image_processor_dict )

							with tempfile.TemporaryDirectory() as tmpdirname:
											image_processor_first.save_pretrained(snake_case_ )
											UpperCamelCase_:							Optional[int]		=  self.image_processing_class.from_pretrained(snake_case_ ).to_dict()

							UpperCamelCase_:							Union[str, Any]		=  image_processor_first.to_dict()
							for key, value in image_processor_first.items():
											if key == "clusters":
															self.assertTrue(np.array_equal(snake_case_  ,		image_processor_second[key] ) )
											else:
															self.assertEqual(image_processor_first[key]  ,		snake_case_ )




			@unittest.skip("""ImageGPT requires clusters at initialization""" )
			def  lowerCAmelCase__		(    self	:    List[Any] ):
							pass







def     A__   (  )					->		Optional[int]:
				UpperCamelCase_:							Optional[int]		=  load_dataset("""hf-internal-testing/fixtures_image_utils"""       ,      split="""test"""  )

				UpperCamelCase_:							Tuple		=  Image.open(dataset[4]["""file"""]  )
				UpperCamelCase_:							Union[str, Any]		=  Image.open(dataset[5]["""file"""]  )

				UpperCamelCase_:							List[str]		=  [imagea, imagea]

				return images





@require_vision
@require_torch
class     _UpperCamelCase			(   unittest.TestCase					):






			'''simple docstring'''

			@slow
			def  lowerCAmelCase__		(    self	:    List[str] ):
							UpperCamelCase_:							List[Any]		=  ImageGPTImageProcessor.from_pretrained("""openai/imagegpt-small""" )

							UpperCamelCase_:							List[str]		=  prepare_images()

							# test non-batched
							UpperCamelCase_:							List[str]		=  image_processing(images[0]  ,		return_tensors="""pt""" )

							self.assertIsInstance(encoding.input_ids  ,		torch.LongTensor )
							self.assertEqual(encoding.input_ids.shape  ,		(1, 1024) )

							UpperCamelCase_:							Union[str, Any]		=  [306, 191, 191]
							self.assertEqual(encoding.input_ids[0, :3].tolist()  ,		snake_case_ )

							# test batched
							UpperCamelCase_:							Optional[int]		=  image_processing(snake_case_  ,		return_tensors="""pt""" )

							self.assertIsInstance(encoding.input_ids  ,		torch.LongTensor )
							self.assertEqual(encoding.input_ids.shape  ,		(2, 1024) )

							UpperCamelCase_:							str		=  [303, 13, 13]
							self.assertEqual(encoding.input_ids[1, -3:].tolist()  ,		snake_case_ )






