






import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import SegformerImageProcessor


class      __SCREAMING_SNAKE_CASE       ( unittest.TestCase):






						def __init__(						self  ,    _UpperCamelCase  ,    _UpperCamelCase=7  ,    _UpperCamelCase=3  ,    _UpperCamelCase=30  ,    _UpperCamelCase=4_00  ,    _UpperCamelCase=True  ,    _UpperCamelCase=None  ,    _UpperCamelCase=True  ,    _UpperCamelCase=[0.5, 0.5, 0.5]  ,    _UpperCamelCase=[0.5, 0.5, 0.5]  ,    _UpperCamelCase=False  ,    ):






												"""simple docstring"""
												lowerCAmelCase__         =    size if size is not None else {'height': 30, 'width': 30}
												lowerCAmelCase__         =    parent
												lowerCAmelCase__         =    batch_size
												lowerCAmelCase__         =    num_channels
												lowerCAmelCase__         =    min_resolution
												lowerCAmelCase__         =    max_resolution
												lowerCAmelCase__         =    do_resize
												lowerCAmelCase__         =    size
												lowerCAmelCase__         =    do_normalize
												lowerCAmelCase__         =    image_mean
												lowerCAmelCase__         =    image_std
												lowerCAmelCase__         =    do_reduce_labels







						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												return {
												    "do_resize": self.do_resize,
												    "size": self.size,
												    "do_normalize": self.do_normalize,
												    "image_mean": self.image_mean,
												    "image_std": self.image_std,
												    "do_reduce_labels": self.do_reduce_labels,
												}







def 						_UpperCamelCase (     )	-> Optional[int]:

						"""simple docstring"""



						lowerCAmelCase__         =    load_dataset('hf-internal-testing/fixtures_ade20k'     ,							split='test'		)

						lowerCAmelCase__         =    Image.open(dataset[0]['file']		)
						lowerCAmelCase__         =    Image.open(dataset[1]['file']		)

						return image, map







def 						_UpperCamelCase (     )	-> int:

						"""simple docstring"""



						lowerCAmelCase__         =    load_dataset('hf-internal-testing/fixtures_ade20k'     ,							split='test'		)

						lowerCAmelCase__         =    Image.open(dataset[0]['file']		)
						lowerCAmelCase__         =    Image.open(dataset[1]['file']		)
						lowerCAmelCase__         =    Image.open(dataset[2]['file']		)
						lowerCAmelCase__         =    Image.open(dataset[3]['file']		)

						return [imagea, imagea], [mapa, mapa]


