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






		def __init__(	self	:							Optional[Any]							,    SCREAMING_SNAKE_CASE__	:							int							,    SCREAMING_SNAKE_CASE__	:							Optional[int]=7							,    SCREAMING_SNAKE_CASE__	:							Any=3							,    SCREAMING_SNAKE_CASE__	:							List[Any]=18							,    SCREAMING_SNAKE_CASE__	:							Optional[Any]=30							,    SCREAMING_SNAKE_CASE__	:							List[Any]=4_00							,    SCREAMING_SNAKE_CASE__	:							Union[str, Any]=True							,    SCREAMING_SNAKE_CASE__	:							Union[str, Any]=None							,    SCREAMING_SNAKE_CASE__	:							Union[str, Any]=True							,    SCREAMING_SNAKE_CASE__	:							List[Any]=None							,    SCREAMING_SNAKE_CASE__	:							Any=True							,    SCREAMING_SNAKE_CASE__	:							List[Any]=[0.48145466, 0.4578275, 0.40821073]							,    SCREAMING_SNAKE_CASE__	:							Optional[int]=[0.26862954, 0.26130258, 0.27577711]							,    SCREAMING_SNAKE_CASE__	:							List[str]=True							,    )  ->							Tuple:
						__lowerCamelCase					      =			size if size is not None else {'''shortest_edge''': 20}
						__lowerCamelCase					      =			crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
						__lowerCamelCase					      =			parent
						__lowerCamelCase					      =			batch_size
						__lowerCamelCase					      =			num_channels
						__lowerCamelCase					      =			image_size
						__lowerCamelCase					      =			min_resolution
						__lowerCamelCase					      =			max_resolution
						__lowerCamelCase					      =			do_resize
						__lowerCamelCase					      =			size
						__lowerCamelCase					      =			do_center_crop
						__lowerCamelCase					      =			crop_size
						__lowerCamelCase					      =			do_normalize
						__lowerCamelCase					      =			image_mean
						__lowerCamelCase					      =			image_std
						__lowerCamelCase					      =			do_convert_rgb






		def 				__A (	self	:							Tuple						)  ->							Dict:
						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	:							Tuple							,    SCREAMING_SNAKE_CASE__	:							Any=False							,    SCREAMING_SNAKE_CASE__	:							Tuple=False							,    SCREAMING_SNAKE_CASE__	:							str=False						)  ->							Tuple:
						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(
														        2_55							,    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(2_55							,    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(_lowerCAmelCase							,    0							,    -1						)						) for x in image_inputs]

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

						return image_inputs





@require_torch
@require_vision
class      lowerCAmelCase__  (			A__						,			unittest.TestCase    ):
		a__ :		Optional[int]   						=	CLIPImageProcessor if is_vision_available() else None






		def 				__A (	self	:							Optional[int]						)  ->							Tuple:
						__lowerCamelCase					      =			CLIPImageProcessingTester(self						)






		@property
		def 				__A (	self	:							str						)  ->							Tuple:
						return self.image_processor_tester.prepare_image_processor_dict()






		def 				__A (	self	:							int						)  ->							Dict:
						__lowerCamelCase					      =			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 				__A (	self	:							Dict						)  ->							int:
						__lowerCamelCase					      =			self.image_processing_class.from_dict(self.image_processor_dict						)
						self.assertEqual(image_processor.size							,    {'''shortest_edge''': 20}						)
						self.assertEqual(image_processor.crop_size							,    {'''height''': 18, '''width''': 18}						)

						__lowerCamelCase					      =			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	:							Any						)  ->							Union[str, Any]:
						pass






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

						# Test not batched input
						__lowerCamelCase					      =			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
						__lowerCamelCase					      =			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 				__A (	self	:							List[Any]						)  ->							Any:
						# Initialize image_processing
						__lowerCamelCase					      =			self.image_processing_class(**self.image_processor_dict						)
						# create random numpy tensors
						__lowerCamelCase					      =			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					      =			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
						__lowerCamelCase					      =			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 				__A (	self	:							Tuple						)  ->							Any:
						# Initialize image_processing
						__lowerCamelCase					      =			self.image_processing_class(**self.image_processor_dict						)
						# create random PyTorch tensors
						__lowerCamelCase					      =			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					      =			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
						__lowerCamelCase					      =			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__  (			A__						,			unittest.TestCase    ):
		a__ :		int   						=	CLIPImageProcessor if is_vision_available() else None






		def 				__A (	self	:							int						)  ->							str:
						__lowerCamelCase					      =			CLIPImageProcessingTester(self							,    num_channels=4						)
						__lowerCamelCase					      =			3






		@property
		def 				__A (	self	:							Tuple						)  ->							Optional[Any]:
						return self.image_processor_tester.prepare_image_processor_dict()






		def 				__A (	self	:							List[Any]						)  ->							int:
						__lowerCamelCase					      =			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 				__A (	self	:							Any						)  ->							Any:
						pass







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

						# Test not batched input
						__lowerCamelCase					      =			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
						__lowerCamelCase					      =			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'''],
						    )							,    )




def 				__magic_name__				(     __lowerCAmelCase			:		list	,						__lowerCAmelCase			:		int	,						__lowerCAmelCase			:		int	,						__lowerCAmelCase			:		int							)    ->     int:
				__lowerCamelCase					      =			int((lower_bound + upper_bound) // 2							)
				if array[r] == value:
								return r
				if lower_bound >= upper_bound:
								return -1
				if array[r] < value:
								return binary_search(__lowerCAmelCase	,						r + 1	,						__lowerCAmelCase	,						__lowerCAmelCase							)
				else:
								return binary_search(__lowerCAmelCase	,						__lowerCAmelCase	,						r - 1	,						__lowerCAmelCase							)




def 				__magic_name__				(     __lowerCAmelCase			:		int	,						__lowerCAmelCase			:		list							)    ->     list:
				__lowerCamelCase					      =			0
				if matrix[index][0] == value:
								return [index, 0]
				while index < len(__lowerCAmelCase							) and matrix[index][0] < value:
								__lowerCamelCase					      =			binary_search(matrix[index]	,						0	,						len(matrix[index]							) - 1	,						__lowerCAmelCase							)
								if r != -1:
												return [index, r]
								index += 1
				return [-1, -1]


if __name__ == "__main__":
					import doctest

					doctest.testmod()



