

'''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 			A_					:






     '''simple docstring'''



     def __init__(						self					:	str	,     lowercase_					:	Dict	,     lowercase_					:	Any=False	,     lowercase_					:	int=13	,     lowercase_					:	Union[str, Any]=(32, 128)	,     lowercase_					:	Any=4	,     lowercase_					:	str=3	,     lowercase_					:	List[Any]=27	,     lowercase_					:	List[Any]=38	,     lowercase_					:	Union[str, Any]=99	,     lowercase_					:	Any=99	,     lowercase_					:	Tuple=32	,     lowercase_					:	Any=5	,     lowercase_					:	Optional[int]=4	,     lowercase_					:	str=4.0	,     lowercase_					:	str=257	,     lowercase_					:	Optional[Any]=None	,     )		->      Tuple:
      UpperCAmelCase			:      List[Any]		   =					parent
      UpperCAmelCase			:      Optional[Any]		   =					is_training
      UpperCAmelCase			:      List[str]		   =					batch_size
      UpperCAmelCase			:      Union[str, Any]		   =					image_size
      UpperCAmelCase			:      Tuple		   =					patch_size
      UpperCAmelCase			:      Tuple		   =					num_channels
      UpperCAmelCase			:      Union[str, Any]		   =					max_token_length
      UpperCAmelCase			:      str		   =					num_character_labels
      UpperCAmelCase			:      int		   =					num_bpe_labels
      UpperCAmelCase			:      Any		   =					num_wordpiece_labels
      UpperCAmelCase			:      int		   =					hidden_size
      UpperCAmelCase			:      Optional[Any]		   =					num_hidden_layers
      UpperCAmelCase			:      Optional[int]		   =					num_attention_heads
      UpperCAmelCase			:      List[Any]		   =					mlp_ratio
      UpperCAmelCase			:      List[str]		   =					patch_embeds_hidden_size
      UpperCAmelCase			:      Dict		   =					output_hidden_states


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


     def   UpperCAmelCase_			(						self					:	Dict			)		->      List[str]:
      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   UpperCAmelCase_			(						self					:	Tuple	,     lowercase_					:	Tuple	,     lowercase_					:	List[Any]			)		->      int:
      UpperCAmelCase			:      Optional[Any]		   =					MgpstrForSceneTextRecognition(lowercase_			)
      model.to(lowercase_			)
      model.eval()
      with torch.no_grad():
       UpperCAmelCase			:      Optional[int]		   =					model(lowercase_			)
      self.parent.assertEqual(
          generated_ids[0][0].shape	,     (self.batch_size, self.max_token_length, self.num_character_labels)			)




     def   UpperCAmelCase_			(						self					:	List[Any]			)		->      int:
      UpperCAmelCase			:      Optional[Any]		   =					self.prepare_config_and_inputs()
      UpperCAmelCase   ,					UpperCAmelCase			:      Tuple		   =					config_and_inputs
      UpperCAmelCase			:      str		   =					{'pixel_values': pixel_values}
      return config, inputs_dict




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






     '''simple docstring'''



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

     UpperCAmelCase_		:      str    						=    False
     UpperCAmelCase_		:      List[str]    						=    False
     UpperCAmelCase_		:      List[Any]    						=    False
     UpperCAmelCase_		:      Optional[int]    						=    False


     def   UpperCAmelCase_			(						self					:	str			)		->      List[str]:
      UpperCAmelCase			:      Optional[Any]		   =					MgpstrModelTester(self			)
      UpperCAmelCase			:      Union[str, Any]		   =					ConfigTester(self	,     config_class=lowercase_	,     has_text_modality=lowercase_			)


     def   UpperCAmelCase_			(						self					:	Dict			)		->      Tuple:
      self.config_tester.run_common_tests()


     def   UpperCAmelCase_			(						self					:	List[str]			)		->      str:
      UpperCAmelCase			:      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   UpperCAmelCase_			(						self					:	List[str]			)		->      str:
      pass


     def   UpperCAmelCase_			(						self					:	Union[str, Any]			)		->      int:
      UpperCAmelCase   ,					UpperCAmelCase			:      str		   =					self.model_tester.prepare_config_and_inputs_for_common()

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


     def   UpperCAmelCase_			(						self					:	Optional[int]			)		->      Tuple:
      UpperCAmelCase   ,					UpperCAmelCase			:      Tuple		   =					self.model_tester.prepare_config_and_inputs_for_common()

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

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


     @unittest.skip(reason='MgpstrModel does not support feedforward chunking'			)
     def   UpperCAmelCase_			(						self					:	int			)		->      List[str]:
      pass


     def   UpperCAmelCase_			(						self					:	Union[str, Any]			)		->      Union[str, Any]:
      UpperCAmelCase   ,					UpperCAmelCase			:      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

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


     def   UpperCAmelCase_			(						self					:	str			)		->      int:
      def check_hidden_states_output(lowercase_					:	Optional[Any]	,     lowercase_					:	List[str]	,     lowercase_					:	Union[str, Any]			):
       UpperCAmelCase			:      Any		   =					model_class(lowercase_			)
       model.to(lowercase_			)
       model.eval()

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

       UpperCAmelCase			:      str		   =					outputs.hidden_states

       UpperCAmelCase			:      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]	,     )

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

      for model_class in self.all_model_classes:
       UpperCAmelCase			:      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"]
       UpperCAmelCase			:      int		   =					True

       check_hidden_states_output(lowercase_	,     lowercase_	,     lowercase_			)


     def   UpperCAmelCase_			(						self					:	Any			)		->      List[str]:
      UpperCAmelCase   ,					UpperCAmelCase			:      Optional[Any]		   =					self.model_tester.prepare_config_and_inputs_for_common()

      UpperCAmelCase			:      List[Any]		   =					_config_zero_init(lowercase_			)
      for model_class in self.all_model_classes:
       UpperCAmelCase			:      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   UpperCAmelCase_			(						self					:	Dict			)		->      Optional[Any]:
      pass




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




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






     '''simple docstring'''



     @slow
     def   UpperCAmelCase_			(						self					:	Optional[Any]			)		->      Union[str, Any]:
      UpperCAmelCase			:      List[str]		   =					'alibaba-damo/mgp-str-base'
      UpperCAmelCase			:      List[Any]		   =					MgpstrForSceneTextRecognition.from_pretrained(lowercase_			).to(lowercase_			)
      UpperCAmelCase			:      List[str]		   =					MgpstrProcessor.from_pretrained(lowercase_			)

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

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

      # verify the logits
      self.assertEqual(outputs.logits[0].shape	,     torch.Size((1, 27, 38)			)			)

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

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

      UpperCAmelCase			:      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_			)			)
      }




