





'''simple docstring'''

from typing import Dict, Optional

import numpy as np

import datasets


a   	=    "\nIoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union\nbetween the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation,\nthe mean IoU of the image is calculated by taking the IoU of each class and averaging them.\n"

a   	=    "\nArgs:\n    predictions (`List[ndarray]`):\n        List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n    references (`List[ndarray]`):\n        List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n    num_labels (`int`):\n        Number of classes (categories).\n    ignore_index (`int`):\n        Index that will be ignored during evaluation.\n    nan_to_num (`int`, *optional*):\n        If specified, NaN values will be replaced by the number defined by the user.\n    label_map (`dict`, *optional*):\n        If specified, dictionary mapping old label indices to new label indices.\n    reduce_labels (`bool`, *optional*, defaults to `False`):\n        Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background,\n        and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255.\n\nReturns:\n    `Dict[str, float | ndarray]` comprising various elements:\n    - *mean_iou* (`float`):\n        Mean Intersection-over-Union (IoU averaged over all categories).\n    - *mean_accuracy* (`float`):\n        Mean accuracy (averaged over all categories).\n    - *overall_accuracy* (`float`):\n        Overall accuracy on all images.\n    - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`):\n        Per category accuracy.\n    - *per_category_iou* (`ndarray` of shape `(num_labels,)`):\n        Per category IoU.\n\nExamples:\n\n    >>> import numpy as np\n\n    >>> mean_iou = datasets.load_metric(\"mean_iou\")\n\n    >>> # suppose one has 3 different segmentation maps predicted\n    >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]])\n    >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]])\n\n    >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]])\n    >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]])\n\n    >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]])\n    >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]])\n\n    >>> predicted = [predicted_1, predicted_2, predicted_3]\n    >>> ground_truth = [actual_1, actual_2, actual_3]\n\n    >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False)\n    >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n    {'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.        ])}\n"

a   	=    "\\n@software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020,\nauthor = {{MMSegmentation Contributors}},\nlicense = {Apache-2.0},\nmonth = {7},\ntitle = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}},\nurl = {https://github.com/open-mmlab/mmsegmentation},\nyear = {2020}\n}"

def     __magic_name__							(				__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase = None				,						__UpperCAmelCase = False				,						)    ->       List[Any]:

   '''simple docstring'''







   if label_map is not None:
      for old_id, new_id in label_map.items():
         __SCREAMING_SNAKE_CASE   				=							new_id

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

   if reduce_labels:
      __SCREAMING_SNAKE_CASE   				=							255
      __SCREAMING_SNAKE_CASE   				=							label - 1
      __SCREAMING_SNAKE_CASE   				=							255

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

   __SCREAMING_SNAKE_CASE   				=							pred_label[pred_label == label]

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

   __SCREAMING_SNAKE_CASE   				=							area_pred_label + area_label - area_intersect

   return area_intersect, area_union, area_pred_label, area_label

