


import unittest
from typing import Dict, List, Optional, Union

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 BridgeTowerImageProcessor


class 	_a							(      unittest.TestCase      ):

							def __init__(							self:     Optional[Any]   ,						UpperCamelCase_:     Any   ,						UpperCamelCase_:     Union[str, Any] = True   ,						UpperCamelCase_:     Optional[Any] = None   ,						UpperCamelCase_:     Tuple = 32   ,						UpperCamelCase_:     Dict = True   ,						UpperCamelCase_:     Tuple = 1 / 255   ,						UpperCamelCase_:     List[Any] = True   ,						UpperCamelCase_:     Dict = True   ,						UpperCamelCase_:     Union[str, Any] = [0.48145466, 0.4578275, 0.40821073]   ,						UpperCamelCase_:     Optional[int] = [0.26862954, 0.26130258, 0.27577711]   ,						UpperCamelCase_:     Optional[Any] = True   ,						UpperCamelCase_:     Union[str, Any]=7   ,						UpperCamelCase_:     Any=30   ,						UpperCamelCase_:     Dict=400   ,						UpperCamelCase_:     Optional[Any]=3   ,						)      ->       int:





									"""simple docstring"""







									lowercase__						=				parent
									lowercase__						=				do_resize
									lowercase__						=				size if size is not None else {'''shortest_edge''': 288}
									lowercase__						=				size_divisor
									lowercase__						=				do_rescale
									lowercase__						=				rescale_factor
									lowercase__						=				do_normalize
									lowercase__						=				do_center_crop
									lowercase__						=				image_mean
									lowercase__						=				image_std
									lowercase__						=				do_pad
									lowercase__						=				batch_size
									lowercase__						=				num_channels
									lowercase__						=				min_resolution
									lowercase__						=				max_resolution

							def 				lowerCamelCase_			(							self:     Optional[Any]							)      ->       List[str]:





									"""simple docstring"""







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


							def 				lowerCamelCase_			(							self:     List[Any]   ,						UpperCamelCase_:     Any   ,						UpperCamelCase_:     Dict=False							)      ->       int:





									"""simple docstring"""







									if not batched:
											lowercase__						=				self.size['''shortest_edge''']
											lowercase__						=				image_inputs[0]
											if isinstance(lowerCAmelCase__   ,						Image.Image							):
													lowercase__					,		lowercase__						=				image.size
											else:
													lowercase__					,		lowercase__						=				image.shape[1], image.shape[2]
											lowercase__						=				size / min(lowerCAmelCase__   ,						lowerCAmelCase__							)
											if h < w:
													lowercase__					,		lowercase__						=				size, scale * w
											else:
													lowercase__					,		lowercase__						=				scale * h, size

											lowercase__						=				int((1_333 / 800) * size							)
											if max(lowerCAmelCase__   ,						lowerCAmelCase__							) > max_size:
													lowercase__						=				max_size / max(lowerCAmelCase__   ,						lowerCAmelCase__							)
													lowercase__						=				newh * scale
													lowercase__						=				neww * scale

											lowercase__					,		lowercase__						=				int(newh + 0.5							), int(neww + 0.5							)
											lowercase__					,		lowercase__						=				(
											    newh // self.size_divisor * self.size_divisor,
											    neww // self.size_divisor * self.size_divisor,
											)

									else:
											lowercase__						=				[]
											for image in image_inputs:
													lowercase__					,		lowercase__						=				self.get_expected_values([image]							)
													expected_values.append((expected_height, expected_width)							)
											lowercase__						=				max(lowerCAmelCase__   ,						key=lambda UpperCamelCase_							: item[0]							)[0]
											lowercase__						=				max(lowerCAmelCase__   ,						key=lambda UpperCamelCase_							: item[1]							)[1]

									return expected_height, expected_width


