



'''simple docstring'''







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__     :






	'''simple docstring'''

	def __init__(  self		:  Tuple   ,		__A		:  Any   ,		__A		:  int=2   ,		__A		:  Dict=32   ,		__A		:  Tuple=16   ,		__A		:  Dict=3   ,		__A		:  str=True   ,		__A		:  List[str]=True   ,		__A		:  Optional[int]=32   ,		__A		:  List[Any]=4   ,		__A		:  List[Any]=[0, 1, 2, 3]   ,		__A		:  Union[str, Any]=4   ,		__A		:  Optional[Any]=37   ,		__A		:  Optional[Any]="gelu"   ,		__A		:  Tuple=0.1   ,		__A		:  List[Any]=0.1   ,		__A		:  List[Any]=0.0_2   ,		__A		:  int=3   ,		__A		:  Tuple=False   ,		__A		:  Optional[Any]=None   ,		)     ->			List[Any]:




								'''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      lowercase__							(  self		:  Optional[int]					)     ->			str:




								'''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      lowercase__							(  self		:  Dict					)     ->			List[Any]:




								'''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=__A   ,		initializer_range=self.initializer_range   ,		is_hybrid=self.is_hybrid   ,		)




	def      lowercase__							(  self		:  Any   ,		__A		:  Optional[Any]   ,		__A		:  str   ,		__A		:  Any					)     ->			str:




								'''simple docstring'''





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




	def      lowercase__							(  self		:  Optional[int]   ,		__A		:  str   ,		__A		:  Optional[Any]   ,		__A		:  int					)     ->			Optional[int]:




								'''simple docstring'''





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




	def      lowercase__							(  self		:  List[Any]   ,		__A		:  Optional[int]   ,		__A		:  str   ,		__A		:  int					)     ->			List[str]:




								'''simple docstring'''





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




	def      lowercase__							(  self		:  Optional[int]					)     ->			Tuple:




								'''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__     (     _A,     _A,     unittest.TestCase				):






	'''simple docstring'''

	A__		=	(DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
	A__		=	(
	    {
	        '''depth-estimation''': DPTForDepthEstimation,
	        '''feature-extraction''': DPTModel,
	        '''image-segmentation''': DPTForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

	A__		=	False
	A__		=	False
	A__		=	False




	def      lowercase__							(  self		:  str					)     ->			Tuple:




								'''simple docstring'''





								lowerCAmelCase__  =   DPTModelTester(self					)
								lowerCAmelCase__  =   ConfigTester(self   ,		config_class=__A   ,		has_text_modality=__A   ,		hidden_size=37					)




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




								'''simple docstring'''





								self.config_tester.run_common_tests()




	@unittest.skip(reason="""DPT does not use inputs_embeds"""					)
	def      lowercase__							(  self		:  Tuple					)     ->			List[str]:




								'''simple docstring'''





								pass




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




								'''simple docstring'''





								lowerCAmelCase__  ,lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs_for_common()

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




	def      lowercase__							(  self		:  Dict					)     ->			int:




								'''simple docstring'''





								lowerCAmelCase__  ,lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
															lowerCAmelCase__  =   model_class(__A					)
															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]   ,		__A					)




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




								'''simple docstring'''





								lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__A					)




	def      lowercase__							(  self		:  Dict					)     ->			str:




								'''simple docstring'''





								lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_depth_estimation(*__A					)




	def      lowercase__							(  self		:  int					)     ->			Any:




								'''simple docstring'''





								lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*__A					)




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




								'''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(__A					):
																						continue

															lowerCAmelCase__  =   model_class(__A					)
															model.to(__A					)
															model.train()
															lowerCAmelCase__  =   self._prepare_for_class(__A   ,		__A   ,		return_labels=__A					)
															lowerCAmelCase__  =   model(**__A					).loss
															loss.backward()




	def      lowercase__							(  self		:  Dict					)     ->			List[Any]:




								'''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(__A					) or not model_class.supports_gradient_checkpointing:
																						continue
															lowerCAmelCase__  =   model_class(__A					)
															model.to(__A					)
															model.gradient_checkpointing_enable()
															model.train()
															lowerCAmelCase__  =   self._prepare_for_class(__A   ,		__A   ,		return_labels=__A					)
															lowerCAmelCase__  =   model(**__A					).loss
															loss.backward()




	def      lowercase__							(  self		:  Tuple					)     ->			Optional[int]:




								'''simple docstring'''





								lowerCAmelCase__  ,lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs_for_common()

								lowerCAmelCase__  =   _config_zero_init(__A					)
								for model_class in self.all_model_classes:
															lowerCAmelCase__  =   model_class(config=__A					)
															# 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      lowercase__							(  self		:  Union[str, Any]					)     ->			Optional[Any]:




								'''simple docstring'''





								pass




	@slow
	def      lowercase__							(  self		:  List[Any]					)     ->			str:




								'''simple docstring'''





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





