

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






					"""simple docstring"""



					def __init__( self    :       List[str]     ,lowercase_    :       Tuple     ,lowercase_    :       int=2     ,lowercase_    :       Dict=3_2     ,lowercase_    :       List[str]=1_6     ,lowercase_    :       Optional[int]=3     ,lowercase_    :       Any=True     ,lowercase_    :       List[Any]=True     ,lowercase_    :       List[Any]=3_2     ,lowercase_    :       Optional[int]=4     ,lowercase_    :       Optional[int]=[0, 1, 2, 3]     ,lowercase_    :       Dict=4     ,lowercase_    :       int=3_7     ,lowercase_    :       Any="gelu"     ,lowercase_    :       Optional[int]=0.1     ,lowercase_    :       Union[str, Any]=0.1     ,lowercase_    :       str=0.02     ,lowercase_    :       Tuple=3     ,lowercase_    :       Any=False     ,lowercase_    :       Optional[Any]=None     ,):
						lowerCAmelCase__			:		Any						       =			parent
						lowerCAmelCase__			:		str						       =			batch_size
						lowerCAmelCase__			:		Dict						       =			image_size
						lowerCAmelCase__			:		List[str]						       =			patch_size
						lowerCAmelCase__			:		Union[str, Any]						       =			num_channels
						lowerCAmelCase__			:		List[str]						       =			is_training
						lowerCAmelCase__			:		Dict						       =			use_labels
						lowerCAmelCase__			:		Optional[Any]						       =			hidden_size
						lowerCAmelCase__			:		Tuple						       =			num_hidden_layers
						lowerCAmelCase__			:		Tuple						       =			backbone_out_indices
						lowerCAmelCase__			:		Union[str, Any]						       =			num_attention_heads
						lowerCAmelCase__			:		Union[str, Any]						       =			intermediate_size
						lowerCAmelCase__			:		Optional[Any]						       =			hidden_act
						lowerCAmelCase__			:		Union[str, Any]						       =			hidden_dropout_prob
						lowerCAmelCase__			:		List[str]						       =			attention_probs_dropout_prob
						lowerCAmelCase__			:		Tuple						       =			initializer_range
						lowerCAmelCase__			:		List[Any]						       =			num_labels
						lowerCAmelCase__			:		Dict						       =			scope
						lowerCAmelCase__			:		int						       =			is_hybrid
						# sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
						lowerCAmelCase__			:		Union[str, Any]						       =			(image_size // patch_size) ** 2
						lowerCAmelCase__			:		Tuple						       =			num_patches + 1







					def  __lowerCAmelCase				( self    :       Optional[int]	):
						lowerCAmelCase__			:		Optional[int]						       =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

						lowerCAmelCase__			:		Optional[Any]						       =			None
						if self.use_labels:
							lowerCAmelCase__			:		str						       =			ids_tensor([self.batch_size, self.image_size, self.image_size]     ,self.num_labels	)

						lowerCAmelCase__			:		List[Any]						       =			self.get_config()

						return config, pixel_values, labels







					def  __lowerCAmelCase				( self    :       Any	):
						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=lowercase_     ,initializer_range=self.initializer_range     ,is_hybrid=self.is_hybrid     ,)







					def  __lowerCAmelCase				( self    :       Optional[Any]     ,lowercase_    :       Optional[Any]     ,lowercase_    :       List[str]     ,lowercase_    :       str	):
						lowerCAmelCase__			:		Optional[Any]						       =			DPTModel(config=lowercase_	)
						model.to(lowercase_	)
						model.eval()
						lowerCAmelCase__			:		int						       =			model(lowercase_	)
						self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size)	)







					def  __lowerCAmelCase				( self    :       List[Any]     ,lowercase_    :       int     ,lowercase_    :       Optional[int]     ,lowercase_    :       List[str]	):
						lowerCAmelCase__			:		int						       =			self.num_labels
						lowerCAmelCase__			:		Optional[int]						       =			DPTForDepthEstimation(lowercase_	)
						model.to(lowercase_	)
						model.eval()
						lowerCAmelCase__			:		List[str]						       =			model(lowercase_	)
						self.parent.assertEqual(result.predicted_depth.shape     ,(self.batch_size, self.image_size, self.image_size)	)







					def  __lowerCAmelCase				( self    :       Tuple     ,lowercase_    :       Union[str, Any]     ,lowercase_    :       Any     ,lowercase_    :       Optional[Any]	):
						lowerCAmelCase__			:		Union[str, Any]						       =			self.num_labels
						lowerCAmelCase__			:		Tuple						       =			DPTForSemanticSegmentation(lowercase_	)
						model.to(lowercase_	)
						model.eval()
						lowerCAmelCase__			:		List[Any]						       =			model(lowercase_     ,labels=lowercase_	)
						self.parent.assertEqual(
						    result.logits.shape     ,(self.batch_size, self.num_labels, self.image_size, self.image_size)	)







					def  __lowerCAmelCase				( self    :       str	):
						lowerCAmelCase__			:		Any						       =			self.prepare_config_and_inputs()
						lowerCAmelCase__       ,lowerCAmelCase__       ,lowerCAmelCase__			:		Tuple						       =			config_and_inputs
						lowerCAmelCase__			:		Tuple						       =			{'''pixel_values''': pixel_values}
						return config, inputs_dict







