"""simple docstring"""




import os
import re
import shutil
import sys
import tempfile
import unittest

import black


__lowerCamelCase											=  os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, "utils"))

import check_copies  # noqa: E402


# This is the reference code that will be used in the tests.
# If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated.
__lowerCamelCase											=  "    def __init__(self, config):\n        super().__init__()\n        self.transform = BertPredictionHeadTransform(config)\n\n        # The output weights are the same as the input embeddings, but there is\n        # an output-only bias for each token.\n        self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n        self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n        # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n        self.decoder.bias = self.bias\n\n    def forward(self, hidden_states):\n        hidden_states = self.transform(hidden_states)\n        hidden_states = self.decoder(hidden_states)\n        return hidden_states\n"
class     UpperCamelCase__(			unittest.TestCase ):





						def 				snake_case__	(       self     )					->				List[str]:
													A__	     =							tempfile.mkdtemp()
													os.makedirs(os.path.join(self.transformer_dir		,'models/bert/'     )     )
													A__	     =							self.transformer_dir
													shutil.copy(
													    os.path.join(__UpperCAmelCase		,'src/transformers/models/bert/modeling_bert.py'     )		,os.path.join(self.transformer_dir		,'models/bert/modeling_bert.py'     )		,)





						def 				snake_case__	(       self     )					->				Tuple:
													A__	     =							'src/transformers'
													shutil.rmtree(self.transformer_dir     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase=None     )					->				str:
													A__	     =							comment + f'''\nclass {class_name}(nn.Module):\n''' + class_code
													if overwrite_result is not None:
																				A__	     =							comment + f'''\nclass {class_name}(nn.Module):\n''' + overwrite_result
													A__	     =							black.Mode(target_versions={black.TargetVersion.PYaa}		,line_length=1_19     )
													A__	     =							black.format_str(__UpperCAmelCase		,mode=__UpperCAmelCase     )
													A__	     =							os.path.join(self.transformer_dir		,'new_code.py'     )
													with open(__UpperCAmelCase		,'w'		,newline='\n'     ) as f:
																				f.write(__UpperCAmelCase     )
													if overwrite_result is None:
																				self.assertTrue(len(check_copies.is_copy_consistent(__UpperCAmelCase     )     ) == 0     )
													else:
																				check_copies.is_copy_consistent(f.name		,overwrite=__UpperCAmelCase     )
																				with open(__UpperCAmelCase		,'r'     ) as f:
																											self.assertTrue(f.read()		,__UpperCAmelCase     )





						def 				snake_case__	(       self     )					->				str:
													A__	     =							check_copies.find_code_in_transformers('models.bert.modeling_bert.BertLMPredictionHead'     )
													self.assertEqual(__UpperCAmelCase		,__UpperCAmelCase     )





						def 				snake_case__	(       self     )					->				Optional[Any]:
													# Base copy consistency
													self.check_copy_consistency(
													    '# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead'		,'BertLMPredictionHead'		,REFERENCE_CODE + '\n'		,)

													# With no empty line at the end
													self.check_copy_consistency(
													    '# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead'		,'BertLMPredictionHead'		,__UpperCAmelCase		,)

													# Copy consistency with rename
													self.check_copy_consistency(
													    '# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel'		,'TestModelLMPredictionHead'		,re.sub('Bert'		,'TestModel'		,__UpperCAmelCase     )		,)

													# Copy consistency with a really long name
													A__	     =							'TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason'
													self.check_copy_consistency(
													    f'''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}'''		,f'''{long_class_name}LMPredictionHead'''		,re.sub('Bert'		,__UpperCAmelCase		,__UpperCAmelCase     )		,)

													# Copy consistency with overwrite
													self.check_copy_consistency(
													    '# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel'		,'TestModelLMPredictionHead'		,__UpperCAmelCase		,overwrite_result=re.sub('Bert'		,'TestModel'		,__UpperCAmelCase     )		,)





						def 				snake_case__	(       self     )					->				Tuple:
													A__	     =							check_copies.LOCALIZED_READMES['README_zh-hans.md']

													A__	     =							(
													    '1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the'
													    ' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for'
													    ' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong'
													    ' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.'
													    ' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),'
													    ' released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and'
													    ' lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same'
													    ' method has been applied to compress GPT2 into'
													    ' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into'
													    ' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),'
													    ' Multilingual BERT into'
													    ' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German'
													    ' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**'
													    ' (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders'
													    ' as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang'
													    ' Luong, Quoc V. Le, Christopher D. Manning.'
													)
													A__	     =							(
													    '1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the'
													    ' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of'
													    ' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian'
													    ' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n'
													)
													A__	     =							(
													    '1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the'
													    ' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of'
													    ' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian'
													    ' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.'
													    ' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文'
													    ' [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and'
													    ' lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same'
													    ' method has been applied to compress GPT2 into'
													    ' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into'
													    ' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),'
													    ' Multilingual BERT into'
													    ' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German'
													    ' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自'
													    ' Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather'
													    ' than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,'
													    ' Christopher D. Manning 发布。\n'
													)

													A__     ,  A__	     =							check_copies.convert_to_localized_md(
													    __UpperCAmelCase		,__UpperCAmelCase		,localized_readme['format_model_list']     )

													self.assertFalse(__UpperCAmelCase     )
													self.assertEqual(__UpperCAmelCase		,__UpperCAmelCase     )

													A__     ,  A__	     =							check_copies.convert_to_localized_md(
													    __UpperCAmelCase		,__UpperCAmelCase		,localized_readme['format_model_list']     )

													# Check whether the number of models is equal to README.md after conversion.
													self.assertTrue(__UpperCAmelCase     )

													A__	     =							(
													    '1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the'
													    ' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for'
													    ' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong'
													    ' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.'
													)
													A__	     =							(
													    '1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and'
													    ' the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of'
													    ' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian'
													    ' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n'
													)
													A__	     =							(
													    '1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the'
													    ' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of'
													    ' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian'
													    ' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n'
													)

													A__     ,  A__	     =							check_copies.convert_to_localized_md(
													    __UpperCAmelCase		,__UpperCAmelCase		,localized_readme['format_model_list']     )

													# Check if the model link is synchronized.
													self.assertEqual(__UpperCAmelCase		,__UpperCAmelCase     )






