



import inspect
import unittest

from transformers import DPTConfig
from transformers.file_utils import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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


if is_torch_available():
							import torch
							from torch import nn

							from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel
							from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import DPTImageProcessor





class  __lowerCAmelCase			:




				def __init__(							self						,    _snake_case						,    _snake_case=2						,    _snake_case=32						,    _snake_case=16						,    _snake_case=3						,    _snake_case=True						,    _snake_case=True						,    _snake_case=32						,    _snake_case=4						,    _snake_case=[0, 1, 2, 3]						,    _snake_case=4						,    _snake_case=37						,    _snake_case="gelu"						,    _snake_case=0.1						,    _snake_case=0.1						,    _snake_case=0.02						,    _snake_case=3						,    _snake_case=False						,    _snake_case=None						,    ):



									"""simple docstring"""





									_lowerCAmelCase      =   parent
									_lowerCAmelCase      =   batch_size
									_lowerCAmelCase      =   image_size
									_lowerCAmelCase      =   patch_size
									_lowerCAmelCase      =   num_channels
									_lowerCAmelCase      =   is_training
									_lowerCAmelCase      =   use_labels
									_lowerCAmelCase      =   hidden_size
									_lowerCAmelCase      =   num_hidden_layers
									_lowerCAmelCase      =   backbone_out_indices
									_lowerCAmelCase      =   num_attention_heads
									_lowerCAmelCase      =   intermediate_size
									_lowerCAmelCase      =   hidden_act
									_lowerCAmelCase      =   hidden_dropout_prob
									_lowerCAmelCase      =   attention_probs_dropout_prob
									_lowerCAmelCase      =   initializer_range
									_lowerCAmelCase      =   num_labels
									_lowerCAmelCase      =   scope
									_lowerCAmelCase      =   is_hybrid
									# sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
									_lowerCAmelCase      =   (image_size // patch_size) ** 2
									_lowerCAmelCase      =   num_patches + 1




				def 							snake_case       (							self		):



									"""simple docstring"""





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

									_lowerCAmelCase      =   None
									if self.use_labels:
														_lowerCAmelCase      =   ids_tensor([self.batch_size, self.image_size, self.image_size]						,    self.num_labels		)

									_lowerCAmelCase      =   self.get_config()

									return config, pixel_values, labels




				def 							snake_case       (							self		):



									"""simple docstring"""





									return DPTConfig(
									    image_size=self.image_size						,    patch_size=self.patch_size						,    num_channels=self.num_channels						,    hidden_size=self.hidden_size						,    num_hidden_layers=self.num_hidden_layers						,    backbone_out_indices=self.backbone_out_indices						,    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						,    is_decoder=_snake_case						,    initializer_range=self.initializer_range						,    is_hybrid=self.is_hybrid						,    )




				def 							snake_case       (							self						,    _snake_case						,    _snake_case						,    _snake_case		):



									"""simple docstring"""





									_lowerCAmelCase      =   DPTModel(config=_snake_case		)
									model.to(_snake_case		)
									model.eval()
									_lowerCAmelCase      =   model(_snake_case		)
									self.parent.assertEqual(result.last_hidden_state.shape						,    (self.batch_size, self.seq_length, self.hidden_size)		)




				def 							snake_case       (							self						,    _snake_case						,    _snake_case						,    _snake_case		):



									"""simple docstring"""





									_lowerCAmelCase      =   self.num_labels
									_lowerCAmelCase      =   DPTForDepthEstimation(_snake_case		)
									model.to(_snake_case		)
									model.eval()
									_lowerCAmelCase      =   model(_snake_case		)
									self.parent.assertEqual(result.predicted_depth.shape						,    (self.batch_size, self.image_size, self.image_size)		)




				def 							snake_case       (							self						,    _snake_case						,    _snake_case						,    _snake_case		):



									"""simple docstring"""





									_lowerCAmelCase      =   self.num_labels
									_lowerCAmelCase      =   DPTForSemanticSegmentation(_snake_case		)
									model.to(_snake_case		)
									model.eval()
									_lowerCAmelCase      =   model(_snake_case						,    labels=_snake_case		)
									self.parent.assertEqual(
									    result.logits.shape						,    (self.batch_size, self.num_labels, self.image_size, self.image_size)		)




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   self.prepare_config_and_inputs()
									_lowerCAmelCase     ,     _lowerCAmelCase     ,     _lowerCAmelCase      =   config_and_inputs
									_lowerCAmelCase      =   {"""pixel_values""": pixel_values}
									return config, inputs_dict





@require_torch
class  __lowerCAmelCase			(					lowerCamelCase__	,						lowerCamelCase__	,						unittest.TestCase	):
				__lowerCamelCase			=    (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
				__lowerCamelCase			=    (
				    {
				        '''depth-estimation''': DPTForDepthEstimation,
				        '''feature-extraction''': DPTModel,
				        '''image-segmentation''': DPTForSemanticSegmentation,
				    }
				    if is_torch_available()
				    else {}
				)

				__lowerCamelCase			=    False
				__lowerCamelCase			=    False
				__lowerCamelCase			=    False




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   DPTModelTester(self		)
									_lowerCAmelCase      =   ConfigTester(self						,    config_class=_snake_case						,    has_text_modality=_snake_case						,    hidden_size=37		)




				def 							snake_case       (							self		):



									"""simple docstring"""





									self.config_tester.run_common_tests()




				@unittest.skip(reason="""DPT does not use inputs_embeds"""		)
				def 							snake_case       (							self		):



									"""simple docstring"""





									pass




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase     ,     _lowerCAmelCase      =   self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
														_lowerCAmelCase      =   model_class(_snake_case		)
														self.assertIsInstance(model.get_input_embeddings()						,    (nn.Module)		)
														_lowerCAmelCase      =   model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(_snake_case						,    nn.Linear		)		)




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase     ,     _lowerCAmelCase      =   self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
														_lowerCAmelCase      =   model_class(_snake_case		)
														_lowerCAmelCase      =   inspect.signature(model.forward		)
														# signature.parameters is an OrderedDict => so arg_names order is deterministic
														_lowerCAmelCase      =   [*signature.parameters.keys()]

														_lowerCAmelCase      =   ["""pixel_values"""]
														self.assertListEqual(arg_names[:1]						,    _snake_case		)




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_snake_case		)




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_depth_estimation(*_snake_case		)




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_semantic_segmentation(*_snake_case		)




				def 							snake_case       (							self		):



									"""simple docstring"""





									for model_class in self.all_model_classes:
														if model_class.__name__ == "DPTForDepthEstimation":
																			continue

														_lowerCAmelCase     ,     _lowerCAmelCase      =   self.model_tester.prepare_config_and_inputs_for_common()
														_lowerCAmelCase      =   True

														if model_class in get_values(_snake_case		):
																			continue

														_lowerCAmelCase      =   model_class(_snake_case		)
														model.to(_snake_case		)
														model.train()
														_lowerCAmelCase      =   self._prepare_for_class(_snake_case						,    _snake_case						,    return_labels=_snake_case		)
														_lowerCAmelCase      =   model(**_snake_case		).loss
														loss.backward()




				def 							snake_case       (							self		):



									"""simple docstring"""





									for model_class in self.all_model_classes:
														if model_class.__name__ == "DPTForDepthEstimation":
																			continue

														_lowerCAmelCase     ,     _lowerCAmelCase      =   self.model_tester.prepare_config_and_inputs_for_common()
														_lowerCAmelCase      =   False
														_lowerCAmelCase      =   True

														if model_class in get_values(_snake_case		) or not model_class.supports_gradient_checkpointing:
																			continue
														_lowerCAmelCase      =   model_class(_snake_case		)
														model.to(_snake_case		)
														model.gradient_checkpointing_enable()
														model.train()
														_lowerCAmelCase      =   self._prepare_for_class(_snake_case						,    _snake_case						,    return_labels=_snake_case		)
														_lowerCAmelCase      =   model(**_snake_case		).loss
														loss.backward()




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase     ,     _lowerCAmelCase      =   self.model_tester.prepare_config_and_inputs_for_common()

									_lowerCAmelCase      =   _config_zero_init(_snake_case		)
									for model_class in self.all_model_classes:
														_lowerCAmelCase      =   model_class(config=_snake_case		)
														# Skip the check for the backbone
														_lowerCAmelCase      =   []
														for name, module in model.named_modules():
																			if module.__class__.__name__ == "DPTViTHybridEmbeddings":
																								_lowerCAmelCase      =   [F'{name}.{key}' for key in module.state_dict().keys()]
																								break

														for name, param in model.named_parameters():
																			if param.requires_grad:
																								if name in backbone_params:
																													continue
																								self.assertIn(
																								    ((param.data.mean() * 1e9).round() / 1e9).item()						,    [0.0, 1.0]						,    msg=F'Parameter {name} of model {model_class} seems not properly initialized'						,    )




				@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""		)
				def 							snake_case       (							self		):



									"""simple docstring"""





									pass




				@slow
				def 							snake_case       (							self		):



									"""simple docstring"""





									for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_lowerCAmelCase      =   DPTModel.from_pretrained(_snake_case		)
														self.assertIsNotNone(_snake_case		)


