



# NOTE: This file is deprecated and will be removed in a future version.
# It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works
from ...utils import deprecate
from ..controlnet.multicontrolnet import MultiControlNetModel  # noqa: F401
from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline  # noqa: F401


deprecate(
    'stable diffusion controlnet',
    '0.22.0',
    'Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.',
    standard_warn=False,
    stacklevel=3,
)










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






 '''simple docstring'''





 def __init__(  self,		A_,		A_=2,		A_=32,		A_=16,		A_=3,		A_=True,		A_=True,		A_=32,		A_=4,		A_=[0, 1, 2, 3],		A_=4,		A_=37,		A_="gelu",		A_=0.1,		A_=0.1,		A_=0.02,		A_=3,		A_=[1, 384, 24, 24],		A_=True,		A_=None,		)	->       Optional[int]:
    UpperCAmelCase__						=parent
    UpperCAmelCase__						=batch_size
    UpperCAmelCase__						=image_size
    UpperCAmelCase__						=patch_size
    UpperCAmelCase__						=num_channels
    UpperCAmelCase__						=is_training
    UpperCAmelCase__						=use_labels
    UpperCAmelCase__						=hidden_size
    UpperCAmelCase__						=num_hidden_layers
    UpperCAmelCase__						=backbone_out_indices
    UpperCAmelCase__						=num_attention_heads
    UpperCAmelCase__						=intermediate_size
    UpperCAmelCase__						=hidden_act
    UpperCAmelCase__						=hidden_dropout_prob
    UpperCAmelCase__						=attention_probs_dropout_prob
    UpperCAmelCase__						=initializer_range
    UpperCAmelCase__						=num_labels
    UpperCAmelCase__						=backbone_featmap_shape
    UpperCAmelCase__						=scope
    UpperCAmelCase__						=is_hybrid
    # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
    UpperCAmelCase__						=(image_size // patch_size) ** 2
    UpperCAmelCase__						=num_patches + 1

 def  __UpperCAmelCase						(  self		)	->       str:
    UpperCAmelCase__						=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

    UpperCAmelCase__						=self.get_config()

    return config, pixel_values, labels

 def  __UpperCAmelCase						(  self		)	->       Dict:
    UpperCAmelCase__						={
        "global_padding": "same",
        "layer_type": "bottleneck",
        "depths": [3, 4, 9],
        "out_features": ["stage1", "stage2", "stage3"],
        "embedding_dynamic_padding": True,
        "hidden_sizes": [96, 192, 384, 768],
        "num_groups": 2,
    }

    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,		backbone_config=A_,		backbone_featmap_shape=self.backbone_featmap_shape,		)

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_		)	->       Optional[Any]:
    UpperCAmelCase__						=DPTModel(config=A_		)
    model.to(A_		)
    model.eval()
    UpperCAmelCase__						=model(A_		)
    self.parent.assertEqual(result.last_hidden_state.shape,		(self.batch_size, self.seq_length, self.hidden_size)		)

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_		)	->       Union[str, Any]:
    UpperCAmelCase__						=self.num_labels
    UpperCAmelCase__						=DPTForDepthEstimation(A_		)
    model.to(A_		)
    model.eval()
    UpperCAmelCase__						=model(A_		)
    self.parent.assertEqual(result.predicted_depth.shape,		(self.batch_size, self.image_size, self.image_size)		)

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_		)	->       Optional[Any]:
    UpperCAmelCase__						=self.num_labels
    UpperCAmelCase__						=DPTForSemanticSegmentation(A_		)
    model.to(A_		)
    model.eval()
    UpperCAmelCase__						=model(A_,		labels=A_		)
    self.parent.assertEqual(
        result.logits.shape,		(self.batch_size, self.num_labels, self.image_size, self.image_size)		)






 def  __UpperCAmelCase						(  self		)	->       int:
    UpperCAmelCase__						=self.prepare_config_and_inputs()
    UpperCAmelCase__			,							UpperCAmelCase__			,							UpperCAmelCase__						=config_and_inputs
    UpperCAmelCase__						={"pixel_values": pixel_values}
    return config, inputs_dict

