





from sklearn.metrics import fa_score

import datasets


UpperCamelCase_												=	'\nThe F1 score is the harmonic mean of the precision and recall. It can be computed with the equation:\nF1 = 2 * (precision * recall) / (precision + recall)\n'


UpperCamelCase_												=	'\nArgs:\n    predictions (`list` of `int`): Predicted labels.\n    references (`list` of `int`): Ground truth labels.\n    labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None.\n    pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1.\n    average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`.\n\n        - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary.\n        - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives.\n        - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n        - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall.\n        - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n    sample_weight (`list` of `float`): Sample weights Defaults to None.\n\nReturns:\n    f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better.\n\nExamples:\n\n    Example 1-A simple binary example\n        >>> f1_metric = datasets.load_metric("f1")\n        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0])\n        >>> print(results)\n        {\'f1\': 0.5}\n\n    Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`.\n        >>> f1_metric = datasets.load_metric("f1")\n        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0)\n        >>> print(round(results[\'f1\'], 2))\n        0.67\n\n    Example 3-The same simple binary example as in Example 1, but with `sample_weight` included.\n        >>> f1_metric = datasets.load_metric("f1")\n        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3])\n        >>> print(round(results[\'f1\'], 2))\n        0.35\n\n    Example 4-A multiclass example, with different values for the `average` input.\n        >>> predictions = [0, 2, 1, 0, 0, 1]\n        >>> references = [0, 1, 2, 0, 1, 2]\n        >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro")\n        >>> print(round(results[\'f1\'], 2))\n        0.27\n        >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro")\n        >>> print(round(results[\'f1\'], 2))\n        0.33\n        >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted")\n        >>> print(round(results[\'f1\'], 2))\n        0.27\n        >>> results = f1_metric.compute(predictions=predictions, references=references, average=None)\n        >>> print(results)\n        {\'f1\': array([0.8, 0. , 0. ])}\n'


UpperCamelCase_												=	'\n@article{scikit-learn,\n    title={Scikit-learn: Machine Learning in {P}ython},\n    author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n           and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n           and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n           Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n    journal={Journal of Machine Learning Research},\n    volume={12},\n    pages={2825--2830},\n    year={2011}\n}\n'

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION,		_KWARGS_DESCRIPTION							)
class     snake_case_		(   datasets.Metric							):






 '''simple docstring'''





 def  __UpperCAmelCase						(  self		)	->       Any:
    return datasets.MetricInfo(
        description=_DESCRIPTION,		citation=_CITATION,		inputs_description=_KWARGS_DESCRIPTION,		features=datasets.Features(
            {
                "predictions": datasets.Sequence(datasets.Value("int32"		)		),
                "references": datasets.Sequence(datasets.Value("int32"		)		),
            }
            if self.config_name == "multilabel"
            else {
                "predictions": datasets.Value("int32"		),
                "references": datasets.Value("int32"		),
            }		),		reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"],		)






 def  __UpperCAmelCase						(  self,		A_,		A_,		A_=None,		A_=1,		A_="binary",		A_=None		)	->       List[str]:
    UpperCAmelCase__						=fa_score(
        A_,		A_,		labels=A_,		pos_label=A_,		average=A_,		sample_weight=A_		)
    return {"f1": float(A_		) if score.size == 1 else score}










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     snake_case_		:






 '''simple docstring'''





 def __init__(  self,		A_,		A_=2,		A_=32,		A_=16,		A_=3,		A_=True,		A_=True,		A_=32,		A_=4,		A_=[0, 1, 2, 3],		A_=4,		A_=37,		A_="gelu",		A_=0.1,		A_=0.1,		A_=0.02,		A_=3,		A_=[1, 384, 24, 24],		A_=True,		A_=None,		)	->       Optional[int]:
    UpperCAmelCase__						=parent
    UpperCAmelCase__						=batch_size
    UpperCAmelCase__						=image_size
    UpperCAmelCase__						=patch_size
    UpperCAmelCase__						=num_channels
    UpperCAmelCase__						=is_training
    UpperCAmelCase__						=use_labels
    UpperCAmelCase__						=hidden_size
    UpperCAmelCase__						=num_hidden_layers
    UpperCAmelCase__						=backbone_out_indices
    UpperCAmelCase__						=num_attention_heads
    UpperCAmelCase__						=intermediate_size
    UpperCAmelCase__						=hidden_act
    UpperCAmelCase__						=hidden_dropout_prob
    UpperCAmelCase__						=attention_probs_dropout_prob
    UpperCAmelCase__						=initializer_range
    UpperCAmelCase__						=num_labels
    UpperCAmelCase__						=backbone_featmap_shape
    UpperCAmelCase__						=scope
    UpperCAmelCase__						=is_hybrid
    # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
    UpperCAmelCase__						=(image_size // patch_size) ** 2
    UpperCAmelCase__						=num_patches + 1

 def  __UpperCAmelCase						(  self		)	->       str:
    UpperCAmelCase__						=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

    UpperCAmelCase__						=self.get_config()

    return config, pixel_values, labels

 def  __UpperCAmelCase						(  self		)	->       Dict:
    UpperCAmelCase__						={
        "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=A_,		initializer_range=self.initializer_range,		is_hybrid=self.is_hybrid,		backbone_config=A_,		backbone_featmap_shape=self.backbone_featmap_shape,		)

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_		)	->       Optional[Any]:
    UpperCAmelCase__						=DPTModel(config=A_		)
    model.to(A_		)
    model.eval()
    UpperCAmelCase__						=model(A_		)
    self.parent.assertEqual(result.last_hidden_state.shape,		(self.batch_size, self.seq_length, self.hidden_size)		)

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_		)	->       Union[str, Any]:
    UpperCAmelCase__						=self.num_labels
    UpperCAmelCase__						=DPTForDepthEstimation(A_		)
    model.to(A_		)
    model.eval()
    UpperCAmelCase__						=model(A_		)
    self.parent.assertEqual(result.predicted_depth.shape,		(self.batch_size, self.image_size, self.image_size)		)

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_		)	->       Optional[Any]:
    UpperCAmelCase__						=self.num_labels
    UpperCAmelCase__						=DPTForSemanticSegmentation(A_		)
    model.to(A_		)
    model.eval()
    UpperCAmelCase__						=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		)	->       int:
    UpperCAmelCase__						=self.prepare_config_and_inputs()
    UpperCAmelCase__			,							UpperCAmelCase__			,							UpperCAmelCase__						=config_and_inputs
    UpperCAmelCase__						={"pixel_values": pixel_values}
    return config, inputs_dict

