
import copy
import unittest

from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
							    MODEL_FOR_QUESTION_ANSWERING_MAPPING,
							    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
							    MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
							    LayoutLMvaConfig,
							    LayoutLMvaForQuestionAnswering,
							    LayoutLMvaForSequenceClassification,
							    LayoutLMvaForTokenClassification,
							    LayoutLMvaModel,
							)
							from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
							from PIL import Image

							from transformers import LayoutLMvaImageProcessor







class        UpperCamelCase__   :

					def __init__(     self       :   int							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   str=2							,					UpperCamelCase__       :   List[str]=3							,					UpperCamelCase__       :   Optional[int]=4							,					UpperCamelCase__       :   Any=2							,					UpperCamelCase__       :   Dict=7							,					UpperCamelCase__       :   Optional[Any]=True							,					UpperCamelCase__       :   int=True							,					UpperCamelCase__       :   List[Any]=True							,					UpperCamelCase__       :   List[str]=True							,					UpperCamelCase__       :   Union[str, Any]=99							,					UpperCamelCase__       :   int=36							,					UpperCamelCase__       :   Optional[int]=3							,					UpperCamelCase__       :   str=4							,					UpperCamelCase__       :   Any=37							,					UpperCamelCase__       :   Dict="gelu"							,					UpperCamelCase__       :   Optional[int]=0.1							,					UpperCamelCase__       :   Union[str, Any]=0.1							,					UpperCamelCase__       :   Optional[Any]=512							,					UpperCamelCase__       :   str=16							,					UpperCamelCase__       :   Dict=2							,					UpperCamelCase__       :   Optional[int]=0.02							,					UpperCamelCase__       :   List[Any]=6							,					UpperCamelCase__       :   Tuple=6							,					UpperCamelCase__       :   Dict=3							,					UpperCamelCase__       :   Optional[Any]=4							,					UpperCamelCase__       :   Optional[Any]=None							,					UpperCamelCase__       :   Union[str, Any]=1_000							,					):






										'''simple docstring'''






										lowercase_  							=			parent
										lowercase_  							=			batch_size
										lowercase_  							=			num_channels
										lowercase_  							=			image_size
										lowercase_  							=			patch_size
										lowercase_  							=			text_seq_length
										lowercase_  							=			is_training
										lowercase_  							=			use_input_mask
										lowercase_  							=			use_token_type_ids
										lowercase_  							=			use_labels
										lowercase_  							=			vocab_size
										lowercase_  							=			hidden_size
										lowercase_  							=			num_hidden_layers
										lowercase_  							=			num_attention_heads
										lowercase_  							=			intermediate_size
										lowercase_  							=			hidden_act
										lowercase_  							=			hidden_dropout_prob
										lowercase_  							=			attention_probs_dropout_prob
										lowercase_  							=			max_position_embeddings
										lowercase_  							=			type_vocab_size
										lowercase_  							=			type_sequence_label_size
										lowercase_  							=			initializer_range
										lowercase_  							=			coordinate_size
										lowercase_  							=			shape_size
										lowercase_  							=			num_labels
										lowercase_  							=			num_choices
										lowercase_  							=			scope
										lowercase_  							=			range_bbox

										# LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
										lowercase_  							=			text_seq_length
										lowercase_  							=			(image_size // patch_size) ** 2 + 1
										lowercase_  							=			self.text_seq_length + self.image_seq_length

					def   UpperCAmelCase__       (     self       :   Any       ):






										'''simple docstring'''






										lowercase_  							=			ids_tensor([self.batch_size, self.text_seq_length]							,					self.vocab_size       )

										lowercase_  							=			ids_tensor([self.batch_size, self.text_seq_length, 4]							,					self.range_bbox       )
										# Ensure that bbox is legal
										for i in range(bbox.shape[0]       ):
															for j in range(bbox.shape[1]       ):
																				if bbox[i, j, 3] < bbox[i, j, 1]:
																									lowercase_  							=			bbox[i, j, 3]
																									lowercase_  							=			bbox[i, j, 1]
																									lowercase_  							=			t
																				if bbox[i, j, 2] < bbox[i, j, 0]:
																									lowercase_  							=			bbox[i, j, 2]
																									lowercase_  							=			bbox[i, j, 0]
																									lowercase_  							=			t

										lowercase_  							=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

										lowercase_  							=			None
										if self.use_input_mask:
															lowercase_  							=			random_attention_mask([self.batch_size, self.text_seq_length]       )

										lowercase_  							=			None
										if self.use_token_type_ids:
															lowercase_  							=			ids_tensor([self.batch_size, self.text_seq_length]							,					self.type_vocab_size       )

										lowercase_  							=			None
										lowercase_  							=			None
										if self.use_labels:
															lowercase_  							=			ids_tensor([self.batch_size]							,					self.type_sequence_label_size       )
															lowercase_  							=			ids_tensor([self.batch_size, self.text_seq_length]							,					self.num_labels       )

										lowercase_  							=			LayoutLMvaConfig(
										    vocab_size=self.vocab_size							,					hidden_size=self.hidden_size							,					num_hidden_layers=self.num_hidden_layers							,					num_attention_heads=self.num_attention_heads							,					intermediate_size=self.intermediate_size							,					hidden_act=self.hidden_act							,					hidden_dropout_prob=self.hidden_dropout_prob							,					attention_probs_dropout_prob=self.attention_probs_dropout_prob							,					max_position_embeddings=self.max_position_embeddings							,					type_vocab_size=self.type_vocab_size							,					initializer_range=self.initializer_range							,					coordinate_size=self.coordinate_size							,					shape_size=self.shape_size							,					input_size=self.image_size							,					patch_size=self.patch_size							,					)

										return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels

					def   UpperCAmelCase__       (     self       :   Optional[int]							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   Any							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   str							,					UpperCamelCase__       :   List[str]       ):






										'''simple docstring'''






										lowercase_  							=			LayoutLMvaModel(config=UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()

										# text + image
										lowercase_  							=			model(UpperCamelCase__							,					pixel_values=UpperCamelCase__       )
										lowercase_  							=			model(
										    UpperCamelCase__							,					bbox=UpperCamelCase__							,					pixel_values=UpperCamelCase__							,					attention_mask=UpperCamelCase__							,					token_type_ids=UpperCamelCase__       )
										lowercase_  							=			model(UpperCamelCase__							,					bbox=UpperCamelCase__							,					pixel_values=UpperCamelCase__							,					token_type_ids=UpperCamelCase__       )
										lowercase_  							=			model(UpperCamelCase__							,					bbox=UpperCamelCase__							,					pixel_values=UpperCamelCase__       )

										self.parent.assertEqual(result.last_hidden_state.shape							,					(self.batch_size, self.seq_length, self.hidden_size)       )

										# text only
										lowercase_  							=			model(UpperCamelCase__       )
										self.parent.assertEqual(
										    result.last_hidden_state.shape							,					(self.batch_size, self.text_seq_length, self.hidden_size)       )

										# image only
										lowercase_  							=			model(pixel_values=UpperCamelCase__       )
										self.parent.assertEqual(
										    result.last_hidden_state.shape							,					(self.batch_size, self.image_seq_length, self.hidden_size)       )

					def   UpperCAmelCase__       (     self       :   Union[str, Any]							,					UpperCamelCase__       :   Optional[Any]							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   int							,					UpperCamelCase__       :   Any							,					UpperCamelCase__       :   List[Any]							,					UpperCamelCase__       :   Optional[Any]							,					UpperCamelCase__       :   List[Any]							,					UpperCamelCase__       :   Optional[Any]       ):






										'''simple docstring'''






										lowercase_  							=			self.num_labels
										lowercase_  							=			LayoutLMvaForSequenceClassification(UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()
										lowercase_  							=			model(
										    UpperCamelCase__							,					bbox=UpperCamelCase__							,					pixel_values=UpperCamelCase__							,					attention_mask=UpperCamelCase__							,					token_type_ids=UpperCamelCase__							,					labels=UpperCamelCase__							,					)
										self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.num_labels)       )

					def   UpperCAmelCase__       (     self       :   Any							,					UpperCamelCase__       :   Union[str, Any]							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   Union[str, Any]							,					UpperCamelCase__       :   Optional[int]							,					UpperCamelCase__       :   Any							,					UpperCamelCase__       :   Union[str, Any]							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   int       ):






										'''simple docstring'''






										lowercase_  							=			self.num_labels
										lowercase_  							=			LayoutLMvaForTokenClassification(config=UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()
										lowercase_  							=			model(
										    UpperCamelCase__							,					bbox=UpperCamelCase__							,					pixel_values=UpperCamelCase__							,					attention_mask=UpperCamelCase__							,					token_type_ids=UpperCamelCase__							,					labels=UpperCamelCase__							,					)
										self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.text_seq_length, self.num_labels)       )

					def   UpperCAmelCase__       (     self       :   Any							,					UpperCamelCase__       :   Dict							,					UpperCamelCase__       :   List[Any]							,					UpperCamelCase__       :   int							,					UpperCamelCase__       :   int							,					UpperCamelCase__       :   Any							,					UpperCamelCase__       :   Dict							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   Tuple       ):






										'''simple docstring'''






										lowercase_  							=			LayoutLMvaForQuestionAnswering(config=UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()
										lowercase_  							=			model(
										    UpperCamelCase__							,					bbox=UpperCamelCase__							,					pixel_values=UpperCamelCase__							,					attention_mask=UpperCamelCase__							,					token_type_ids=UpperCamelCase__							,					start_positions=UpperCamelCase__							,					end_positions=UpperCamelCase__							,					)
										self.parent.assertEqual(result.start_logits.shape							,					(self.batch_size, self.seq_length)       )
										self.parent.assertEqual(result.end_logits.shape							,					(self.batch_size, self.seq_length)       )







					def   UpperCAmelCase__       (     self       :   Any       ):






										'''simple docstring'''






										lowercase_  							=			self.prepare_config_and_inputs()
										(
										    (
										    lowercase_					
										)							,					(
										    lowercase_					
										)							,					(
										    lowercase_					
										)							,					(
										    lowercase_					
										)							,					(
										    lowercase_					
										)							,					(
										    lowercase_					
										)							,					(
										    lowercase_					
										)							,					(
										    lowercase_					
										)							,										
										)  							=			config_and_inputs
										lowercase_  							=			{
										    """input_ids""": input_ids,
										    """bbox""": bbox,
										    """pixel_values""": pixel_values,
										    """token_type_ids""": token_type_ids,
										    """attention_mask""": input_mask,
										}
										return config, inputs_dict







@require_torch
class        UpperCamelCase__   (	__magic_name__							,				__magic_name__							,				unittest.TestCase   ):
					__SCREAMING_SNAKE_CASE       :  Tuple						=   False
					__SCREAMING_SNAKE_CASE       :  str						=   False
					__SCREAMING_SNAKE_CASE       :  Dict						=   False

					__SCREAMING_SNAKE_CASE       :  Tuple						=   (
					    (
					        LayoutLMvaModel,
					        LayoutLMvaForSequenceClassification,
					        LayoutLMvaForTokenClassification,
					        LayoutLMvaForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					__SCREAMING_SNAKE_CASE       :  Union[str, Any]						=   (
					    {'document-question-answering': LayoutLMvaForQuestionAnswering, 'feature-extraction': LayoutLMvaModel}
					    if is_torch_available()
					    else {}
					)

					def   UpperCAmelCase__       (     self       :   Any							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   str							,					UpperCamelCase__       :   Any							,					UpperCamelCase__       :   int							,					UpperCamelCase__       :   Dict       ):






										'''simple docstring'''






										return True

					def   UpperCAmelCase__       (     self       :   Optional[Any]       ):






										'''simple docstring'''






										lowercase_  							=			LayoutLMvaModelTester(self       )
										lowercase_  							=			ConfigTester(self							,					config_class=UpperCamelCase__							,					hidden_size=37       )

					def   UpperCAmelCase__       (     self       :   Any							,					UpperCamelCase__       :   Dict							,					UpperCamelCase__       :   Any							,					UpperCamelCase__       :   Optional[int]=False       ):






										'''simple docstring'''






										lowercase_  							=			copy.deepcopy(UpperCamelCase__       )
										if model_class in get_values(UpperCamelCase__       ):
															lowercase_  							=			{
															    k: v.unsqueeze(1       ).expand(-1							,					self.model_tester.num_choices							,					-1       ).contiguous()
															    if isinstance(UpperCamelCase__							,					torch.Tensor       ) and v.ndim > 1
															    else v
															    for k, v in inputs_dict.items()
															}
										if return_labels:
															if model_class in get_values(UpperCamelCase__       ):
																				lowercase_  							=			torch.ones(self.model_tester.batch_size							,					dtype=torch.long							,					device=UpperCamelCase__       )
															elif model_class in get_values(UpperCamelCase__       ):
																				lowercase_  							=			torch.zeros(
																				    self.model_tester.batch_size							,					dtype=torch.long							,					device=UpperCamelCase__       )
																				lowercase_  							=			torch.zeros(
																				    self.model_tester.batch_size							,					dtype=torch.long							,					device=UpperCamelCase__       )
															elif model_class in [
															    *get_values(UpperCamelCase__       ),
															]:
																				lowercase_  							=			torch.zeros(
																				    self.model_tester.batch_size							,					dtype=torch.long							,					device=UpperCamelCase__       )
															elif model_class in [
															    *get_values(UpperCamelCase__       ),
															]:
																				lowercase_  							=			torch.zeros(
																				    (self.model_tester.batch_size, self.model_tester.text_seq_length)							,					dtype=torch.long							,					device=UpperCamelCase__							,					)

										return inputs_dict

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






										'''simple docstring'''






										self.config_tester.run_common_tests()

					def   UpperCAmelCase__       (     self       :   Any       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   Optional[Any]       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
															lowercase_  							=			type
															self.model_tester.create_and_check_model(*UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   str       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   int       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   Dict       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__       )







					@slow
					def   UpperCAmelCase__       (     self       :   Any       ):






										'''simple docstring'''






										for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowercase_  							=			LayoutLMvaModel.from_pretrained(UpperCamelCase__       )
															self.assertIsNotNone(UpperCamelCase__       )



def 			UpperCAmelCase_				(      ):
					lowercase_  							=			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
					return image







@require_torch
class        UpperCamelCase__   (	unittest.TestCase   ):

					@cached_property
					def   UpperCAmelCase__       (     self       :   List[Any]       ):






										'''simple docstring'''






										return LayoutLMvaImageProcessor(apply_ocr=UpperCamelCase__       ) if is_vision_available() else None







					@slow
					def   UpperCAmelCase__       (     self       :   str       ):






										'''simple docstring'''






										lowercase_  							=			LayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""       ).to(UpperCamelCase__       )

										lowercase_  							=			self.default_image_processor
										lowercase_  							=			prepare_img()
										lowercase_  							=			image_processor(images=UpperCamelCase__							,					return_tensors="""pt"""       ).pixel_values.to(UpperCamelCase__       )

										lowercase_  							=			torch.tensor([[1, 2]]       )
										lowercase_  							=			torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]       ).unsqueeze(0       )

										# forward pass
										lowercase_  							=			model(
										    input_ids=input_ids.to(UpperCamelCase__       )							,					bbox=bbox.to(UpperCamelCase__       )							,					pixel_values=pixel_values.to(UpperCamelCase__       )							,					)

										# verify the logits
										lowercase_  							=			torch.Size((1, 199, 768)       )
										self.assertEqual(outputs.last_hidden_state.shape							,					UpperCamelCase__       )

										lowercase_  							=			torch.tensor(
										    [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]]       ).to(UpperCamelCase__       )

										self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]							,					UpperCamelCase__							,					atol=1e-4       )       )






