






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


      def __init__(    self				:	Optional[int]							,   a				:	List[str]							,   a				:	str=2							,   a				:	Dict=3_2							,   a				:	Tuple=1_6							,   a				:	str=3							,   a				:	Union[str, Any]=True							,   a				:	Union[str, Any]=True							,   a				:	List[str]=3_2							,   a				:	Union[str, Any]=4							,   a				:	Optional[int]=[0, 1, 2, 3]							,   a				:	Dict=4							,   a				:	Optional[Any]=3_7							,   a				:	Tuple="gelu"							,   a				:	Optional[Any]=0.1							,   a				:	List[str]=0.1							,   a				:	Tuple=0.0_2							,   a				:	Any=3							,   a				:	Optional[int]=[1, 3_8_4, 2_4, 2_4]							,   a				:	int=True							,   a				:	Any=None							,   ):





            """simple docstring"""







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


      def _UpperCamelCase						(    self				:	Dict						):





            """simple docstring"""







            __snake_case      :    Optional[int]  			=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

            __snake_case      :    List[Any]  			=self.get_config()

            return config, pixel_values, labels


      def _UpperCamelCase						(    self				:	Optional[Any]						):





            """simple docstring"""







            __snake_case      :    Tuple  			={
                '''global_padding''': '''same''',
                '''layer_type''': '''bottleneck''',
                '''depths''': [3, 4, 9],
                '''out_features''': ['''stage1''', '''stage2''', '''stage3'''],
                '''embedding_dynamic_padding''': True,
                '''hidden_sizes''': [9_6, 1_9_2, 3_8_4, 7_6_8],
                '''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				:	List[Any]							,   a				:	Dict							,   a				:	Optional[Any]							,   a				:	int						):





            """simple docstring"""







            __snake_case      :    Tuple  			=DPTModel(config=a						)
            model.to(a						)
            model.eval()
            __snake_case      :    List[str]  			=model(a						)
            self.parent.assertEqual(result.last_hidden_state.shape							,   (self.batch_size, self.seq_length, self.hidden_size)						)


      def _UpperCamelCase						(    self				:	Optional[Any]							,   a				:	Optional[int]							,   a				:	List[Any]							,   a				:	List[str]						):





            """simple docstring"""







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


      def _UpperCamelCase						(    self				:	int							,   a				:	str							,   a				:	Union[str, Any]							,   a				:	Optional[int]						):





            """simple docstring"""







            __snake_case      :    str  			=self.num_labels
            __snake_case      :    Dict  			=DPTForSemanticSegmentation(a						)
            model.to(a						)
            model.eval()
            __snake_case      :    Optional[Any]  			=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				:	Tuple						):





            """simple docstring"""







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



@require_torch
class      _lowercase (     lowerCAmelCase		,  lowerCAmelCase		,  unittest.TestCase						):
      _a   :     str								=   (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
      _a   :     Dict								=   (
          {
              '''depth-estimation''': DPTForDepthEstimation,
              '''feature-extraction''': DPTModel,
              '''image-segmentation''': DPTForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      _a   :     Optional[int]								=   False
      _a   :     List[Any]								=   False
      _a   :     int								=   False


      def _UpperCamelCase						(    self				:	int						):





            """simple docstring"""







            __snake_case      :    Any  			=DPTModelTester(self						)
            __snake_case      :    int  			=ConfigTester(self							,   config_class=a							,   has_text_modality=a							,   hidden_size=3_7						)


      def _UpperCamelCase						(    self				:	int						):





            """simple docstring"""







            self.config_tester.run_common_tests()


      @unittest.skip(reason='''DPT does not use inputs_embeds'''						)
      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            pass


      def _UpperCamelCase						(    self				:	Any						):





            """simple docstring"""







            __snake_case      :    Union[str, Any]  			=self.model_tester.prepare_config_and_inputs_for_common()

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


      def _UpperCamelCase						(    self				:	int						):





            """simple docstring"""







            __snake_case      :    Dict  			=self.model_tester.prepare_config_and_inputs_for_common()

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

                  __snake_case      :    Dict  			=['''pixel_values''']
                  self.assertListEqual(arg_names[:1]							,   a						)


      def _UpperCamelCase						(    self				:	List[Any]						):





            """simple docstring"""







            __snake_case      :    List[str]  			=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*a						)


      def _UpperCamelCase						(    self				:	List[Any]						):





            """simple docstring"""







            __snake_case      :    Optional[int]  			=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_depth_estimation(*a						)


      def _UpperCamelCase						(    self				:	Optional[Any]						):





            """simple docstring"""







            __snake_case      :    List[Any]  			=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_semantic_segmentation(*a						)


      def _UpperCamelCase						(    self				:	Dict						):





            """simple docstring"""







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

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

                  if model_class in get_values(a						):
                        continue

                  __snake_case      :    Tuple  			=model_class(a						)
                  model.to(a						)
                  model.train()
                  __snake_case      :    List[str]  			=self._prepare_for_class(a							,   a							,   return_labels=a						)
                  __snake_case      :    int  			=model(**a						).loss
                  loss.backward()


      def _UpperCamelCase						(    self				:	str						):





            """simple docstring"""







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

                  __snake_case      :    Tuple  			=self.model_tester.prepare_config_and_inputs_for_common()
                  __snake_case      :    int  			=False
                  __snake_case      :    str  			=True

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


      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            __snake_case      :    Optional[Any]  			=self.model_tester.prepare_config_and_inputs_for_common()

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





            """simple docstring"""







            pass


      @slow
      def _UpperCamelCase						(    self				:	Optional[Any]						):





            """simple docstring"""







            for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]:
                  __snake_case      :    Dict  			=DPTModel.from_pretrained(a						)
                  self.assertIsNotNone(a						)







      def _UpperCamelCase						(    self				:	List[Any]						):





            """simple docstring"""







            __snake_case      :    int  			=self.model_tester.prepare_config_and_inputs_for_common()
            __snake_case      :    Tuple  			='''add'''
            with self.assertRaises(a						):
                  __snake_case      :    List[Any]  			=DPTForDepthEstimation(a						)



def     __lowercase					(    )					->					Any:
      __snake_case      :    Any  			=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
      return image


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


      def _UpperCamelCase						(    self				:	Union[str, Any]						):





            """simple docstring"""







            __snake_case      :    Tuple  			=DPTImageProcessor.from_pretrained('''Intel/dpt-hybrid-midas'''						)
            __snake_case      :    Dict  			=DPTForDepthEstimation.from_pretrained('''Intel/dpt-hybrid-midas'''						).to(a						)

            __snake_case      :    List[Any]  			=prepare_img()
            __snake_case      :    Optional[Any]  			=image_processor(images=a							,   return_tensors='''pt'''						).to(a						)

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

            # verify the predicted depth
            __snake_case      :    List[Any]  			=torch.Size((1, 3_8_4, 3_8_4)						)
            self.assertEqual(predicted_depth.shape							,   a						)

            __snake_case      :    Union[str, Any]  			=torch.tensor(
                [[[5.6_4_3_7, 5.6_1_4_6, 5.6_5_1_1], [5.4_3_7_1, 5.5_6_4_9, 5.5_9_5_8], [5.5_2_1_5, 5.5_1_8_4, 5.5_2_9_3]]]						).to(a						)

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










"""simple docstring"""





import unittest

import numpy as np

from transformers.testing_utils import is_flaky, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import DonutImageProcessor


class      _lowercase (     unittest.TestCase						):


      def __init__(    self				:	List[Any]							,   a				:	int							,   a				:	Dict=7							,   a				:	int=3							,   a				:	int=1_8							,   a				:	Dict=3_0							,   a				:	Dict=4_0_0							,   a				:	Optional[Any]=True							,   a				:	Dict=None							,   a				:	int=True							,   a				:	Dict=False							,   a				:	int=True							,   a				:	str=True							,   a				:	List[str]=[0.5, 0.5, 0.5]							,   a				:	Optional[Any]=[0.5, 0.5, 0.5]							,   ):





            """simple docstring"""







            __snake_case      :    List[Any]  			=parent
            __snake_case      :    List[Any]  			=batch_size
            __snake_case      :    str  			=num_channels
            __snake_case      :    Dict  			=image_size
            __snake_case      :    str  			=min_resolution
            __snake_case      :    Tuple  			=max_resolution
            __snake_case      :    str  			=do_resize
            __snake_case      :    Any  			=size if size is not None else {'''height''': 1_8, '''width''': 2_0}
            __snake_case      :    List[Any]  			=do_thumbnail
            __snake_case      :    Tuple  			=do_align_axis
            __snake_case      :    Any  			=do_pad
            __snake_case      :    Dict  			=do_normalize
            __snake_case      :    List[Any]  			=image_mean
            __snake_case      :    Any  			=image_std







      def _UpperCamelCase						(    self				:	str						):





            """simple docstring"""







            return {
                "do_resize": self.do_resize,
                "size": self.size,
                "do_thumbnail": self.do_thumbnail,
                "do_align_long_axis": self.do_align_axis,
                "do_pad": self.do_pad,
                "do_normalize": self.do_normalize,
                "image_mean": self.image_mean,
                "image_std": self.image_std,
            }



@require_torch
@require_vision
class      _lowercase (     lowerCAmelCase		,  unittest.TestCase						):
      _a   :     str								=   DonutImageProcessor if is_vision_available() else None


      def _UpperCamelCase						(    self				:	Optional[int]						):





            """simple docstring"""







            __snake_case      :    Optional[Any]  			=DonutImageProcessingTester(self						)


      @property
      def _UpperCamelCase						(    self				:	Optional[int]						):





            """simple docstring"""







            return self.image_processor_tester.prepare_image_processor_dict()


      def _UpperCamelCase						(    self				:	List[str]						):





            """simple docstring"""







            __snake_case      :    Optional[Any]  			=self.image_processing_class(**self.image_processor_dict						)
            self.assertTrue(hasattr(a							,   '''do_resize'''						)						)
            self.assertTrue(hasattr(a							,   '''size'''						)						)
            self.assertTrue(hasattr(a							,   '''do_thumbnail'''						)						)
            self.assertTrue(hasattr(a							,   '''do_align_long_axis'''						)						)
            self.assertTrue(hasattr(a							,   '''do_pad'''						)						)
            self.assertTrue(hasattr(a							,   '''do_normalize'''						)						)
            self.assertTrue(hasattr(a							,   '''image_mean'''						)						)
            self.assertTrue(hasattr(a							,   '''image_std'''						)						)


      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            __snake_case      :    Dict  			=self.image_processing_class.from_dict(self.image_processor_dict						)
            self.assertEqual(image_processor.size							,   {'''height''': 1_8, '''width''': 2_0}						)

            __snake_case      :    List[Any]  			=self.image_processing_class.from_dict(self.image_processor_dict							,   size=4_2						)
            self.assertEqual(image_processor.size							,   {'''height''': 4_2, '''width''': 4_2}						)

            # Previous config had dimensions in (width, height) order
            __snake_case      :    Tuple  			=self.image_processing_class.from_dict(self.image_processor_dict							,   size=(4_2, 8_4)						)
            self.assertEqual(image_processor.size							,   {'''height''': 8_4, '''width''': 4_2}						)


      def _UpperCamelCase						(    self				:	Any						):





            """simple docstring"""







            pass


      @is_flaky()
      def _UpperCamelCase						(    self				:	Union[str, Any]						):





            """simple docstring"""







            __snake_case      :    List[Any]  			=self.image_processing_class(**self.image_processor_dict						)
            # create random PIL images
            __snake_case      :    List[Any]  			=prepare_image_inputs(self.image_processor_tester							,   equal_resolution=a						)
            for image in image_inputs:
                  self.assertIsInstance(a							,   Image.Image						)

            # Test not batched input
            __snake_case      :    str  			=image_processing(image_inputs[0]							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    Optional[int]  			=image_processing(a							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )


      @is_flaky()
      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            __snake_case      :    Tuple  			=self.image_processing_class(**self.image_processor_dict						)
            # create random numpy tensors
            __snake_case      :    Optional[int]  			=prepare_image_inputs(self.image_processor_tester							,   equal_resolution=a							,   numpify=a						)
            for image in image_inputs:
                  self.assertIsInstance(a							,   np.ndarray						)

            # Test not batched input
            __snake_case      :    List[Any]  			=image_processing(image_inputs[0]							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    int  			=image_processing(a							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )







      @is_flaky()
      def _UpperCamelCase						(    self				:	List[str]						):





            """simple docstring"""







            __snake_case      :    Dict  			=self.image_processing_class(**self.image_processor_dict						)
            # create random PyTorch tensors
            __snake_case      :    Dict  			=prepare_image_inputs(self.image_processor_tester							,   equal_resolution=a							,   torchify=a						)
            for image in image_inputs:
                  self.assertIsInstance(a							,   torch.Tensor						)

            # Test not batched input
            __snake_case      :    Dict  			=image_processing(image_inputs[0]							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    Optional[int]  			=image_processing(a							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )


