"""simple docstring"""






import unittest

from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    PreTrainedTokenizer,
    is_vision_available,
)
from transformers.pipelines import ImageClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
						from PIL import Image
else:
						class     snake_case	:




											"""simple docstring"""

											@staticmethod
											def 		__lowerCAmelCase						(    *lowerCamelCase__       :     Dict   ,**lowerCamelCase__       :     Dict      ):
															pass
@is_pipeline_test
@require_torch_or_tf
@require_vision
class     snake_case	(			unittest.TestCase    ):




					"""simple docstring"""

					snake_case__       =			MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
					snake_case__       =			TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING







					def 		__lowerCAmelCase						(    self       :     Optional[int]   ,lowerCamelCase__       :     Optional[int]   ,lowerCamelCase__       :     Tuple   ,lowerCamelCase__       :     int      ):
									UpperCAmelCase__					=							ImageClassificationPipeline(model=lowerCamelCase__   ,image_processor=lowerCamelCase__   ,top_k=2      )
									UpperCAmelCase__					=							[
									    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      ),
									    'http://images.cocodataset.org/val2017/000000039769.jpg',
									]
									return image_classifier, examples







					def 		__lowerCAmelCase						(    self       :     int   ,lowerCamelCase__       :     str   ,lowerCamelCase__       :     Optional[int]      ):
									UpperCAmelCase__					=							image_classifier('./tests/fixtures/tests_samples/COCO/000000039769.png'      )

									self.assertEqual(
									    lowerCamelCase__   ,[
									        {'score': ANY(lowerCamelCase__      ), 'label': ANY(lowerCamelCase__      )},
									        {'score': ANY(lowerCamelCase__      ), 'label': ANY(lowerCamelCase__      )},
									    ]   ,)

									import datasets

									UpperCAmelCase__					=							datasets.load_dataset('hf-internal-testing/fixtures_image_utils'   ,'image'   ,split='test'      )

									# Accepts URL + PIL.Image + lists
									UpperCAmelCase__					=							image_classifier(
									    [
									        Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      ),
									        'http://images.cocodataset.org/val2017/000000039769.jpg',
									        # RGBA
									        dataset[0]['file'],
									        # LA
									        dataset[1]['file'],
									        # L
									        dataset[2]['file'],
									    ]      )
									self.assertEqual(
									    lowerCamelCase__   ,[
									        [
									            {'score': ANY(lowerCamelCase__      ), 'label': ANY(lowerCamelCase__      )},
									            {'score': ANY(lowerCamelCase__      ), 'label': ANY(lowerCamelCase__      )},
									        ],
									        [
									            {'score': ANY(lowerCamelCase__      ), 'label': ANY(lowerCamelCase__      )},
									            {'score': ANY(lowerCamelCase__      ), 'label': ANY(lowerCamelCase__      )},
									        ],
									        [
									            {'score': ANY(lowerCamelCase__      ), 'label': ANY(lowerCamelCase__      )},
									            {'score': ANY(lowerCamelCase__      ), 'label': ANY(lowerCamelCase__      )},
									        ],
									        [
									            {'score': ANY(lowerCamelCase__      ), 'label': ANY(lowerCamelCase__      )},
									            {'score': ANY(lowerCamelCase__      ), 'label': ANY(lowerCamelCase__      )},
									        ],
									        [
									            {'score': ANY(lowerCamelCase__      ), 'label': ANY(lowerCamelCase__      )},
									            {'score': ANY(lowerCamelCase__      ), 'label': ANY(lowerCamelCase__      )},
									        ],
									    ]   ,)







					@require_torch
					def 		__lowerCAmelCase						(    self       :     Any      ):
									UpperCAmelCase__					=							'hf-internal-testing/tiny-random-vit'
									UpperCAmelCase__					=							pipeline('image-classification'   ,model=lowerCamelCase__      )

									UpperCAmelCase__					=							image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'      )
									self.assertEqual(
									    nested_simplify(lowerCamelCase__   ,decimals=4      )   ,[{'label': 'LABEL_1', 'score': 0.5_7_4}, {'label': 'LABEL_0', 'score': 0.4_2_6}]   ,)

									UpperCAmelCase__					=							image_classifier(
									    [
									        'http://images.cocodataset.org/val2017/000000039769.jpg',
									        'http://images.cocodataset.org/val2017/000000039769.jpg',
									    ]   ,top_k=2   ,)
									self.assertEqual(
									    nested_simplify(lowerCamelCase__   ,decimals=4      )   ,[
									        [{'label': 'LABEL_1', 'score': 0.5_7_4}, {'label': 'LABEL_0', 'score': 0.4_2_6}],
									        [{'label': 'LABEL_1', 'score': 0.5_7_4}, {'label': 'LABEL_0', 'score': 0.4_2_6}],
									    ]   ,)







					@require_tf
					def 		__lowerCAmelCase						(    self       :     int      ):
									UpperCAmelCase__					=							'hf-internal-testing/tiny-random-vit'
									UpperCAmelCase__					=							pipeline('image-classification'   ,model=lowerCamelCase__   ,framework='tf'      )

									UpperCAmelCase__					=							image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'      )
									self.assertEqual(
									    nested_simplify(lowerCamelCase__   ,decimals=4      )   ,[{'label': 'LABEL_1', 'score': 0.5_7_4}, {'label': 'LABEL_0', 'score': 0.4_2_6}]   ,)

									UpperCAmelCase__					=							image_classifier(
									    [
									        'http://images.cocodataset.org/val2017/000000039769.jpg',
									        'http://images.cocodataset.org/val2017/000000039769.jpg',
									    ]   ,top_k=2   ,)
									self.assertEqual(
									    nested_simplify(lowerCamelCase__   ,decimals=4      )   ,[
									        [{'label': 'LABEL_1', 'score': 0.5_7_4}, {'label': 'LABEL_0', 'score': 0.4_2_6}],
									        [{'label': 'LABEL_1', 'score': 0.5_7_4}, {'label': 'LABEL_0', 'score': 0.4_2_6}],
									    ]   ,)







					def 		__lowerCAmelCase						(    self       :     Optional[int]      ):
									UpperCAmelCase__					=							PreTrainedTokenizer()

									# Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
									UpperCAmelCase__					=							pipeline(
									    'image-classification'   ,model='hf-internal-testing/tiny-random-vit'   ,tokenizer=lowerCamelCase__      )

									self.assertIs(image_classifier.tokenizer   ,lowerCamelCase__      )







					@slow
					@require_torch
					def 		__lowerCAmelCase						(    self       :     int      ):
									# Perceiver is not tested by `run_pipeline_test` properly.
									# That is because the type of feature_extractor and model preprocessor need to be kept
									# in sync, which is not the case in the current design
									UpperCAmelCase__					=							pipeline('image-classification'   ,model='deepmind/vision-perceiver-conv'      )
									UpperCAmelCase__					=							image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'      )
									self.assertEqual(
									    nested_simplify(lowerCamelCase__   ,decimals=4      )   ,[
									        {'score': 0.4_3_8_5, 'label': 'tabby, tabby cat'},
									        {'score': 0.3_2_1, 'label': 'tiger cat'},
									        {'score': 0.0_5_0_2, 'label': 'Egyptian cat'},
									        {'score': 0.0_1_3_7, 'label': 'crib, cot'},
									        {'score': 0.0_0_7, 'label': 'radiator'},
									    ]   ,)

									UpperCAmelCase__					=							pipeline('image-classification'   ,model='deepmind/vision-perceiver-fourier'      )
									UpperCAmelCase__					=							image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'      )
									self.assertEqual(
									    nested_simplify(lowerCamelCase__   ,decimals=4      )   ,[
									        {'score': 0.5_6_5_8, 'label': 'tabby, tabby cat'},
									        {'score': 0.1_3_0_9, 'label': 'tiger cat'},
									        {'score': 0.0_7_2_2, 'label': 'Egyptian cat'},
									        {'score': 0.0_7_0_7, 'label': 'remote control, remote'},
									        {'score': 0.0_0_8_2, 'label': 'computer keyboard, keypad'},
									    ]   ,)

									UpperCAmelCase__					=							pipeline('image-classification'   ,model='deepmind/vision-perceiver-learned'      )
									UpperCAmelCase__					=							image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'      )
									self.assertEqual(
									    nested_simplify(lowerCamelCase__   ,decimals=4      )   ,[
									        {'score': 0.3_0_2_2, 'label': 'tabby, tabby cat'},
									        {'score': 0.2_3_6_2, 'label': 'Egyptian cat'},
									        {'score': 0.1_8_5_6, 'label': 'tiger cat'},
									        {'score': 0.0_3_2_4, 'label': 'remote control, remote'},
									        {'score': 0.0_0_9_6, 'label': 'quilt, comforter, comfort, puff'},
									    ]   ,)




"""simple docstring"""






from sklearn.metrics import accuracy_score

import datasets


lowerCAmelCase__  :  List[str]											=     '\nAccuracy is the proportion of correct predictions among the total number of cases processed. It can be computed with:\nAccuracy = (TP + TN) / (TP + TN + FP + FN)\n Where:\nTP: True positive\nTN: True negative\nFP: False positive\nFN: False negative\n'


lowerCAmelCase__  :  List[str]											=     '\nArgs:\n    predictions (`list` of `int`): Predicted labels.\n    references (`list` of `int`): Ground truth labels.\n    normalize (`boolean`): If set to False, returns the number of correctly classified samples. Otherwise, returns the fraction of correctly classified samples. Defaults to True.\n    sample_weight (`list` of `float`): Sample weights Defaults to None.\n\nReturns:\n    accuracy (`float` or `int`): Accuracy score. Minimum possible value is 0. Maximum possible value is 1.0, or the number of examples input, if `normalize` is set to `True`.. A higher score means higher accuracy.\n\nExamples:\n\n    Example 1-A simple example\n        >>> accuracy_metric = datasets.load_metric("accuracy")\n        >>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0])\n        >>> print(results)\n        {\'accuracy\': 0.5}\n\n    Example 2-The same as Example 1, except with `normalize` set to `False`.\n        >>> accuracy_metric = datasets.load_metric("accuracy")\n        >>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0], normalize=False)\n        >>> print(results)\n        {\'accuracy\': 3.0}\n\n    Example 3-The same as Example 1, except with `sample_weight` set.\n        >>> accuracy_metric = datasets.load_metric("accuracy")\n        >>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0], sample_weight=[0.5, 2, 0.7, 0.5, 9, 0.4])\n        >>> print(results)\n        {\'accuracy\': 0.8778625954198473}\n'


