




"""simple docstring"""





import inspect
import unittest

import requests

from transformers import MgpstrConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch
							from torch import nn

							from transformers import MgpstrForSceneTextRecognition


if is_vision_available():
							from PIL import Image

							from transformers import MgpstrProcessor

class    __lowercase							:


				'''simple docstring'''


				def __init__( self :  str   ,SCREAMING_SNAKE_CASE_ :  Dict   ,SCREAMING_SNAKE_CASE_ :  Any=False   ,SCREAMING_SNAKE_CASE_ :  int=1_3   ,SCREAMING_SNAKE_CASE_ :  Union[str, Any]=(3_2, 1_2_8)   ,SCREAMING_SNAKE_CASE_ :  Any=4   ,SCREAMING_SNAKE_CASE_ :  str=3   ,SCREAMING_SNAKE_CASE_ :  List[Any]=2_7   ,SCREAMING_SNAKE_CASE_ :  List[Any]=3_8   ,SCREAMING_SNAKE_CASE_ :  Union[str, Any]=9_9   ,SCREAMING_SNAKE_CASE_ :  Any=9_9   ,SCREAMING_SNAKE_CASE_ :  Tuple=3_2   ,SCREAMING_SNAKE_CASE_ :  Any=5   ,SCREAMING_SNAKE_CASE_ :  Optional[int]=4   ,SCREAMING_SNAKE_CASE_ :  str=4.0   ,SCREAMING_SNAKE_CASE_ :  str=2_5_7   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]=None   ,):
										snake_case__							:       List[Any]				      =       parent
										snake_case__							:       Optional[Any]				      =       is_training
										snake_case__							:       List[str]				      =       batch_size
										snake_case__							:       Union[str, Any]				      =       image_size
										snake_case__							:       Tuple				      =       patch_size
										snake_case__							:       Tuple				      =       num_channels
										snake_case__							:       Union[str, Any]				      =       max_token_length
										snake_case__							:       str				      =       num_character_labels
										snake_case__							:       int				      =       num_bpe_labels
										snake_case__							:       Any				      =       num_wordpiece_labels
										snake_case__							:       int				      =       hidden_size
										snake_case__							:       Optional[Any]				      =       num_hidden_layers
										snake_case__							:       Optional[int]				      =       num_attention_heads
										snake_case__							:       List[Any]				      =       mlp_ratio
										snake_case__							:       List[str]				      =       patch_embeds_hidden_size
										snake_case__							:       Dict				      =       output_hidden_states

				def    _lowerCamelCase       ( self :  Dict   ):
										snake_case__							:       Tuple				      =       floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]   )
										snake_case__							:       Union[str, Any]				      =       self.get_config()
										return config, pixel_values

				def    _lowerCamelCase       ( self :  Dict   ):
										return MgpstrConfig(
										    image_size=self.image_size   ,patch_size=self.patch_size   ,num_channels=self.num_channels   ,max_token_length=self.max_token_length   ,num_character_labels=self.num_character_labels   ,num_bpe_labels=self.num_bpe_labels   ,num_wordpiece_labels=self.num_wordpiece_labels   ,hidden_size=self.hidden_size   ,num_hidden_layers=self.num_hidden_layers   ,num_attention_heads=self.num_attention_heads   ,mlp_ratio=self.mlp_ratio   ,output_hidden_states=self.output_hidden_states   ,)

				def    _lowerCamelCase       ( self :  Tuple   ,SCREAMING_SNAKE_CASE_ :  Tuple   ,SCREAMING_SNAKE_CASE_ :  List[Any]   ):
										snake_case__							:       Optional[Any]				      =       MgpstrForSceneTextRecognition(lowercase_   )
										model.to(lowercase_   )
										model.eval()
										with torch.no_grad():
																snake_case__							:       Optional[int]				      =       model(lowercase_   )
										self.parent.assertEqual(
										    generated_ids[0][0].shape   ,(self.batch_size, self.max_token_length, self.num_character_labels)   )





				def    _lowerCamelCase       ( self :  List[Any]   ):
										snake_case__							:       Optional[Any]				      =       self.prepare_config_and_inputs()
										snake_case__							:       Tuple				      =       config_and_inputs
										snake_case__							:       str				      =       {'pixel_values': pixel_values}
										return config, inputs_dict







