




import inspect
import unittest

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

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


if is_torch_available():
  import torch
  from torch import nn

  from transformers import ViTMSNForImageClassification, ViTMSNModel
  from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import ViTImageProcessor





class    __lowercase						:







   def __init__(	self      ,							UpperCamelCase      ,							UpperCamelCase=13      ,							UpperCamelCase=30      ,							UpperCamelCase=2      ,							UpperCamelCase=3      ,							UpperCamelCase=True      ,							UpperCamelCase=True      ,							UpperCamelCase=32      ,							UpperCamelCase=5      ,							UpperCamelCase=4      ,							UpperCamelCase=37      ,							UpperCamelCase="gelu"      ,							UpperCamelCase=0.1      ,							UpperCamelCase=0.1      ,							UpperCamelCase=10      ,							UpperCamelCase=0.02      ,							UpperCamelCase=None      ,							)				->    int:
     __a			=					parent
     __a			=					batch_size
     __a			=					image_size
     __a			=					patch_size
     __a			=					num_channels
     __a			=					is_training
     __a			=					use_labels
     __a			=					hidden_size
     __a			=					num_hidden_layers
     __a			=					num_attention_heads
     __a			=					intermediate_size
     __a			=					hidden_act
     __a			=					hidden_dropout_prob
     __a			=					attention_probs_dropout_prob
     __a			=					type_sequence_label_size
     __a			=					initializer_range
     __a			=					scope

     # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
     __a			=					(image_size // patch_size) ** 2
     __a			=					num_patches + 1







   def 		UpperCamelCase__			(	self      )				->    Dict:
     __a			=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

     __a			=					None
     if self.use_labels:
       __a			=					ids_tensor([self.batch_size]      ,							self.type_sequence_label_size      )

     __a			=					self.get_config()

     return config, pixel_values, labels







   def 		UpperCamelCase__			(	self      )				->    str:
     return ViTMSNConfig(
         image_size=self.image_size      ,							patch_size=self.patch_size      ,							num_channels=self.num_channels      ,							hidden_size=self.hidden_size      ,							num_hidden_layers=self.num_hidden_layers      ,							num_attention_heads=self.num_attention_heads      ,							intermediate_size=self.intermediate_size      ,							hidden_act=self.hidden_act      ,							hidden_dropout_prob=self.hidden_dropout_prob      ,							attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,							initializer_range=self.initializer_range      ,							)







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      )				->    Optional[Any]:
     __a			=					ViTMSNModel(config=UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()
     __a			=					model(UpperCamelCase      )
     self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.hidden_size)      )







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      )				->    Tuple:
     __a			=					self.type_sequence_label_size
     __a			=					ViTMSNForImageClassification(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()
     __a			=					model(UpperCamelCase      ,							labels=UpperCamelCase      )
     print('Pixel and labels shape: {pixel_values.shape}, {labels.shape}'      )
     print('Labels: {labels}'      )
     self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.type_sequence_label_size)      )

     # test greyscale images
     __a			=					1
     __a			=					ViTMSNForImageClassification(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()

     __a			=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
     __a			=					model(UpperCamelCase      )
     self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.type_sequence_label_size)      )







   def 		UpperCamelCase__			(	self      )				->    Optional[int]:
     __a			=					self.prepare_config_and_inputs()
     __a	,       __a	,       __a			=					config_and_inputs
     __a			=					{'pixel_values': pixel_values}
     return config, inputs_dict





@require_torch
class    __lowercase						(  __magic_name__       ,							__magic_name__       ,							unittest.TestCase ):
   _a							=      (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else ()
   _a							=      (
       {"""feature-extraction""": ViTMSNModel, """image-classification""": ViTMSNForImageClassification}
       if is_torch_available()
       else {}
   )

   _a							=      False
   _a							=      False
   _a							=      False
   _a							=      False







   def 		UpperCamelCase__			(	self      )				->    List[Any]:
     __a			=					ViTMSNModelTester(self      )
     __a			=					ConfigTester(self      ,							config_class=UpperCamelCase      ,							has_text_modality=UpperCamelCase      ,							hidden_size=37      )







   def 		UpperCamelCase__			(	self      )				->    Any:
     self.config_tester.run_common_tests()







   @unittest.skip(reason='ViTMSN does not use inputs_embeds'      )
   def 		UpperCamelCase__			(	self      )				->    str:
     pass







   def 		UpperCamelCase__			(	self      )				->    Tuple:
     __a	,       __a			=					self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
       __a			=					model_class(UpperCamelCase      )
       self.assertIsInstance(model.get_input_embeddings()      ,							(nn.Module)      )
       __a			=					model.get_output_embeddings()
       self.assertTrue(x is None or isinstance(UpperCamelCase      ,							nn.Linear      )      )







   def 		UpperCamelCase__			(	self      )				->    Any:
     __a	,       __a			=					self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
       __a			=					model_class(UpperCamelCase      )
       __a			=					inspect.signature(model.forward      )
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       __a			=					[*signature.parameters.keys()]

       __a			=					['pixel_values']
       self.assertListEqual(arg_names[:1]      ,							UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Dict:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Any:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*UpperCamelCase      )







   @slow
   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __a			=					ViTMSNModel.from_pretrained(UpperCamelCase      )
       self.assertIsNotNone(UpperCamelCase      )


def 						SCREAMING_SNAKE_CASE       (      ):
  __a			=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
  return image





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







   @cached_property
   def 		UpperCamelCase__			(	self      )				->    Dict:
     return ViTImageProcessor.from_pretrained('facebook/vit-msn-small'      ) if is_vision_available() else None







   @slow
   def 		UpperCamelCase__			(	self      )				->    Optional[int]:
     torch.manual_seed(2      )
     __a			=					ViTMSNForImageClassification.from_pretrained('facebook/vit-msn-small'      ).to(UpperCamelCase      )

     __a			=					self.default_image_processor
     __a			=					prepare_img()
     __a			=					image_processor(images=UpperCamelCase      ,							return_tensors='pt'      ).to(UpperCamelCase      )

     # forward pass
     with torch.no_grad():
       __a			=					model(**UpperCamelCase      )

     # verify the logits
     __a			=					torch.Size((1, 1000)      )
     self.assertEqual(outputs.logits.shape      ,							UpperCamelCase      )

     __a			=					torch.tensor([-0.0_803, -0.4_454, -0.2_375]      ).to(UpperCamelCase      )

     self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,							UpperCamelCase      ,							atol=1e-4      )      )