import cva
import numpy as np







class        UpperCamelCase__   :

					def __init__(     self       :   List[str]							,					UpperCamelCase__       :   float							,					UpperCamelCase__       :   int       ):






										'''simple docstring'''






										if k in (0.04, 0.06):
															lowercase_  							=			k
															lowercase_  							=			window_size
										else:
															raise ValueError("""invalid k value"""       )

					def __str__(     self       :   Optional[int]       ):






										'''simple docstring'''






										return str(self.k       )







					def   UpperCAmelCase__       (     self       :   Optional[Any]							,					UpperCamelCase__       :   str       ):






										'''simple docstring'''






										lowercase_  							=			cva.imread(UpperCamelCase__							,					0       )
										lowercase_							,					lowercase_  							=			img.shape
										lowercase_  							=			[]
										lowercase_  							=			img.copy()
										lowercase_  							=			cva.cvtColor(UpperCamelCase__							,					cva.COLOR_GRAY2RGB       )
										lowercase_							,					lowercase_  							=			np.gradient(UpperCamelCase__       )
										lowercase_  							=			dx**2
										lowercase_  							=			dy**2
										lowercase_  							=			dx * dy
										lowercase_  							=			0.04
										lowercase_  							=			self.window_size // 2
										for y in range(UpperCamelCase__							,					h - offset       ):
															for x in range(UpperCamelCase__							,					w - offset       ):
																				lowercase_  							=			ixx[
																				    y - offset : y + offset + 1, x - offset : x + offset + 1
																				].sum()
																				lowercase_  							=			iyy[
																				    y - offset : y + offset + 1, x - offset : x + offset + 1
																				].sum()
																				lowercase_  							=			ixy[
																				    y - offset : y + offset + 1, x - offset : x + offset + 1
																				].sum()

																				lowercase_  							=			(wxx * wyy) - (wxy**2)
																				lowercase_  							=			wxx + wyy
																				lowercase_  							=			det - k * (trace**2)
																				# Can change the value
																				if r > 0.5:
																									corner_list.append([x, y, r]       )
																									color_img.itemset((y, x, 0)							,					0       )
																									color_img.itemset((y, x, 1)							,					0       )
																									color_img.itemset((y, x, 2)							,					255       )
										return color_img, corner_list


if __name__ == "__main__":
							a					  =     HarrisCorner(0.04, 3)
							a  ,					a					  =     edge_detect.detect('path_to_image')
							cva.imwrite('detect.png', color_img)