@require_torch
class        SCREAMING_SNAKE_CASE      (	a_					,							a_					,							unittest.TestCase ):






					"""simple docstring"""



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

					lowercase__       				= False
					lowercase__       				= False
					lowercase__       				= False







					def  __lowerCAmelCase				( self    :       int	):
						lowerCAmelCase__			:		int						       =			DPTModelTester(self	)
						lowerCAmelCase__			:		Dict						       =			ConfigTester(self     ,config_class=lowercase_     ,has_text_modality=lowercase_     ,hidden_size=3_7	)







					def  __lowerCAmelCase				( self    :       str	):
						self.config_tester.run_common_tests()







					@unittest.skip(reason='''DPT does not use inputs_embeds'''	)
					def  __lowerCAmelCase				( self    :       Dict	):
						pass







					def  __lowerCAmelCase				( self    :       int	):
						lowerCAmelCase__       ,lowerCAmelCase__			:		str						       =			self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							lowerCAmelCase__			:		Union[str, Any]						       =			model_class(lowercase_	)
							self.assertIsInstance(model.get_input_embeddings()     ,(nn.Module)	)
							lowerCAmelCase__			:		Tuple						       =			model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(lowercase_     ,nn.Linear	)	)







					def  __lowerCAmelCase				( self    :       List[str]	):
						lowerCAmelCase__       ,lowerCAmelCase__			:		int						       =			self.model_tester.prepare_config_and_inputs_for_common()

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

							lowerCAmelCase__			:		Tuple						       =			['''pixel_values''']
							self.assertListEqual(arg_names[:1]     ,lowercase_	)







					def  __lowerCAmelCase				( self    :       Union[str, Any]	):
						lowerCAmelCase__			:		List[str]						       =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowercase_	)







					def  __lowerCAmelCase				( self    :       Any	):
						lowerCAmelCase__			:		Optional[int]						       =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_depth_estimation(*lowercase_	)







					def  __lowerCAmelCase				( self    :       Union[str, Any]	):
						lowerCAmelCase__			:		Tuple						       =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_semantic_segmentation(*lowercase_	)







					def  __lowerCAmelCase				( self    :       Optional[int]	):
						for model_class in self.all_model_classes:
							if model_class.__name__ == "DPTForDepthEstimation":
								continue

							lowerCAmelCase__       ,lowerCAmelCase__			:		int						       =			self.model_tester.prepare_config_and_inputs_for_common()
							lowerCAmelCase__			:		Optional[Any]						       =			True

							if model_class in get_values(lowercase_	):
								continue

							lowerCAmelCase__			:		Dict						       =			model_class(lowercase_	)
							model.to(lowercase_	)
							model.train()
							lowerCAmelCase__			:		str						       =			self._prepare_for_class(lowercase_     ,lowercase_     ,return_labels=lowercase_	)
							lowerCAmelCase__			:		Tuple						       =			model(**lowercase_	).loss
							loss.backward()







					def  __lowerCAmelCase				( self    :       str	):
						for model_class in self.all_model_classes:
							if model_class.__name__ == "DPTForDepthEstimation":
								continue

							lowerCAmelCase__       ,lowerCAmelCase__			:		Dict						       =			self.model_tester.prepare_config_and_inputs_for_common()
							lowerCAmelCase__			:		Optional[int]						       =			False
							lowerCAmelCase__			:		Optional[Any]						       =			True

							if model_class in get_values(lowercase_	) or not model_class.supports_gradient_checkpointing:
								continue
							lowerCAmelCase__			:		List[str]						       =			model_class(lowercase_	)
							model.to(lowercase_	)
							model.gradient_checkpointing_enable()
							model.train()
							lowerCAmelCase__			:		Optional[Any]						       =			self._prepare_for_class(lowercase_     ,lowercase_     ,return_labels=lowercase_	)
							lowerCAmelCase__			:		int						       =			model(**lowercase_	).loss
							loss.backward()







					def  __lowerCAmelCase				( self    :       str	):
						lowerCAmelCase__       ,lowerCAmelCase__			:		int						       =			self.model_tester.prepare_config_and_inputs_for_common()

						lowerCAmelCase__			:		Optional[Any]						       =			_config_zero_init(lowercase_	)
						for model_class in self.all_model_classes:
							lowerCAmelCase__			:		Dict						       =			model_class(config=lowercase_	)
							# Skip the check for the backbone
							lowerCAmelCase__			:		Dict						       =			[]
							for name, module in model.named_modules():
								if module.__class__.__name__ == "DPTViTHybridEmbeddings":
									lowerCAmelCase__			:		Tuple						       =			[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  __lowerCAmelCase				( self    :       str	):
						pass







					@slow
					def  __lowerCAmelCase				( self    :       List[str]	):
						for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCAmelCase__			:		Tuple						       =			DPTModel.from_pretrained(lowercase_	)
							self.assertIsNotNone(lowercase_	)

def 						__SCREAMING_SNAKE_CASE						(   ):
	lowerCAmelCase__			:		int						       =			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
	return image







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






					"""simple docstring"""



					def  __lowerCAmelCase				( self    :       Dict	):
						lowerCAmelCase__			:		str						       =			DPTImageProcessor.from_pretrained('''Intel/dpt-large'''	)
						lowerCAmelCase__			:		str						       =			DPTForDepthEstimation.from_pretrained('''Intel/dpt-large'''	).to(lowercase_	)

						lowerCAmelCase__			:		Any						       =			prepare_img()
						lowerCAmelCase__			:		Optional[Any]						       =			image_processor(images=lowercase_     ,return_tensors='''pt'''	).to(lowercase_	)

						# forward pass
						with torch.no_grad():
							lowerCAmelCase__			:		List[str]						       =			model(**lowercase_	)
							lowerCAmelCase__			:		Union[str, Any]						       =			outputs.predicted_depth

						# verify the predicted depth
						lowerCAmelCase__			:		List[str]						       =			torch.Size((1, 3_8_4, 3_8_4)	)
						self.assertEqual(predicted_depth.shape     ,lowercase_	)

						lowerCAmelCase__			:		Any						       =			torch.tensor(
						    [[6.3199, 6.3629, 6.4148], [6.3850, 6.3615, 6.4166], [6.3519, 6.3176, 6.3575]]	).to(lowercase_	)

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







					def  __lowerCAmelCase				( self    :       List[Any]	):
						lowerCAmelCase__			:		Dict						       =			DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''	)
						lowerCAmelCase__			:		List[str]						       =			DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''	).to(lowercase_	)

						lowerCAmelCase__			:		Tuple						       =			prepare_img()
						lowerCAmelCase__			:		int						       =			image_processor(images=lowercase_     ,return_tensors='''pt'''	).to(lowercase_	)

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

						# verify the logits
						lowerCAmelCase__			:		Optional[Any]						       =			torch.Size((1, 1_5_0, 4_8_0, 4_8_0)	)
						self.assertEqual(outputs.logits.shape     ,lowercase_	)

						lowerCAmelCase__			:		Dict						       =			torch.tensor(
						    [[4.0480, 4.2420, 4.4360], [4.3124, 4.5693, 4.8261], [4.5768, 4.8965, 5.2163]]	).to(lowercase_	)

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







					def  __lowerCAmelCase				( self    :       int	):
						lowerCAmelCase__			:		Tuple						       =			DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''	)
						lowerCAmelCase__			:		Optional[int]						       =			DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''	).to(lowercase_	)

						lowerCAmelCase__			:		Optional[Any]						       =			prepare_img()
						lowerCAmelCase__			:		Any						       =			image_processor(images=lowercase_     ,return_tensors='''pt'''	).to(lowercase_	)

						# forward pass
						with torch.no_grad():
							lowerCAmelCase__			:		List[str]						       =			model(**lowercase_	)

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

						lowerCAmelCase__			:		Optional[Any]						       =			image_processor.post_process_semantic_segmentation(outputs=lowercase_     ,target_sizes=[(5_0_0, 3_0_0)]	)
						lowerCAmelCase__			:		Dict						       =			torch.Size((5_0_0, 3_0_0)	)
						self.assertEqual(segmentation[0].shape     ,lowercase_	)

						lowerCAmelCase__			:		int						       =			image_processor.post_process_semantic_segmentation(outputs=lowercase_	)
						lowerCAmelCase__			:		Optional[int]						       =			torch.Size((4_8_0, 4_8_0)	)
						self.assertEqual(segmentation[0].shape     ,lowercase_	)