def 		_UpperCAmelCase    (				):





					"""simple docstring"""

					_lowerCAmelCase      =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
					return image





@require_torch
@require_vision
@slow
class  __lowerCAmelCase			(					unittest.TestCase	):




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   DPTImageProcessor.from_pretrained("""Intel/dpt-large"""		)
									_lowerCAmelCase      =   DPTForDepthEstimation.from_pretrained("""Intel/dpt-large"""		).to(_snake_case		)

									_lowerCAmelCase      =   prepare_img()
									_lowerCAmelCase      =   image_processor(images=_snake_case						,    return_tensors="""pt"""		).to(_snake_case		)

									# forward pass
									with torch.no_grad():
														_lowerCAmelCase      =   model(**_snake_case		)
														_lowerCAmelCase      =   outputs.predicted_depth

									# verify the predicted depth
									_lowerCAmelCase      =   torch.Size((1, 384, 384)		)
									self.assertEqual(predicted_depth.shape						,    _snake_case		)

									_lowerCAmelCase      =   torch.tensor(
									    [[6.3199, 6.3629, 6.4148], [6.3850, 6.3615, 6.4166], [6.3519, 6.3176, 6.3575]]		).to(_snake_case		)

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




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   DPTImageProcessor.from_pretrained("""Intel/dpt-large-ade"""		)
									_lowerCAmelCase      =   DPTForSemanticSegmentation.from_pretrained("""Intel/dpt-large-ade"""		).to(_snake_case		)

									_lowerCAmelCase      =   prepare_img()
									_lowerCAmelCase      =   image_processor(images=_snake_case						,    return_tensors="""pt"""		).to(_snake_case		)

									# forward pass
									with torch.no_grad():
														_lowerCAmelCase      =   model(**_snake_case		)

									# verify the logits
									_lowerCAmelCase      =   torch.Size((1, 150, 480, 480)		)
									self.assertEqual(outputs.logits.shape						,    _snake_case		)

									_lowerCAmelCase      =   torch.tensor(
									    [[4.0480, 4.2420, 4.4360], [4.3124, 4.5693, 4.8261], [4.5768, 4.8965, 5.2163]]		).to(_snake_case		)

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




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   DPTImageProcessor.from_pretrained("""Intel/dpt-large-ade"""		)
									_lowerCAmelCase      =   DPTForSemanticSegmentation.from_pretrained("""Intel/dpt-large-ade"""		).to(_snake_case		)

									_lowerCAmelCase      =   prepare_img()
									_lowerCAmelCase      =   image_processor(images=_snake_case						,    return_tensors="""pt"""		).to(_snake_case		)

									# forward pass
									with torch.no_grad():
														_lowerCAmelCase      =   model(**_snake_case		)

									_lowerCAmelCase      =   outputs.logits.detach().cpu()

									_lowerCAmelCase      =   image_processor.post_process_semantic_segmentation(outputs=_snake_case						,    target_sizes=[(500, 300)]		)
									_lowerCAmelCase      =   torch.Size((500, 300)		)
									self.assertEqual(segmentation[0].shape						,    _snake_case		)

									_lowerCAmelCase      =   image_processor.post_process_semantic_segmentation(outputs=_snake_case		)
									_lowerCAmelCase      =   torch.Size((480, 480)		)
									self.assertEqual(segmentation[0].shape						,    _snake_case		)








