




"""simple docstring"""




from typing import Dict, Optional

import numpy as np

import datasets


lowerCAmelCase				:     List[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.
"""

lowerCAmelCase				:     Tuple					=				"""
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.        ])}
"""

lowerCAmelCase				:     Optional[Any]					=				"""\
@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        a__					(							snake_case__			,					snake_case__			,					snake_case__			,					snake_case__			,					snake_case__ = None			,					snake_case__ = False			,					)		->   Optional[int]:
			if label_map is not None:
						for old_id, new_id in label_map.items():
									lowerCamelCase      =   new_id

    # turn into Numpy arrays
			lowerCamelCase      =   np.array(snake_case__							)
			lowerCamelCase      =   np.array(snake_case__							)

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

			lowerCamelCase      =   label != ignore_index
			lowerCamelCase      =   np.not_equal(snake_case__			,					snake_case__							)
			lowerCamelCase      =   pred_label[mask]
			lowerCamelCase      =   np.array(snake_case__							)[mask]

			lowerCamelCase      =   pred_label[pred_label == label]

			lowerCamelCase      =   np.histogram(snake_case__			,					bins=snake_case__			,					range=(0, num_labels - 1)							)[0]
			lowerCamelCase      =   np.histogram(snake_case__			,					bins=snake_case__			,					range=(0, num_labels - 1)							)[0]
			lowerCamelCase      =   np.histogram(snake_case__			,					bins=snake_case__			,					range=(0, num_labels - 1)							)[0]

			lowerCamelCase      =   area_pred_label + area_label - area_intersect

			return area_intersect, area_union, area_pred_label, area_label



def        a__					(							snake_case__			,					snake_case__			,					snake_case__			,					snake_case__			,					snake_case__ = None			,					snake_case__ = False			,					)		->   Tuple:
			lowerCamelCase      =   np.zeros((num_labels,)			,					dtype=np.floataa							)
			lowerCamelCase      =   np.zeros((num_labels,)			,					dtype=np.floataa							)
			lowerCamelCase      =   np.zeros((num_labels,)			,					dtype=np.floataa							)
			lowerCamelCase      =   np.zeros((num_labels,)			,					dtype=np.floataa							)
			for result, gt_seg_map in zip(snake_case__			,					snake_case__							):
						lowerCamelCase ,  lowerCamelCase ,  lowerCamelCase ,  lowerCamelCase      =   intersect_and_union(
						    snake_case__			,					snake_case__			,					snake_case__			,					snake_case__			,					snake_case__			,					snake_case__							)
						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        a__					(							snake_case__			,					snake_case__			,					snake_case__			,					snake_case__			,					snake_case__ = None			,					snake_case__ = None			,					snake_case__ = False			,					)		->   Union[str, Any]:
			lowerCamelCase ,  lowerCamelCase ,  lowerCamelCase ,  lowerCamelCase      =   total_intersect_and_union(
			    snake_case__			,					snake_case__			,					snake_case__			,					snake_case__			,					snake_case__			,					snake_case__							)

			# compute metrics
			lowerCamelCase      =   {}

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

			lowerCamelCase      =   np.nanmean(snake_case__							)
			lowerCamelCase      =   np.nanmean(snake_case__							)
			lowerCamelCase      =   all_acc
			lowerCamelCase      =   iou
			lowerCamelCase      =   acc

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

			return metrics






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






				'''simple docstring'''





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							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						,					):





							"""simple docstring"""
							lowerCamelCase      =   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 collections
import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


lowerCAmelCase				:     Any					=				logging.get_logger(__name__)

lowerCAmelCase				:     int					=				"""▁"""

lowerCAmelCase				:     str					=				{"""vocab_file""": """prophetnet.tokenizer"""}

lowerCAmelCase				:     Union[str, Any]					=				{
    """vocab_file""": {
        """microsoft/xprophetnet-large-wiki100-cased""": (
            """https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer"""
        ),
    }
}

lowerCAmelCase				:     Any					=				{
    """microsoft/xprophetnet-large-wiki100-cased""": {"""do_lower_case""": False},
}

lowerCAmelCase				:     List[Any]					=				{
    """microsoft/xprophetnet-large-wiki100-cased""": 512,
}



def        a__					(							snake_case__							)		->   int:
			lowerCamelCase      =   collections.OrderedDict()
			with open(snake_case__			,					"""r"""			,					encoding="""utf-8"""							) as reader:
						lowerCamelCase      =   reader.readlines()
			for index, token in enumerate(snake_case__							):
						lowerCamelCase      =   token.rstrip("""\n"""							)
						lowerCamelCase      =   index
			return vocab