lowerCAmelCase__  :  Dict											=     '\n@article{scikit-learn,\n  title={Scikit-learn: Machine Learning in {P}ython},\n  author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n  journal={Journal of Machine Learning Research},\n  volume={12},\n  pages={2825--2830},\n  year={2011}\n}\n'
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION				,   _KWARGS_DESCRIPTION    )
class     snake_case	(			datasets.Metric    ):




					"""simple docstring"""

					def 		__lowerCAmelCase						(    self       :     List[Any]      ):
									return datasets.MetricInfo(
									    description=_DESCRIPTION   ,citation=_CITATION   ,inputs_description=_KWARGS_DESCRIPTION   ,features=datasets.Features(
									        {
									            'predictions': datasets.Sequence(datasets.Value('int32'      )      ),
									            'references': datasets.Sequence(datasets.Value('int32'      )      ),
									        }
									        if self.config_name == 'multilabel'
									        else {
									            'predictions': datasets.Value('int32'      ),
									            'references': datasets.Value('int32'      ),
									        }      )   ,reference_urls=['https://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html']   ,)







					def 		__lowerCAmelCase						(    self       :     int   ,lowerCamelCase__       :     Any   ,lowerCamelCase__       :     List[str]   ,lowerCamelCase__       :     Dict=True   ,lowerCamelCase__       :     Tuple=None      ):
									return {
									    "accuracy": float(
									        accuracy_score(lowerCamelCase__   ,lowerCamelCase__   ,normalize=lowerCamelCase__   ,sample_weight=lowerCamelCase__      )      )
									}