# Lint as: python3

import hashlib
from typing import Union


def 		_UpperCAmelCase    (				snake_case      ):





					"""simple docstring"""

					if isinstance(snake_case     ,						snake_case      ):
										# Data already in bytes, returns as it as
										return hash_data
					elif isinstance(snake_case     ,						snake_case      ):
										# We keep the data as it as for it ot be later encoded to UTF-8
										# However replace `\\` with `/` for Windows compatibility
										_lowerCAmelCase      =   hash_data.replace("""\\"""     ,						"""/"""      )
					elif isinstance(snake_case     ,						snake_case      ):
										_lowerCAmelCase      =   str(snake_case      )
					else:
										# If data is not of the required type, raise error
										raise InvalidKeyError(snake_case      )

					return hash_data.encode("""utf-8"""      )





class  __lowerCAmelCase			(					lowerCamelCase__	):




				def __init__(							self						,    _snake_case		):



									"""simple docstring"""





									_lowerCAmelCase      =   """\nFAILURE TO GENERATE DATASET: Invalid key type detected"""
									_lowerCAmelCase      =   F'\nFound Key {hash_data} of type {type(_snake_case		)}'
									_lowerCAmelCase      =   """\nKeys should be either str, int or bytes type"""
									super().__init__(F'{self.prefix}{self.err_msg}{self.suffix}'		)





class  __lowerCAmelCase			(					lowerCamelCase__	):




				def __init__(							self						,    _snake_case						,    _snake_case						,    _snake_case=""		):



									"""simple docstring"""





									_lowerCAmelCase      =   key
									_lowerCAmelCase      =   duplicate_key_indices
									_lowerCAmelCase      =   fix_msg
									_lowerCAmelCase      =   """Found multiple examples generated with the same key"""
									if len(_snake_case		) <= 20:
														_lowerCAmelCase      =   F'\nThe examples at index {", ".join(_snake_case		)} have the key {key}'
									else:
														_lowerCAmelCase      =   F'\nThe examples at index {", ".join(duplicate_key_indices[:20]		)}... ({len(_snake_case		) - 20} more) have the key {key}'
									_lowerCAmelCase      =   """\n""" + fix_msg if fix_msg else """"""
									super().__init__(F'{self.prefix}{self.err_msg}{self.suffix}'		)





class  __lowerCAmelCase			:




				def __init__(							self						,    _snake_case		):



									"""simple docstring"""





									_lowerCAmelCase      =   hashlib.mda(_as_bytes(_snake_case		)		)




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									_lowerCAmelCase      =   self._split_mda.copy()
									_lowerCAmelCase      =   _as_bytes(_snake_case		)
									mda.update(_snake_case		)
									# Convert to integer with hexadecimal conversion
									return int(mda.hexdigest()						,    16		)



