
"""simple docstring"""




import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_flax_cross_test,
    require_flax,
    require_torch,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import is_flax_available, is_torch_available, is_vision_available

from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_flax_bert import FlaxBertModelTester
from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester
from ..vit.test_modeling_flax_vit import FlaxViTModelTester


if is_flax_available():
					from transformers import (
					    FlaxBertModel,
					    FlaxCLIPVisionModel,
					    FlaxVisionTextDualEncoderModel,
					    FlaxViTModel,
					    VisionTextDualEncoderConfig,
					    VisionTextDualEncoderProcessor,
					)
					from transformers.modeling_flax_pytorch_utils import (
					    convert_pytorch_state_dict_to_flax,
					    load_flax_weights_in_pytorch_model,
					)


if is_torch_available():
					import torch

					from transformers import VisionTextDualEncoderModel

if is_vision_available():
					from PIL import Image

def lowercase_  (   _snake_case						):
							if isinstance(lowerCAmelCase_   ,collections.abc.Iterable						):
														return x
							return (x, x)



@require_flax
class 	lowerCAmelCase_						:


				"""simple docstring"""



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		str:



											"""simple docstring"""




											pass



				def 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




											pass



				def 	__magic_name__    (self    )					->		List[Any]:



											"""simple docstring"""




											pass



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   np.abs((a - b)    ).max()
											self.assertLessEqual(__lowercase   ,       __lowercase   ,       F'''Difference between torch and flax is {diff} (>= {tol}).'''    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=None   ,       **SCREAMING_SNAKE_CASE__    )					->		int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   VisionTextDualEncoderConfig.from_vision_text_configs(__lowercase   ,       __lowercase    )

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   FlaxVisionTextDualEncoderModel(__lowercase    )

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   model(input_ids=__lowercase   ,       pixel_values=__lowercase   ,       attention_mask=__lowercase    )

											self.assertEqual(output["""text_embeds"""].shape   ,       (input_ids.shape[0], config.projection_dim)    )
											self.assertEqual(output["""image_embeds"""].shape   ,       (pixel_values.shape[0], config.projection_dim)    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=None   ,       **SCREAMING_SNAKE_CASE__    )					->		List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   self.get_vision_text_model(__lowercase   ,       __lowercase    )
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   {'''vision_model''': vision_model, '''text_model''': text_model}
											SCREAMING_SNAKE_CASE__						:       int           =   FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**__lowercase    )

											SCREAMING_SNAKE_CASE__						:       Tuple           =   model(input_ids=__lowercase   ,       pixel_values=__lowercase   ,       attention_mask=__lowercase    )

											self.assertEqual(output["""text_embeds"""].shape   ,       (input_ids.shape[0], model.config.projection_dim)    )
											self.assertEqual(output["""image_embeds"""].shape   ,       (pixel_values.shape[0], model.config.projection_dim)    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=None   ,       **SCREAMING_SNAKE_CASE__    )					->		Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Dict           =   self.get_vision_text_model(__lowercase   ,       __lowercase    )
											SCREAMING_SNAKE_CASE__						:       str           =   {'''vision_model''': vision_model, '''text_model''': text_model}
											SCREAMING_SNAKE_CASE__						:       Tuple           =   FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**__lowercase    )

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   model(input_ids=__lowercase   ,       pixel_values=__lowercase   ,       attention_mask=__lowercase    )
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   output[0]

											with tempfile.TemporaryDirectory() as tmpdirname:
																		model.save_pretrained(__lowercase    )
																		SCREAMING_SNAKE_CASE__						:       int           =   FlaxVisionTextDualEncoderModel.from_pretrained(__lowercase    )

																		SCREAMING_SNAKE_CASE__						:       Any           =   model(input_ids=__lowercase   ,       pixel_values=__lowercase   ,       attention_mask=__lowercase    )
																		SCREAMING_SNAKE_CASE__						:       List[str]           =   after_output[0]
																		SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   np.amax(np.abs(out_a - out_a    )    )
																		self.assertLessEqual(__lowercase   ,       1E-3    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=None   ,       **SCREAMING_SNAKE_CASE__    )					->		List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.get_vision_text_model(__lowercase   ,       __lowercase    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   {'''vision_model''': vision_model, '''text_model''': text_model}
											SCREAMING_SNAKE_CASE__						:       int           =   FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**__lowercase    )

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   model(
											    input_ids=__lowercase   ,       pixel_values=__lowercase   ,       attention_mask=__lowercase   ,       output_attentions=__lowercase    )

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   output.vision_model_output.attentions
											self.assertEqual(len(__lowercase    )   ,       vision_config.num_hidden_layers    )

											# in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   to_atuple(vision_model.config.image_size    )
											SCREAMING_SNAKE_CASE__						:       int           =   to_atuple(vision_model.config.patch_size    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
											SCREAMING_SNAKE_CASE__						:       int           =   num_patches + 1
											self.assertEqual(vision_attentions[0].shape[-3:]   ,       (vision_config.num_attention_heads, seq_len, seq_len)    )

											SCREAMING_SNAKE_CASE__						:       int           =   output.text_model_output.attentions
											self.assertEqual(len(__lowercase    )   ,       text_config.num_hidden_layers    )

											self.assertEqual(
											    text_attentions[0].shape[-3:]   ,       (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])   ,       )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		List[str]:



											"""simple docstring"""




											pt_model.to(__lowercase    )
											pt_model.eval()

											# prepare inputs
											SCREAMING_SNAKE_CASE__						:       Dict           =   inputs_dict
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   {k: torch.tensor(v.tolist()    ) for k, v in flax_inputs.items()}

											with torch.no_grad():
																		SCREAMING_SNAKE_CASE__						:       Dict           =   pt_model(**__lowercase    ).to_tuple()

											SCREAMING_SNAKE_CASE__						:       str           =   fx_model(**__lowercase    ).to_tuple()
											self.assertEqual(len(__lowercase    )   ,       len(__lowercase    )   ,       """Output lengths differ between Flax and PyTorch"""    )
											for fx_output, pt_output in zip(fx_outputs[:4]   ,       pt_outputs[:4]    ):
																		self.assert_almost_equals(__lowercase   ,       pt_output.numpy()   ,       4E-2    )

											# PT -> Flax
											with tempfile.TemporaryDirectory() as tmpdirname:
																		pt_model.save_pretrained(__lowercase    )
																		SCREAMING_SNAKE_CASE__						:       Tuple           =   FlaxVisionTextDualEncoderModel.from_pretrained(__lowercase   ,       from_pt=__lowercase    )

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   fx_model_loaded(**__lowercase    ).to_tuple()
											self.assertEqual(len(__lowercase    )   ,       len(__lowercase    )   ,       """Output lengths differ between Flax and PyTorch"""    )
											for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4]   ,       pt_outputs[:4]    ):
																		self.assert_almost_equals(__lowercase   ,       pt_output.numpy()   ,       4E-2    )

											# Flax -> PT
											with tempfile.TemporaryDirectory() as tmpdirname:
																		fx_model.save_pretrained(__lowercase    )
																		SCREAMING_SNAKE_CASE__						:       Optional[int]           =   VisionTextDualEncoderModel.from_pretrained(__lowercase   ,       from_flax=__lowercase    )

											pt_model_loaded.to(__lowercase    )
											pt_model_loaded.eval()

											with torch.no_grad():
																		SCREAMING_SNAKE_CASE__						:       List[Any]           =   pt_model_loaded(**__lowercase    ).to_tuple()

											self.assertEqual(len(__lowercase    )   ,       len(__lowercase    )   ,       """Output lengths differ between Flax and PyTorch"""    )
											for fx_output, pt_output_loaded in zip(fx_outputs[:4]   ,       pt_outputs_loaded[:4]    ):
																		self.assert_almost_equals(__lowercase   ,       pt_output_loaded.numpy()   ,       4E-2    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   VisionTextDualEncoderConfig.from_vision_text_configs(__lowercase   ,       __lowercase    )

											SCREAMING_SNAKE_CASE__						:       Tuple           =   VisionTextDualEncoderModel(__lowercase    )
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   FlaxVisionTextDualEncoderModel(__lowercase    )

											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   convert_pytorch_state_dict_to_flax(pt_model.state_dict()   ,       __lowercase    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   fx_state

											self.check_pt_flax_equivalence(__lowercase   ,       __lowercase   ,       __lowercase    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   VisionTextDualEncoderConfig.from_vision_text_configs(__lowercase   ,       __lowercase    )

											SCREAMING_SNAKE_CASE__						:       int           =   VisionTextDualEncoderModel(__lowercase    )
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   FlaxVisionTextDualEncoderModel(__lowercase    )

											SCREAMING_SNAKE_CASE__						:       Dict           =   load_flax_weights_in_pytorch_model(__lowercase   ,       fx_model.params    )

											self.check_pt_flax_equivalence(__lowercase   ,       __lowercase   ,       __lowercase    )



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   self.prepare_config_and_inputs()
											self.check_model_from_pretrained_configs(**__lowercase    )



				def 	__magic_name__    (self    )					->		Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       str           =   self.prepare_config_and_inputs()
											self.check_vision_text_dual_encoder_from_pretrained(**__lowercase    )



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       int           =   self.prepare_config_and_inputs()
											self.check_save_load(**__lowercase    )



				def 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.prepare_config_and_inputs()
											self.check_vision_text_output_attention(**__lowercase    )



				@is_pt_flax_cross_test
				def 	__magic_name__    (self    )					->		Union[str, Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   self.prepare_config_and_inputs()
											SCREAMING_SNAKE_CASE__						:       Tuple           =   config_inputs_dict.pop("""vision_config"""    )
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   config_inputs_dict.pop("""text_config"""    )

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   config_inputs_dict

											self.check_equivalence_pt_to_flax(__lowercase   ,       __lowercase   ,       __lowercase    )
											self.check_equivalence_flax_to_pt(__lowercase   ,       __lowercase   ,       __lowercase    )



				@slow
				def 	__magic_name__    (self    )					->		Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.get_pretrained_model_and_inputs()

											SCREAMING_SNAKE_CASE__						:       Tuple           =   model_a(**__lowercase    )
											SCREAMING_SNAKE_CASE__						:       int           =   outputs[0]

											with tempfile.TemporaryDirectory() as tmp_dirname:
																		model_a.save_pretrained(__lowercase    )
																		SCREAMING_SNAKE_CASE__						:       Any           =   FlaxVisionTextDualEncoderModel.from_pretrained(__lowercase    )

																		SCREAMING_SNAKE_CASE__						:       str           =   model_a(**__lowercase    )
																		SCREAMING_SNAKE_CASE__						:       int           =   after_outputs[0]
																		SCREAMING_SNAKE_CASE__						:       Any           =   np.amax(np.abs(out_a - out_a    )    )
																		self.assertLessEqual(__lowercase   ,       1E-5    )






@require_flax
class 	lowerCAmelCase_						(UpperCAmelCase__ ,					unittest.TestCase  ):


				"""simple docstring"""



				def 	__magic_name__    (self    )					->		Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
											    """hf-internal-testing/tiny-random-vit"""   ,       """hf-internal-testing/tiny-bert"""   ,       vision_from_pt=__lowercase   ,       text_from_pt=__lowercase   ,       )
											SCREAMING_SNAKE_CASE__						:       int           =   13
											SCREAMING_SNAKE_CASE__						:       Any           =   floats_tensor(
											    [
											        batch_size,
											        model.config.vision_config.num_channels,
											        model.config.vision_config.image_size,
											        model.config.vision_config.image_size,
											    ]    )
											SCREAMING_SNAKE_CASE__						:       str           =   ids_tensor([batch_size, 4]   ,       model.config.text_config.vocab_size    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   random_attention_mask([batch_size, 4]    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask}

											return model, inputs



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   FlaxViTModel(__lowercase    )
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   FlaxBertModel(__lowercase    )
											return vision_model, text_model



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   FlaxViTModelTester(self    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   FlaxBertModelTester(self    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   vit_model_tester.prepare_config_and_inputs()
											SCREAMING_SNAKE_CASE__						:       int           =   bert_model_tester.prepare_config_and_inputs()

											SCREAMING_SNAKE_CASE__						:       Tuple           =   vision_config_and_inputs

											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   text_config_and_inputs

											# make sure that cross attention layers are added
											return {
											    "text_config": text_config,
											    "vision_config": vision_config,
											    "pixel_values": pixel_values,
											    "attention_mask": attention_mask,
											    "input_ids": input_ids,
											    "token_type_ids": token_type_ids,
											}






@require_torch
class 	lowerCAmelCase_						(UpperCAmelCase__ ,					unittest.TestCase  ):


				"""simple docstring"""



				def 	__magic_name__    (self    )					->		Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Dict           =   FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
											    """hf-internal-testing/tiny-random-clip"""   ,       """hf-internal-testing/tiny-bert"""   ,       vision_from_pt=__lowercase   ,       text_from_pt=__lowercase   ,       )
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   13
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   floats_tensor(
											    [
											        batch_size,
											        model.config.vision_config.num_channels,
											        model.config.vision_config.image_size,
											        model.config.vision_config.image_size,
											    ]    )
											SCREAMING_SNAKE_CASE__						:       Tuple           =   ids_tensor([batch_size, 4]   ,       model.config.text_config.vocab_size    )
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   random_attention_mask([batch_size, 4]    )
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask}

											return model, inputs



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   FlaxCLIPVisionModel(__lowercase    )
											SCREAMING_SNAKE_CASE__						:       Tuple           =   FlaxBertModel(__lowercase    )
											return vision_model, text_model



				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   FlaxCLIPVisionModelTester(self    )
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   FlaxBertModelTester(self    )
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   clip_model_tester.prepare_config_and_inputs()
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   bert_model_tester.prepare_config_and_inputs()

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   vision_config_and_inputs

											SCREAMING_SNAKE_CASE__						:       Any           =   text_config_and_inputs

											# make sure that cross attention layers are added
											return {
											    "text_config": text_config,
											    "vision_config": vision_config,
											    "pixel_values": pixel_values,
											    "attention_mask": attention_mask,
											    "input_ids": input_ids,
											    "token_type_ids": token_type_ids,
											}






@require_flax
@require_vision
class 	lowerCAmelCase_						(unittest.TestCase  ):


				"""simple docstring"""



				@slow
				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[Any]           =   FlaxVisionTextDualEncoderModel.from_pretrained("""clip-italian/clip-italian"""   ,       logit_scale_init_value=1.0    )
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   VisionTextDualEncoderProcessor.from_pretrained("""clip-italian/clip-italian"""    )

											SCREAMING_SNAKE_CASE__						:       Dict           =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   processor(
											    text=["""una foto di un gatto""", """una foto di un cane"""]   ,       images=__lowercase   ,       padding=__lowercase   ,       return_tensors="""np"""    )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   model(**__lowercase    )

											# verify the logits
											self.assertEqual(outputs.logits_per_image.shape   ,       (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])    )
											self.assertEqual(
											    outputs.logits_per_text.shape   ,       (inputs.input_ids.shape[0], inputs.pixel_values.shape[0])   ,       )

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   np.array([[1.2284727, 0.3104122]]    )

											self.assertTrue(np.allclose(outputs.logits_per_image   ,       __lowercase   ,       atol=1E-3    )    )








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 MobileNetVaImageProcessor







class  snake_case__(							unittest.TestCase							):







						'''simple docstring'''




						def __init__( self      ,  __lowercase      ,  __lowercase=7      ,  __lowercase=3      ,  __lowercase=1_8      ,  __lowercase=3_0      ,  __lowercase=4_0_0      ,  __lowercase=True      ,  __lowercase=None      ,  __lowercase=True      ,  __lowercase=None      ,  )    ->			List[Any]:
										lowerCAmelCase_       :					Optional[Any]			 = size if size is not None else {'''shortest_edge''': 2_0}
										lowerCAmelCase_       :					Any			 = crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}
										lowerCAmelCase_       :					Any			 = parent
										lowerCAmelCase_       :					Any			 = batch_size
										lowerCAmelCase_       :					Optional[int]			 = num_channels
										lowerCAmelCase_       :					Tuple			 = image_size
										lowerCAmelCase_       :					List[str]			 = min_resolution
										lowerCAmelCase_       :					Dict			 = max_resolution
										lowerCAmelCase_       :					Tuple			 = do_resize
										lowerCAmelCase_       :					Optional[Any]			 = size
										lowerCAmelCase_       :					Union[str, Any]			 = do_center_crop
										lowerCAmelCase_       :					Optional[Any]			 = crop_size




						def   lowercase_       ( self							)    ->			Union[str, Any]:
										return {
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "do_center_crop": self.do_center_crop,
										    "crop_size": self.crop_size,
										}







@require_torch
@require_vision
class  snake_case__(							UpperCAmelCase__,  unittest.TestCase							):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__					:     Dict    =							MobileNetVaImageProcessor if is_vision_available() else None




						def   lowercase_       ( self							)    ->			List[str]:
										lowerCAmelCase_       :					Union[str, Any]			 = MobileNetVaImageProcessingTester(self							)




						@property
						def   lowercase_       ( self							)    ->			Dict:
										return self.image_processor_tester.prepare_image_processor_dict()




						def   lowercase_       ( self							)    ->			List[Any]:
										lowerCAmelCase_       :					Union[str, 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      ,  '''crop_size'''							)							)




						def   lowercase_       ( self							)    ->			int:
										lowerCAmelCase_       :					str			 = self.image_processing_class.from_dict(self.image_processor_dict							)
										self.assertEqual(image_processor.size      ,  {'''shortest_edge''': 2_0}							)
										self.assertEqual(image_processor.crop_size      ,  {'''height''': 1_8, '''width''': 1_8}							)

										lowerCAmelCase_       :					Tuple			 = self.image_processing_class.from_dict(self.image_processor_dict      ,  size=4_2      ,  crop_size=8_4							)
										self.assertEqual(image_processor.size      ,  {'''shortest_edge''': 4_2}							)
										self.assertEqual(image_processor.crop_size      ,  {'''height''': 8_4, '''width''': 8_4}							)




						def   lowercase_       ( self							)    ->			Tuple:
										pass




						def   lowercase_       ( self							)    ->			Union[str, Any]:
										# Initialize image_processing
										lowerCAmelCase_       :					Optional[int]			 = self.image_processing_class(**self.image_processor_dict							)
										# create random PIL images
										lowerCAmelCase_       :					Union[str, Any]			 = prepare_image_inputs(self.image_processor_tester      ,  equal_resolution=__lowercase							)
										for image in image_inputs:
														self.assertIsInstance(__lowercase      ,  Image.Image							)

										# Test not batched input
										lowerCAmelCase_       :					Tuple			 = 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_       :					Tuple			 = 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   lowercase_       ( self							)    ->			Optional[int]:
										# Initialize image_processing
										lowerCAmelCase_       :					Tuple			 = self.image_processing_class(**self.image_processor_dict							)
										# create random numpy tensors
										lowerCAmelCase_       :					Any			 = prepare_image_inputs(self.image_processor_tester      ,  equal_resolution=__lowercase      ,  numpify=__lowercase							)
										for image in image_inputs:
														self.assertIsInstance(__lowercase      ,  np.ndarray							)

										# Test not batched input
										lowerCAmelCase_       :					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_       :					Tuple			 = 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   lowercase_       ( self							)    ->			Any:
										# Initialize image_processing
										lowerCAmelCase_       :					Union[str, Any]			 = self.image_processing_class(**self.image_processor_dict							)
										# create random PyTorch tensors
										lowerCAmelCase_       :					int			 = prepare_image_inputs(self.image_processor_tester      ,  equal_resolution=__lowercase      ,  torchify=__lowercase							)
										for image in image_inputs:
														self.assertIsInstance(__lowercase      ,  torch.Tensor							)

										# Test not batched input
										lowerCAmelCase_       :					List[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_       :					Dict			 = 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'''],
										    )      ,  )