@require_torch
class     snake_case_		(   a,		a,		unittest.TestCase							):






 '''simple docstring'''





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

 __UpperCamelCase       =      False
 __UpperCamelCase       =      False
 __UpperCamelCase       =      False

 def  __UpperCAmelCase						(  self		)	->       int:
    UpperCAmelCase__						=DPTModelTester(self		)
    UpperCAmelCase__						=ConfigTester(self,		config_class=A_,		has_text_modality=A_,		hidden_size=37		)

 def  __UpperCAmelCase						(  self		)	->       Optional[Any]:
    self.config_tester.run_common_tests()

 @unittest.skip(reason="DPT does not use inputs_embeds"		)
 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    pass

 def  __UpperCAmelCase						(  self		)	->       str:
    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()

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

 def  __UpperCAmelCase						(  self		)	->       Optional[int]:
    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()

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

       UpperCAmelCase__						=["pixel_values"]
       self.assertListEqual(arg_names[:1],		A_		)

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*A_		)

 def  __UpperCAmelCase						(  self		)	->       str:
    UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_depth_estimation(*A_		)

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_semantic_segmentation(*A_		)

 def  __UpperCAmelCase						(  self		)	->       Any:
    for model_class in self.all_model_classes:
       if model_class.__name__ == "DPTForDepthEstimation":
          continue

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

       if model_class in get_values(A_		):
          continue

       UpperCAmelCase__						=model_class(A_		)
       model.to(A_		)
       model.train()
       UpperCAmelCase__						=self._prepare_for_class(A_,		A_,		return_labels=A_		)
       UpperCAmelCase__						=model(**A_		).loss
       loss.backward()

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    for model_class in self.all_model_classes:
       if model_class.__name__ == "DPTForDepthEstimation":
          continue

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

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

 def  __UpperCAmelCase						(  self		)	->       int:
    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()

    UpperCAmelCase__						=_config_zero_init(A_		)
    for model_class in self.all_model_classes:
       UpperCAmelCase__						=model_class(config=A_		)
       # Skip the check for the backbone
       UpperCAmelCase__						=[]
       for name, module in model.named_modules():
          if module.__class__.__name__ == "DPTViTHybridEmbeddings":
             UpperCAmelCase__						=[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		)	->       List[Any]:
    pass

 @slow
 def  __UpperCAmelCase						(  self		)	->       Optional[int]:
    for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]:
       UpperCAmelCase__						=DPTModel.from_pretrained(A_		)
       self.assertIsNotNone(A_		)






 def  __UpperCAmelCase						(  self		)	->       Any:
    # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type
    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()
    UpperCAmelCase__						="add"
    with self.assertRaises(A_		):
       UpperCAmelCase__						=DPTForDepthEstimation(A_		)






def 				_UpperCAmelCase			(    ):




   '''simple docstring'''

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




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






 '''simple docstring'''





 def  __UpperCAmelCase						(  self		)	->       Union[str, Any]:
    UpperCAmelCase__						=DPTImageProcessor.from_pretrained("Intel/dpt-hybrid-midas"		)
    UpperCAmelCase__						=DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas"		).to(A_		)

    UpperCAmelCase__						=prepare_img()
    UpperCAmelCase__						=image_processor(images=A_,		return_tensors="pt"		).to(A_		)

    # forward pass
    with torch.no_grad():
       UpperCAmelCase__						=model(**A_		)
       UpperCAmelCase__						=outputs.predicted_depth

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

    UpperCAmelCase__						=torch.tensor(
        [[[5.64_37, 5.61_46, 5.65_11], [5.43_71, 5.56_49, 5.59_58], [5.52_15, 5.51_84, 5.52_93]]]		).to(A_		)

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