class        __magic_name__				(			UpperCAmelCase__							):






				'''simple docstring'''
				__UpperCamelCase         =		VOCAB_FILES_NAMES
				__UpperCamelCase         =		PRETRAINED_VOCAB_FILES_MAP
				__UpperCamelCase         =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				__UpperCamelCase         =		["input_ids", "attention_mask"]





				def __init__(			self						,					_a						,					_a="[SEP]"						,					_a="[SEP]"						,					_a="[SEP]"						,					_a="[UNK]"						,					_a="[PAD]"						,					_a="[CLS]"						,					_a="[MASK]"						,					_a = None						,					**_a						,					):





							"""simple docstring"""
							lowerCamelCase      =   {} if sp_model_kwargs is None else sp_model_kwargs

							super().__init__(
							    bos_token=_a						,					eos_token=_a						,					sep_token=_a						,					unk_token=_a						,					pad_token=_a						,					cls_token=_a						,					mask_token=_a						,					sp_model_kwargs=self.sp_model_kwargs						,					**_a						,					)

							try:
										import sentencepiece as spm
							except ImportError:
										logger.warning(
										    """You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece"""
										    """ pip install sentencepiece"""					)
										raise

							lowerCamelCase      =   spm.SentencePieceProcessor(**self.sp_model_kwargs					)
							self.sp_model.Load(str(_a					)					)
							lowerCamelCase      =   vocab_file

							# Original fairseq vocab and spm vocab must be "aligned":
							# Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
							# -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
							# fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
							# spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

							# put special tokens and [unused] tokens into the vocab
							lowerCamelCase      =   {"""[PAD]""": 0, """[CLS]""": 1, """[SEP]""": 2, """[UNK]""": 3, """[MASK]""": 4}

							for i in range(10					):
										lowerCamelCase      =   f'[unused{i}]'
										lowerCamelCase      =   5 + i

							# The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab
							lowerCamelCase      =   12
							lowerCamelCase      =   {v: k for k, v in self.fairseq_tokens_to_ids.items()}
							for k in self.fairseq_tokens_to_ids.keys():
										self.unique_no_split_tokens.append(_a					)





				def __getstate__(			self					):





							"""simple docstring"""
							lowerCamelCase      =   self.__dict__.copy()
							lowerCamelCase      =   None
							return state





				def __setstate__(			self						,					_a					):





							"""simple docstring"""
							lowerCamelCase      =   d
							try:
										import sentencepiece as spm
							except ImportError:
										logger.warning(
										    """You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece"""
										    """ pip install sentencepiece"""					)
										raise

							# for backward compatibility
							if not hasattr(self						,					"""sp_model_kwargs"""					):
										lowerCamelCase      =   {}

							lowerCamelCase      =   spm.SentencePieceProcessor(**self.sp_model_kwargs					)
							self.sp_model.Load(self.vocab_file					)





				def   _lowerCAmelCase							(			self						,					_a						,					_a = None						,					_a = False					):





							"""simple docstring"""

							if already_has_special_tokens:
										return super().get_special_tokens_mask(
										    token_ids_a=_a						,					token_ids_a=_a						,					already_has_special_tokens=_a					)

							if token_ids_a is None:
										return ([0] * len(_a					)) + [1]
							return ([0] * len(_a					)) + [1] + ([0] * len(_a					)) + [1]





				def   _lowerCAmelCase							(			self						,					_a						,					_a = None					):





							"""simple docstring"""
							lowerCamelCase      =   [self.sep_token_id]

							if token_ids_a is None:
										return len(token_ids_a + sep					) * [0]
							return len(token_ids_a + sep + sep + token_ids_a + sep					) * [0]





				@property
				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							return len(self.sp_model					) + self.fairseq_offset





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   {self.convert_ids_to_tokens(_a					): i for i in range(self.vocab_size					)}
							vocab.update(self.added_tokens_encoder					)
							return vocab





				def   _lowerCAmelCase							(			self						,					_a					):





							"""simple docstring"""
							return self.sp_model.encode(_a						,					out_type=_a					)





				def   _lowerCAmelCase							(			self						,					_a					):





							"""simple docstring"""
							if token in self.fairseq_tokens_to_ids:
										return self.fairseq_tokens_to_ids[token]
							lowerCamelCase      =   self.sp_model.PieceToId(_a					)

							# Need to return unknown token if the SP model returned 0
							return spm_id + self.fairseq_offset if spm_id else self.unk_token_id





				def   _lowerCAmelCase							(			self						,					_a					):





							"""simple docstring"""
							if index in self.fairseq_ids_to_tokens:
										return self.fairseq_ids_to_tokens[index]
							return self.sp_model.IdToPiece(index - self.fairseq_offset					)





				def   _lowerCAmelCase							(			self						,					_a					):





							"""simple docstring"""
							lowerCamelCase      =   """""".join(_a					).replace(_a						,					""" """					).strip()
							return out_string





				def   _lowerCAmelCase							(			self						,					_a						,					_a = None					):





							"""simple docstring"""
							if not os.path.isdir(_a					):
										logger.error(f'Vocabulary path ({save_directory}) should be a directory'					)
										return
							lowerCamelCase      =   os.path.join(
							    _a						,					(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]					)

							if os.path.abspath(self.vocab_file					) != os.path.abspath(_a					) and os.path.isfile(self.vocab_file					):
										copyfile(self.vocab_file						,					_a					)
							elif not os.path.isfile(self.vocab_file					):
										with open(_a						,					"""wb"""					) as fi:
													lowerCamelCase      =   self.sp_model.serialized_model_proto()
													fi.write(_a					)

							return (out_vocab_file,)





				def   _lowerCAmelCase							(			self						,					_a						,					_a = None					):





							"""simple docstring"""

							if token_ids_a is None:
										return token_ids_a + [self.sep_token_id]
							lowerCamelCase      =   [self.sep_token_id]
							return token_ids_a + sep + token_ids_a + sep