@require_torch
class    __lowercase							(      _snake_case    , _snake_case    , unittest.TestCase						):


				'''simple docstring'''


				_lowerCAmelCase						      =(MgpstrForSceneTextRecognition,) if is_torch_available() else ()
				_lowerCAmelCase						      ={"""feature-extraction""": MgpstrForSceneTextRecognition} if is_torch_available() else {}
				_lowerCAmelCase						      =False

				_lowerCAmelCase						      =False
				_lowerCAmelCase						      =False
				_lowerCAmelCase						      =False
				_lowerCAmelCase						      =False

				def    _lowerCamelCase       ( self :  str   ):
										snake_case__							:       Optional[Any]				      =       MgpstrModelTester(self   )
										snake_case__							:       Union[str, Any]				      =       ConfigTester(self   ,config_class=lowercase_   ,has_text_modality=lowercase_   )

				def    _lowerCamelCase       ( self :  Dict   ):
										self.config_tester.run_common_tests()

				def    _lowerCamelCase       ( self :  List[str]   ):
										snake_case__							:       Union[str, Any]				      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowercase_   )

				@unittest.skip(reason='''MgpstrModel does not use inputs_embeds'''   )
				def    _lowerCamelCase       ( self :  List[str]   ):
										pass

				def    _lowerCamelCase       ( self :  Union[str, Any]   ):
										snake_case__							:       str				      =       self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																snake_case__							:       int				      =       model_class(lowercase_   )
																self.assertIsInstance(model.get_input_embeddings()   ,(nn.Module)   )
																snake_case__							:       Optional[int]				      =       model.get_output_embeddings()
																self.assertTrue(x is None or isinstance(lowercase_   ,nn.Linear   )   )

				def    _lowerCamelCase       ( self :  Optional[int]   ):
										snake_case__							:       Tuple				      =       self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																snake_case__							:       Union[str, Any]				      =       model_class(lowercase_   )
																snake_case__							:       Optional[int]				      =       inspect.signature(model.forward   )
																# signature.parameters is an OrderedDict => so arg_names order is deterministic
																snake_case__							:       Union[str, Any]				      =       [*signature.parameters.keys()]

																snake_case__							:       Dict				      =       ['pixel_values']
																self.assertListEqual(arg_names[:1]   ,lowercase_   )

				@unittest.skip(reason='''MgpstrModel does not support feedforward chunking'''   )
				def    _lowerCamelCase       ( self :  int   ):
										pass

				def    _lowerCamelCase       ( self :  Union[str, Any]   ):
										snake_case__							:       List[Any]				      =       self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																if not model_class.supports_gradient_checkpointing:
																						continue

																snake_case__							:       Union[str, Any]				      =       True
																snake_case__							:       List[Any]				      =       model_class(lowercase_   )
																self.assertTrue(model.is_gradient_checkpointing   )

				def    _lowerCamelCase       ( self :  str   ):
										def check_hidden_states_output(SCREAMING_SNAKE_CASE_ :  Optional[Any]   ,SCREAMING_SNAKE_CASE_ :  List[str]   ,SCREAMING_SNAKE_CASE_ :  Union[str, Any]   ):
																snake_case__							:       Any				      =       model_class(lowercase_   )
																model.to(lowercase_   )
																model.eval()

																with torch.no_grad():
																						snake_case__							:       Union[str, Any]				      =       model(**self._prepare_for_class(lowercase_   ,lowercase_   )   )

																snake_case__							:       str				      =       outputs.hidden_states

																snake_case__							:       Tuple				      =       getattr(
																    self.model_tester   ,'''expected_num_hidden_layers'''   ,self.model_tester.num_hidden_layers + 1   )
																self.assertEqual(len(lowercase_   )   ,lowercase_   )

																self.assertListEqual(
																    list(hidden_states[0].shape[-2:]   )   ,[self.model_tester.patch_embeds_hidden_size, self.model_tester.hidden_size]   ,)

										snake_case__							:       Optional[Any]				      =       self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																snake_case__							:       Optional[Any]				      =       True
																check_hidden_states_output(lowercase_   ,lowercase_   ,lowercase_   )

																# check that output_hidden_states also work using config
																del inputs_dict["output_hidden_states"]
																snake_case__							:       int				      =       True

																check_hidden_states_output(lowercase_   ,lowercase_   ,lowercase_   )

				def    _lowerCamelCase       ( self :  Any   ):
										snake_case__							:       Optional[Any]				      =       self.model_tester.prepare_config_and_inputs_for_common()

										snake_case__							:       List[Any]				      =       _config_zero_init(lowercase_   )
										for model_class in self.all_model_classes:
																snake_case__							:       Tuple				      =       model_class(config=lowercase_   )
																for name, param in model.named_parameters():
																						if isinstance(lowercase_   ,(nn.Linear, nn.Convad, nn.LayerNorm)   ):
																												if param.requires_grad:
																																		self.assertIn(
																																		    ((param.data.mean() * 1E9).round() / 1E9).item()   ,[0.0, 1.0]   ,msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''   ,)





				@unittest.skip(reason='''Retain_grad is tested in individual model tests'''   )
				def    _lowerCamelCase       ( self :  Dict   ):
										pass