@require_torch
class     snake_case_		(   a,		a,		unittest.TestCase							):






 '''simple docstring'''





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

 __UpperCamelCase       =      False
 __UpperCamelCase       =      False
 __UpperCamelCase       =      False

 def  __UpperCAmelCase						(  self		)	->       int:
    UpperCAmelCase__						=DPTModelTester(self		)
    UpperCAmelCase__						=ConfigTester(self,		config_class=A_,		has_text_modality=A_,		hidden_size=37		)

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

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

 def  __UpperCAmelCase						(  self		)	->       str:
    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       UpperCAmelCase__						=model_class(A_		)
       self.assertIsInstance(model.get_input_embeddings(),		(nn.Module)		)
       UpperCAmelCase__						=model.get_output_embeddings()
       self.assertTrue(x is None or isinstance(A_,		nn.Linear		)		)

 def  __UpperCAmelCase						(  self		)	->       Optional[int]:
    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()

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

       UpperCAmelCase__						=["pixel_values"]
       self.assertListEqual(arg_names[:1],		A_		)

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*A_		)

 def  __UpperCAmelCase						(  self		)	->       str:
    UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_depth_estimation(*A_		)

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_semantic_segmentation(*A_		)

 def  __UpperCAmelCase						(  self		)	->       Any:
    for model_class in self.all_model_classes:
       if model_class.__name__ == "DPTForDepthEstimation":
          continue

       UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()
       UpperCAmelCase__						=True

       if model_class in get_values(A_		):
          continue

       UpperCAmelCase__						=model_class(A_		)
       model.to(A_		)
       model.train()
       UpperCAmelCase__						=self._prepare_for_class(A_,		A_,		return_labels=A_		)
       UpperCAmelCase__						=model(**A_		).loss
       loss.backward()

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    for model_class in self.all_model_classes:
       if model_class.__name__ == "DPTForDepthEstimation":
          continue

       UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()
       UpperCAmelCase__						=False
       UpperCAmelCase__						=True

       if model_class in get_values(A_		) or not model_class.supports_gradient_checkpointing:
          continue
       UpperCAmelCase__						=model_class(A_		)
       model.to(A_		)
       model.gradient_checkpointing_enable()
       model.train()
       UpperCAmelCase__						=self._prepare_for_class(A_,		A_,		return_labels=A_		)
       UpperCAmelCase__						=model(**A_		).loss
       loss.backward()

 def  __UpperCAmelCase						(  self		)	->       int:
    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()

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

 @slow
 def  __UpperCAmelCase						(  self		)	->       Optional[int]:
    for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]:
       UpperCAmelCase__						=DPTModel.from_pretrained(A_		)
       self.assertIsNotNone(A_		)






 def  __UpperCAmelCase						(  self		)	->       Any:
    # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type
    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()
    UpperCAmelCase__						="add"
    with self.assertRaises(A_		):
       UpperCAmelCase__						=DPTForDepthEstimation(A_		)






def 				_UpperCAmelCase			(    ):




   '''simple docstring'''

   UpperCAmelCase__						=Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"     )
   return image




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






 '''simple docstring'''





 def  __UpperCAmelCase						(  self		)	->       Union[str, Any]:
    UpperCAmelCase__						=DPTImageProcessor.from_pretrained("Intel/dpt-hybrid-midas"		)
    UpperCAmelCase__						=DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas"		).to(A_		)

    UpperCAmelCase__						=prepare_img()
    UpperCAmelCase__						=image_processor(images=A_,		return_tensors="pt"		).to(A_		)

    # forward pass
    with torch.no_grad():
       UpperCAmelCase__						=model(**A_		)
       UpperCAmelCase__						=outputs.predicted_depth

    # verify the predicted depth
    UpperCAmelCase__						=torch.Size((1, 384, 384)		)
    self.assertEqual(predicted_depth.shape,		A_		)

    UpperCAmelCase__						=torch.tensor(
        [[[5.64_37, 5.61_46, 5.65_11], [5.43_71, 5.56_49, 5.59_58], [5.52_15, 5.51_84, 5.52_93]]]		).to(A_		)

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