def 			_lowerCAmelCase(      ) ->							str:
							lowerCAmelCase__  =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
							return image






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






	'''simple docstring'''

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




								'''simple docstring'''





								lowerCAmelCase__  =   DPTImageProcessor.from_pretrained("""Intel/dpt-large"""					)
								lowerCAmelCase__  =   DPTForDepthEstimation.from_pretrained("""Intel/dpt-large"""					).to(__A					)

								lowerCAmelCase__  =   prepare_img()
								lowerCAmelCase__  =   image_processor(images=__A   ,		return_tensors="""pt"""					).to(__A					)

								# forward pass
								with torch.no_grad():
															lowerCAmelCase__  =   model(**__A					)
															lowerCAmelCase__  =   outputs.predicted_depth

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

								lowerCAmelCase__  =   torch.tensor(
								    [[6.3_1_9_9, 6.3_6_2_9, 6.4_1_4_8], [6.3_8_5_0, 6.3_6_1_5, 6.4_1_6_6], [6.3_5_1_9, 6.3_1_7_6, 6.3_5_7_5]]					).to(__A					)

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




	def      lowercase__							(  self		:  str					)     ->			int:




								'''simple docstring'''





								lowerCAmelCase__  =   DPTImageProcessor.from_pretrained("""Intel/dpt-large-ade"""					)
								lowerCAmelCase__  =   DPTForSemanticSegmentation.from_pretrained("""Intel/dpt-large-ade"""					).to(__A					)

								lowerCAmelCase__  =   prepare_img()
								lowerCAmelCase__  =   image_processor(images=__A   ,		return_tensors="""pt"""					).to(__A					)

								# forward pass
								with torch.no_grad():
															lowerCAmelCase__  =   model(**__A					)

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

								lowerCAmelCase__  =   torch.tensor(
								    [[4.0_4_8_0, 4.2_4_2_0, 4.4_3_6_0], [4.3_1_2_4, 4.5_6_9_3, 4.8_2_6_1], [4.5_7_6_8, 4.8_9_6_5, 5.2_1_6_3]]					).to(__A					)

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




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




								'''simple docstring'''





								lowerCAmelCase__  =   DPTImageProcessor.from_pretrained("""Intel/dpt-large-ade"""					)
								lowerCAmelCase__  =   DPTForSemanticSegmentation.from_pretrained("""Intel/dpt-large-ade"""					).to(__A					)

								lowerCAmelCase__  =   prepare_img()
								lowerCAmelCase__  =   image_processor(images=__A   ,		return_tensors="""pt"""					).to(__A					)

								# forward pass
								with torch.no_grad():
															lowerCAmelCase__  =   model(**__A					)

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

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

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










'''simple docstring'''