"""simple docstring"""





from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


__UpperCamelCase						:   List[Any]							   =	logging.get_logger(__name__)







class        SCREAMING_SNAKE_CASE      (	a_ ):






					"""simple docstring"""



					lowercase__       				= ["pixel_values"]







					def __init__( self    :       Tuple     ,lowercase_    :       bool = True     ,lowercase_    :       Optional[Dict[str, int]] = None     ,lowercase_    :       PILImageResampling = PILImageResampling.BILINEAR     ,lowercase_    :       bool = True     ,lowercase_    :       Union[int, float] = 1 / 2_5_5     ,lowercase_    :       bool = True     ,lowercase_    :       Optional[Union[float, List[float]]] = None     ,lowercase_    :       Optional[Union[float, List[float]]] = None     ,**lowercase_    :       Tuple     ,):
						super().__init__(**lowercase_	)
						lowerCAmelCase__			:		Any						       =			size if size is not None else {'''height''': 2_2_4, '''width''': 2_2_4}
						lowerCAmelCase__			:		Tuple						       =			get_size_dict(lowercase_	)
						lowerCAmelCase__			:		Tuple						       =			do_resize
						lowerCAmelCase__			:		Tuple						       =			do_rescale
						lowerCAmelCase__			:		Union[str, Any]						       =			do_normalize
						lowerCAmelCase__			:		Optional[int]						       =			size
						lowerCAmelCase__			:		List[Any]						       =			resample
						lowerCAmelCase__			:		int						       =			rescale_factor
						lowerCAmelCase__			:		int						       =			image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
						lowerCAmelCase__			:		Any						       =			image_std if image_std is not None else IMAGENET_STANDARD_STD







					def  __lowerCAmelCase				( self    :       List[Any]     ,lowercase_    :       np.ndarray     ,lowercase_    :       Dict[str, int]     ,lowercase_    :       PILImageResampling = PILImageResampling.BILINEAR     ,lowercase_    :       Optional[Union[str, ChannelDimension]] = None     ,**lowercase_    :       str     ,):
						lowerCAmelCase__			:		Union[str, Any]						       =			get_size_dict(lowercase_	)
						if "height" not in size or "width" not in size:
							raise ValueError(F'The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}'	)
						return resize(
						    lowercase_     ,size=(size['''height'''], size['''width'''])     ,resample=lowercase_     ,data_format=lowercase_     ,**lowercase_	)







					def  __lowerCAmelCase				( self    :       List[Any]     ,lowercase_    :       np.ndarray     ,lowercase_    :       float     ,lowercase_    :       Optional[Union[str, ChannelDimension]] = None     ,**lowercase_    :       Tuple	):
						return rescale(lowercase_     ,scale=lowercase_     ,data_format=lowercase_     ,**lowercase_	)







					def  __lowerCAmelCase				( self    :       Dict     ,lowercase_    :       np.ndarray     ,lowercase_    :       Union[float, List[float]]     ,lowercase_    :       Union[float, List[float]]     ,lowercase_    :       Optional[Union[str, ChannelDimension]] = None     ,**lowercase_    :       Union[str, Any]     ,):
						return normalize(lowercase_     ,mean=lowercase_     ,std=lowercase_     ,data_format=lowercase_     ,**lowercase_	)







					def  __lowerCAmelCase				( self    :       List[str]     ,lowercase_    :       ImageInput     ,lowercase_    :       Optional[bool] = None     ,lowercase_    :       Dict[str, int] = None     ,lowercase_    :       PILImageResampling = None     ,lowercase_    :       Optional[bool] = None     ,lowercase_    :       Optional[float] = None     ,lowercase_    :       Optional[bool] = None     ,lowercase_    :       Optional[Union[float, List[float]]] = None     ,lowercase_    :       Optional[Union[float, List[float]]] = None     ,lowercase_    :       Optional[Union[str, TensorType]] = None     ,lowercase_    :       Union[str, ChannelDimension] = ChannelDimension.FIRST     ,**lowercase_    :       List[Any]     ,):
						lowerCAmelCase__			:		Optional[int]						       =			do_resize if do_resize is not None else self.do_resize
						lowerCAmelCase__			:		Any						       =			do_rescale if do_rescale is not None else self.do_rescale
						lowerCAmelCase__			:		Dict						       =			do_normalize if do_normalize is not None else self.do_normalize
						lowerCAmelCase__			:		Dict						       =			resample if resample is not None else self.resample
						lowerCAmelCase__			:		Dict						       =			rescale_factor if rescale_factor is not None else self.rescale_factor
						lowerCAmelCase__			:		Optional[Any]						       =			image_mean if image_mean is not None else self.image_mean
						lowerCAmelCase__			:		str						       =			image_std if image_std is not None else self.image_std

						lowerCAmelCase__			:		Any						       =			size if size is not None else self.size
						lowerCAmelCase__			:		Dict						       =			get_size_dict(lowercase_	)

						lowerCAmelCase__			:		Union[str, Any]						       =			make_list_of_images(lowercase_	)

						if not valid_images(lowercase_	):
							raise ValueError(
							    '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
							    '''torch.Tensor, tf.Tensor or jax.ndarray.'''	)

						if do_resize and size is None:
							raise ValueError('''Size must be specified if do_resize is True.'''	)

						if do_rescale and rescale_factor is None:
							raise ValueError('''Rescale factor must be specified if do_rescale is True.'''	)

						# All transformations expect numpy arrays.
						lowerCAmelCase__			:		Dict						       =			[to_numpy_array(lowercase_	) for image in images]

						if do_resize:
							lowerCAmelCase__			:		Optional[Any]						       =			[self.resize(image=lowercase_     ,size=lowercase_     ,resample=lowercase_	) for image in images]

						if do_rescale:
							lowerCAmelCase__			:		List[Any]						       =			[self.rescale(image=lowercase_     ,scale=lowercase_	) for image in images]

						if do_normalize:
							lowerCAmelCase__			:		Dict						       =			[self.normalize(image=lowercase_     ,mean=lowercase_     ,std=lowercase_	) for image in images]

						lowerCAmelCase__			:		Tuple						       =			[to_channel_dimension_format(lowercase_     ,lowercase_	) for image in images]

						lowerCAmelCase__			:		int						       =			{'''pixel_values''': images}
						return BatchFeature(data=lowercase_     ,tensor_type=lowercase_	)




