





'''simple docstring'''




from typing import Dict, Optional

import numpy as np

import datasets


_A:					Optional[Any]							=				"""
IoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union
between the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation,
the mean IoU of the image is calculated by taking the IoU of each class and averaging them.
"""

_A:					Optional[Any]							=				"""
Args:
    predictions (`List[ndarray]`):
        List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.
    references (`List[ndarray]`):
        List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.
    num_labels (`int`):
        Number of classes (categories).
    ignore_index (`int`):
        Index that will be ignored during evaluation.
    nan_to_num (`int`, *optional*):
        If specified, NaN values will be replaced by the number defined by the user.
    label_map (`dict`, *optional*):
        If specified, dictionary mapping old label indices to new label indices.
    reduce_labels (`bool`, *optional*, defaults to `False`):
        Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background,
        and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255.

Returns:
    `Dict[str, float | ndarray]` comprising various elements:
    - *mean_iou* (`float`):
        Mean Intersection-over-Union (IoU averaged over all categories).
    - *mean_accuracy* (`float`):
        Mean accuracy (averaged over all categories).
    - *overall_accuracy* (`float`):
        Overall accuracy on all images.
    - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`):
        Per category accuracy.
    - *per_category_iou* (`ndarray` of shape `(num_labels,)`):
        Per category IoU.

Examples:

    >>> import numpy as np

    >>> mean_iou = datasets.load_metric(\"mean_iou\")

    >>> # suppose one has 3 different segmentation maps predicted
    >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]])
    >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]])

    >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]])
    >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]])

    >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]])
    >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]])

    >>> predicted = [predicted_1, predicted_2, predicted_3]
    >>> ground_truth = [actual_1, actual_2, actual_3]

    >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False)
    >>> print(results) # doctest: +NORMALIZE_WHITESPACE
    {'mean_iou': 0.47750000000000004, 'mean_accuracy': 0.5916666666666666, 'overall_accuracy': 0.5263157894736842, 'per_category_iou': array([0.   , 0.   , 0.375, 0.4  , 0.5  , 0.   , 0.5  , 1.   , 1.   , 1.   ]), 'per_category_accuracy': array([0.        , 0.        , 0.75      , 0.66666667, 1.        , 0.        , 0.5       , 1.        , 1.        , 1.        ])}
"""

_A:					int							=				"""\
@software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020,
author = {{MMSegmentation Contributors}},
license = {Apache-2.0},
month = {7},
title = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}},
url = {https://github.com/open-mmlab/mmsegmentation},
year = {2020}
}"""







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase = None       ,    _lowerCAmelCase = False       ,    )->     Optional[int]:
 if label_map is not None:
  for old_id, new_id in label_map.items():
   __UpperCAmelCase					=   new_id

    # turn into Numpy arrays
 __UpperCAmelCase					=   np.array(_lowerCAmelCase    )
 __UpperCAmelCase					=   np.array(_lowerCAmelCase    )

 if reduce_labels:
  __UpperCAmelCase					=   2_55
  __UpperCAmelCase					=   label - 1
  __UpperCAmelCase					=   2_55

 __UpperCAmelCase					=   label != ignore_index
 __UpperCAmelCase					=   np.not_equal(_lowerCAmelCase       ,    _lowerCAmelCase    )
 __UpperCAmelCase					=   pred_label[mask]
 __UpperCAmelCase					=   np.array(_lowerCAmelCase    )[mask]

 __UpperCAmelCase					=   pred_label[pred_label == label]

 __UpperCAmelCase					=   np.histogram(_lowerCAmelCase       ,    bins=_lowerCAmelCase       ,    range=(0, num_labels - 1)    )[0]
 __UpperCAmelCase					=   np.histogram(_lowerCAmelCase       ,    bins=_lowerCAmelCase       ,    range=(0, num_labels - 1)    )[0]
 __UpperCAmelCase					=   np.histogram(_lowerCAmelCase       ,    bins=_lowerCAmelCase       ,    range=(0, num_labels - 1)    )[0]

 __UpperCAmelCase					=   area_pred_label + area_label - area_intersect

 return area_intersect, area_union, area_pred_label, area_label







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase = None       ,    _lowerCAmelCase = False       ,    )->     Union[str, Any]:
 __UpperCAmelCase					=   np.zeros((num_labels,)       ,    dtype=np.floataa    )
 __UpperCAmelCase					=   np.zeros((num_labels,)       ,    dtype=np.floataa    )
 __UpperCAmelCase					=   np.zeros((num_labels,)       ,    dtype=np.floataa    )
 __UpperCAmelCase					=   np.zeros((num_labels,)       ,    dtype=np.floataa    )
 for result, gt_seg_map in zip(_lowerCAmelCase       ,    _lowerCAmelCase    ):
  __UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					=   intersect_and_union(
      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )
  total_area_intersect += area_intersect
  total_area_union += area_union
  total_area_pred_label += area_pred_label
  total_area_label += area_label
 return total_area_intersect, total_area_union, total_area_pred_label, total_area_label







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase = None       ,    _lowerCAmelCase = None       ,    _lowerCAmelCase = False       ,    )->     Optional[Any]:
 __UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					=   total_intersect_and_union(
     _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )

 # compute metrics
 __UpperCAmelCase					=   {}

 __UpperCAmelCase					=   total_area_intersect.sum() / total_area_label.sum()
 __UpperCAmelCase					=   total_area_intersect / total_area_union
 __UpperCAmelCase					=   total_area_intersect / total_area_label

 __UpperCAmelCase					=   np.nanmean(_lowerCAmelCase    )
 __UpperCAmelCase					=   np.nanmean(_lowerCAmelCase    )
 __UpperCAmelCase					=   all_acc
 __UpperCAmelCase					=   iou
 __UpperCAmelCase					=   acc

 if nan_to_num is not None:
  __UpperCAmelCase					=   {metric: np.nan_to_num(_lowerCAmelCase       ,    nan=_lowerCAmelCase    ) for metric, metric_value in metrics.items()}

 return metrics






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



    def 					__lowerCamelCase						(  self					):
     return datasets.MetricInfo(
         description=_DESCRIPTION     ,			citation=_CITATION     ,			inputs_description=_KWARGS_DESCRIPTION     ,			features=datasets.Features(
             # 1st Seq - height dim, 2nd - width dim
             {
                 'predictions': datasets.Sequence(datasets.Sequence(datasets.Value('uint16'					)					)					),
                 'references': datasets.Sequence(datasets.Sequence(datasets.Value('uint16'					)					)					),
             }					)     ,			reference_urls=[
             'https://github.com/open-mmlab/mmsegmentation/blob/71c201b1813267d78764f306a297ca717827c4bf/mmseg/core/evaluation/metrics.py'
         ]     ,			)




    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A = None     ,			__A = None     ,			__A = False     ,			):
     __UpperCAmelCase					=   mean_iou(
         results=__A     ,			gt_seg_maps=__A     ,			num_labels=__A     ,			ignore_index=__A     ,			nan_to_num=__A     ,			label_map=__A     ,			reduce_labels=__A     ,			)
     return iou_result












'''simple docstring'''




import unittest

from transformers import DebertaVaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        DebertaVaForMaskedLM,
        DebertaVaForMultipleChoice,
        DebertaVaForQuestionAnswering,
        DebertaVaForSequenceClassification,
        DebertaVaForTokenClassification,
        DebertaVaModel,
    )
    from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST






class UpperCAmelCase							(		UpperCAmelCase_							):



    def __init__(  self     ,			__A     ,			__A=13     ,			__A=7     ,			__A=True     ,			__A=True     ,			__A=True     ,			__A=True     ,			__A=99     ,			__A=32     ,			__A=5     ,			__A=4     ,			__A=37     ,			__A="gelu"     ,			__A=0.1     ,			__A=0.1     ,			__A=512     ,			__A=16     ,			__A=2     ,			__A=0.0_2     ,			__A=False     ,			__A=True     ,			__A="None"     ,			__A=3     ,			__A=4     ,			__A=None     ,			):
     __UpperCAmelCase					=   parent
     __UpperCAmelCase					=   batch_size
     __UpperCAmelCase					=   seq_length
     __UpperCAmelCase					=   is_training
     __UpperCAmelCase					=   use_input_mask
     __UpperCAmelCase					=   use_token_type_ids
     __UpperCAmelCase					=   use_labels
     __UpperCAmelCase					=   vocab_size
     __UpperCAmelCase					=   hidden_size
     __UpperCAmelCase					=   num_hidden_layers
     __UpperCAmelCase					=   num_attention_heads
     __UpperCAmelCase					=   intermediate_size
     __UpperCAmelCase					=   hidden_act
     __UpperCAmelCase					=   hidden_dropout_prob
     __UpperCAmelCase					=   attention_probs_dropout_prob
     __UpperCAmelCase					=   max_position_embeddings
     __UpperCAmelCase					=   type_vocab_size
     __UpperCAmelCase					=   type_sequence_label_size
     __UpperCAmelCase					=   initializer_range
     __UpperCAmelCase					=   num_labels
     __UpperCAmelCase					=   num_choices
     __UpperCAmelCase					=   relative_attention
     __UpperCAmelCase					=   position_biased_input
     __UpperCAmelCase					=   pos_att_type
     __UpperCAmelCase					=   scope



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   ids_tensor([self.batch_size, self.seq_length]     ,			self.vocab_size					)

     __UpperCAmelCase					=   None
     if self.use_input_mask:
      __UpperCAmelCase					=   ids_tensor([self.batch_size, self.seq_length]     ,			vocab_size=2					)

     __UpperCAmelCase					=   None
     if self.use_token_type_ids:
      __UpperCAmelCase					=   ids_tensor([self.batch_size, self.seq_length]     ,			self.type_vocab_size					)

     __UpperCAmelCase					=   None
     __UpperCAmelCase					=   None
     __UpperCAmelCase					=   None
     if self.use_labels:
      __UpperCAmelCase					=   ids_tensor([self.batch_size]     ,			self.type_sequence_label_size					)
      __UpperCAmelCase					=   ids_tensor([self.batch_size, self.seq_length]     ,			self.num_labels					)
      __UpperCAmelCase					=   ids_tensor([self.batch_size]     ,			self.num_choices					)

     __UpperCAmelCase					=   self.get_config()

     return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



    def 					__lowerCamelCase						(  self					):
     return DebertaVaConfig(
         vocab_size=self.vocab_size     ,			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     ,			max_position_embeddings=self.max_position_embeddings     ,			type_vocab_size=self.type_vocab_size     ,			initializer_range=self.initializer_range     ,			relative_attention=self.relative_attention     ,			position_biased_input=self.position_biased_input     ,			pos_att_type=self.pos_att_type     ,			)



    def 					__lowerCamelCase						(  self     ,			__A					):
     self.parent.assertListEqual(list(result.loss.size()					)     ,			[]					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   DebertaVaModel(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(__A     ,			attention_mask=__A     ,			token_type_ids=__A					)[0]
     __UpperCAmelCase					=   model(__A     ,			token_type_ids=__A					)[0]
     __UpperCAmelCase					=   model(__A					)[0]

     self.parent.assertListEqual(list(sequence_output.size()					)     ,			[self.batch_size, self.seq_length, self.hidden_size]					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   DebertaVaForMaskedLM(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(__A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			labels=__A					)

     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.seq_length, self.vocab_size)					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   self.num_labels
     __UpperCAmelCase					=   DebertaVaForSequenceClassification(__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(__A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			labels=__A					)
     self.parent.assertListEqual(list(result.logits.size()					)     ,			[self.batch_size, self.num_labels]					)
     self.check_loss_output(__A					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   self.num_labels
     __UpperCAmelCase					=   DebertaVaForTokenClassification(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(__A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			labels=__A					)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.seq_length, self.num_labels)					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   DebertaVaForQuestionAnswering(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(
         __A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			start_positions=__A     ,			end_positions=__A     ,			)
     self.parent.assertEqual(result.start_logits.shape     ,			(self.batch_size, self.seq_length)					)
     self.parent.assertEqual(result.end_logits.shape     ,			(self.batch_size, self.seq_length)					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   DebertaVaForMultipleChoice(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   input_ids.unsqueeze(1					).expand(-1     ,			self.num_choices     ,			-1					).contiguous()
     __UpperCAmelCase					=   token_type_ids.unsqueeze(1					).expand(-1     ,			self.num_choices     ,			-1					).contiguous()
     __UpperCAmelCase					=   input_mask.unsqueeze(1					).expand(-1     ,			self.num_choices     ,			-1					).contiguous()
     __UpperCAmelCase					=   model(
         __A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			labels=__A     ,			)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_choices)					)




    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.prepare_config_and_inputs()
     (
         (
         __UpperCAmelCase
     )					,    (
         __UpperCAmelCase
     )					,    (
         __UpperCAmelCase
     )					,    (
         __UpperCAmelCase
     )					,    (
         __UpperCAmelCase
     )					,    (
         __UpperCAmelCase
     )					,    (
         __UpperCAmelCase
     )					,    
     )					=   config_and_inputs
     __UpperCAmelCase					=   {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
     return config, inputs_dict






@require_torch
class UpperCAmelCase							(		UpperCAmelCase_					,			UpperCAmelCase_					,			unittest.TestCase							):
    _A :			Optional[Any]						   =       (
        (
            DebertaVaModel,
            DebertaVaForMaskedLM,
            DebertaVaForSequenceClassification,
            DebertaVaForTokenClassification,
            DebertaVaForQuestionAnswering,
            DebertaVaForMultipleChoice,
        )
        if is_torch_available()
        else ()
    )
    _A :			int						   =       (
        {
            """feature-extraction""": DebertaVaModel,
            """fill-mask""": DebertaVaForMaskedLM,
            """question-answering""": DebertaVaForQuestionAnswering,
            """text-classification""": DebertaVaForSequenceClassification,
            """token-classification""": DebertaVaForTokenClassification,
            """zero-shot""": DebertaVaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )

    _A :			List[str]						   =       True
    _A :			Union[str, Any]						   =       False
    _A :			int						   =       False
    _A :			Dict						   =       False
    _A :			int						   =       False



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   DebertaVaModelTester(self					)
     __UpperCAmelCase					=   ConfigTester(self     ,			config_class=__A     ,			hidden_size=37					)



    def 					__lowerCamelCase						(  self					):
     self.config_tester.run_common_tests()



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_model(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_sequence_classification(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_masked_lm(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_question_answering(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_token_classification(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_multiple_choice(*__A					)




    @slow
    def 					__lowerCamelCase						(  self					):
     for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      __UpperCAmelCase					=   DebertaVaModel.from_pretrained(__A					)
      self.assertIsNotNone(__A					)






@require_torch
@require_sentencepiece
@require_tokenizers
class UpperCAmelCase							(		unittest.TestCase							):



    @unittest.skip(reason='Model not available yet'					)
    def 					__lowerCamelCase						(  self					):
     pass




    @slow
    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   DebertaVaModel.from_pretrained('microsoft/deberta-v2-xlarge'					)

     __UpperCAmelCase					=   torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]					)
     __UpperCAmelCase					=   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
     with torch.no_grad():
      __UpperCAmelCase					=   model(__A     ,			attention_mask=__A					)[0]
     # compare the actual values for a slice.
     __UpperCAmelCase					=   torch.tensor(
         [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]					)
     self.assertTrue(torch.allclose(output[:, 1:4, 1:4]     ,			__A     ,			atol=1E-4					)     ,			f'{output[:, 1:4, 1:4]}'					)