@require_torch
@require_vision
class 	_a							(      lowerCamelCase_					,     unittest.TestCase      ):
							_lowercase     :      Dict  				=			BridgeTowerImageProcessor if is_vision_available() else None

							def 				lowerCamelCase_			(							self:     Optional[int]							)      ->       str:





									"""simple docstring"""







									lowercase__						=				BridgeTowerImageProcessingTester(self							)

							@property
							def 				lowerCamelCase_			(							self:     Union[str, Any]							)      ->       Optional[int]:





									"""simple docstring"""







									return self.image_processor_tester.prepare_image_processor_dict()

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





									"""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__   ,						'''size'''							)							)
									self.assertTrue(hasattr(lowerCAmelCase__   ,						'''size_divisor'''							)							)

							def 				lowerCamelCase_			(							self:     Any							)      ->       Optional[int]:





									"""simple docstring"""







									pass

							def 				lowerCamelCase_			(							self:     Tuple							)      ->       Optional[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

									lowercase__					,		lowercase__						=				self.image_processor_tester.get_expected_values(lowerCAmelCase__							)
									self.assertEqual(
									    encoded_images.shape   ,						(1, self.image_processor_tester.num_channels, expected_height, expected_width)   ,						)

									# Test batched
									lowercase__						=				image_processing(lowerCAmelCase__   ,						return_tensors='''pt'''							).pixel_values

									lowercase__					,		lowercase__						=				self.image_processor_tester.get_expected_values(lowerCAmelCase__   ,						batched=lowerCAmelCase__							)
									self.assertEqual(
									    encoded_images.shape   ,						(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        expected_height,
									        expected_width,
									    )   ,						)

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





									"""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

									lowercase__					,		lowercase__						=				self.image_processor_tester.get_expected_values(lowerCAmelCase__							)
									self.assertEqual(
									    encoded_images.shape   ,						(1, self.image_processor_tester.num_channels, expected_height, expected_width)   ,						)

									# Test batched
									lowercase__						=				image_processing(lowerCAmelCase__   ,						return_tensors='''pt'''							).pixel_values

									lowercase__					,		lowercase__						=				self.image_processor_tester.get_expected_values(lowerCAmelCase__   ,						batched=lowerCAmelCase__							)
									self.assertEqual(
									    encoded_images.shape   ,						(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        expected_height,
									        expected_width,
									    )   ,						)


							def 				lowerCamelCase_			(							self:     Optional[int]							)      ->       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

									lowercase__					,		lowercase__						=				self.image_processor_tester.get_expected_values(lowerCAmelCase__							)
									self.assertEqual(
									    encoded_images.shape   ,						(1, self.image_processor_tester.num_channels, expected_height, expected_width)   ,						)

									# Test batched
									lowercase__						=				image_processing(lowerCAmelCase__   ,						return_tensors='''pt'''							).pixel_values

									lowercase__					,		lowercase__						=				self.image_processor_tester.get_expected_values(lowerCAmelCase__   ,						batched=lowerCAmelCase__							)
									self.assertEqual(
									    encoded_images.shape   ,						(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        expected_height,
									        expected_width,
									    )   ,						)






from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase	=							logging.get_logger(__name__)

lowerCAmelCase	=							{
    'unc-nlp/lxmert-base-uncased': 'https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json',
}


class 	_a							(      UpperCamelCase__      ):
							_lowercase     :      List[Any]  				=			'''lxmert'''
							_lowercase     :      Any  				=			{}

							def __init__(							self:     Any   ,						UpperCamelCase_:     List[Any]=30_522   ,						UpperCamelCase_:     int=768   ,						UpperCamelCase_:     Optional[Any]=12   ,						UpperCamelCase_:     Dict=9_500   ,						UpperCamelCase_:     List[Any]=1_600   ,						UpperCamelCase_:     List[Any]=400   ,						UpperCamelCase_:     List[str]=3_072   ,						UpperCamelCase_:     int="gelu"   ,						UpperCamelCase_:     Any=0.1   ,						UpperCamelCase_:     int=0.1   ,						UpperCamelCase_:     List[str]=512   ,						UpperCamelCase_:     Optional[int]=2   ,						UpperCamelCase_:     Union[str, Any]=0.02   ,						UpperCamelCase_:     Dict=1E-1_2   ,						UpperCamelCase_:     List[Any]=9   ,						UpperCamelCase_:     List[Any]=5   ,						UpperCamelCase_:     Optional[Any]=5   ,						UpperCamelCase_:     str=2_048   ,						UpperCamelCase_:     Dict=4   ,						UpperCamelCase_:     Any=6.67   ,						UpperCamelCase_:     Dict=True   ,						UpperCamelCase_:     List[str]=True   ,						UpperCamelCase_:     Dict=True   ,						UpperCamelCase_:     Any=True   ,						UpperCamelCase_:     Tuple=True   ,						UpperCamelCase_:     Optional[Any]=True   ,						UpperCamelCase_:     Any=True   ,						**UpperCamelCase_:     Union[str, Any]   ,						)      ->       Union[str, Any]:





									"""simple docstring"""







									lowercase__						=				vocab_size
									lowercase__						=				hidden_size
									lowercase__						=				num_attention_heads
									lowercase__						=				hidden_act
									lowercase__						=				intermediate_size
									lowercase__						=				hidden_dropout_prob
									lowercase__						=				attention_probs_dropout_prob
									lowercase__						=				max_position_embeddings
									lowercase__						=				type_vocab_size
									lowercase__						=				initializer_range
									lowercase__						=				layer_norm_eps
									lowercase__						=				num_qa_labels
									lowercase__						=				num_object_labels
									lowercase__						=				num_attr_labels
									lowercase__						=				l_layers
									lowercase__						=				x_layers
									lowercase__						=				r_layers
									lowercase__						=				visual_feat_dim
									lowercase__						=				visual_pos_dim
									lowercase__						=				visual_loss_normalizer
									lowercase__						=				task_matched
									lowercase__						=				task_mask_lm
									lowercase__						=				task_obj_predict
									lowercase__						=				task_qa
									lowercase__						=				visual_obj_loss
									lowercase__						=				visual_attr_loss
									lowercase__						=				visual_feat_loss
									lowercase__						=				{'''vision''': r_layers, '''cross_encoder''': x_layers, '''language''': l_layers}
									super().__init__(**UpperCamelCase_							)