'''simple docstring'''


from typing import List

import jiwer
import jiwer.transforms as tr
from packaging import version

import datasets
from datasets.config import PY_VERSION


if PY_VERSION < version.parse("3.8"):
  import importlib_metadata
else:
  import importlib.metadata as importlib_metadata


UpperCAmelCase_ 		=      ""


if version.parse(importlib_metadata.version("jiwer")) < version.parse("2.3.0"):





  class    __lowercase						(  tr.AbstractTransform ):







     def __init__(	self      ,							UpperCamelCase = " "      )				->    Tuple:
       __a			=					sentence_delimiter







     def 		UpperCamelCase__			(	self      ,							UpperCamelCase      )				->    Union[str, Any]:
       return list(UpperCamelCase      )







     def 		UpperCamelCase__			(	self      ,							UpperCamelCase      )				->    int:
       __a			=					[]
       for sent_idx, sentence in enumerate(UpperCamelCase      ):
         chars.extend(self.process_string(UpperCamelCase      )      )
         if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(UpperCamelCase      ) - 1:
           chars.append(self.sentence_delimiter      )
       return chars

  UpperCAmelCase_ 		=      tr.Compose(
      [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
  )
else:
  UpperCAmelCase_ 		=      tr.Compose(
      [
          tr.RemoveMultipleSpaces(),
          tr.Strip(),
          tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
          tr.ReduceToListOfListOfChars(),
      ]
  )


UpperCAmelCase_ 		=      "\\n@inproceedings{inproceedings,\n    author = {Morris, Andrew and Maier, Viktoria and Green, Phil},\n    year = {2004},\n    month = {01},\n    pages = {},\n    title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}\n}\n"

UpperCAmelCase_ 		=      "\\nCharacter error rate (CER) is a common metric of the performance of an automatic speech recognition system.\n\nCER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information.\n\nCharacter error rate can be computed as:\n\nCER = (S + D + I) / N = (S + D + I) / (S + D + C)\n\nwhere\n\nS is the number of substitutions,\nD is the number of deletions,\nI is the number of insertions,\nC is the number of correct characters,\nN is the number of characters in the reference (N=S+D+C).\n\nCER's output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the\nperformance of the ASR system with a CER of 0 being a perfect score.\n"

UpperCAmelCase_ 		=      "\nComputes CER score of transcribed segments against references.\nArgs:\n    references: list of references for each speech input.\n    predictions: list of transcribtions to score.\n    concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result.\nReturns:\n    (float): the character error rate\n\nExamples:\n\n    >>> predictions = [\"this is the prediction\", \"there is an other sample\"]\n    >>> references = [\"this is the reference\", \"there is another one\"]\n    >>> cer = datasets.load_metric(\"cer\")\n    >>> cer_score = cer.compute(predictions=predictions, references=references)\n    >>> print(cer_score)\n    0.34146341463414637\n"





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







   def 		UpperCamelCase__			(	self      )				->    Any:
     return datasets.MetricInfo(
         description=_DESCRIPTION      ,							citation=_CITATION      ,							inputs_description=_KWARGS_DESCRIPTION      ,							features=datasets.Features(
             {
                 'predictions': datasets.Value('string'      ,							id='sequence'      ),
                 'references': datasets.Value('string'      ,							id='sequence'      ),
             }      )      ,							codebase_urls=['https://github.com/jitsi/jiwer/']      ,							reference_urls=[
             'https://en.wikipedia.org/wiki/Word_error_rate',
             'https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates',
         ]      ,							)







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase=False      )				->    int:
     if concatenate_texts:
       return jiwer.compute_measures(
           UpperCamelCase      ,							UpperCamelCase      ,							truth_transform=UpperCamelCase      ,							hypothesis_transform=UpperCamelCase      ,							)["wer"]

     __a			=					0
     __a			=					0
     for prediction, reference in zip(UpperCamelCase      ,							UpperCamelCase      ):
       __a			=					jiwer.compute_measures(
           UpperCamelCase      ,							UpperCamelCase      ,							truth_transform=UpperCamelCase      ,							hypothesis_transform=UpperCamelCase      ,							)
       incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
       total += measures["substitutions"] + measures["deletions"] + measures["hits"]

     return incorrect / total



