



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



	def __init__(       self			,	UpperCAmelCase__			,	UpperCAmelCase__=7			,	UpperCAmelCase__=3			,	UpperCAmelCase__=18			,	UpperCAmelCase__=30			,	UpperCAmelCase__=400			,	UpperCAmelCase__=True			,	UpperCAmelCase__=None			,	UpperCAmelCase__=True			,	UpperCAmelCase__=None			,	UpperCAmelCase__=True			,	UpperCAmelCase__=[0.48_145_466, 0.4_578_275, 0.40_821_073]			,	UpperCAmelCase__=[0.26_862_954, 0.26_130_258, 0.27_577_711]			,	UpperCAmelCase__=True			,	):
							A__							=       size if size is not None else {"height": 224, "width": 224}
							A__							=       crop_size if crop_size is not None else {"height": 18, "width": 18}
							A__							=       parent
							A__							=       batch_size
							A__							=       num_channels
							A__							=       image_size
							A__							=       min_resolution
							A__							=       max_resolution
							A__							=       do_resize
							A__							=       size
							A__							=       do_center_crop
							A__							=       crop_size
							A__							=       do_normalize
							A__							=       image_mean
							A__							=       image_std
							A__							=       do_convert_rgb



	def 			__A							(       self		):
							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 			__A							(       self			,	UpperCAmelCase__=False			,	UpperCAmelCase__=False			,	UpperCAmelCase__=False		):
							assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

							if torchify:
													A__							=       [torch.from_numpy(UpperCAmelCase__		) for x in image_inputs]

							return image_inputs






@require_torch
@require_vision
class      UpperCamelCase    (						_UpperCAmelCase       ,    unittest.TestCase     ):
	lowerCAmelCase	:   Tuple				=	ChineseCLIPImageProcessor if is_vision_available() else None



	def 			__A							(       self		):
							A__							=       ChineseCLIPImageProcessingTester(self			,	do_center_crop=UpperCAmelCase__		)



	@property
	def 			__A							(       self		):
							return self.image_processor_tester.prepare_image_processor_dict()



	def 			__A							(       self		):
							A__							=       self.image_processing_class(**self.image_processor_dict		)
							self.assertTrue(hasattr(UpperCAmelCase__			,	"do_resize"		)		)
							self.assertTrue(hasattr(UpperCAmelCase__			,	"size"		)		)
							self.assertTrue(hasattr(UpperCAmelCase__			,	"do_center_crop"		)		)
							self.assertTrue(hasattr(UpperCAmelCase__			,	"center_crop"		)		)
							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 			__A							(       self		):
							A__							=       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}		)

							A__							=       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 			__A							(       self		):
							pass



	def 			__A							(       self		):
							# Initialize image_processing
							A__							=       self.image_processing_class(**self.image_processor_dict		)
							# create random PIL images
							A__							=       self.image_processor_tester.prepare_inputs(equal_resolution=UpperCAmelCase__		)
							for image in image_inputs:
													self.assertIsInstance(UpperCAmelCase__			,	Image.Image		)

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



	def 			__A							(       self		):
							# Initialize image_processing
							A__							=       self.image_processing_class(**self.image_processor_dict		)
							# create random numpy tensors
							A__							=       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
							A__							=       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
							A__							=       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.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )			,	)



	def 			__A							(       self		):
							# Initialize image_processing
							A__							=       self.image_processing_class(**self.image_processor_dict		)
							# create random PyTorch tensors
							A__							=       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
							A__							=       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
							A__							=       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.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )			,	)






@require_torch
@require_vision
class      UpperCamelCase    (						_UpperCAmelCase       ,    unittest.TestCase     ):
	lowerCAmelCase	:   Optional[int]				=	ChineseCLIPImageProcessor if is_vision_available() else None



	def 			__A							(       self		):
							A__							=       ChineseCLIPImageProcessingTester(self			,	num_channels=4			,	do_center_crop=UpperCAmelCase__		)
							A__							=       3



	@property
	def 			__A							(       self		):
							return self.image_processor_tester.prepare_image_processor_dict()



	def 			__A							(       self		):
							A__							=       self.image_processing_class(**self.image_processor_dict		)
							self.assertTrue(hasattr(UpperCAmelCase__			,	"do_resize"		)		)
							self.assertTrue(hasattr(UpperCAmelCase__			,	"size"		)		)
							self.assertTrue(hasattr(UpperCAmelCase__			,	"do_center_crop"		)		)
							self.assertTrue(hasattr(UpperCAmelCase__			,	"center_crop"		)		)
							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 			__A							(       self		):
							pass



	def 			__A							(       self		):
							# Initialize image_processing
							A__							=       self.image_processing_class(**self.image_processor_dict		)
							# create random PIL images
							A__							=       self.image_processor_tester.prepare_inputs(equal_resolution=UpperCAmelCase__		)
							for image in image_inputs:
													self.assertIsInstance(UpperCAmelCase__			,	Image.Image		)

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










from .dependency_versions_table import deps
from .utils.versions import require_version, require_version_core


# define which module versions we always want to check at run time
# (usually the ones defined in `install_requires` in setup.py)
#
# order specific notes:
# - tqdm must be checked before tokenizers

UpperCAmelCase_							:      List[Any]      =  [
    "python",
    "tqdm",
    "regex",
    "requests",
    "packaging",
    "filelock",
    "numpy",
    "tokenizers",
    "huggingface-hub",
    "safetensors",
    "accelerate",
    "pyyaml",
]

for pkg in pkgs_to_check_at_runtime:
						if pkg in deps:
												if pkg == "tokenizers":
																		# must be loaded here, or else tqdm check may fail
																		from .utils import is_tokenizers_available

																		if not is_tokenizers_available():
																								continue  # not required, check version only if installed
												elif pkg == "accelerate":
																		# must be loaded here, or else tqdm check may fail
																		from .utils import is_accelerate_available

																		# Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of
																		# Transformers with PyTorch
																		if not is_accelerate_available():
																								continue  # not required, check version only if installed

												require_version_core(deps[pkg])
						else:
												raise ValueError(F'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''')
def  UpperCamelCase						(       _A			:				List[Any]			,							_A			:				int=None    )->       Optional[int]:





						"""simple docstring"""





						require_version(deps[pkg]			,							_A    )





