"""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        UpperCamelCase		:
   def __init__(			self     ,__UpperCamelCase     ,__UpperCamelCase=2     ,__UpperCamelCase=32     ,__UpperCamelCase=16     ,__UpperCamelCase=3     ,__UpperCamelCase=True     ,__UpperCamelCase=True     ,__UpperCamelCase=32     ,__UpperCamelCase=4     ,__UpperCamelCase=[0, 1, 2, 3]     ,__UpperCamelCase=4     ,__UpperCamelCase=37     ,__UpperCamelCase="gelu"     ,__UpperCamelCase=0.1     ,__UpperCamelCase=0.1     ,__UpperCamelCase=0.02     ,__UpperCamelCase=3     ,__UpperCamelCase=[1, 384, 24, 24]     ,__UpperCamelCase=True     ,__UpperCamelCase=None     ,)	->      Tuple:


        '''simple docstring'''


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


        '''simple docstring'''


        lowercase_	:				Optional[Any]													=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

        lowercase_	:				str													=	self.get_config()

        return config, pixel_values, labels
   def 			_UpperCAmelCase   (			self			)	->      Any:


        '''simple docstring'''


        lowercase_	:				Optional[int]													=	{
            '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=__UpperCamelCase     ,initializer_range=self.initializer_range     ,is_hybrid=self.is_hybrid     ,backbone_config=__UpperCamelCase     ,backbone_featmap_shape=self.backbone_featmap_shape     ,)
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)	->      Tuple:


        '''simple docstring'''


        lowercase_	:				List[Any]													=	DPTModel(config=__UpperCamelCase			)
        model.to(__UpperCamelCase			)
        model.eval()
        lowercase_	:				str													=	model(__UpperCamelCase			)
        self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size)			)
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)	->      Any:


        '''simple docstring'''


        lowercase_	:				List[str]													=	self.num_labels
        lowercase_	:				Any													=	DPTForDepthEstimation(__UpperCamelCase			)
        model.to(__UpperCamelCase			)
        model.eval()
        lowercase_	:				Any													=	model(__UpperCamelCase			)
        self.parent.assertEqual(result.predicted_depth.shape     ,(self.batch_size, self.image_size, self.image_size)			)
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)	->      int:


        '''simple docstring'''


        lowercase_	:				Optional[int]													=	self.num_labels
        lowercase_	:				Dict													=	DPTForSemanticSegmentation(__UpperCamelCase			)
        model.to(__UpperCamelCase			)
        model.eval()
        lowercase_	:				Any													=	model(__UpperCamelCase     ,labels=__UpperCamelCase			)
        self.parent.assertEqual(
            result.logits.shape     ,(self.batch_size, self.num_labels, self.image_size, self.image_size)			)







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


        '''simple docstring'''


        lowercase_	:				Dict													=	self.prepare_config_and_inputs()
        lowercase_							,  lowercase_							,  lowercase_	:				Optional[Any]													=	config_and_inputs
        lowercase_	:				Any													=	{'pixel_values': pixel_values}
        return config, inputs_dict







@require_torch
class        UpperCamelCase		(			lowercase_						,      lowercase_						,      unittest.TestCase  ):
   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 			_UpperCAmelCase   (			self			)	->      Dict:


        '''simple docstring'''


        lowercase_	:				Optional[Any]													=	DPTModelTester(self			)
        lowercase_	:				Tuple													=	ConfigTester(self     ,config_class=__UpperCamelCase     ,has_text_modality=__UpperCamelCase     ,hidden_size=37			)
   def 			_UpperCAmelCase   (			self			)	->      Tuple:


        '''simple docstring'''


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


        '''simple docstring'''


        pass
   def 			_UpperCAmelCase   (			self			)	->      Optional[int]:


        '''simple docstring'''


        lowercase_							,  lowercase_	:				str													=	self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase_	:				Dict													=	model_class(__UpperCamelCase			)
             self.assertIsInstance(model.get_input_embeddings()     ,(nn.Module)			)
             lowercase_	:				Any													=	model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(__UpperCamelCase     ,nn.Linear			)			)
   def 			_UpperCAmelCase   (			self			)	->      Optional[Any]:


        '''simple docstring'''


        lowercase_							,  lowercase_	:				int													=	self.model_tester.prepare_config_and_inputs_for_common()

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

             lowercase_	:				List[Any]													=	['pixel_values']
             self.assertListEqual(arg_names[:1]     ,__UpperCamelCase			)
   def 			_UpperCAmelCase   (			self			)	->      int:


        '''simple docstring'''


        lowercase_	:				Tuple													=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__UpperCamelCase			)
   def 			_UpperCAmelCase   (			self			)	->      str:


        '''simple docstring'''


        lowercase_	:				Any													=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_depth_estimation(*__UpperCamelCase			)
   def 			_UpperCAmelCase   (			self			)	->      Optional[Any]:


        '''simple docstring'''


        lowercase_	:				Optional[Any]													=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_semantic_segmentation(*__UpperCamelCase			)
   def 			_UpperCAmelCase   (			self			)	->      Optional[int]:


        '''simple docstring'''


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

             lowercase_							,  lowercase_	:				List[Any]													=	self.model_tester.prepare_config_and_inputs_for_common()
             lowercase_	:				str													=	True

             if model_class in get_values(__UpperCamelCase			):
                  continue

             lowercase_	:				Union[str, Any]													=	model_class(__UpperCamelCase			)
             model.to(__UpperCamelCase			)
             model.train()
             lowercase_	:				Any													=	self._prepare_for_class(__UpperCamelCase     ,__UpperCamelCase     ,return_labels=__UpperCamelCase			)
             lowercase_	:				Dict													=	model(**__UpperCamelCase			).loss
             loss.backward()
   def 			_UpperCAmelCase   (			self			)	->      Any:


        '''simple docstring'''


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

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

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


        '''simple docstring'''


        lowercase_							,  lowercase_	:				Optional[int]													=	self.model_tester.prepare_config_and_inputs_for_common()

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


        '''simple docstring'''


        pass
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Union[str, Any]:


        '''simple docstring'''


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







   def 			_UpperCAmelCase   (			self			)	->      Tuple:


        '''simple docstring'''


        lowercase_							,  lowercase_	:				int													=	self.model_tester.prepare_config_and_inputs_for_common()
        lowercase_	:				Dict													=	'add'
        with self.assertRaises(__UpperCamelCase			):
             lowercase_	:				List[Any]													=	DPTForDepthEstimation(__UpperCamelCase			)






def 			lowercase__(       ):
     lowercase_	:				Dict													=	Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      )
     return image







@require_torch
@require_vision
@slow
class        UpperCamelCase		(			unittest.TestCase  ):
   def 			_UpperCAmelCase   (			self			)	->      Union[str, Any]:


        '''simple docstring'''


        lowercase_	:				Optional[int]													=	DPTImageProcessor.from_pretrained('Intel/dpt-hybrid-midas'			)
        lowercase_	:				int													=	DPTForDepthEstimation.from_pretrained('Intel/dpt-hybrid-midas'			).to(__UpperCamelCase			)

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

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

        # verify the predicted depth
        lowercase_	:				Any													=	torch.Size((1, 384, 384)			)
        self.assertEqual(predicted_depth.shape     ,__UpperCamelCase			)

        lowercase_	:				Tuple													=	torch.tensor(
            [[[5.6437, 5.6146, 5.6511], [5.4371, 5.5649, 5.5958], [5.5215, 5.5184, 5.5293]]]			).to(__UpperCamelCase			)

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


"""simple docstring"""







import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
       import jax

       from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel







class        UpperCamelCase		(			unittest.TestCase  ):
   def __init__(			self     ,__UpperCamelCase     ,__UpperCamelCase=13     ,__UpperCamelCase=30     ,__UpperCamelCase=2     ,__UpperCamelCase=3     ,__UpperCamelCase=True     ,__UpperCamelCase=True     ,__UpperCamelCase=32     ,__UpperCamelCase=5     ,__UpperCamelCase=4     ,__UpperCamelCase=37     ,__UpperCamelCase="gelu"     ,__UpperCamelCase=0.1     ,__UpperCamelCase=0.1     ,__UpperCamelCase=10     ,__UpperCamelCase=0.02     ,)	->      Tuple:


        '''simple docstring'''


        lowercase_	:				Tuple													=	parent
        lowercase_	:				Union[str, Any]													=	batch_size
        lowercase_	:				int													=	image_size
        lowercase_	:				Tuple													=	patch_size
        lowercase_	:				Optional[int]													=	num_channels
        lowercase_	:				Union[str, Any]													=	is_training
        lowercase_	:				Dict													=	use_labels
        lowercase_	:				Optional[int]													=	hidden_size
        lowercase_	:				List[str]													=	num_hidden_layers
        lowercase_	:				Optional[Any]													=	num_attention_heads
        lowercase_	:				Optional[int]													=	intermediate_size
        lowercase_	:				Tuple													=	hidden_act
        lowercase_	:				int													=	hidden_dropout_prob
        lowercase_	:				str													=	attention_probs_dropout_prob
        lowercase_	:				str													=	type_sequence_label_size
        lowercase_	:				Optional[int]													=	initializer_range

        # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
        lowercase_	:				str													=	(image_size // patch_size) ** 2
        lowercase_	:				Optional[int]													=	num_patches + 1
   def 			_UpperCAmelCase   (			self			)	->      Tuple:


        '''simple docstring'''


        lowercase_	:				Tuple													=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

        lowercase_	:				List[Any]													=	ViTConfig(
            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     ,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=__UpperCamelCase     ,initializer_range=self.initializer_range     ,)

        return config, pixel_values
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase			)	->      Dict:


        '''simple docstring'''


        lowercase_	:				List[Any]													=	FlaxViTModel(config=__UpperCamelCase			)
        lowercase_	:				Dict													=	model(__UpperCamelCase			)
        # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
        lowercase_	:				Union[str, Any]													=	(self.image_size, self.image_size)
        lowercase_	:				List[Any]													=	(self.patch_size, self.patch_size)
        lowercase_	:				List[str]													=	(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
        self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, num_patches + 1, self.hidden_size)			)
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase			)	->      Union[str, Any]:


        '''simple docstring'''


        lowercase_	:				List[Any]													=	self.type_sequence_label_size
        lowercase_	:				str													=	FlaxViTForImageClassification(config=__UpperCamelCase			)
        lowercase_	:				Optional[Any]													=	model(__UpperCamelCase			)
        self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.type_sequence_label_size)			)

        # test greyscale images
        lowercase_	:				Union[str, Any]													=	1
        lowercase_	:				Optional[int]													=	FlaxViTForImageClassification(__UpperCamelCase			)

        lowercase_	:				List[Any]													=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
        lowercase_	:				str													=	model(__UpperCamelCase			)







   def 			_UpperCAmelCase   (			self			)	->      Any:


        '''simple docstring'''


        lowercase_	:				Optional[int]													=	self.prepare_config_and_inputs()
        (
            (
            lowercase_
        )							,  (
            lowercase_
        )							,  
        )	:				List[Any]													=	config_and_inputs
        lowercase_	:				Tuple													=	{'pixel_values': pixel_values}
        return config, inputs_dict







@require_flax
class        UpperCamelCase		(			lowercase_						,      unittest.TestCase  ):
   lowercase							=  (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()
   def 			_UpperCAmelCase   (			self			)	->      None:


        '''simple docstring'''


        lowercase_	:				Optional[Any]													=	FlaxViTModelTester(self			)
        lowercase_	:				Union[str, Any]													=	ConfigTester(self     ,config_class=__UpperCamelCase     ,has_text_modality=__UpperCamelCase     ,hidden_size=37			)
   def 			_UpperCAmelCase   (			self			)	->      List[str]:


        '''simple docstring'''


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


        '''simple docstring'''


        lowercase_	:				Optional[Any]													=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__UpperCamelCase			)
   def 			_UpperCAmelCase   (			self			)	->      Optional[Any]:


        '''simple docstring'''


        lowercase_	:				List[Any]													=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase			)
   def 			_UpperCAmelCase   (			self			)	->      List[Any]:


        '''simple docstring'''


        lowercase_							,  lowercase_	:				Any													=	self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase_	:				Optional[Any]													=	model_class(__UpperCamelCase			)
             lowercase_	:				Tuple													=	inspect.signature(model.__call__			)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             lowercase_	:				Union[str, Any]													=	[*signature.parameters.keys()]

             lowercase_	:				str													=	['pixel_values']
             self.assertListEqual(arg_names[:1]     ,__UpperCamelCase			)
   def 			_UpperCAmelCase   (			self			)	->      Optional[Any]:


        '''simple docstring'''


        lowercase_							,  lowercase_	:				str													=	self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             with self.subTest(model_class.__name__			):
                  lowercase_	:				Optional[Any]													=	self._prepare_for_class(__UpperCamelCase     ,__UpperCamelCase			)
                  lowercase_	:				Dict													=	model_class(__UpperCamelCase			)

                  @jax.jit
                  def model_jitted(__UpperCamelCase     ,**__UpperCamelCase			):
                       return model(pixel_values=__UpperCamelCase     ,**__UpperCamelCase			)

                  with self.subTest('JIT Enabled'			):
                       lowercase_	:				Optional[int]													=	model_jitted(**__UpperCamelCase			).to_tuple()

                  with self.subTest('JIT Disabled'			):
                       with jax.disable_jit():
                            lowercase_	:				List[str]													=	model_jitted(**__UpperCamelCase			).to_tuple()

                  self.assertEqual(len(__UpperCamelCase			)     ,len(__UpperCamelCase			)			)
                  for jitted_output, output in zip(__UpperCamelCase     ,__UpperCamelCase			):
                       self.assertEqual(jitted_output.shape     ,output.shape			)







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


        '''simple docstring'''


        for model_class_name in self.all_model_classes:
             lowercase_	:				Optional[int]													=	model_class_name.from_pretrained('google/vit-base-patch16-224'			)
             lowercase_	:				int													=	model(np.ones((1, 3, 224, 224)			)			)
             self.assertIsNotNone(__UpperCamelCase			)

