






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, prepare_image_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import LevitImageProcessor


class 							__UpperCamelCase     (  unittest.TestCase ):







	def __init__(	self   :				Union[str, Any]							,					_lowerCAmelCase   :				Optional[Any]							,					_lowerCAmelCase   :				Dict=7							,					_lowerCAmelCase   :				Dict=3							,					_lowerCAmelCase   :				List[str]=18							,					_lowerCAmelCase   :				str=30							,					_lowerCAmelCase   :				Tuple=400							,					_lowerCAmelCase   :				Union[str, Any]=True							,					_lowerCAmelCase   :				str=None							,					_lowerCAmelCase   :				Any=True							,					_lowerCAmelCase   :				List[Any]=None							,					_lowerCAmelCase   :				Optional[Any]=True							,					_lowerCAmelCase   :				str=[0.5, 0.5, 0.5]							,					_lowerCAmelCase   :				Dict=[0.5, 0.5, 0.5]							,					)		->			Optional[Any]:
		"""simple docstring"""







		__lowercase   							=	size if size is not None else {"""shortest_edge""": 18}
		__lowercase   							=	crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
		__lowercase   							=	parent
		__lowercase   							=	batch_size
		__lowercase   							=	num_channels
		__lowercase   							=	image_size
		__lowercase   							=	min_resolution
		__lowercase   							=	max_resolution
		__lowercase   							=	do_resize
		__lowercase   							=	size
		__lowercase   							=	do_center_crop
		__lowercase   							=	crop_size
		__lowercase   							=	do_normalize
		__lowercase   							=	image_mean
		__lowercase   							=	image_std







	def 			_a (	self   :				Optional[Any]      )		->			Optional[int]:
		"""simple docstring"""







		return {
		    "image_mean": self.image_mean,
		    "image_std": self.image_std,
		    "do_normalize": self.do_normalize,
		    "do_resize": self.do_resize,
		    "do_center_crop": self.do_center_crop,
		    "size": self.size,
		    "crop_size": self.crop_size,
		}


@require_torch
@require_vision
class 							__UpperCamelCase     (  _A ,      unittest.TestCase ):
	__snake_case						:str =							LevitImageProcessor if is_vision_available() else None







	def 			_a (	self   :				Optional[Any]      )		->			Dict:
		"""simple docstring"""







		__lowercase   							=	LevitImageProcessingTester(self      )







	@property
	def 			_a (	self   :				str      )		->			str:
		"""simple docstring"""







		return self.image_processor_tester.prepare_image_processor_dict()







	def 			_a (	self   :				Tuple      )		->			Optional[int]:
		"""simple docstring"""







		__lowercase   							=	self.image_processing_class(**self.image_processor_dict      )
		self.assertTrue(hasattr(__lowerCAmelCase							,					"""image_mean"""      )      )
		self.assertTrue(hasattr(__lowerCAmelCase							,					"""image_std"""      )      )
		self.assertTrue(hasattr(__lowerCAmelCase							,					"""do_normalize"""      )      )
		self.assertTrue(hasattr(__lowerCAmelCase							,					"""do_resize"""      )      )
		self.assertTrue(hasattr(__lowerCAmelCase							,					"""do_center_crop"""      )      )
		self.assertTrue(hasattr(__lowerCAmelCase							,					"""size"""      )      )







	def 			_a (	self   :				Any      )		->			Union[str, Any]:
		"""simple docstring"""







		__lowercase   							=	self.image_processing_class.from_dict(self.image_processor_dict      )
		self.assertEqual(image_processor.size							,					{"""shortest_edge""": 18}      )
		self.assertEqual(image_processor.crop_size							,					{"""height""": 18, """width""": 18}      )

		__lowercase   							=	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   :				Optional[int]      )		->			Dict:
		"""simple docstring"""







		pass







	def 			_a (	self   :				List[Any]      )		->			Any:
		"""simple docstring"""







		__lowercase   							=	self.image_processing_class(**self.image_processor_dict      )
		# create random PIL images
		__lowercase   							=	prepare_image_inputs(self.image_processor_tester							,					equal_resolution=__lowerCAmelCase      )
		for image in image_inputs:
			self.assertIsInstance(__lowerCAmelCase							,					Image.Image      )

		# Test not batched input
		__lowercase   							=	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
		__lowercase   							=	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   :				Optional[Any]      )		->			Tuple:
		"""simple docstring"""







		__lowercase   							=	self.image_processing_class(**self.image_processor_dict      )
		# create random numpy tensors
		__lowercase   							=	prepare_image_inputs(self.image_processor_tester							,					equal_resolution=__lowerCAmelCase							,					numpify=__lowerCAmelCase      )
		for image in image_inputs:
			self.assertIsInstance(__lowerCAmelCase							,					np.ndarray      )

		# Test not batched input
		__lowercase   							=	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
		__lowercase   							=	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   :				Union[str, Any]      )		->			Dict:
		"""simple docstring"""







		__lowercase   							=	self.image_processing_class(**self.image_processor_dict      )
		# create random PyTorch tensors
		__lowercase   							=	prepare_image_inputs(self.image_processor_tester							,					equal_resolution=__lowerCAmelCase							,					torchify=__lowerCAmelCase      )
		for image in image_inputs:
			self.assertIsInstance(__lowerCAmelCase							,					torch.Tensor      )

		# Test not batched input
		__lowercase   							=	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
		__lowercase   							=	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 	SCREAMING_SNAKE_CASE_							(	_snake_case	:bytes				)  ->     str:
   return "".join([hex(_snake_case				)[2:].zfill(2				).upper() for byte in list(_snake_case				)]				)

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:str				)  ->     bytes:
   # Check data validity, following RFC3548
   # https://www.ietf.org/rfc/rfc3548.txt
   if (len(_snake_case				) % 2) != 0:
      raise ValueError(
          '''Base16 encoded data is invalid:
Data does not have an even number of hex digits.'''				)
   # Check the character set - the standard base16 alphabet
   # is uppercase according to RFC3548 section 6
   if not set(_snake_case				) <= set('''0123456789ABCDEF'''				):
      raise ValueError(
          '''Base16 encoded data is invalid:
Data is not uppercase hex or it contains invalid characters.'''				)
   # For every two hexadecimal digits (= a byte), turn it into an integer.
   # Then, string the result together into bytes, and return it.
   return bytes(int(data[i] + data[i + 1]	,   16				) for i in range(0	,   len(_snake_case				)	,   2				)				)


if __name__ == "__main__":
   import doctest

   doctest.testmod()



