


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							,					lowerCAmelCase__							,					lowerCAmelCase__=2							,					lowerCAmelCase__=32							,					lowerCAmelCase__=16							,					lowerCAmelCase__=3							,					lowerCAmelCase__=True							,					lowerCAmelCase__=True							,					lowerCAmelCase__=32							,					lowerCAmelCase__=4							,					lowerCAmelCase__=[0, 1, 2, 3]							,					lowerCAmelCase__=4							,					lowerCAmelCase__=37							,					lowerCAmelCase__="gelu"							,					lowerCAmelCase__=0.1							,					lowerCAmelCase__=0.1							,					lowerCAmelCase__=0.0_2							,					lowerCAmelCase__=3							,					lowerCAmelCase__=False							,					lowerCAmelCase__=None							,					)   ->							str:




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




        '''simple docstring'''
        lowercase__:  Optional[int]         =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

        lowercase__:  Dict         =  self.get_config()

        return config, pixel_values, labels
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Union[str, 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=lowerCAmelCase__							,					initializer_range=self.initializer_range							,					is_hybrid=self.is_hybrid							,					)
 def 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)   ->							Any:




        '''simple docstring'''
        lowercase__:  Optional[Any]         =  DPTModel(config=lowerCAmelCase__					)
        model.to(lowerCAmelCase__					)
        model.eval()
        lowercase__:  Tuple         =  model(lowerCAmelCase__					)
        self.parent.assertEqual(result.last_hidden_state.shape							,					(self.batch_size, self.seq_length, self.hidden_size)					)
 def 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)   ->							str:




        '''simple docstring'''
        lowercase__:  Dict         =  self.num_labels
        lowercase__:  Dict         =  DPTForDepthEstimation(lowerCAmelCase__					)
        model.to(lowerCAmelCase__					)
        model.eval()
        lowercase__:  Union[str, Any]         =  model(lowerCAmelCase__					)
        self.parent.assertEqual(result.predicted_depth.shape							,					(self.batch_size, self.image_size, self.image_size)					)
 def 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)   ->							Tuple:




        '''simple docstring'''
        lowercase__:  int         =  self.num_labels
        lowercase__:  str         =  DPTForSemanticSegmentation(lowerCAmelCase__					)
        model.to(lowerCAmelCase__					)
        model.eval()
        lowercase__:  Optional[int]         =  model(lowerCAmelCase__							,					labels=lowerCAmelCase__					)
        self.parent.assertEqual(
            result.logits.shape							,					(self.batch_size, self.num_labels, self.image_size, self.image_size)					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[str]:




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





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

 __lowercase					:				int  	=					False
 __lowercase					:				Any  	=					False
 __lowercase					:				Any  	=					False
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							str:




        '''simple docstring'''
        lowercase__:  Any         =  DPTModelTester(self					)
        lowercase__:  List[str]         =  ConfigTester(self							,					config_class=lowerCAmelCase__							,					has_text_modality=lowerCAmelCase__							,					hidden_size=37					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Tuple:




        '''simple docstring'''
        self.config_tester.run_common_tests()
 @unittest.skip(reason='DPT does not use inputs_embeds'					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[int]:




        '''simple docstring'''
        pass
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Dict:




        '''simple docstring'''
        lowercase__     ,		lowercase__:  str         =  self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               lowercase__:  List[Any]         =  model_class(lowerCAmelCase__					)
               self.assertIsInstance(model.get_input_embeddings()							,					(nn.Module)					)
               lowercase__:  Dict         =  model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(lowerCAmelCase__							,					nn.Linear					)					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[Any]:




        '''simple docstring'''
        lowercase__     ,		lowercase__:  Optional[Any]         =  self.model_tester.prepare_config_and_inputs_for_common()

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

               lowercase__:  int         =  ['pixel_values']
               self.assertListEqual(arg_names[:1]							,					lowerCAmelCase__					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							str:




        '''simple docstring'''
        lowercase__:  Optional[Any]         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase__					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[Any]:




        '''simple docstring'''
        lowercase__:  Dict         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_depth_estimation(*lowerCAmelCase__					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[str]:




        '''simple docstring'''
        lowercase__:  int         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_semantic_segmentation(*lowerCAmelCase__					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[str]:




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

               lowercase__     ,		lowercase__:  List[str]         =  self.model_tester.prepare_config_and_inputs_for_common()
               lowercase__:  Optional[int]         =  True

               if model_class in get_values(lowerCAmelCase__					):
                      continue

               lowercase__:  Optional[Any]         =  model_class(lowerCAmelCase__					)
               model.to(lowerCAmelCase__					)
               model.train()
               lowercase__:  List[Any]         =  self._prepare_for_class(lowerCAmelCase__							,					lowerCAmelCase__							,					return_labels=lowerCAmelCase__					)
               lowercase__:  List[str]         =  model(**lowerCAmelCase__					).loss
               loss.backward()
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[str]:




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

               lowercase__     ,		lowercase__:  Any         =  self.model_tester.prepare_config_and_inputs_for_common()
               lowercase__:  int         =  False
               lowercase__:  List[Any]         =  True

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




        '''simple docstring'''
        lowercase__     ,		lowercase__:  Optional[int]         =  self.model_tester.prepare_config_and_inputs_for_common()

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




        '''simple docstring'''
        pass
 @slow
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Union[str, Any]:




        '''simple docstring'''
        for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__:  Any         =  DPTModel.from_pretrained(lowerCAmelCase__					)
               self.assertIsNotNone(lowerCAmelCase__					)





def   snake_case_	(    )    ->							Optional[int]:
       lowercase__:  int         =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'	)
       return image





@require_torch
@require_vision
@slow
class     __a     (       unittest.TestCase ):
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[int]:




        '''simple docstring'''
        lowercase__:  Union[str, Any]         =  DPTImageProcessor.from_pretrained('Intel/dpt-large'					)
        lowercase__:  Optional[Any]         =  DPTForDepthEstimation.from_pretrained('Intel/dpt-large'					).to(lowerCAmelCase__					)

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

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

        # verify the predicted depth
        lowercase__:  Union[str, Any]         =  torch.Size((1, 384, 384)					)
        self.assertEqual(predicted_depth.shape							,					lowerCAmelCase__					)

        lowercase__:  Optional[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(lowerCAmelCase__					)

        self.assertTrue(torch.allclose(outputs.predicted_depth[0, :3, :3]							,					lowerCAmelCase__							,					atol=1E-4					)					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							int:




        '''simple docstring'''
        lowercase__:  Union[str, Any]         =  DPTImageProcessor.from_pretrained('Intel/dpt-large-ade'					)
        lowercase__:  Union[str, Any]         =  DPTForSemanticSegmentation.from_pretrained('Intel/dpt-large-ade'					).to(lowerCAmelCase__					)

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

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

        # verify the logits
        lowercase__:  List[str]         =  torch.Size((1, 150, 480, 480)					)
        self.assertEqual(outputs.logits.shape							,					lowerCAmelCase__					)

        lowercase__:  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(lowerCAmelCase__					)

        self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]							,					lowerCAmelCase__							,					atol=1E-4					)					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Any:




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

        lowercase__:  Dict         =  prepare_img()
        lowercase__:  Union[str, Any]         =  image_processor(images=lowerCAmelCase__							,					return_tensors='pt'					).to(lowerCAmelCase__					)

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

        lowercase__:  int         =  outputs.logits.detach().cpu()

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

        lowercase__:  Optional[Any]         =  image_processor.post_process_semantic_segmentation(outputs=lowerCAmelCase__					)
        lowercase__:  Any         =  torch.Size((480, 480)					)
        self.assertEqual(segmentation[0].shape							,					lowerCAmelCase__					)






from typing import TYPE_CHECKING

from ...utils import _LazyModule


__lowerCAmelCase									=				{'''tokenization_phobert''': ['''PhobertTokenizer''']}


if TYPE_CHECKING:
 from .tokenization_phobert import PhobertTokenizer

else:
 import sys

 __lowerCAmelCase									=				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


