code: 
  
  
  
  
  '''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 ,  a__ ,  a__=2 ,  a__=3_2 ,  a__=1_6 ,  a__=3 ,  a__=True ,  a__=True ,  a__=3_2 ,  a__=4 ,  a__=[0, 1, 2, 3] ,  a__=4 ,  a__=3_7 ,  a__="gelu" ,  a__=0.1 ,  a__=0.1 ,  a__=0.02 ,  a__=3 ,  a__=False ,  a__=None ,  )       ->   Dict:
           SCREAMING_SNAKE_CASE__			:				Optional[int]       						= parent
           SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= batch_size
           SCREAMING_SNAKE_CASE__			:				List[Any]       						= image_size
           SCREAMING_SNAKE_CASE__			:				List[str]       						= patch_size
           SCREAMING_SNAKE_CASE__			:				List[Any]       						= num_channels
           SCREAMING_SNAKE_CASE__			:				List[Any]       						= is_training
           SCREAMING_SNAKE_CASE__			:				List[str]       						= use_labels
           SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= hidden_size
           SCREAMING_SNAKE_CASE__			:				Dict       						= num_hidden_layers
           SCREAMING_SNAKE_CASE__			:				List[str]       						= backbone_out_indices
           SCREAMING_SNAKE_CASE__			:				List[str]       						= num_attention_heads
           SCREAMING_SNAKE_CASE__			:				Any       						= intermediate_size
           SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= hidden_act
           SCREAMING_SNAKE_CASE__			:				str       						= hidden_dropout_prob
           SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= attention_probs_dropout_prob
           SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= initializer_range
           SCREAMING_SNAKE_CASE__			:				Dict       						= num_labels
           SCREAMING_SNAKE_CASE__			:				Any       						= scope
           SCREAMING_SNAKE_CASE__			:				Dict       						= is_hybrid
           # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
           SCREAMING_SNAKE_CASE__			:				List[str]       						= (image_size // patch_size) ** 2
           SCREAMING_SNAKE_CASE__			:				List[str]       						= num_patches + 1
  
  
  
  
  
  
        def    A__(self					)       ->   Dict:
           SCREAMING_SNAKE_CASE__			:				List[str]       						= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)
  
           SCREAMING_SNAKE_CASE__			:				List[Any]       						= None
           if self.use_labels:
              SCREAMING_SNAKE_CASE__			:				Tuple       						= ids_tensor([self.batch_size, self.image_size, self.image_size] ,  self.num_labels					)
  
           SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= self.get_config()
  
           return config, pixel_values, labels
  
  
  
  
  
  
        def    A__(self					)       ->   str:
           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 ,  )
  
  
  
  
  
  
        def    A__(self ,  a__ ,  a__ ,  a__					)       ->   str:
           SCREAMING_SNAKE_CASE__			:				Tuple       						= DPTModel(config=a__					)
           model.to(a__					)
           model.eval()
           SCREAMING_SNAKE_CASE__			:				int       						= model(a__					)
           self.parent.assertEqual(result.last_hidden_state.shape ,  (self.batch_size, self.seq_length, self.hidden_size)					)
  
  
  
  
  
  
        def    A__(self ,  a__ ,  a__ ,  a__					)       ->   Tuple:
           SCREAMING_SNAKE_CASE__			:				List[Any]       						= self.num_labels
           SCREAMING_SNAKE_CASE__			:				List[str]       						= DPTForDepthEstimation(a__					)
           model.to(a__					)
           model.eval()
           SCREAMING_SNAKE_CASE__			:				Dict       						= model(a__					)
           self.parent.assertEqual(result.predicted_depth.shape ,  (self.batch_size, self.image_size, self.image_size)					)
  
  
  
  
  
  
        def    A__(self ,  a__ ,  a__ ,  a__					)       ->   str:
           SCREAMING_SNAKE_CASE__			:				Dict       						= self.num_labels
           SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= DPTForSemanticSegmentation(a__					)
           model.to(a__					)
           model.eval()
           SCREAMING_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    A__(self					)       ->   List[Any]:
           SCREAMING_SNAKE_CASE__			:				int       						= self.prepare_config_and_inputs()
           SCREAMING_SNAKE_CASE__						,				SCREAMING_SNAKE_CASE__						,				SCREAMING_SNAKE_CASE__			:				int       						= config_and_inputs
           SCREAMING_SNAKE_CASE__			:				List[str]       						= {'''pixel_values''': pixel_values}
           return config, inputs_dict
  
  
  
  
  
  
  @require_torch
  class 						lowercase     (   UpperCAmelCase_				,			UpperCAmelCase_				,			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    A__(self					)       ->   List[Any]:
           SCREAMING_SNAKE_CASE__			:				str       						= DPTModelTester(self					)
           SCREAMING_SNAKE_CASE__			:				Tuple       						= ConfigTester(self ,  config_class=a__ ,  has_text_modality=a__ ,  hidden_size=3_7					)
  
  
  
  
  
  
        def    A__(self					)       ->   int:
           self.config_tester.run_common_tests()
  
  
  
  
  
  
        @unittest.skip(reason='''DPT does not use inputs_embeds'''					)
        def    A__(self					)       ->   Tuple:
           pass
  
  
  
  
  
  
        def    A__(self					)       ->   Optional[int]:
           SCREAMING_SNAKE_CASE__						,				SCREAMING_SNAKE_CASE__			:				int       						= self.model_tester.prepare_config_and_inputs_for_common()
  
           for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE__			:				Optional[int]       						= model_class(a__					)
              self.assertIsInstance(model.get_input_embeddings() ,  (nn.Module)					)
              SCREAMING_SNAKE_CASE__			:				Optional[int]       						= model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(a__ ,  nn.Linear					)					)
  
  
  
  
  
  
        def    A__(self					)       ->   Union[str, Any]:
           SCREAMING_SNAKE_CASE__						,				SCREAMING_SNAKE_CASE__			:				List[str]       						= self.model_tester.prepare_config_and_inputs_for_common()
  
           for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= model_class(a__					)
              SCREAMING_SNAKE_CASE__			:				List[Any]       						= inspect.signature(model.forward					)
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= [*signature.parameters.keys()]
  
              SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= ['''pixel_values''']
              self.assertListEqual(arg_names[:1] ,  a__					)
  
  
  
  
  
  
        def    A__(self					)       ->   List[Any]:
           SCREAMING_SNAKE_CASE__			:				Optional[int]       						= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*a__					)
  
  
  
  
  
  
        def    A__(self					)       ->   Optional[Any]:
           SCREAMING_SNAKE_CASE__			:				str       						= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_depth_estimation(*a__					)
  
  
  
  
  
  
        def    A__(self					)       ->   int:
           SCREAMING_SNAKE_CASE__			:				List[Any]       						= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_semantic_segmentation(*a__					)
  
  
  
  
  
  
        def    A__(self					)       ->   str:
           for model_class in self.all_model_classes:
              if model_class.__name__ == "DPTForDepthEstimation":
                 continue
  
              SCREAMING_SNAKE_CASE__						,				SCREAMING_SNAKE_CASE__			:				str       						= self.model_tester.prepare_config_and_inputs_for_common()
              SCREAMING_SNAKE_CASE__			:				int       						= True
  
              if model_class in get_values(a__					):
                 continue
  
              SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= model_class(a__					)
              model.to(a__					)
              model.train()
              SCREAMING_SNAKE_CASE__			:				Dict       						= self._prepare_for_class(a__ ,  a__ ,  return_labels=a__					)
              SCREAMING_SNAKE_CASE__			:				str       						= model(**a__					).loss
              loss.backward()
  
  
  
  
  
  
        def    A__(self					)       ->   int:
           for model_class in self.all_model_classes:
              if model_class.__name__ == "DPTForDepthEstimation":
                 continue
  
              SCREAMING_SNAKE_CASE__						,				SCREAMING_SNAKE_CASE__			:				str       						= self.model_tester.prepare_config_and_inputs_for_common()
              SCREAMING_SNAKE_CASE__			:				int       						= False
              SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= True
  
              if model_class in get_values(a__					) or not model_class.supports_gradient_checkpointing:
                 continue
              SCREAMING_SNAKE_CASE__			:				int       						= model_class(a__					)
              model.to(a__					)
              model.gradient_checkpointing_enable()
              model.train()
              SCREAMING_SNAKE_CASE__			:				Dict       						= self._prepare_for_class(a__ ,  a__ ,  return_labels=a__					)
              SCREAMING_SNAKE_CASE__			:				Optional[int]       						= model(**a__					).loss
              loss.backward()
  
  
  
  
  
  
        def    A__(self					)       ->   Union[str, Any]:
           SCREAMING_SNAKE_CASE__						,				SCREAMING_SNAKE_CASE__			:				Tuple       						= self.model_tester.prepare_config_and_inputs_for_common()
  
           SCREAMING_SNAKE_CASE__			:				Dict       						= _config_zero_init(a__					)
           for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE__			:				Any       						= model_class(config=a__					)
              # Skip the check for the backbone
              SCREAMING_SNAKE_CASE__			:				Tuple       						= []
              for name, module in model.named_modules():
                 if module.__class__.__name__ == "DPTViTHybridEmbeddings":
                    SCREAMING_SNAKE_CASE__			:				int       						= [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    A__(self					)       ->   List[str]:
           pass
  
  
  
  
  
  
        @slow
        def    A__(self					)       ->   str:
           for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE__			:				List[str]       						= DPTModel.from_pretrained(a__					)
              self.assertIsNotNone(a__					)
  
  
  def 				UpperCamelCase	(  ):
  
  
  
  
     '''simple docstring'''
  
     SCREAMING_SNAKE_CASE__			:				List[Any]       						= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
     return image
  
  
  
  
  
  
  
  @require_torch
  @require_vision
  @slow
  class 						lowercase     (   unittest.TestCase):
  
  
  
  
  
  
        def    A__(self					)       ->   Optional[Any]:
           SCREAMING_SNAKE_CASE__			:				Any       						= DPTImageProcessor.from_pretrained('''Intel/dpt-large'''					)
           SCREAMING_SNAKE_CASE__			:				Dict       						= DPTForDepthEstimation.from_pretrained('''Intel/dpt-large'''					).to(a__					)
  
           SCREAMING_SNAKE_CASE__			:				List[Any]       						= prepare_img()
           SCREAMING_SNAKE_CASE__			:				Optional[int]       						= image_processor(images=a__ ,  return_tensors='''pt'''					).to(a__					)
  
           # forward pass
           with torch.no_grad():
              SCREAMING_SNAKE_CASE__			:				List[str]       						= model(**a__					)
              SCREAMING_SNAKE_CASE__			:				Optional[int]       						= outputs.predicted_depth
  
           # verify the predicted depth
           SCREAMING_SNAKE_CASE__			:				List[str]       						= torch.Size((1, 3_8_4, 3_8_4)					)
           self.assertEqual(predicted_depth.shape ,  a__					)
  
           SCREAMING_SNAKE_CASE__			:				Optional[int]       						= torch.tensor(
               [[6.31_99, 6.36_29, 6.41_48], [6.38_50, 6.36_15, 6.41_66], [6.35_19, 6.31_76, 6.35_75]]					).to(a__					)
  
           self.assertTrue(torch.allclose(outputs.predicted_depth[0, :3, :3] ,  a__ ,  atol=1E-4					)					)
  
  
  
  
  
  
        def    A__(self					)       ->   Optional[int]:
           SCREAMING_SNAKE_CASE__			:				Dict       						= DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''					)
           SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''					).to(a__					)
  
           SCREAMING_SNAKE_CASE__			:				int       						= prepare_img()
           SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= image_processor(images=a__ ,  return_tensors='''pt'''					).to(a__					)
  
           # forward pass
           with torch.no_grad():
              SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= model(**a__					)
  
           # verify the logits
           SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= torch.Size((1, 1_5_0, 4_8_0, 4_8_0)					)
           self.assertEqual(outputs.logits.shape ,  a__					)
  
           SCREAMING_SNAKE_CASE__			:				str       						= torch.tensor(
               [[4.04_80, 4.24_20, 4.43_60], [4.31_24, 4.56_93, 4.82_61], [4.57_68, 4.89_65, 5.21_63]]					).to(a__					)
  
           self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] ,  a__ ,  atol=1E-4					)					)
  
  
  
  
  
  
        def    A__(self					)       ->   Dict:
           SCREAMING_SNAKE_CASE__			:				List[str]       						= DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''					)
           SCREAMING_SNAKE_CASE__			:				Tuple       						= DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''					).to(a__					)
  
           SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= prepare_img()
           SCREAMING_SNAKE_CASE__			:				Dict       						= image_processor(images=a__ ,  return_tensors='''pt'''					).to(a__					)
  
           # forward pass
           with torch.no_grad():
              SCREAMING_SNAKE_CASE__			:				Optional[int]       						= model(**a__					)
  
           SCREAMING_SNAKE_CASE__			:				List[Any]       						= outputs.logits.detach().cpu()
  
           SCREAMING_SNAKE_CASE__			:				Optional[int]       						= image_processor.post_process_semantic_segmentation(outputs=a__ ,  target_sizes=[(5_0_0, 3_0_0)]					)
           SCREAMING_SNAKE_CASE__			:				List[Any]       						= torch.Size((5_0_0, 3_0_0)					)
           self.assertEqual(segmentation[0].shape ,  a__					)
  
           SCREAMING_SNAKE_CASE__			:				str       						= image_processor.post_process_semantic_segmentation(outputs=a__					)
           SCREAMING_SNAKE_CASE__			:				str       						= torch.Size((4_8_0, 4_8_0)					)
           self.assertEqual(segmentation[0].shape ,  a__					)
  
  
style_context: 
  
  
  
  
  '''simple docstring'''
  
  
  
  
  
  import argparse
  import json
  import re
  import string
  import sys
  from collections import Counter
  
  
  def 				UpperCamelCase	(  snake_case   ):
  
  
  
  
     '''simple docstring'''
  
     def remove_articles(snake_case   ):
        return re.sub(R'''\b(a|an|the)\b'''	,     ''' '''	,     snake_case   )
  
     def white_space_fix(snake_case   ):
        return " ".join(text.split()   )
  
     def remove_punc(snake_case   ):
        SCREAMING_SNAKE_CASE__			:				Optional[int]       						= set(string.punctuation   )
        return "".join(ch for ch in text if ch not in exclude   )
  
     def lower(snake_case   ):
        return text.lower()
  
     return white_space_fix(remove_articles(remove_punc(lower(snake_case   )   )   )   )
  
  
  def 				UpperCamelCase	(  snake_case	,     snake_case   ):
  
  
  
  
     '''simple docstring'''
  
     SCREAMING_SNAKE_CASE__			:				Tuple       						= normalize_answer(snake_case   ).split()
     SCREAMING_SNAKE_CASE__			:				List[Any]       						= normalize_answer(snake_case   ).split()
     SCREAMING_SNAKE_CASE__			:				Dict       						= Counter(snake_case   ) & Counter(snake_case   )
     SCREAMING_SNAKE_CASE__			:				int       						= sum(common.values()   )
     if num_same == 0:
        return 0
     SCREAMING_SNAKE_CASE__			:				Tuple       						= 1.0 * num_same / len(snake_case   )
     SCREAMING_SNAKE_CASE__			:				Tuple       						= 1.0 * num_same / len(snake_case   )
     SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= (2 * precision * recall) / (precision + recall)
     return fa
  
  
  def 				UpperCamelCase	(  snake_case	,     snake_case   ):
  
  
  
  
     '''simple docstring'''
  
     return normalize_answer(snake_case   ) == normalize_answer(snake_case   )
  
  
  def 				UpperCamelCase	(  snake_case	,     snake_case	,     snake_case   ):
  
  
  
  
     '''simple docstring'''
  
     SCREAMING_SNAKE_CASE__			:				Any       						= []
     for ground_truth in ground_truths:
        SCREAMING_SNAKE_CASE__			:				List[Any]       						= metric_fn(snake_case	,     snake_case   )
        scores_for_ground_truths.append(snake_case   )
     return max(snake_case   )
  
  
  def 				UpperCamelCase	(  snake_case	,     snake_case   ):
  
  
  
  
     '''simple docstring'''
  
     SCREAMING_SNAKE_CASE__			:				str       						= 0
     SCREAMING_SNAKE_CASE__			:				Optional[int]       						= []
     for passage in dataset:
        for qa in passage["qas"]:
           total += 1
           if qa["id"] not in predictions:
              SCREAMING_SNAKE_CASE__			:				Any       						= f"""Unanswered question {qa["id"]} will receive score 0."""
              print(snake_case	,     file=sys.stderr   )
              continue
  
           SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= [x['''text'''] for x in qa['''answers''']]
           SCREAMING_SNAKE_CASE__			:				Optional[int]       						= predictions[qa['''id''']]
  
           SCREAMING_SNAKE_CASE__			:				List[str]       						= metric_max_over_ground_truths(snake_case	,     snake_case	,     snake_case   )
           if int(_exact_match   ) == 1:
              correct_ids.append(qa['''id''']   )
           exact_match += _exact_match
  
           fa += metric_max_over_ground_truths(snake_case	,     snake_case	,     snake_case   )
  
     SCREAMING_SNAKE_CASE__			:				int       						= exact_match / total
     SCREAMING_SNAKE_CASE__			:				Optional[int]       						= fa / total
  
     return {"exact_match": exact_match, "f1": fa}, correct_ids
  
  
  if __name__ == "__main__":
         lowercase__			=	'''1.0'''
         lowercase__			=	argparse.ArgumentParser('''Official evaluation script for ReCoRD v1.0.''')
         parser.add_argument('''data_file''', help='''The dataset file in JSON format.''')
         parser.add_argument('''pred_file''', help='''The model prediction file in JSON format.''')
         parser.add_argument('''--output_correct_ids''', action='''store_true''', help='''Output the correctly answered query IDs.''')
         lowercase__			=	parser.parse_args()
  
         with open(args.data_file) as data_file:
                lowercase__			=	json.load(data_file)
                if dataset_json["version"] != expected_version:
                       print(
                           F"Evaluation expects v-{expected_version}, but got dataset with v-{dataset_json['version']}",
                           file=sys.stderr,
                       )
                lowercase__			=	dataset_json['''data''']
  
         with open(args.pred_file) as pred_file:
                lowercase__			=	json.load(pred_file)
  
         lowercase__	,     lowercase__			=	evaluate(dataset, predictions)
  
         if args.output_correct_ids:
                print(F"Output {len(correct_ids)} correctly answered question IDs.")
                with open('''correct_ids.json''', '''w''') as f:
                       json.dump(correct_ids, f)
  
  