




"""simple docstring"""





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


				'''simple docstring'''


				def __init__(				self				:       Tuple ,SCREAMING_SNAKE_CASE_				:       Any ,SCREAMING_SNAKE_CASE_				:       str=7 ,SCREAMING_SNAKE_CASE_				:       Dict=3 ,SCREAMING_SNAKE_CASE_				:       Any=1_8 ,SCREAMING_SNAKE_CASE_				:       Union[str, Any]=3_0 ,SCREAMING_SNAKE_CASE_				:       List[Any]=4_0_0 ,SCREAMING_SNAKE_CASE_				:       Union[str, Any]=True ,SCREAMING_SNAKE_CASE_				:       Dict=None ,SCREAMING_SNAKE_CASE_				:       Optional[Any]=True ,SCREAMING_SNAKE_CASE_				:       Union[str, Any]=False ,SCREAMING_SNAKE_CASE_				:       Dict=[0.48145466, 0.4578275, 0.40821073] ,SCREAMING_SNAKE_CASE_				:       Any=[0.26862954, 0.26130258, 0.27577711] ,SCREAMING_SNAKE_CASE_				:       Union[str, Any]=True ,):
										snake_case__    :   List[Any]							     =   size if size is not None else {'''height''': 2_0, '''width''': 2_0}
										snake_case__    :   List[str]							     =   parent
										snake_case__    :   List[str]							     =   batch_size
										snake_case__    :   List[str]							     =   num_channels
										snake_case__    :   Any							     =   image_size
										snake_case__    :   Dict							     =   min_resolution
										snake_case__    :   Optional[int]							     =   max_resolution
										snake_case__    :   List[Any]							     =   do_resize
										snake_case__    :   Tuple							     =   size
										snake_case__    :   str							     =   do_normalize
										snake_case__    :   Union[str, Any]							     =   image_mean
										snake_case__    :   List[str]							     =   image_std
										snake_case__    :   Tuple							     =   do_pad
										snake_case__    :   List[str]							     =   do_convert_rgb

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

										if equal_resolution:
																snake_case__    :   Optional[int]							     =   []
																for i in range(self.batch_size ):
																						image_inputs.append(
																						    np.random.randint(
																						        2_5_5 ,size=(self.num_channels, self.max_resolution, self.max_resolution) ,dtype=np.uinta ) )
										else:
																snake_case__    :   Dict							     =   []
																for i in range(self.batch_size ):
																						snake_case__    :   Any							     =   np.random.choice(np.arange(self.min_resolution ,self.max_resolution ) ,2 )
																						image_inputs.append(np.random.randint(2_5_5 ,size=(self.num_channels, width, height) ,dtype=np.uinta ) )

										if not numpify and not torchify:
																# PIL expects the channel dimension as last dimension
																snake_case__    :   List[Any]							     =   [Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE_ ,0 ,-1 ) ) for x in image_inputs]

										if torchify:
																snake_case__    :   List[Any]							     =   [torch.from_numpy(SCREAMING_SNAKE_CASE_ ) for x in image_inputs]

										return image_inputs