"""simple docstring"""




from typing import Dict, Optional

import numpy as np

import datasets


__lowerCamelCase											=  "\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"

__lowerCamelCase											=  "\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"

__lowerCamelCase											=  "\\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   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__ = None       ,    UpperCamelCase__ = False       ,    ):






							"""simple docstring"""





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

    # turn into Numpy arrays
							A__	     =							np.array(UpperCamelCase__				)
							A__	     =							np.array(UpperCamelCase__				)

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

							A__	     =							label != ignore_index
							A__	     =							np.not_equal(UpperCamelCase__       ,    UpperCamelCase__				)
							A__	     =							pred_label[mask]
							A__	     =							np.array(UpperCamelCase__				)[mask]

							A__	     =							pred_label[pred_label == label]

							A__	     =							np.histogram(UpperCamelCase__       ,    bins=UpperCamelCase__       ,    range=(0, num_labels - 1)				)[0]
							A__	     =							np.histogram(UpperCamelCase__       ,    bins=UpperCamelCase__       ,    range=(0, num_labels - 1)				)[0]
							A__	     =							np.histogram(UpperCamelCase__       ,    bins=UpperCamelCase__       ,    range=(0, num_labels - 1)				)[0]

							A__	     =							area_pred_label + area_label - area_intersect

							return area_intersect, area_union, area_pred_label, area_label






def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__ = None       ,    UpperCamelCase__ = False       ,    ):






							"""simple docstring"""





							A__	     =							np.zeros((num_labels,)       ,    dtype=np.floataa				)
							A__	     =							np.zeros((num_labels,)       ,    dtype=np.floataa				)
							A__	     =							np.zeros((num_labels,)       ,    dtype=np.floataa				)
							A__	     =							np.zeros((num_labels,)       ,    dtype=np.floataa				)
							for result, gt_seg_map in zip(UpperCamelCase__       ,    UpperCamelCase__				):
														A__     ,  A__     ,  A__     ,  A__	     =							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   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__ = None       ,    UpperCamelCase__ = None       ,    UpperCamelCase__ = False       ,    ):






							"""simple docstring"""





							A__     ,  A__     ,  A__     ,  A__	     =							total_intersect_and_union(
							    UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__				)

							# compute metrics
							A__	     =							{}

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

							A__	     =							np.nanmean(UpperCamelCase__				)
							A__	     =							np.nanmean(UpperCamelCase__				)
							A__	     =							all_acc
							A__	     =							iou
							A__	     =							acc

							if nan_to_num is not None:
														A__	     =							{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     UpperCamelCase__(			datasets.Metric ):





						def 				snake_case__	(       self     )					->				str:
													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 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase = None		,__UpperCAmelCase = None		,__UpperCAmelCase = False		,)					->				Tuple:
													A__	     =							mean_iou(
													    results=__UpperCAmelCase		,gt_seg_maps=__UpperCAmelCase		,num_labels=__UpperCAmelCase		,ignore_index=__UpperCAmelCase		,nan_to_num=__UpperCAmelCase		,label_map=__UpperCAmelCase		,reduce_labels=__UpperCAmelCase		,)
													return iou_result





