






"""simple docstring"""



import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import MaskaFormerConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel

						if is_vision_available():
												from transformers import MaskaFormerImageProcessor

if is_vision_available():
						from PIL import Image





class 					_UpperCAmelCase       :




					def __init__(				self     :						List[str]    ,						A     :						List[str]    ,						A     :						List[str]=2    ,						A     :						Tuple=True    ,						A     :						Union[str, Any]=False    ,						A     :						Optional[int]=10    ,						A     :						Dict=3    ,						A     :						Union[str, Any]=32 * 8    ,						A     :						Union[str, Any]=32 * 8    ,						A     :						Optional[int]=4    ,						A     :						Any=64    ,						)  ->  str:
							lowercase_ : List[str]							       =   parent
							lowercase_ : Any							       =   batch_size
							lowercase_ : Union[str, Any]							       =   is_training
							lowercase_ : Optional[Any]							       =   use_auxiliary_loss
							lowercase_ : List[Any]							       =   num_queries
							lowercase_ : Any							       =   num_channels
							lowercase_ : Tuple							       =   min_size
							lowercase_ : List[str]							       =   max_size
							lowercase_ : str							       =   num_labels
							lowercase_ : Tuple							       =   hidden_dim
							lowercase_ : int							       =   hidden_dim




					def   A  (				self     :						int   )  ->  Dict:
							lowercase_ : Union[str, Any]							       =   floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]   ).to(
							    A   )

							lowercase_ : Dict							       =   torch.ones([self.batch_size, self.min_size, self.max_size]    ,						device=A   )

							lowercase_ : Tuple							       =   (
							    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]    ,						device=A   ) > 0.5
							).float()
							lowercase_ : Optional[int]							       =   (torch.rand((self.batch_size, self.num_labels)    ,						device=A   ) > 0.5).long()

							lowercase_ : List[str]							       =   self.get_config()
							return config, pixel_values, pixel_mask, mask_labels, class_labels




					def   A  (				self     :						Optional[int]   )  ->  Optional[Any]:
							lowercase_ : Optional[Any]							       =   MaskaFormerConfig(
							    hidden_size=self.hidden_dim    ,						)
							lowercase_ : str							       =   self.num_queries
							lowercase_ : Dict							       =   self.num_labels

							lowercase_ : Union[str, Any]							       =   [1, 1, 1, 1]
							lowercase_ : Tuple							       =   self.num_channels

							lowercase_ : int							       =   64
							lowercase_ : Optional[Any]							       =   1_28
							lowercase_ : int							       =   self.hidden_dim
							lowercase_ : List[Any]							       =   self.hidden_dim
							lowercase_ : Union[str, Any]							       =   self.hidden_dim
							return config




					def   A  (				self     :						Optional[int]   )  ->  Dict:
							lowercase_       ,					lowercase_       ,					lowercase_       ,					lowercase_       ,					lowercase_ : List[Any]							       =   self.prepare_config_and_inputs()
							lowercase_ : List[str]							       =   {'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
							return config, inputs_dict




					def   A  (				self     :						List[Any]    ,						A     :						int    ,						A     :						int   )  ->  Tuple:
							lowercase_ : List[str]							       =   output.encoder_hidden_states
							lowercase_ : Tuple							       =   output.pixel_decoder_hidden_states
							lowercase_ : int							       =   output.transformer_decoder_hidden_states

							self.parent.assertTrue(len(A   )    ,						len(config.backbone_config.depths   )   )
							self.parent.assertTrue(len(A   )    ,						len(config.backbone_config.depths   )   )
							self.parent.assertTrue(len(A   )    ,						config.decoder_layers   )




					def   A  (				self     :						Any    ,						A     :						Optional[Any]    ,						A     :						Optional[Any]    ,						A     :						int    ,						A     :						int=False   )  ->  Union[str, Any]:
							with torch.no_grad():
									lowercase_ : List[Any]							       =   MaskaFormerModel(config=A   )
									model.to(A   )
									model.eval()

									lowercase_ : Any							       =   model(pixel_values=A    ,						pixel_mask=A   )
									lowercase_ : Tuple							       =   model(A    ,						output_hidden_states=A   )

							self.parent.assertEqual(
							    output.transformer_decoder_last_hidden_state.shape    ,						(self.batch_size, self.num_queries, self.hidden_dim)    ,						)
							# let's ensure the other two hidden state exists
							self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None   )
							self.parent.assertTrue(output.encoder_last_hidden_state is not None   )

							if output_hidden_states:
									self.check_output_hidden_state(A    ,						A   )






					def   A  (				self     :						Optional[int]    ,						A     :						Union[str, Any]    ,						A     :						Tuple    ,						A     :						str    ,						A     :						Optional[Any]    ,						A     :						Tuple   )  ->  Optional[Any]:
							lowercase_ : Union[str, Any]							       =   MaskaFormerForUniversalSegmentation(config=A   )
							model.to(A   )
							model.eval()

							def comm_check_on_output(A     :						List[Any]   ):
									# let's still check that all the required stuff is there
									self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None   )
									self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None   )
									self.parent.assertTrue(result.encoder_last_hidden_state is not None   )
									# okay, now we need to check the logits shape
									# due to the encoder compression, masks have a //4 spatial size
									self.parent.assertEqual(
									    result.masks_queries_logits.shape    ,						(self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)    ,						)
									# + 1 for null class
									self.parent.assertEqual(
									    result.class_queries_logits.shape    ,						(self.batch_size, self.num_queries, self.num_labels + 1)   )

							with torch.no_grad():
									lowercase_ : Dict							       =   model(pixel_values=A    ,						pixel_mask=A   )
									lowercase_ : Dict							       =   model(A   )

									comm_check_on_output(A   )

									lowercase_ : str							       =   model(
									    pixel_values=A    ,						pixel_mask=A    ,						mask_labels=A    ,						class_labels=A   )

							comm_check_on_output(A   )

							self.parent.assertTrue(result.loss is not None   )
							self.parent.assertEqual(result.loss.shape    ,						torch.Size([1]   )   )





