



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




	'''simple docstring'''





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

				"""simple docstring"""

				__lowerCAmelCase :    List[str]				=			parent
				__lowerCAmelCase :    Tuple				=			batch_size
				__lowerCAmelCase :    List[Any]				=			image_size
				__lowerCAmelCase :    Dict				=			patch_size
				__lowerCAmelCase :    List[str]				=			num_channels
				__lowerCAmelCase :    Tuple				=			is_training
				__lowerCAmelCase :    Tuple				=			use_labels
				__lowerCAmelCase :    Tuple				=			hidden_size
				__lowerCAmelCase :    int				=			num_hidden_layers
				__lowerCAmelCase :    int				=			backbone_out_indices
				__lowerCAmelCase :    Optional[int]				=			num_attention_heads
				__lowerCAmelCase :    Optional[int]				=			intermediate_size
				__lowerCAmelCase :    Optional[Any]				=			hidden_act
				__lowerCAmelCase :    List[Any]				=			hidden_dropout_prob
				__lowerCAmelCase :    str				=			attention_probs_dropout_prob
				__lowerCAmelCase :    Tuple				=			initializer_range
				__lowerCAmelCase :    List[Any]				=			num_labels
				__lowerCAmelCase :    str				=			backbone_featmap_shape
				__lowerCAmelCase :    List[str]				=			scope
				__lowerCAmelCase :    str				=			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 :    Any				=			num_patches + 1

	def 				_SCREAMING_SNAKE_CASE  (	self:						Tuple)							->			int:

				"""simple docstring"""

				__lowerCAmelCase :    List[Any]				=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

				__lowerCAmelCase :    Union[str, Any]				=			self.get_config()

				return config, pixel_values, labels

	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[int])							->			Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    List[str]				=			{
				    "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=_SCREAMING_SNAKE_CASE			,     initializer_range=self.initializer_range			,     is_hybrid=self.is_hybrid			,     backbone_config=_SCREAMING_SNAKE_CASE			,     backbone_featmap_shape=self.backbone_featmap_shape			,     )

	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[Any]			,     _SCREAMING_SNAKE_CASE:						Dict			,     _SCREAMING_SNAKE_CASE:						List[Any]			,     _SCREAMING_SNAKE_CASE:						Union[str, Any])							->			int:

				"""simple docstring"""

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

	def 				_SCREAMING_SNAKE_CASE  (	self:						Union[str, Any]			,     _SCREAMING_SNAKE_CASE:						Tuple			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]			,     _SCREAMING_SNAKE_CASE:						int)							->			Tuple:

				"""simple docstring"""

				__lowerCAmelCase :    Optional[Any]				=			self.num_labels
				__lowerCAmelCase :    Dict				=			DPTForDepthEstimation(_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase :    Optional[int]				=			model(_SCREAMING_SNAKE_CASE)
				self.parent.assertEqual(result.predicted_depth.shape			,     (self.batch_size, self.image_size, self.image_size))

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

				"""simple docstring"""

				__lowerCAmelCase :    List[str]				=			self.num_labels
				__lowerCAmelCase :    Any				=			DPTForSemanticSegmentation(_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase :    Optional[int]				=			model(_SCREAMING_SNAKE_CASE			,     labels=_SCREAMING_SNAKE_CASE)
				self.parent.assertEqual(
				    result.logits.shape			,     (self.batch_size, self.num_labels, self.image_size, self.image_size))







	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[int])							->			Any:

				"""simple docstring"""

				__lowerCAmelCase :    Optional[int]				=			self.prepare_config_and_inputs()
				__lowerCAmelCase		,      __lowerCAmelCase		,      __lowerCAmelCase :    str				=			config_and_inputs
				__lowerCAmelCase :    Optional[int]				=			{"pixel_values": pixel_values}
				return config, inputs_dict






@require_torch
class      A__  (    __SCREAMING_SNAKE_CASE    ,						__SCREAMING_SNAKE_CASE    ,						unittest.TestCase				):




	'''simple docstring'''





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

	SCREAMING_SNAKE_CASE        =      False
	SCREAMING_SNAKE_CASE        =      False
	SCREAMING_SNAKE_CASE        =      False

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

				"""simple docstring"""

				__lowerCAmelCase :    str				=			DPTModelTester(self)
				__lowerCAmelCase :    Tuple				=			ConfigTester(self			,     config_class=_SCREAMING_SNAKE_CASE			,     has_text_modality=_SCREAMING_SNAKE_CASE			,     hidden_size=37)

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

				"""simple docstring"""

				self.config_tester.run_common_tests()

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

				"""simple docstring"""

				pass

	def 				_SCREAMING_SNAKE_CASE  (	self:						int)							->			List[Any]:

				"""simple docstring"""

				__lowerCAmelCase		,      __lowerCAmelCase :    Dict				=			self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							__lowerCAmelCase :    Optional[int]				=			model_class(_SCREAMING_SNAKE_CASE)
							self.assertIsInstance(model.get_input_embeddings()			,     (nn.Module))
							__lowerCAmelCase :    List[str]				=			model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE			,     nn.Linear))

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

				"""simple docstring"""

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

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

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

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

				"""simple docstring"""

				__lowerCAmelCase :    List[Any]				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Dict)							->			int:

				"""simple docstring"""

				__lowerCAmelCase :    Optional[int]				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_depth_estimation(*_SCREAMING_SNAKE_CASE)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Any)							->			Any:

				"""simple docstring"""

				__lowerCAmelCase :    Tuple				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_semantic_segmentation(*_SCREAMING_SNAKE_CASE)

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

				"""simple docstring"""

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

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

							if model_class in get_values(_SCREAMING_SNAKE_CASE):
										continue

							__lowerCAmelCase :    List[str]				=			model_class(_SCREAMING_SNAKE_CASE)
							model.to(_SCREAMING_SNAKE_CASE)
							model.train()
							__lowerCAmelCase :    str				=			self._prepare_for_class(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE			,     return_labels=_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase :    List[str]				=			model(**_SCREAMING_SNAKE_CASE).loss
							loss.backward()

	def 				_SCREAMING_SNAKE_CASE  (	self:						int)							->			Optional[Any]:

				"""simple docstring"""

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

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

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

	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[int])							->			Any:

				"""simple docstring"""

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

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

				"""simple docstring"""

				pass

	@slow
	def 				_SCREAMING_SNAKE_CASE  (	self:						str)							->			Optional[Any]:

				"""simple docstring"""

				for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]:
							__lowerCAmelCase :    Optional[int]				=			DPTModel.from_pretrained(_SCREAMING_SNAKE_CASE)
							self.assertIsNotNone(_SCREAMING_SNAKE_CASE)







	def 				_SCREAMING_SNAKE_CASE  (	self:						Tuple)							->			int:

				"""simple docstring"""

				__lowerCAmelCase		,      __lowerCAmelCase :    Optional[int]				=			self.model_tester.prepare_config_and_inputs_for_common()
				__lowerCAmelCase :    Optional[int]				=			"add"
				with self.assertRaises(_SCREAMING_SNAKE_CASE):
							__lowerCAmelCase :    List[str]				=			DPTForDepthEstimation(_SCREAMING_SNAKE_CASE)






