


'''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    __a :

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







													"""simple docstring"""
													UpperCAmelCase_     : Any         =					parent
													UpperCAmelCase_     : List[Any]         =					batch_size
													UpperCAmelCase_     : Optional[int]         =					image_size
													UpperCAmelCase_     : List[str]         =					patch_size
													UpperCAmelCase_     : Any         =					num_channels
													UpperCAmelCase_     : Optional[int]         =					is_training
													UpperCAmelCase_     : Optional[int]         =					use_labels
													UpperCAmelCase_     : Dict         =					hidden_size
													UpperCAmelCase_     : Dict         =					num_hidden_layers
													UpperCAmelCase_     : Tuple         =					backbone_out_indices
													UpperCAmelCase_     : Optional[Any]         =					num_attention_heads
													UpperCAmelCase_     : List[Any]         =					intermediate_size
													UpperCAmelCase_     : Union[str, Any]         =					hidden_act
													UpperCAmelCase_     : Union[str, Any]         =					hidden_dropout_prob
													UpperCAmelCase_     : List[str]         =					attention_probs_dropout_prob
													UpperCAmelCase_     : Dict         =					initializer_range
													UpperCAmelCase_     : Union[str, Any]         =					num_labels
													UpperCAmelCase_     : Any         =					scope
													UpperCAmelCase_     : Tuple         =					is_hybrid
													# sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
													UpperCAmelCase_     : Any         =					(image_size // patch_size) ** 2
													UpperCAmelCase_     : Union[str, Any]         =					num_patches + 1

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







													"""simple docstring"""
													UpperCAmelCase_     : int         =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

													UpperCAmelCase_     : List[str]         =					self.get_config()

													return config, pixel_values, labels

							def       UpperCAmelCase__		(							self							:       int				)   ->    int:







													"""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=lowercase__    ,       initializer_range=self.initializer_range    ,       is_hybrid=self.is_hybrid    ,       )

							def       UpperCAmelCase__		(							self							:       Dict    ,       __magic_name__							:       List[Any]    ,       __magic_name__							:       Any    ,       __magic_name__							:       int				)   ->    List[str]:







													"""simple docstring"""
													UpperCAmelCase_     : Optional[Any]         =					DPTModel(config=lowercase__				)
													model.to(lowercase__				)
													model.eval()
													UpperCAmelCase_     : Optional[Any]         =					model(lowercase__				)
													self.parent.assertEqual(result.last_hidden_state.shape    ,       (self.batch_size, self.seq_length, self.hidden_size)				)

							def       UpperCAmelCase__		(							self							:       Dict    ,       __magic_name__							:       Union[str, Any]    ,       __magic_name__							:       Dict    ,       __magic_name__							:       str				)   ->    Dict:







													"""simple docstring"""
													UpperCAmelCase_     : Optional[int]         =					self.num_labels
													UpperCAmelCase_     : Dict         =					DPTForDepthEstimation(lowercase__				)
													model.to(lowercase__				)
													model.eval()
													UpperCAmelCase_     : List[Any]         =					model(lowercase__				)
													self.parent.assertEqual(result.predicted_depth.shape    ,       (self.batch_size, self.image_size, self.image_size)				)

							def       UpperCAmelCase__		(							self							:       Optional[Any]    ,       __magic_name__							:       str    ,       __magic_name__							:       List[Any]    ,       __magic_name__							:       List[Any]				)   ->    str:







													"""simple docstring"""
													UpperCAmelCase_     : Any         =					self.num_labels
													UpperCAmelCase_     : Tuple         =					DPTForSemanticSegmentation(lowercase__				)
													model.to(lowercase__				)
													model.eval()
													UpperCAmelCase_     : str         =					model(lowercase__    ,       labels=lowercase__				)
													self.parent.assertEqual(
													    result.logits.shape    ,       (self.batch_size, self.num_labels, self.image_size, self.image_size)				)



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







													"""simple docstring"""
													UpperCAmelCase_     : str         =					self.prepare_config_and_inputs()
													UpperCAmelCase_     : Optional[Any]         =					config_and_inputs
													UpperCAmelCase_     : str         =					{"pixel_values": pixel_values}
													return config, inputs_dict





@require_torch
class    __a (_UpperCAmelCase    ,						_UpperCAmelCase    ,						unittest.TestCase       ):
							__a   :				Dict       =    (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
							__a   :				List[Any]       =    (
							    {
							        "depth-estimation": DPTForDepthEstimation,
							        "feature-extraction": DPTModel,
							        "image-segmentation": DPTForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							__a   :				Union[str, Any]       =    False
							__a   :				Union[str, Any]       =    False
							__a   :				str       =    False

							def       UpperCAmelCase__		(							self							:       Optional[Any]				)   ->    Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_     : Optional[int]         =					DPTModelTester(self				)
													UpperCAmelCase_     : List[Any]         =					ConfigTester(self    ,       config_class=lowercase__    ,       has_text_modality=lowercase__    ,       hidden_size=37				)

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







													"""simple docstring"""
													self.config_tester.run_common_tests()

							@unittest.skip(reason='''DPT does not use inputs_embeds'''				)
							def       UpperCAmelCase__		(							self							:       Any				)   ->    int:







													"""simple docstring"""
													pass

							def       UpperCAmelCase__		(							self							:       Tuple				)   ->    Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_     : Optional[Any]         =					self.model_tester.prepare_config_and_inputs_for_common()

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

							def       UpperCAmelCase__		(							self							:       int				)   ->    str:







													"""simple docstring"""
													UpperCAmelCase_     : List[str]         =					self.model_tester.prepare_config_and_inputs_for_common()

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

																			UpperCAmelCase_     : Optional[int]         =					["pixel_values"]
																			self.assertListEqual(arg_names[:1]    ,       lowercase__				)

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







													"""simple docstring"""
													UpperCAmelCase_     : Tuple         =					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*lowercase__				)

							def       UpperCAmelCase__		(							self							:       Dict				)   ->    str:







													"""simple docstring"""
													UpperCAmelCase_     : Any         =					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_depth_estimation(*lowercase__				)

							def       UpperCAmelCase__		(							self							:       List[str]				)   ->    int:







													"""simple docstring"""
													UpperCAmelCase_     : Optional[Any]         =					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_semantic_segmentation(*lowercase__				)

							def       UpperCAmelCase__		(							self							:       Optional[int]				)   ->    List[str]:







													"""simple docstring"""






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

																			UpperCAmelCase_     : Optional[int]         =					self.model_tester.prepare_config_and_inputs_for_common()
																			UpperCAmelCase_     : Dict         =					True

																			if model_class in get_values(lowercase__				):
																									continue

																			UpperCAmelCase_     : Union[str, Any]         =					model_class(lowercase__				)
																			model.to(lowercase__				)
																			model.train()
																			UpperCAmelCase_     : List[Any]         =					self._prepare_for_class(lowercase__    ,       lowercase__    ,       return_labels=lowercase__				)
																			UpperCAmelCase_     : Union[str, Any]         =					model(**lowercase__				).loss
																			loss.backward()

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







													"""simple docstring"""






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

																			UpperCAmelCase_     : List[Any]         =					self.model_tester.prepare_config_and_inputs_for_common()
																			UpperCAmelCase_     : int         =					False
																			UpperCAmelCase_     : List[str]         =					True

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

							def       UpperCAmelCase__		(							self							:       str				)   ->    Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_     : Optional[Any]         =					self.model_tester.prepare_config_and_inputs_for_common()

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







													"""simple docstring"""
													pass



							@slow
							def       UpperCAmelCase__		(							self							:       int				)   ->    Optional[int]:







													"""simple docstring"""






													for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			UpperCAmelCase_     : int         =					DPTModel.from_pretrained(lowercase__				)
																			self.assertIsNotNone(lowercase__				)





def     lowerCamelCase_ (					)	->		Optional[Any]:
						UpperCAmelCase_     : Union[str, Any]         =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
						return image


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

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







													"""simple docstring"""
													UpperCAmelCase_     : int         =					DPTImageProcessor.from_pretrained('''Intel/dpt-large'''				)
													UpperCAmelCase_     : Optional[Any]         =					DPTForDepthEstimation.from_pretrained('''Intel/dpt-large'''				).to(lowercase__				)

													UpperCAmelCase_     : List[str]         =					prepare_img()
													UpperCAmelCase_     : int         =					image_processor(images=lowercase__    ,       return_tensors='''pt'''				).to(lowercase__				)

													# forward pass
													with torch.no_grad():
																			UpperCAmelCase_     : Dict         =					model(**lowercase__				)
																			UpperCAmelCase_     : Optional[int]         =					outputs.predicted_depth

													# verify the predicted depth
													UpperCAmelCase_     : Any         =					torch.Size((1, 3_84, 3_84)				)
													self.assertEqual(predicted_depth.shape    ,       lowercase__				)

													UpperCAmelCase_     : Any         =					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(lowercase__				)

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

							def       UpperCAmelCase__		(							self							:       int				)   ->    List[str]:







													"""simple docstring"""
													UpperCAmelCase_     : Tuple         =					DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''				)
													UpperCAmelCase_     : str         =					DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''				).to(lowercase__				)

													UpperCAmelCase_     : Optional[Any]         =					prepare_img()
													UpperCAmelCase_     : str         =					image_processor(images=lowercase__    ,       return_tensors='''pt'''				).to(lowercase__				)

													# forward pass
													with torch.no_grad():
																			UpperCAmelCase_     : Union[str, Any]         =					model(**lowercase__				)

													# verify the logits
													UpperCAmelCase_     : Union[str, Any]         =					torch.Size((1, 1_50, 4_80, 4_80)				)
													self.assertEqual(outputs.logits.shape    ,       lowercase__				)

													UpperCAmelCase_     : Optional[int]         =					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(lowercase__				)

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



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







													"""simple docstring"""
													UpperCAmelCase_     : str         =					DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''				)
													UpperCAmelCase_     : Optional[Any]         =					DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''				).to(lowercase__				)

													UpperCAmelCase_     : List[str]         =					prepare_img()
													UpperCAmelCase_     : int         =					image_processor(images=lowercase__    ,       return_tensors='''pt'''				).to(lowercase__				)

													# forward pass
													with torch.no_grad():
																			UpperCAmelCase_     : Optional[int]         =					model(**lowercase__				)

													UpperCAmelCase_     : Union[str, Any]         =					outputs.logits.detach().cpu()

													UpperCAmelCase_     : Union[str, Any]         =					image_processor.post_process_semantic_segmentation(outputs=lowercase__    ,       target_sizes=[(5_00, 3_00)]				)
													UpperCAmelCase_     : Union[str, Any]         =					torch.Size((5_00, 3_00)				)
													self.assertEqual(segmentation[0].shape    ,       lowercase__				)

													UpperCAmelCase_     : Dict         =					image_processor.post_process_semantic_segmentation(outputs=lowercase__				)
													UpperCAmelCase_     : Optional[Any]         =					torch.Size((4_80, 4_80)				)
													self.assertEqual(segmentation[0].shape    ,       lowercase__				)












'''simple docstring'''







from typing import List, Optional, Tuple, Union

import torch

from ...models import UNetaDModel
from ...schedulers import PNDMScheduler
from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput


class    __a (lowerCamelCase       ):
							__a   :				UNetaDModel
							__a   :				PNDMScheduler

							def __init__(							self							:       Optional[Any]    ,       __magic_name__							:       UNetaDModel    ,       __magic_name__							:       PNDMScheduler				)   ->    Tuple:







													"""simple docstring"""
													super().__init__()

													UpperCAmelCase_     : Optional[int]         =					PNDMScheduler.from_config(scheduler.config				)

													self.register_modules(unet=__magic_name__    ,       scheduler=__magic_name__				)



							@torch.no_grad()
							def __call__(							self							:       Union[str, Any]    ,       __magic_name__							:       int = 1    ,       __magic_name__							:       int = 50    ,       __magic_name__							:       Optional[Union[torch.Generator, List[torch.Generator]]] = None    ,       __magic_name__							:       Optional[str] = "pil"    ,       __magic_name__							:       bool = True    ,       **__magic_name__							:       Dict    ,       )   ->    Union[ImagePipelineOutput, Tuple]:







													"""simple docstring"""
													UpperCAmelCase_     : Dict         =					randn_tensor(
													    (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size)    ,       generator=__magic_name__    ,       device=self.device    ,       )

													self.scheduler.set_timesteps(__magic_name__				)
													for t in self.progress_bar(self.scheduler.timesteps				):
																			UpperCAmelCase_     : Optional[int]         =					self.unet(__magic_name__    ,       __magic_name__				).sample

																			UpperCAmelCase_     : str         =					self.scheduler.step(__magic_name__    ,       __magic_name__    ,       __magic_name__				).prev_sample

													UpperCAmelCase_     : int         =					(image / 2 + 0.5).clamp(0    ,       1				)
													UpperCAmelCase_     : Any         =					image.cpu().permute(0    ,       2    ,       3    ,       1				).numpy()
													if output_type == "pil":
																			UpperCAmelCase_     : Optional[int]         =					self.numpy_to_pil(__magic_name__				)

													if not return_dict:
																			return (image,)

													return ImagePipelineOutput(images=__magic_name__				)