from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	bool						) ->							torch.Tensor:
							# The first operation in a checkpoint can't be in-place, but it's
							# nice to have in-place addition during inference. Thus...
							if not inplace:
														lowerCAmelCase__  =   ma + ma
							else:
														ma += ma

							return ma
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	List[int]						) ->							torch.Tensor:
							lowerCAmelCase__  =   -1 * len(UpperCAmelCase_						)
							lowerCAmelCase__  =   list(range(len(tensor.shape[:zero_index]						)						)						)
							return tensor.permute(first_inds + [zero_index + i for i in inds]						)
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	int						) ->							torch.Tensor:
							return t.reshape(t.shape[:-no_dims] + (-1,)						)
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	int					, UpperCAmelCase_      :	float = 1E-4						) ->							torch.Tensor:
							lowerCAmelCase__  =   mask.expand(*value.shape						)
							return torch.sum(mask * value					, dim=UpperCAmelCase_						) / (eps + torch.sum(UpperCAmelCase_					, dim=UpperCAmelCase_						))
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.types.Number = 2.31_25					, UpperCAmelCase_      :	torch.types.Number = 21.68_75					, UpperCAmelCase_      :	int = 64						) ->							torch.Tensor:
							lowerCAmelCase__  =   torch.linspace(UpperCAmelCase_					, UpperCAmelCase_					, no_bins - 1					, device=pts.device						)
							lowerCAmelCase__  =   torch.sqrt(torch.sum((pts.unsqueeze(-2						) - pts.unsqueeze(-3						)) ** 2					, dim=-1						)						)
							return torch.bucketize(UpperCAmelCase_					, UpperCAmelCase_						)
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[list], Any]					, UpperCAmelCase_      :	List[dict]						) ->							dict:
							lowerCAmelCase__  =   dicts[0]
							lowerCAmelCase__  =   {}
							for k, v in first.items():
														lowerCAmelCase__  =   [d[k] for d in dicts]
														if isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
																					lowerCAmelCase__  =   dict_multimap(UpperCAmelCase_					, UpperCAmelCase_						)
														else:
																					lowerCAmelCase__  =   fn(UpperCAmelCase_						)

							return new_dict
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.Tensor						) ->							torch.Tensor:
							lowerCAmelCase__  =   v_bins.view(((1,) * len(x.shape						)) + (len(UpperCAmelCase_						),)						)
							lowerCAmelCase__  =   x[..., None] - reshaped_bins
							lowerCAmelCase__  =   torch.argmin(torch.abs(UpperCAmelCase_						)					, dim=-1						)
							return nn.functional.one_hot(UpperCAmelCase_					, num_classes=len(UpperCAmelCase_						)						).float()
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	int = 0					, UpperCAmelCase_      :	int = 0						) ->							torch.Tensor:
							lowerCAmelCase__  =   []
							for i, s in enumerate(data.shape[:no_batch_dims]						):
														lowerCAmelCase__  =   torch.arange(UpperCAmelCase_						)
														lowerCAmelCase__  =   r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape						) - i - 1)))						)
														ranges.append(UpperCAmelCase_						)

							lowerCAmelCase__  =   [slice(UpperCAmelCase_						) for _ in range(len(data.shape						) - no_batch_dims						)]
							lowerCAmelCase__  =   inds
							ranges.extend(UpperCAmelCase_						)
							# Matt note: Editing this to get around the behaviour of using a list as an array index changing
							# in recent Numpy versions
							return data[tuple(UpperCAmelCase_						)]


_UpperCamelCase  						=			TypeVar("""T""")
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	Dict[Any, Union[dict, list, tuple, T]]					, UpperCAmelCase_      :	Type[T]						) ->							Dict[Any, Union[dict, list, tuple, Any]]:
							lowerCAmelCase__  =   {}
							for k, v in dic.items():
														if isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
																					lowerCAmelCase__  =   dict_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						)
														else:
																					lowerCAmelCase__  =   tree_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						)

							return new_dict
@overload
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	T					, UpperCAmelCase_      :	Type[T]						) ->							Any:
							...
@overload
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	dict					, UpperCAmelCase_      :	Type[T]						) ->							dict:
							...
@overload
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	list					, UpperCAmelCase_      :	Type[T]						) ->							list:
							...
@overload
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	tuple					, UpperCAmelCase_      :	Type[T]						) ->							tuple:
							...
def 			_lowerCAmelCase(      UpperCAmelCase_      :	str					, UpperCAmelCase_      :	Dict					, UpperCAmelCase_      :	Union[str, Any]						) ->							List[str]:
							if isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
														return dict_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						)
							elif isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
														return [tree_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						) for x in tree]
							elif isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
														return tuple(tree_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						) for x in tree						)
							elif isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
														return fn(UpperCAmelCase_						)
							else:
														print(type(UpperCAmelCase_						)						)
														raise ValueError("""Not supported"""						)






_UpperCamelCase  						=			partial(tree_map, leaf_type=torch.Tensor)