@require_torch
@require_vision
class      __SCREAMING_SNAKE_CASE       ( __lowercase		,       unittest.TestCase):
						_SCREAMING_SNAKE_CASE						:							int				=       SegformerImageProcessor if is_vision_available() else None






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    SegformerImageProcessingTester(self )






						@property
						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												return self.image_processor_tester.prepare_image_processor_dict()






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    self.image_processing_class(**self.image_processor_dict )
												self.assertTrue(hasattr(_UpperCamelCase  ,    'do_resize' ) )
												self.assertTrue(hasattr(_UpperCamelCase  ,    'size' ) )
												self.assertTrue(hasattr(_UpperCamelCase  ,    'do_normalize' ) )
												self.assertTrue(hasattr(_UpperCamelCase  ,    'image_mean' ) )
												self.assertTrue(hasattr(_UpperCamelCase  ,    'image_std' ) )
												self.assertTrue(hasattr(_UpperCamelCase  ,    'do_reduce_labels' ) )






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    self.image_processing_class.from_dict(self.image_processor_dict )
												self.assertEqual(image_processor.size  ,    {'height': 30, 'width': 30} )
												self.assertEqual(image_processor.do_reduce_labels  ,    _UpperCamelCase )

												lowerCAmelCase__         =    self.image_processing_class.from_dict(self.image_processor_dict  ,    size=42  ,    reduce_labels=_UpperCamelCase )
												self.assertEqual(image_processor.size  ,    {'height': 42, 'width': 42} )
												self.assertEqual(image_processor.do_reduce_labels  ,    _UpperCamelCase )






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												pass






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												# Initialize image_processing
												lowerCAmelCase__         =    self.image_processing_class(**self.image_processor_dict )
												# create random PIL images
												lowerCAmelCase__         =    prepare_image_inputs(self.image_processor_tester  ,    equal_resolution=_UpperCamelCase )
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase  ,    Image.Image )

												# Test not batched input
												lowerCAmelCase__         =    image_processing(image_inputs[0]  ,    return_tensors='pt' ).pixel_values
												self.assertEqual(
												    encoded_images.shape  ,    (
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )

												# Test batched
												lowerCAmelCase__         =    image_processing(_UpperCamelCase  ,    return_tensors='pt' ).pixel_values
												self.assertEqual(
												    encoded_images.shape  ,    (
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												# Initialize image_processing
												lowerCAmelCase__         =    self.image_processing_class(**self.image_processor_dict )
												# create random numpy tensors
												lowerCAmelCase__         =    prepare_image_inputs(self.image_processor_tester  ,    equal_resolution=_UpperCamelCase  ,    numpify=_UpperCamelCase )
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase  ,    np.ndarray )

												# Test not batched input
												lowerCAmelCase__         =    image_processing(image_inputs[0]  ,    return_tensors='pt' ).pixel_values
												self.assertEqual(
												    encoded_images.shape  ,    (
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )

												# Test batched
												lowerCAmelCase__         =    image_processing(_UpperCamelCase  ,    return_tensors='pt' ).pixel_values
												self.assertEqual(
												    encoded_images.shape  ,    (
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												# Initialize image_processing
												lowerCAmelCase__         =    self.image_processing_class(**self.image_processor_dict )
												# create random PyTorch tensors
												lowerCAmelCase__         =    prepare_image_inputs(self.image_processor_tester  ,    equal_resolution=_UpperCamelCase  ,    torchify=_UpperCamelCase )
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase  ,    torch.Tensor )

												# Test not batched input
												lowerCAmelCase__         =    image_processing(image_inputs[0]  ,    return_tensors='pt' ).pixel_values
												self.assertEqual(
												    encoded_images.shape  ,    (
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )

												# Test batched
												lowerCAmelCase__         =    image_processing(_UpperCamelCase  ,    return_tensors='pt' ).pixel_values
												self.assertEqual(
												    encoded_images.shape  ,    (
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												# Initialize image_processing
												lowerCAmelCase__         =    self.image_processing_class(**self.image_processor_dict )
												# create random PyTorch tensors
												lowerCAmelCase__         =    prepare_image_inputs(self.image_processor_tester  ,    equal_resolution=_UpperCamelCase  ,    torchify=_UpperCamelCase )
												lowerCAmelCase__         =    []
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase  ,    torch.Tensor )
																		maps.append(torch.zeros(image.shape[-2:] ).long() )

												# Test not batched input
												lowerCAmelCase__         =    image_processing(image_inputs[0]  ,    maps[0]  ,    return_tensors='pt' )
												self.assertEqual(
												    encoding['pixel_values'].shape  ,    (
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )
												self.assertEqual(
												    encoding['labels'].shape  ,    (
												        1,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )
												self.assertEqual(encoding['labels'].dtype  ,    torch.long )
												self.assertTrue(encoding['labels'].min().item() >= 0 )
												self.assertTrue(encoding['labels'].max().item() <= 2_55 )

												# Test batched
												lowerCAmelCase__         =    image_processing(_UpperCamelCase  ,    _UpperCamelCase  ,    return_tensors='pt' )
												self.assertEqual(
												    encoding['pixel_values'].shape  ,    (
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )
												self.assertEqual(
												    encoding['labels'].shape  ,    (
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )
												self.assertEqual(encoding['labels'].dtype  ,    torch.long )
												self.assertTrue(encoding['labels'].min().item() >= 0 )
												self.assertTrue(encoding['labels'].max().item() <= 2_55 )

												# Test not batched input (PIL images)
												lowerCAmelCase__    ,							lowerCAmelCase__         =    prepare_semantic_single_inputs()

												lowerCAmelCase__         =    image_processing(_UpperCamelCase  ,    _UpperCamelCase  ,    return_tensors='pt' )
												self.assertEqual(
												    encoding['pixel_values'].shape  ,    (
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )
												self.assertEqual(
												    encoding['labels'].shape  ,    (
												        1,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )
												self.assertEqual(encoding['labels'].dtype  ,    torch.long )
												self.assertTrue(encoding['labels'].min().item() >= 0 )
												self.assertTrue(encoding['labels'].max().item() <= 2_55 )

												# Test batched input (PIL images)
												lowerCAmelCase__    ,							lowerCAmelCase__         =    prepare_semantic_batch_inputs()

												lowerCAmelCase__         =    image_processing(_UpperCamelCase  ,    _UpperCamelCase  ,    return_tensors='pt' )
												self.assertEqual(
												    encoding['pixel_values'].shape  ,    (
												        2,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )
												self.assertEqual(
												    encoding['labels'].shape  ,    (
												        2,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )  ,    )
												self.assertEqual(encoding['labels'].dtype  ,    torch.long )
												self.assertTrue(encoding['labels'].min().item() >= 0 )
												self.assertTrue(encoding['labels'].max().item() <= 2_55 )







						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												# Initialize image_processing
												lowerCAmelCase__         =    self.image_processing_class(**self.image_processor_dict )

												# ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
												lowerCAmelCase__    ,							lowerCAmelCase__         =    prepare_semantic_single_inputs()
												lowerCAmelCase__         =    image_processing(_UpperCamelCase  ,    _UpperCamelCase  ,    return_tensors='pt' )
												self.assertTrue(encoding['labels'].min().item() >= 0 )
												self.assertTrue(encoding['labels'].max().item() <= 1_50 )

												lowerCAmelCase__         =    True
												lowerCAmelCase__         =    image_processing(_UpperCamelCase  ,    _UpperCamelCase  ,    return_tensors='pt' )
												self.assertTrue(encoding['labels'].min().item() >= 0 )
												self.assertTrue(encoding['labels'].max().item() <= 2_55 )













import importlib
from typing import List, Optional, Union

from seqeval.metrics import accuracy_score, classification_report

import datasets


__snake_case     :     Optional[int]       =  """\
@inproceedings{ramshaw-marcus-1995-text,
    title = \"Text Chunking using Transformation-Based Learning\",
    author = \"Ramshaw, Lance  and
      Marcus, Mitch\",
    booktitle = \"Third Workshop on Very Large Corpora\",
    year = \"1995\",
    url = \"https://www.aclweb.org/anthology/W95-0107\",
}
@misc{seqeval,
  title={{seqeval}: A Python framework for sequence labeling evaluation},
  url={https://github.com/chakki-works/seqeval},
  note={Software available from https://github.com/chakki-works/seqeval},
  author={Hiroki Nakayama},
  year={2018},
}
"""

__snake_case     :     Any       =  """\
seqeval is a Python framework for sequence labeling evaluation.
seqeval can evaluate the performance of chunking tasks such as named-entity recognition, part-of-speech tagging, semantic role labeling and so on.

This is well-tested by using the Perl script conlleval, which can be used for
measuring the performance of a system that has processed the CoNLL-2000 shared task data.

seqeval supports following formats:
IOB1
IOB2
IOE1
IOE2
IOBES

See the [README.md] file at https://github.com/chakki-works/seqeval for more information.
"""

__snake_case     :     Optional[int]       =  """
Produces labelling scores along with its sufficient statistics
from a source against one or more references.

Args:
    predictions: List of List of predicted labels (Estimated targets as returned by a tagger)
    references: List of List of reference labels (Ground truth (correct) target values)
    suffix: True if the IOB prefix is after type, False otherwise. default: False
    scheme: Specify target tagging scheme. Should be one of [\"IOB1\", \"IOB2\", \"IOE1\", \"IOE2\", \"IOBES\", \"BILOU\"].
        default: None
    mode: Whether to count correct entity labels with incorrect I/B tags as true positives or not.
        If you want to only count exact matches, pass mode=\"strict\". default: None.
    sample_weight: Array-like of shape (n_samples,), weights for individual samples. default: None
    zero_division: Which value to substitute as a metric value when encountering zero division. Should be on of 0, 1,
        \"warn\". \"warn\" acts as 0, but the warning is raised.

Returns:
    'scores': dict. Summary of the scores for overall and per type
        Overall:
            'accuracy': accuracy,
            'precision': precision,
            'recall': recall,
            'f1': F1 score, also known as balanced F-score or F-measure,
        Per type:
            'precision': precision,
            'recall': recall,
            'f1': F1 score, also known as balanced F-score or F-measure
Examples:

    >>> predictions = [['O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
    >>> references = [['O', 'O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
    >>> seqeval = datasets.load_metric(\"seqeval\")
    >>> results = seqeval.compute(predictions=predictions, references=references)
    >>> print(list(results.keys()))
    ['MISC', 'PER', 'overall_precision', 'overall_recall', 'overall_f1', 'overall_accuracy']
    >>> print(results[\"overall_f1\"])
    0.5
    >>> print(results[\"PER\"][\"f1\"])
    1.0
"""


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






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												return datasets.MetricInfo(
												    description=_DESCRIPTION  ,    citation=_CITATION  ,    homepage='https://github.com/chakki-works/seqeval'  ,    inputs_description=_KWARGS_DESCRIPTION  ,    features=datasets.Features(
												        {
												            'predictions': datasets.Sequence(datasets.Value('string'  ,    id='label' )  ,    id='sequence' ),
												            'references': datasets.Sequence(datasets.Value('string'  ,    id='label' )  ,    id='sequence' ),
												        } )  ,    codebase_urls=['https://github.com/chakki-works/seqeval']  ,    reference_urls=['https://github.com/chakki-works/seqeval']  ,    )







						def    UpperCamelCase__   (						self  ,    _UpperCamelCase  ,    _UpperCamelCase  ,    _UpperCamelCase = False  ,    _UpperCamelCase = None  ,    _UpperCamelCase = None  ,    _UpperCamelCase = None  ,    _UpperCamelCase = "warn"  ,    ):






												"""simple docstring"""
												if scheme is not None:
																		try:
																								lowerCAmelCase__         =    importlib.import_module('seqeval.scheme' )
																								lowerCAmelCase__         =    getattr(_UpperCamelCase  ,    _UpperCamelCase )
																		except AttributeError:
																								raise ValueError(F"Scheme should be one of [IOB1, IOB2, IOE1, IOE2, IOBES, BILOU], got {scheme}" )
												lowerCAmelCase__         =    classification_report(
												    y_true=_UpperCamelCase  ,    y_pred=_UpperCamelCase  ,    suffix=_UpperCamelCase  ,    output_dict=_UpperCamelCase  ,    scheme=_UpperCamelCase  ,    mode=_UpperCamelCase  ,    sample_weight=_UpperCamelCase  ,    zero_division=_UpperCamelCase  ,    )
												report.pop('macro avg' )
												report.pop('weighted avg' )
												lowerCAmelCase__         =    report.pop('micro avg' )

												lowerCAmelCase__         =    {
												    type_name: {
												        'precision': score['precision'],
												        'recall': score['recall'],
												        'f1': score['f1-score'],
												        'number': score['support'],
												    }
												    for type_name, score in report.items()
												}
												lowerCAmelCase__         =    overall_score['precision']
												lowerCAmelCase__         =    overall_score['recall']
												lowerCAmelCase__         =    overall_score['f1-score']
												lowerCAmelCase__         =    accuracy_score(y_true=_UpperCamelCase  ,    y_pred=_UpperCamelCase )

												return scores