@require_torch
class 					_UpperCAmelCase       ( _A   ,						_A   ,						unittest.TestCase							):
					SCREAMING_SNAKE_CASE_		:				Tuple							    =   (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
					SCREAMING_SNAKE_CASE_		:				Optional[int]							    =   {"feature-extraction": MaskaFormerModel} if is_torch_available() else {}

					SCREAMING_SNAKE_CASE_		:				Tuple							    =   False
					SCREAMING_SNAKE_CASE_		:				Union[str, Any]							    =   False
					SCREAMING_SNAKE_CASE_		:				Union[str, Any]							    =   False
					SCREAMING_SNAKE_CASE_		:				int							    =   False




					def   A  (				self     :						Dict   )  ->  Dict:
							lowercase_ : Any							       =   MaskaFormerModelTester(self   )
							lowercase_ : Union[str, Any]							       =   ConfigTester(self    ,						config_class=A    ,						has_text_modality=A   )




					def   A  (				self     :						Union[str, Any]   )  ->  List[str]:
							self.config_tester.run_common_tests()




					def   A  (				self     :						Optional[Any]   )  ->  Optional[Any]:
							lowercase_       ,					lowercase_ : Optional[int]							       =   self.model_tester.prepare_config_and_inputs_for_common()
							self.model_tester.create_and_check_maskaformer_model(A    ,						**A    ,						output_hidden_states=A   )




					def   A  (				self     :						List[str]   )  ->  Any:
							lowercase_ : Dict							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*A   )




					@unittest.skip(reason='''Mask2Former does not use inputs_embeds'''   )
					def   A  (				self     :						Dict   )  ->  List[Any]:
							pass




					@unittest.skip(reason='''Mask2Former does not have a get_input_embeddings method'''   )
					def   A  (				self     :						Union[str, Any]   )  ->  Optional[Any]:
							pass




					@unittest.skip(reason='''Mask2Former is not a generative model'''   )
					def   A  (				self     :						int   )  ->  List[Any]:
							pass




					@unittest.skip(reason='''Mask2Former does not use token embeddings'''   )
					def   A  (				self     :						Tuple   )  ->  Union[str, Any]:
							pass




					@require_torch_multi_gpu
					@unittest.skip(
					    reason='''Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''   )
					def   A  (				self     :						Dict   )  ->  List[str]:
							pass




					@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''   )
					def   A  (				self     :						Tuple   )  ->  int:
							pass




					def   A  (				self     :						Optional[int]   )  ->  int:
							lowercase_       ,					lowercase_ : Tuple							       =   self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									lowercase_ : List[str]							       =   model_class(A   )
									lowercase_ : int							       =   inspect.signature(model.forward   )
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									lowercase_ : Any							       =   [*signature.parameters.keys()]

									lowercase_ : Optional[Any]							       =   ['''pixel_values''']
									self.assertListEqual(arg_names[:1]    ,						A   )




					@slow
					def   A  (				self     :						Tuple   )  ->  Optional[Any]:
							for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
									lowercase_ : Tuple							       =   MaskaFormerModel.from_pretrained(A   )
									self.assertIsNotNone(A   )




					def   A  (				self     :						Tuple   )  ->  List[Any]:
							lowercase_ : Dict							       =   (self.model_tester.min_size,) * 2
							lowercase_ : Optional[Any]							       =   {
							    '''pixel_values''': torch.randn((2, 3, *size)    ,						device=A   ),
							    '''mask_labels''': torch.randn((2, 10, *size)    ,						device=A   ),
							    '''class_labels''': torch.zeros(2    ,						10    ,						device=A   ).long(),
							}
							lowercase_ : int							       =   self.model_tester.get_config()

							lowercase_ : str							       =   MaskaFormerForUniversalSegmentation(A   ).to(A   )
							lowercase_ : Optional[int]							       =   model(**A   )
							self.assertTrue(outputs.loss is not None   )




					def   A  (				self     :						str   )  ->  List[str]:
							lowercase_       ,					lowercase_ : Optional[Any]							       =   self.model_tester.prepare_config_and_inputs_for_common()
							self.model_tester.create_and_check_maskaformer_model(A    ,						**A    ,						output_hidden_states=A   )




					def   A  (				self     :						List[Any]   )  ->  Optional[int]:
							lowercase_       ,					lowercase_ : str							       =   self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									lowercase_ : List[str]							       =   model_class(A   ).to(A   )
									lowercase_ : Any							       =   model(**A    ,						output_attentions=A   )
									self.assertTrue(outputs.attentions is not None   )




					def   A  (				self     :						int   )  ->  str:
							if not self.model_tester.is_training:
									return

							lowercase_ : Any							       =   self.all_model_classes[1]
							lowercase_       ,					lowercase_       ,					lowercase_       ,					lowercase_       ,					lowercase_ : List[str]							       =   self.model_tester.prepare_config_and_inputs()

							lowercase_ : Tuple							       =   model_class(A   )
							model.to(A   )
							model.train()

							lowercase_ : Optional[int]							       =   model(A    ,						mask_labels=A    ,						class_labels=A   ).loss
							loss.backward()






					def   A  (				self     :						Any   )  ->  Union[str, Any]:
							lowercase_ : Optional[int]							       =   self.all_model_classes[1]
							lowercase_       ,					lowercase_       ,					lowercase_       ,					lowercase_       ,					lowercase_ : Optional[int]							       =   self.model_tester.prepare_config_and_inputs()
							lowercase_ : int							       =   True
							lowercase_ : Tuple							       =   True

							lowercase_ : List[str]							       =   model_class(A   ).to(A   )
							model.train()

							lowercase_ : Dict							       =   model(A    ,						mask_labels=A    ,						class_labels=A   )

							lowercase_ : Optional[Any]							       =   outputs.encoder_hidden_states[0]
							encoder_hidden_states.retain_grad()

							lowercase_ : str							       =   outputs.pixel_decoder_hidden_states[0]
							pixel_decoder_hidden_states.retain_grad()

							lowercase_ : List[str]							       =   outputs.transformer_decoder_hidden_states[0]
							transformer_decoder_hidden_states.retain_grad()

							lowercase_ : Union[str, Any]							       =   outputs.attentions[0]
							attentions.retain_grad()

							outputs.loss.backward(retain_graph=A   )

							self.assertIsNotNone(encoder_hidden_states.grad   )
							self.assertIsNotNone(pixel_decoder_hidden_states.grad   )
							self.assertIsNotNone(transformer_decoder_hidden_states.grad   )
							self.assertIsNotNone(attentions.grad   )
__A		:       Any  				=		1E-4



def 						lowercase    (				):
		lowercase_ : Optional[int]							       =   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
		return image






@require_vision
@slow
class 					_UpperCAmelCase       ( unittest.TestCase							):




					@cached_property
					def   A  (				self     :						Optional[int]   )  ->  str:
							return "facebook/mask2former-swin-small-coco-instance"




					@cached_property
					def   A  (				self     :						Optional[int]   )  ->  List[str]:
							return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints   ) if is_vision_available() else None




					def   A  (				self     :						Tuple   )  ->  Tuple:
							lowercase_ : Optional[Any]							       =   MaskaFormerModel.from_pretrained(self.model_checkpoints   ).to(A   )
							lowercase_ : Any							       =   self.default_image_processor
							lowercase_ : int							       =   prepare_img()
							lowercase_ : Optional[int]							       =   image_processor(A    ,						return_tensors='''pt'''   ).to(A   )
							lowercase_ : Dict							       =   inputs['''pixel_values'''].shape
							# check size is divisible by 32
							self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0   )
							# check size
							self.assertEqual(A    ,						(1, 3, 3_84, 3_84)   )

							with torch.no_grad():
									lowercase_ : Dict							       =   model(**A   )

							lowercase_ : List[str]							       =   torch.tensor(
							    [[-0.2790, -1.0717, -1.1668], [-0.5128, -0.3128, -0.4987], [-0.5832, 0.1971, -0.0197]]   ).to(A   )
							self.assertTrue(
							    torch.allclose(
							        outputs.encoder_last_hidden_state[0, 0, :3, :3]    ,						A    ,						atol=A   )   )

							lowercase_ : str							       =   torch.tensor(
							    [[0.8973, 1.1847, 1.1776], [1.1934, 1.5040, 1.5128], [1.1153, 1.4486, 1.4951]]   ).to(A   )
							self.assertTrue(
							    torch.allclose(
							        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]    ,						A    ,						atol=A   )   )

							lowercase_ : Tuple							       =   torch.tensor(
							    [[2.1152, 1.7000, -0.8603], [1.5808, 1.8004, -0.9353], [1.6043, 1.7495, -0.5999]]   ).to(A   )
							self.assertTrue(
							    torch.allclose(
							        outputs.transformer_decoder_last_hidden_state[0, :3, :3]    ,						A    ,						atol=A   )   )




					def   A  (				self     :						Union[str, Any]   )  ->  Dict:
							lowercase_ : List[Any]							       =   MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints   ).to(A   ).eval()
							lowercase_ : Optional[int]							       =   self.default_image_processor
							lowercase_ : List[str]							       =   prepare_img()
							lowercase_ : List[Any]							       =   image_processor(A    ,						return_tensors='''pt'''   ).to(A   )
							lowercase_ : Any							       =   inputs['''pixel_values'''].shape
							# check size is divisible by 32
							self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0   )
							# check size
							self.assertEqual(A    ,						(1, 3, 3_84, 3_84)   )

							with torch.no_grad():
									lowercase_ : List[Any]							       =   model(**A   )
							# masks_queries_logits
							lowercase_ : Union[str, Any]							       =   outputs.masks_queries_logits
							self.assertEqual(
							    masks_queries_logits.shape    ,						(1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)   )
							lowercase_ : Optional[int]							       =   [
							    [-8.7839, -9.0056, -8.8121],
							    [-7.4104, -7.0313, -6.5401],
							    [-6.6105, -6.3427, -6.4675],
							]
							lowercase_ : str							       =   torch.tensor(A   ).to(A   )
							self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]    ,						A    ,						atol=A   )   )
							# class_queries_logits
							lowercase_ : List[str]							       =   outputs.class_queries_logits
							self.assertEqual(class_queries_logits.shape    ,						(1, model.config.num_queries, model.config.num_labels + 1)   )
							lowercase_ : Optional[Any]							       =   torch.tensor(
							    [
							        [1.8324, -8.0835, -4.1922],
							        [0.8450, -9.0050, -3.6053],
							        [0.3045, -7.7293, -3.0275],
							    ]   ).to(A   )
							self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]    ,						A    ,						atol=A   )   )






					def   A  (				self     :						Union[str, Any]   )  ->  Tuple:
							lowercase_ : List[Any]							       =   MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints   ).to(A   ).eval()
							lowercase_ : List[str]							       =   self.default_image_processor

							lowercase_ : Union[str, Any]							       =   image_processor(
							    [np.zeros((3, 8_00, 13_33)   ), np.zeros((3, 8_00, 13_33)   )]    ,						segmentation_maps=[np.zeros((3_84, 3_84)   ).astype(np.floataa   ), np.zeros((3_84, 3_84)   ).astype(np.floataa   )]    ,						return_tensors='''pt'''    ,						)

							lowercase_ : Optional[int]							       =   inputs['''pixel_values'''].to(A   )
							lowercase_ : Union[str, Any]							       =   [el.to(A   ) for el in inputs['''mask_labels''']]
							lowercase_ : str							       =   [el.to(A   ) for el in inputs['''class_labels''']]

							with torch.no_grad():
									lowercase_ : List[Any]							       =   model(**A   )

							self.assertTrue(outputs.loss is not None   )











"""simple docstring"""


import unittest

import numpy as np

from transformers.testing_utils import require_pytesseract, require_torch
from transformers.utils import is_pytesseract_available, is_torch_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
       import torch

if is_pytesseract_available():
       from PIL import Image

       from transformers import LayoutLMvaImageProcessor


class 					snake_case		(							unittest.TestCase						):


   '''simple docstring'''

   def __init__(	self :     List[Any],       _lowerCamelCase :     int,       _lowerCamelCase :     List[Any]=7,       _lowerCamelCase :     int=3,       _lowerCamelCase :     Optional[Any]=18,       _lowerCamelCase :     Any=30,       _lowerCamelCase :     str=4_00,       _lowerCamelCase :     int=True,       _lowerCamelCase :     Union[str, Any]=None,       _lowerCamelCase :     str=True,       ):

     '''simple docstring'''
     __A									= size if size is not None else {'''height''': 18, '''width''': 18}
     __A									= parent
     __A									= batch_size
     __A									= num_channels
     __A									= image_size
     __A									= min_resolution
     __A									= max_resolution
     __A									= do_resize
     __A									= size
     __A									= apply_ocr






   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any]  ):

     '''simple docstring'''
     return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr}





@require_torch
@require_pytesseract
class 					snake_case		(							_lowerCAmelCase   ,	unittest.TestCase						):


   '''simple docstring'''

   A_						:	Optional[int]						 = LayoutLMvaImageProcessor if is_pytesseract_available() else None


   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict  ):

     '''simple docstring'''
     __A									= LayoutLMvaImageProcessingTester(self  )


   @property
   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any]  ):

     '''simple docstring'''
     return self.image_processor_tester.prepare_image_processor_dict()


   def 				_SCREAMING_SNAKE_CASE				(	self :     int  ):

     '''simple docstring'''
     __A									= self.image_processing_class(**self.image_processor_dict  )
     self.assertTrue(hasattr(_lowerCamelCase,       '''do_resize'''  )  )
     self.assertTrue(hasattr(_lowerCamelCase,       '''size'''  )  )
     self.assertTrue(hasattr(_lowerCamelCase,       '''apply_ocr'''  )  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any]  ):

     '''simple docstring'''
     __A									= self.image_processing_class.from_dict(self.image_processor_dict  )
     self.assertEqual(image_processor.size,       {'''height''': 18, '''width''': 18}  )

     __A									= self.image_processing_class.from_dict(self.image_processor_dict,       size=42  )
     self.assertEqual(image_processor.size,       {'''height''': 42, '''width''': 42}  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     pass


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     # Initialize image_processing
     __A									= self.image_processing_class(**self.image_processor_dict  )
     # create random PIL images
     __A									= prepare_image_inputs(self.image_processor_tester,       equal_resolution=_lowerCamelCase  )
     for image in image_inputs:
       self.assertIsInstance(_lowerCamelCase,       Image.Image  )

     # Test not batched input
     __A									= image_processing(image_inputs[0],       return_tensors='''pt'''  )
     self.assertEqual(
         encoding.pixel_values.shape,       (
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),       )

     self.assertIsInstance(encoding.words,       _lowerCamelCase  )
     self.assertIsInstance(encoding.boxes,       _lowerCamelCase  )

     # Test batched
     __A									= 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.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),       )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Any  ):

     '''simple docstring'''
     # Initialize image_processing
     __A									= self.image_processing_class(**self.image_processor_dict  )
     # create random numpy tensors
     __A									= 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
     __A									= 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.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),       )

     # Test batched
     __A									= 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.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),       )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     # Initialize image_processing
     __A									= self.image_processing_class(**self.image_processor_dict  )
     # create random PyTorch tensors
     __A									= 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
     __A									= 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.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),       )

     # Test batched
     __A									= 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.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),       )






   def 				_SCREAMING_SNAKE_CASE				(	self :     List[str]  ):

     '''simple docstring'''
     # with apply_OCR = True
     __A									= LayoutLMvaImageProcessor()

     from datasets import load_dataset

     __A									= load_dataset('''hf-internal-testing/fixtures_docvqa''',       split='''test'''  )

     __A									= Image.open(ds[0]['''file''']  ).convert('''RGB'''  )

     __A									= image_processing(_lowerCamelCase,       return_tensors='''pt'''  )

     self.assertEqual(encoding.pixel_values.shape,       (1, 3, 2_24, 2_24)  )
     self.assertEqual(len(encoding.words  ),       len(encoding.boxes  )  )

     # fmt: off
     # the words and boxes were obtained with Tesseract 4.1.1
     __A									= [['''11:14''', '''to''', '''11:39''', '''a.m''', '''11:39''', '''to''', '''11:44''', '''a.m.''', '''11:44''', '''a.m.''', '''to''', '''12:25''', '''p.m.''', '''12:25''', '''to''', '''12:58''', '''p.m.''', '''12:58''', '''to''', '''4:00''', '''p.m.''', '''2:00''', '''to''', '''5:00''', '''p.m.''', '''Coffee''', '''Break''', '''Coffee''', '''will''', '''be''', '''served''', '''for''', '''men''', '''and''', '''women''', '''in''', '''the''', '''lobby''', '''adjacent''', '''to''', '''exhibit''', '''area.''', '''Please''', '''move''', '''into''', '''exhibit''', '''area.''', '''(Exhibits''', '''Open)''', '''TRRF''', '''GENERAL''', '''SESSION''', '''(PART''', '''|)''', '''Presiding:''', '''Lee''', '''A.''', '''Waller''', '''TRRF''', '''Vice''', '''President''', '''“Introductory''', '''Remarks”''', '''Lee''', '''A.''', '''Waller,''', '''TRRF''', '''Vice''', '''Presi-''', '''dent''', '''Individual''', '''Interviews''', '''with''', '''TRRF''', '''Public''', '''Board''', '''Members''', '''and''', '''Sci-''', '''entific''', '''Advisory''', '''Council''', '''Mem-''', '''bers''', '''Conducted''', '''by''', '''TRRF''', '''Treasurer''', '''Philip''', '''G.''', '''Kuehn''', '''to''', '''get''', '''answers''', '''which''', '''the''', '''public''', '''refrigerated''', '''warehousing''', '''industry''', '''is''', '''looking''', '''for.''', '''Plus''', '''questions''', '''from''', '''the''', '''floor.''', '''Dr.''', '''Emil''', '''M.''', '''Mrak,''', '''University''', '''of''', '''Cal-''', '''ifornia,''', '''Chairman,''', '''TRRF''', '''Board;''', '''Sam''', '''R.''', '''Cecil,''', '''University''', '''of''', '''Georgia''', '''College''', '''of''', '''Agriculture;''', '''Dr.''', '''Stanley''', '''Charm,''', '''Tufts''', '''University''', '''School''', '''of''', '''Medicine;''', '''Dr.''', '''Robert''', '''H.''', '''Cotton,''', '''ITT''', '''Continental''', '''Baking''', '''Company;''', '''Dr.''', '''Owen''', '''Fennema,''', '''University''', '''of''', '''Wis-''', '''consin;''', '''Dr.''', '''Robert''', '''E.''', '''Hardenburg,''', '''USDA.''', '''Questions''', '''and''', '''Answers''', '''Exhibits''', '''Open''', '''Capt.''', '''Jack''', '''Stoney''', '''Room''', '''TRRF''', '''Scientific''', '''Advisory''', '''Council''', '''Meeting''', '''Ballroom''', '''Foyer''']]  # noqa: E231
     __A									= [[[1_41, 57, 2_14, 69], [2_28, 58, 2_52, 69], [1_41, 75, 2_16, 88], [2_30, 79, 2_80, 88], [1_42, 2_60, 2_18, 2_73], [2_30, 2_61, 2_55, 2_73], [1_43, 2_79, 2_18, 2_90], [2_31, 2_82, 2_90, 2_91], [1_43, 3_42, 2_18, 3_54], [2_31, 3_45, 2_89, 3_55], [2_02, 3_62, 2_27, 3_73], [1_43, 3_79, 2_20, 3_92], [2_31, 3_82, 2_91, 3_94], [1_44, 7_14, 2_20, 7_26], [2_31, 7_15, 2_56, 7_26], [1_44, 7_32, 2_20, 7_45], [2_32, 7_36, 2_91, 7_47], [1_44, 7_69, 2_18, 7_82], [2_31, 7_70, 2_56, 7_82], [1_41, 7_88, 2_02, 8_01], [2_15, 7_91, 2_74, 8_04], [1_43, 8_26, 2_04, 8_38], [2_15, 8_26, 2_40, 8_38], [1_42, 8_44, 2_02, 8_57], [2_15, 8_47, 2_74, 8_59], [3_34, 57, 4_27, 69], [4_40, 57, 5_22, 69], [3_69, 75, 4_61, 88], [4_69, 75, 5_16, 88], [5_28, 76, 5_62, 88], [5_70, 76, 6_67, 88], [6_75, 75, 7_11, 87], [7_21, 79, 7_78, 88], [7_89, 75, 8_40, 88], [3_69, 97, 4_70, 1_07], [4_84, 94, 5_07, 1_06], [5_18, 94, 5_62, 1_07], [5_76, 94, 6_55, 1_10], [6_68, 94, 7_92, 1_09], [8_04, 95, 8_29, 1_07], [3_69, 1_13, 4_65, 1_25], [4_77, 1_16, 5_47, 1_25], [5_62, 1_13, 6_58, 1_25], [6_71, 1_16, 7_48, 1_25], [7_61, 1_13, 8_11, 1_25], [3_69, 1_31, 4_65, 1_43], [4_77, 1_33, 5_48, 1_43], [5_63, 1_30, 6_98, 1_45], [7_10, 1_30, 8_02, 1_46], [3_36, 1_71, 4_12, 1_83], [4_23, 1_71, 5_72, 1_83], [5_82, 1_70, 7_16, 1_84], [7_28, 1_71, 8_17, 1_87], [8_29, 1_71, 8_44, 1_86], [3_38, 1_97, 4_82, 2_12], [5_07, 1_96, 5_57, 2_09], [5_69, 1_96, 5_95, 2_08], [6_10, 1_96, 7_02, 2_09], [5_05, 2_14, 5_83, 2_26], [5_95, 2_14, 6_56, 2_27], [6_70, 2_15, 8_07, 2_27], [3_35, 2_59, 5_43, 2_74], [5_56, 2_59, 7_08, 2_72], [3_72, 2_79, 4_22, 2_91], [4_35, 2_79, 4_60, 2_91], [4_74, 2_79, 5_74, 2_92], [5_87, 2_78, 6_64, 2_91], [6_76, 2_78, 7_38, 2_91], [7_51, 2_79, 8_34, 2_91], [3_72, 2_98, 4_34, 3_10], [3_35, 3_41, 4_83, 3_54], [4_97, 3_41, 6_55, 3_54], [6_67, 3_41, 7_28, 3_54], [7_40, 3_41, 8_25, 3_54], [3_35, 3_60, 4_30, 3_72], [4_42, 3_60, 5_34, 3_72], [5_45, 3_59, 6_87, 3_72], [6_97, 3_60, 7_54, 3_72], [7_65, 3_60, 8_23, 3_73], [3_34, 3_78, 4_28, 3_91], [4_40, 3_78, 5_77, 3_94], [5_90, 3_78, 7_05, 3_91], [7_20, 3_78, 8_01, 3_91], [3_34, 3_97, 4_00, 4_09], [3_70, 4_16, 5_29, 4_29], [5_44, 4_16, 5_76, 4_32], [5_87, 4_16, 6_65, 4_28], [6_77, 4_16, 8_14, 4_29], [3_72, 4_35, 4_52, 4_50], [4_65, 4_34, 4_95, 4_47], [5_11, 4_34, 6_00, 4_47], [6_11, 4_36, 6_37, 4_47], [6_49, 4_36, 6_94, 4_51], [7_05, 4_38, 8_24, 4_47], [3_69, 4_53, 4_52, 4_66], [4_64, 4_54, 5_09, 4_66], [5_22, 4_53, 6_11, 4_69], [6_25, 4_53, 7_92, 4_69], [3_70, 4_72, 5_56, 4_88], [5_70, 4_72, 6_84, 4_87], [6_97, 4_72, 7_18, 4_85], [7_32, 4_72, 8_35, 4_88], [3_69, 4_90, 4_11, 5_03], [4_25, 4_90, 4_84, 5_03], [4_96, 4_90, 6_35, 5_06], [6_45, 4_90, 7_07, 5_03], [7_18, 4_91, 7_61, 5_03], [7_71, 4_90, 8_40, 5_03], [3_36, 5_10, 3_74, 5_21], [3_88, 5_10, 4_47, 5_22], [4_60, 5_10, 4_89, 5_21], [5_03, 5_10, 5_80, 5_22], [5_92, 5_09, 7_36, 5_25], [7_45, 5_09, 7_70, 5_22], [7_81, 5_09, 8_40, 5_22], [3_38, 5_28, 4_34, 5_41], [4_48, 5_28, 5_96, 5_41], [6_09, 5_27, 6_87, 5_40], [7_00, 5_28, 7_92, 5_41], [3_36, 5_46, 3_97, 5_59], [4_07, 5_46, 4_31, 5_59], [4_43, 5_46, 5_25, 5_60], [5_37, 5_46, 6_80, 5_62], [6_88, 5_46, 7_14, 5_59], [7_22, 5_46, 8_37, 5_62], [3_36, 5_65, 4_49, 5_81], [4_61, 5_65, 4_85, 5_77], [4_97, 5_65, 6_65, 5_81], [6_81, 5_65, 7_18, 5_77], [7_32, 5_65, 8_37, 5_80], [3_37, 5_84, 4_38, 5_97], [4_52, 5_83, 5_21, 5_96], [5_35, 5_84, 6_77, 5_99], [6_90, 5_83, 7_87, 5_96], [8_01, 5_83, 8_25, 5_96], [3_38, 6_02, 4_78, 6_15], [4_92, 6_02, 5_30, 6_14], [5_43, 6_02, 6_38, 6_15], [6_50, 6_02, 6_76, 6_14], [6_88, 6_02, 7_88, 6_15], [8_02, 6_02, 8_43, 6_14], [3_37, 6_21, 5_02, 6_33], [5_16, 6_21, 6_15, 6_37], [6_29, 6_21, 7_74, 6_36], [7_89, 6_21, 8_27, 6_33], [3_37, 6_39, 4_18, 6_52], [4_32, 6_40, 5_71, 6_53], [5_87, 6_39, 7_31, 6_55], [7_43, 6_39, 7_69, 6_52], [7_80, 6_39, 8_41, 6_52], [3_38, 6_58, 4_40, 6_73], [4_55, 6_58, 4_91, 6_70], [5_08, 6_58, 6_02, 6_71], [6_16, 6_58, 6_38, 6_70], [6_54, 6_58, 8_35, 6_74], [3_37, 6_77, 4_29, 6_89], [3_37, 7_14, 4_82, 7_26], [4_95, 7_14, 5_48, 7_26], [5_61, 7_14, 6_83, 7_26], [3_38, 7_70, 4_61, 7_82], [4_74, 7_69, 5_54, 7_85], [4_89, 7_88, 5_62, 8_03], [5_76, 7_88, 6_43, 8_01], [6_56, 7_87, 7_51, 8_04], [7_64, 7_88, 8_44, 8_01], [3_34, 8_25, 4_21, 8_38], [4_30, 8_24, 5_74, 8_38], [5_84, 8_24, 7_23, 8_41], [3_35, 8_44, 4_50, 8_57], [4_64, 8_43, 5_83, 8_60], [6_28, 8_62, 7_55, 8_75], [7_69, 8_61, 8_48, 8_78]]]  # noqa: E231
     # fmt: on

     self.assertListEqual(encoding.words,       _lowerCamelCase  )
     self.assertListEqual(encoding.boxes,       _lowerCamelCase  )

     # with apply_OCR = False
     __A									= LayoutLMvaImageProcessor(apply_ocr=_lowerCamelCase  )

     __A									= image_processing(_lowerCamelCase,       return_tensors='''pt'''  )

     self.assertEqual(encoding.pixel_values.shape,       (1, 3, 2_24, 2_24)  )