def      _lowercase							(    )		->  int:
			__lowerCAmelCase :    Dict				=			Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
			return image





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




	'''simple docstring'''





	def 				_SCREAMING_SNAKE_CASE  (	self:						int)							->			Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    int				=			DPTImageProcessor.from_pretrained("Intel/dpt-hybrid-midas")
				__lowerCAmelCase :    Optional[Any]				=			DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase :    str				=			prepare_img()
				__lowerCAmelCase :    Tuple				=			image_processor(images=_SCREAMING_SNAKE_CASE			,     return_tensors="pt").to(_SCREAMING_SNAKE_CASE)

				# forward pass
				with torch.no_grad():
							__lowerCAmelCase :    int				=			model(**_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase :    int				=			outputs.predicted_depth

				# verify the predicted depth
				__lowerCAmelCase :    str				=			torch.Size((1, 384, 384))
				self.assertEqual(predicted_depth.shape			,     _SCREAMING_SNAKE_CASE)

				__lowerCAmelCase :    Any				=			torch.tensor(
				    [[[5.6437, 5.6146, 5.6511], [5.4371, 5.5649, 5.5958], [5.5215, 5.5184, 5.5293]]]).to(_SCREAMING_SNAKE_CASE)

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




"""simple docstring"""






from ...configuration_utils import PretrainedConfig
from ...utils import logging


__snake_case				:  Dict =       logging.get_logger(__name__)

__snake_case				:  int =       {
    'uclanlp/visualbert-vqa': 'https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json',
    'uclanlp/visualbert-vqa-pre': 'https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json',
    'uclanlp/visualbert-vqa-coco-pre': (
        'https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json'
    ),
    'uclanlp/visualbert-vcr': 'https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json',
    'uclanlp/visualbert-vcr-pre': 'https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json',
    'uclanlp/visualbert-vcr-coco-pre': (
        'https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json'
    ),
    'uclanlp/visualbert-nlvr2': 'https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json',
    'uclanlp/visualbert-nlvr2-pre': 'https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json',
    'uclanlp/visualbert-nlvr2-coco-pre': (
        'https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json'
    )
    # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert
}



class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE        =      'visual_bert'

	def __init__(	self:						int			,     _SCREAMING_SNAKE_CASE:						Dict=3_0522			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]=768			,     _SCREAMING_SNAKE_CASE:						List[str]=512			,     _SCREAMING_SNAKE_CASE:						Optional[int]=12			,     _SCREAMING_SNAKE_CASE:						List[str]=12			,     _SCREAMING_SNAKE_CASE:						Tuple=3072			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]="gelu"			,     _SCREAMING_SNAKE_CASE:						Tuple=0.1			,     _SCREAMING_SNAKE_CASE:						str=0.1			,     _SCREAMING_SNAKE_CASE:						List[Any]=512			,     _SCREAMING_SNAKE_CASE:						Any=2			,     _SCREAMING_SNAKE_CASE:						Optional[int]=0.02			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]=1e-12			,     _SCREAMING_SNAKE_CASE:						List[str]=False			,     _SCREAMING_SNAKE_CASE:						int=True			,     _SCREAMING_SNAKE_CASE:						str=1			,     _SCREAMING_SNAKE_CASE:						Tuple=0			,     _SCREAMING_SNAKE_CASE:						Any=2			,     **_SCREAMING_SNAKE_CASE:						List[str]			,     )							->			str:

				"""simple docstring"""

				super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE			,     bos_token_id=_SCREAMING_SNAKE_CASE			,     eos_token_id=_SCREAMING_SNAKE_CASE			,     **_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase :    str				=			vocab_size
				__lowerCAmelCase :    Tuple				=			max_position_embeddings
				__lowerCAmelCase :    Any				=			hidden_size
				__lowerCAmelCase :    Union[str, Any]				=			visual_embedding_dim
				__lowerCAmelCase :    List[str]				=			num_hidden_layers
				__lowerCAmelCase :    Optional[int]				=			num_attention_heads
				__lowerCAmelCase :    str				=			intermediate_size
				__lowerCAmelCase :    int				=			hidden_act
				__lowerCAmelCase :    Union[str, Any]				=			hidden_dropout_prob
				__lowerCAmelCase :    int				=			attention_probs_dropout_prob
				__lowerCAmelCase :    List[Any]				=			initializer_range
				__lowerCAmelCase :    Dict				=			type_vocab_size
				__lowerCAmelCase :    Optional[int]				=			layer_norm_eps
				__lowerCAmelCase :    Union[str, Any]				=			bypass_transformer
				__lowerCAmelCase :    List[str]				=			special_visual_initialize