def 		lowerCAmelCase				(			):
						'''simple docstring'''


						snake_case__							:       Dict				      =       'https://i.postimg.cc/ZKwLg2Gw/367-14.png'
						snake_case__							:       int				      =       Image.open(requests.get(UpperCAmelCase_   ,      stream=UpperCAmelCase_		).raw		).convert('''RGB'''		)
						return im

@require_vision
@require_torch
class    __lowercase							(      unittest.TestCase						):


				'''simple docstring'''


				@slow
				def    _lowerCamelCase       ( self :  Optional[Any]   ):
										snake_case__							:       List[str]				      =       'alibaba-damo/mgp-str-base'
										snake_case__							:       List[Any]				      =       MgpstrForSceneTextRecognition.from_pretrained(lowercase_   ).to(lowercase_   )
										snake_case__							:       List[str]				      =       MgpstrProcessor.from_pretrained(lowercase_   )

										snake_case__							:       List[Any]				      =       prepare_img()
										snake_case__							:       Optional[Any]				      =       processor(images=lowercase_   ,return_tensors='''pt'''   ).pixel_values.to(lowercase_   )

										# forward pass
										with torch.no_grad():
																snake_case__							:       int				      =       model(lowercase_   )

										# verify the logits
										self.assertEqual(outputs.logits[0].shape   ,torch.Size((1, 2_7, 3_8)   )   )

										snake_case__							:       Optional[int]				      =       processor.batch_decode(outputs.logits   )
										snake_case__							:       Tuple				      =       'ticket'

										self.assertEqual(out_strs['''generated_text'''][0]   ,lowercase_   )

										snake_case__							:       Optional[int]				      =       torch.tensor(
										    [[[-39.5397, -44.4024, -36.1844], [-61.4709, -63.8639, -58.3454], [-74.0225, -68.5494, -71.2164]]]   ,device=lowercase_   ,)

										self.assertTrue(torch.allclose(outputs.logits[0][:, 1:4, 1:4]   ,lowercase_   ,atol=1E-4   )   )





'''simple docstring'''

from sklearn.metrics import accuracy_score

import datasets


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


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


lowercase__            =     "\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 			A_					(							datasets.Metric				):






     '''simple docstring'''



     def   UpperCAmelCase_			(						self					:	int			)		->      str:
      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   UpperCAmelCase_			(						self					:	str	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	Any	,     lowercase_					:	Tuple=True	,     lowercase_					:	Union[str, Any]=None			)		->      Dict:
      return {
          "accuracy": float(
              accuracy_score(lowercase_	,     lowercase_	,     normalize=lowercase_	,     sample_weight=lowercase_			)			)
      }