@require_torch
@require_vision
class  __lowercase    (     snake_case_  ,				unittest.TestCase		):


				'''simple docstring'''


				_lowerCAmelCase		     =BlipImageProcessor if is_vision_available() else None

				def  _lowerCamelCase   (				self				:       int ):
										snake_case__    :   List[str]							     =   BlipImageProcessingTester(self )

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

				def  _lowerCamelCase   (				self				:       Union[str, Any] ):
										snake_case__    :   List[Any]							     =   self.image_processing_class(**self.image_processor_dict )
										self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,'''do_resize''' ) )
										self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,'''size''' ) )
										self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,'''do_normalize''' ) )
										self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,'''image_mean''' ) )
										self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,'''image_std''' ) )
										self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,'''do_convert_rgb''' ) )

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

				def  _lowerCamelCase   (				self				:       List[str] ):
										# Initialize image_processor
										snake_case__    :   int							     =   self.image_processing_class(**self.image_processor_dict )
										# create random PIL images
										snake_case__    :   Optional[Any]							     =   self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE_ )
										for image in image_inputs:
																self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,Image.Image )

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

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

										# Test not batched input
										snake_case__    :   List[str]							     =   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
										snake_case__    :   Any							     =   image_processor(SCREAMING_SNAKE_CASE_ ,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  __lowercase    (     snake_case_  ,				unittest.TestCase		):


				'''simple docstring'''


				_lowerCAmelCase		     =BlipImageProcessor if is_vision_available() else None

				def  _lowerCamelCase   (				self				:       List[Any] ):
										snake_case__    :   Union[str, Any]							     =   BlipImageProcessingTester(self ,num_channels=4 )
										snake_case__    :   Tuple							     =   3

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

				def  _lowerCamelCase   (				self				:       Union[str, Any] ):
										snake_case__    :   Optional[int]							     =   self.image_processing_class(**self.image_processor_dict )
										self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,'''do_resize''' ) )
										self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,'''size''' ) )
										self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,'''do_normalize''' ) )
										self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,'''image_mean''' ) )
										self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,'''image_std''' ) )
										self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,'''do_convert_rgb''' ) )

				def  _lowerCamelCase   (				self				:       str ):
										pass





				def  _lowerCamelCase   (				self				:       List[Any] ):
										# Initialize image_processor
										snake_case__    :   str							     =   self.image_processing_class(**self.image_processor_dict )
										# create random PIL images
										snake_case__    :   Optional[int]							     =   self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE_ )
										for image in image_inputs:
																self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,Image.Image )

										# Test not batched input
										snake_case__    :   Tuple							     =   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
										snake_case__    :   Optional[int]							     =   image_processor(SCREAMING_SNAKE_CASE_ ,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'''],
										    ) ,)








"""simple docstring"""





class  __lowercase    :


				'''simple docstring'''


				class  __lowercase    :


								'''simple docstring'''


								_lowerCAmelCase		     ="_prev", "_data", "_next"

								def __init__(				self				:       List[Any] ,SCREAMING_SNAKE_CASE_				:       int ,SCREAMING_SNAKE_CASE_				:       Union[str, Any] ,SCREAMING_SNAKE_CASE_				:       Any ):
														snake_case__    :   Any							     =   link_p
														snake_case__    :   List[str]							     =   element
														snake_case__    :   str							     =   link_n





								def  _lowerCamelCase   (				self				:       Optional[Any] ):
														return (
														    F''' Prev -> {self._prev is not None}, Next -> {self._next is not None}'''
														)







				def __init__(				self				:       Any ):
										snake_case__    :   int							     =   self._Node(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Optional[int]							     =   self._Node(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Union[str, Any]							     =   self._trailer
										snake_case__    :   Union[str, Any]							     =   self._header
										snake_case__    :   Any							     =   0

				def __len__(				self				:       str ):
										return self._size

				def  _lowerCamelCase   (				self				:       Union[str, Any] ):
										return self.__len__() == 0

				def  _lowerCamelCase   (				self				:       Any ,SCREAMING_SNAKE_CASE_				:       List[Any] ,SCREAMING_SNAKE_CASE_				:       List[str] ,SCREAMING_SNAKE_CASE_				:       Dict ):
										# Create new_node by setting it's prev.link -> header
										# setting it's next.link -> trailer
										snake_case__    :   Dict							     =   self._Node(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Any							     =   new_node
										snake_case__    :   str							     =   new_node
										self._size += 1
										return self





				def  _lowerCamelCase   (				self				:       Union[str, Any] ,SCREAMING_SNAKE_CASE_				:       Dict ):
										snake_case__    :   Union[str, Any]							     =   node._prev
										snake_case__    :   Union[str, Any]							     =   node._next

										snake_case__    :   List[Any]							     =   successor
										snake_case__    :   Dict							     =   predecessor
										self._size -= 1
										snake_case__    :   Dict							     =   node._data
										snake_case__    :   List[str]							     =   None
										del node
										return temp







class  __lowercase    (     _DoublyLinkedBase		):


				'''simple docstring'''


				def  _lowerCamelCase   (				self				:       Optional[int] ):
										if self.is_empty():
																raise Exception('''List is empty''' )
										return self._header._next._data

				def  _lowerCamelCase   (				self				:       Union[str, Any] ):
										if self.is_empty():
																raise Exception('''List is empty''' )
										return self._trailer._prev._data

				def  _lowerCamelCase   (				self				:       Any ,SCREAMING_SNAKE_CASE_				:       Dict ):
										return self._insert(self._header ,SCREAMING_SNAKE_CASE_ ,self._header._next )

				def  _lowerCamelCase   (				self				:       Dict ,SCREAMING_SNAKE_CASE_				:       Union[str, Any] ):
										return self._insert(self._trailer._prev ,SCREAMING_SNAKE_CASE_ ,self._trailer )

				def  _lowerCamelCase   (				self				:       Optional[int] ):
										if self.is_empty():
																raise IndexError('''remove_first from empty list''' )
										return self._delete(self._header._next )





				def  _lowerCamelCase   (				self				:       Optional[Any] ):
										if self.is_empty():
																raise IndexError('''remove_first from empty list''' )
										return self._delete(self._trailer._prev )


