



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




		'''simple docstring'''



		def __init__(self		,__lowerCamelCase		,__lowerCamelCase=7		,__lowerCamelCase=3		,__lowerCamelCase=18		,__lowerCamelCase=30		,__lowerCamelCase=4_00		,__lowerCamelCase=True		,__lowerCamelCase=None		,__lowerCamelCase=True		,__lowerCamelCase=None		,__lowerCamelCase=True		,__lowerCamelCase=[0.4814_5466, 0.457_8275, 0.4082_1073]		,__lowerCamelCase=[0.2686_2954, 0.2613_0258, 0.2757_7711]		,__lowerCamelCase=True		,)					->      Optional[int]:

						"""simple docstring"""






						lowerCAmelCase__					:    List[str]												=					size if size is not None else {'''height''': 2_24, '''width''': 2_24}
						lowerCAmelCase__					:    int												=					crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
						lowerCAmelCase__					:    str												=					parent
						lowerCAmelCase__					:    Optional[int]												=					batch_size
						lowerCAmelCase__					:    Optional[int]												=					num_channels
						lowerCAmelCase__					:    Optional[int]												=					image_size
						lowerCAmelCase__					:    Union[str, Any]												=					min_resolution
						lowerCAmelCase__					:    List[str]												=					max_resolution
						lowerCAmelCase__					:    Any												=					do_resize
						lowerCAmelCase__					:    Any												=					size
						lowerCAmelCase__					:    int												=					do_center_crop
						lowerCAmelCase__					:    Tuple												=					crop_size
						lowerCAmelCase__					:    Optional[int]												=					do_normalize
						lowerCAmelCase__					:    Dict												=					image_mean
						lowerCAmelCase__					:    Any												=					image_std
						lowerCAmelCase__					:    Tuple												=					do_convert_rgb



		def 		lowerCAmelCase__				(self			)					->      List[str]:

						"""simple docstring"""






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

						"""simple docstring"""






						assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

						if equal_resolution:
										lowerCAmelCase__					:    Union[str, Any]												=					[]
										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__					:    Tuple												=					[]
										for i in range(self.batch_size			):
														lowerCAmelCase__						,		lowerCAmelCase__					:    Union[str, Any]												=					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__					:    Dict												=					[Image.fromarray(np.moveaxis(lowercase_		,0		,-1			)			) for x in image_inputs]

						if torchify:
										lowerCAmelCase__					:    Tuple												=					[torch.from_numpy(lowercase_			) for x in image_inputs]

						return image_inputs



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




		'''simple docstring'''



		snake_case_     =ChineseCLIPImageProcessor if is_vision_available() else None



		def 		lowerCAmelCase__				(self			)					->      int:

						"""simple docstring"""






						lowerCAmelCase__					:    str												=					ChineseCLIPImageProcessingTester(self		,do_center_crop=lowercase_			)



		@property
		def 		lowerCAmelCase__				(self			)					->      str:

						"""simple docstring"""






						return self.image_processor_tester.prepare_image_processor_dict()



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

						"""simple docstring"""






						lowerCAmelCase__					:    Optional[Any]												=					self.image_processing_class(**self.image_processor_dict			)
						self.assertTrue(hasattr(lowercase_		,'''do_resize'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''size'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''do_center_crop'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''center_crop'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''do_normalize'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''image_mean'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''image_std'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''do_convert_rgb'''			)			)



		def 		lowerCAmelCase__				(self			)					->      List[str]:

						"""simple docstring"""






						lowerCAmelCase__					:    Union[str, Any]												=					self.image_processing_class.from_dict(self.image_processor_dict			)
						self.assertEqual(image_processor.size		,{'''height''': 2_24, '''width''': 2_24}			)
						self.assertEqual(image_processor.crop_size		,{'''height''': 18, '''width''': 18}			)

						lowerCAmelCase__					:    Any												=					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			)					->      Optional[Any]:

						"""simple docstring"""






						pass



		def 		lowerCAmelCase__				(self			)					->      str:

						"""simple docstring"""






						lowerCAmelCase__					:    str												=					self.image_processing_class(**self.image_processor_dict			)
						# create random PIL images
						lowerCAmelCase__					:    Dict												=					self.image_processor_tester.prepare_inputs(equal_resolution=lowercase_			)
						for image in image_inputs:
										self.assertIsInstance(lowercase_		,Image.Image			)

						# Test not batched input
						lowerCAmelCase__					:    int												=					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__					:    List[str]												=					image_processing(lowercase_		,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:

						"""simple docstring"""






						lowerCAmelCase__					:    Optional[Any]												=					self.image_processing_class(**self.image_processor_dict			)
						# create random numpy tensors
						lowerCAmelCase__					:    Tuple												=					self.image_processor_tester.prepare_inputs(equal_resolution=lowercase_		,numpify=lowercase_			)
						for image in image_inputs:
										self.assertIsInstance(lowercase_		,np.ndarray			)

						# Test not batched input
						lowerCAmelCase__					:    Dict												=					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__					:    List[Any]												=					image_processing(lowercase_		,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:

						"""simple docstring"""






						lowerCAmelCase__					:    str												=					self.image_processing_class(**self.image_processor_dict			)
						# create random PyTorch tensors
						lowerCAmelCase__					:    int												=					self.image_processor_tester.prepare_inputs(equal_resolution=lowercase_		,torchify=lowercase_			)
						for image in image_inputs:
										self.assertIsInstance(lowercase_		,torch.Tensor			)

						# Test not batched input
						lowerCAmelCase__					:    Optional[Any]												=					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__					:    List[str]												=					image_processing(lowercase_		,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__      (							_lowerCamelCase				,    unittest.TestCase):




		'''simple docstring'''



		snake_case_     =ChineseCLIPImageProcessor if is_vision_available() else None



		def 		lowerCAmelCase__				(self			)					->      str:

						"""simple docstring"""






						lowerCAmelCase__					:    List[Any]												=					ChineseCLIPImageProcessingTester(self		,num_channels=4		,do_center_crop=lowercase_			)
						lowerCAmelCase__					:    List[str]												=					3



		@property
		def 		lowerCAmelCase__				(self			)					->      Optional[int]:

						"""simple docstring"""






						return self.image_processor_tester.prepare_image_processor_dict()



		def 		lowerCAmelCase__				(self			)					->      Tuple:

						"""simple docstring"""






						lowerCAmelCase__					:    str												=					self.image_processing_class(**self.image_processor_dict			)
						self.assertTrue(hasattr(lowercase_		,'''do_resize'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''size'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''do_center_crop'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''center_crop'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''do_normalize'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''image_mean'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''image_std'''			)			)
						self.assertTrue(hasattr(lowercase_		,'''do_convert_rgb'''			)			)



		def 		lowerCAmelCase__				(self			)					->      str:

						"""simple docstring"""






						pass



		def 		lowerCAmelCase__				(self			)					->      Optional[Any]:

						"""simple docstring"""






						lowerCAmelCase__					:    Optional[int]												=					self.image_processing_class(**self.image_processor_dict			)
						# create random PIL images
						lowerCAmelCase__					:    Optional[int]												=					self.image_processor_tester.prepare_inputs(equal_resolution=lowercase_			)
						for image in image_inputs:
										self.assertIsInstance(lowercase_		,Image.Image			)

						# Test not batched input
						lowerCAmelCase__					:    Union[str, Any]												=					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__					:    str												=					image_processing(lowercase_		,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 typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


__snake_case    :		Optional[int]  ={
    'configuration_vision_text_dual_encoder': ['VisionTextDualEncoderConfig'],
    'processing_vision_text_dual_encoder': ['VisionTextDualEncoderProcessor'],
}


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__snake_case    :		int  =['VisionTextDualEncoderModel']


try:
							if not is_flax_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__snake_case    :		List[str]  =['FlaxVisionTextDualEncoderModel']

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__snake_case    :		Tuple  =['TFVisionTextDualEncoderModel']


if TYPE_CHECKING:
							from .configuration_vision_text_dual_encoder import VisionTextDualEncoderConfig
							from .processing_vision_text_dual_encoder import VisionTextDualEncoderProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_vision_text_dual_encoder import VisionTextDualEncoderModel

							try:
														if not is_flax_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_flax_vision_text_dual_encoder import FlaxVisionTextDualEncoderModel

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_vision_text_dual_encoder import TFVisionTextDualEncoderModel


else:
							import sys

							__snake_case    :		str  =_LazyModule(__name__, globals()['__file__'], _import_structure)