def     __magic_name__							(				__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase = None				,						__UpperCAmelCase = False				,						)    ->       Optional[int]:

   '''simple docstring'''







   __SCREAMING_SNAKE_CASE   				=							np.zeros((num_labels,)				,						dtype=np.floataa   )
   __SCREAMING_SNAKE_CASE   				=							np.zeros((num_labels,)				,						dtype=np.floataa   )
   __SCREAMING_SNAKE_CASE   				=							np.zeros((num_labels,)				,						dtype=np.floataa   )
   __SCREAMING_SNAKE_CASE   				=							np.zeros((num_labels,)				,						dtype=np.floataa   )
   for result, gt_seg_map in zip(__UpperCAmelCase				,						__UpperCAmelCase   ):
      __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE   				=							intersect_and_union(
          __UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase   )
      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     __magic_name__							(				__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase = None				,						__UpperCAmelCase = None				,						__UpperCAmelCase = False				,						)    ->       Dict:

   '''simple docstring'''







   __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE   				=							total_intersect_and_union(
       __UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase				,						__UpperCAmelCase   )

   # compute metrics
   __SCREAMING_SNAKE_CASE   				=							{}

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

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

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

   return metrics







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





   def   UpperCAmelCase__  (       self					:      Any     ):

      '''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   UpperCAmelCase__  (       self					:      int					,lowerCamelCase					:      Optional[int]					,lowerCamelCase					:      Any					,lowerCamelCase					:      int					,lowerCamelCase					:      bool					,lowerCamelCase					:      Optional[int] = None					,lowerCamelCase					:      Optional[Dict[int, int]] = None					,lowerCamelCase					:      bool = False					,):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							mean_iou(
          results=lowerCamelCase					,gt_seg_maps=lowerCamelCase					,num_labels=lowerCamelCase					,ignore_index=lowerCamelCase					,nan_to_num=lowerCamelCase					,label_map=lowerCamelCase					,reduce_labels=lowerCamelCase					,)
      return iou_result










import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


UpperCAmelCase_		 =					logging.get_logger(__name__)

UpperCAmelCase_		 =					"""▁"""

UpperCAmelCase_		 =					{"""vocab_file""": """sentencepiece.bpe.model""", """monolingual_vocab_file""": """dict.txt"""}

UpperCAmelCase_		 =					{
    """vocab_file""": {
        """vinai/bartpho-syllable""": """https://huggingface.co/vinai/bartpho-syllable/resolve/main/sentencepiece.bpe.model""",
    },
    """monolingual_vocab_file""": {
        """vinai/bartpho-syllable""": """https://huggingface.co/vinai/bartpho-syllable/resolve/main/dict.txt""",
    },
}

UpperCAmelCase_		 =					{"""vinai/bartpho-syllable""": 1_0_2_4}


class 	lowerCamelCase__	(       _A):



 """simple docstring"""

 a__					: int =  VOCAB_FILES_NAMES
 a__					: Tuple =  PRETRAINED_VOCAB_FILES_MAP
 a__					: Optional[int] =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 a__					: Tuple =  ["input_ids", "attention_mask"]

 def __init__(    self					:					Union[str, Any]			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					Union[str, Any]="<s>"			,						__lowerCAmelCase					:					Dict="</s>"			,						__lowerCAmelCase					:					List[Any]="</s>"			,						__lowerCAmelCase					:					Optional[Any]="<s>"			,						__lowerCAmelCase					:					Tuple="<unk>"			,						__lowerCAmelCase					:					int="<pad>"			,						__lowerCAmelCase					:					Optional[Any]="<mask>"			,						__lowerCAmelCase					:					Optional[Dict[str, Any]] = None			,						**__lowerCAmelCase					:					Tuple			,						)   ->	None:
    # Mask token behave like a normal word, i.e. include the space before it
    _A      =       AddedToken(__lowerCAmelCase			,						lstrip=__lowerCAmelCase			,						rstrip=__lowerCAmelCase      ) if isinstance(__lowerCAmelCase			,						__lowerCAmelCase      ) else mask_token

    _A      =       {} if sp_model_kwargs is None else sp_model_kwargs

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

    _A      =       vocab_file
    _A      =       monolingual_vocab_file
    _A      =       spm.SentencePieceProcessor(**self.sp_model_kwargs      )
    self.sp_model.Load(str(__lowerCAmelCase      )      )

    # Load the reduced vocab

    # Keep order of special tokens for backward compatibility
    _A      =       {}
    _A      =       0
    for token in [bos_token, pad_token, eos_token, unk_token, sep_token, cls_token]:
       if str(__lowerCAmelCase      ) not in self.fairseq_tokens_to_ids:
          _A      =       cnt
          cnt += 1
    with open(__lowerCAmelCase			,						'''r'''			,						encoding='''utf-8'''      ) as f:
       for line in f.readlines():
          _A      =       line.strip().split()[0]
          _A      =       len(self.fairseq_tokens_to_ids      )
    if str(__lowerCAmelCase      ) not in self.fairseq_tokens_to_ids:
       _A      =       len(self.fairseq_tokens_to_ids      )

    _A      =       {v: k for k, v in self.fairseq_tokens_to_ids.items()}

 def __getstate__(    self					:					Any      )   ->	List[Any]:
    _A      =       self.__dict__.copy()
    _A      =       None
    _A      =       self.sp_model.serialized_model_proto()
    return state

 def __setstate__(    self					:					Union[str, Any]			,						__lowerCAmelCase					:					Dict      )   ->	List[Any]:
    _A      =       d

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

    _A      =       spm.SentencePieceProcessor(**self.sp_model_kwargs      )
    self.sp_model.LoadFromSerializedProto(self.sp_model_proto      )

 def     snake_case_      (    self					:					Optional[Any]			,						__lowerCAmelCase					:					List[int]			,						__lowerCAmelCase					:					Optional[List[int]] = None      )   ->	List[int]:

    if token_ids_a is None:
       return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
    _A      =       [self.cls_token_id]
    _A      =       [self.sep_token_id]
    return cls + token_ids_a + sep + sep + token_ids_a + sep

 def     snake_case_      (    self					:					List[Any]			,						__lowerCAmelCase					:					List[int]			,						__lowerCAmelCase					:					Optional[List[int]] = None			,						__lowerCAmelCase					:					bool = False      )   ->	List[int]:

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

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

 def     snake_case_      (    self					:					Any			,						__lowerCAmelCase					:					List[int]			,						__lowerCAmelCase					:					Optional[List[int]] = None      )   ->	List[int]:
    _A      =       [self.sep_token_id]
    _A      =       [self.cls_token_id]

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

 @property
 def     snake_case_      (    self					:					Optional[int]      )   ->	Union[str, Any]:
    return len(self.fairseq_ids_to_tokens      )

 def     snake_case_      (    self					:					Dict      )   ->	Optional[Any]:
    _A      =       {self.convert_ids_to_tokens(__lowerCAmelCase      ): i for i in range(self.vocab_size      )}
    vocab.update(self.added_tokens_encoder      )
    return vocab

 def     snake_case_      (    self					:					List[str]			,						__lowerCAmelCase					:					str      )   ->	List[str]:
    return self.sp_model.encode(__lowerCAmelCase			,						out_type=__lowerCAmelCase      )

 def     snake_case_      (    self					:					str			,						__lowerCAmelCase					:					Optional[Any]      )   ->	Dict:
    if token in self.fairseq_tokens_to_ids:
       return self.fairseq_tokens_to_ids[token]
    else:
       return self.unk_token_id

 def     snake_case_      (    self					:					int			,						__lowerCAmelCase					:					Optional[int]      )   ->	List[str]:
    return self.fairseq_ids_to_tokens[index]

 def     snake_case_      (    self					:					List[str]			,						__lowerCAmelCase					:					Union[str, Any]      )   ->	Tuple:
    _A      =       ''''''.join(__lowerCAmelCase      ).replace(__lowerCAmelCase			,						''' '''      ).strip()
    return out_string

 def     snake_case_      (    self					:					str			,						__lowerCAmelCase					:					str			,						__lowerCAmelCase					:					Optional[str] = None      )   ->	Tuple[str]:
    if not os.path.isdir(__lowerCAmelCase      ):
       logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''      )
       return
    _A      =       os.path.join(
        __lowerCAmelCase			,						(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']      )
    _A      =       os.path.join(
        __lowerCAmelCase			,						(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''monolingual_vocab_file''']			,						)

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

    if os.path.abspath(self.monolingual_vocab_file      ) != os.path.abspath(
        __lowerCAmelCase      ) and os.path.isfile(self.monolingual_vocab_file      ):
       copyfile(self.monolingual_vocab_file			,						__lowerCAmelCase      )
    elif not os.path.isfile(self.monolingual_vocab_file      ):
       with open(__lowerCAmelCase			,						'''w'''			,						encoding='''utf-8'''      ) as fp:
          for token in self.fairseq_tokens_to_ids:
             if token not in self.all_special_tokens:
                fp.write(f'''{str(__lowerCAmelCase      )} \n'''      )

    return out_vocab_file, out_monolingual_vocab_file



