
"""simple docstring"""






from jiwer import compute_measures

import datasets


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

UpperCamelCase_							=					'''\
Word error rate (WER) is a common metric of the performance of an automatic speech recognition system.

The general difficulty of measuring performance lies in the fact that the recognized word sequence can have a different length from the reference word sequence (supposedly the correct one). The WER is derived from the Levenshtein distance, working at the word level instead of the phoneme level. The WER is a valuable tool for comparing different systems as well as for evaluating improvements within one system. This kind of measurement, however, provides no details on the nature of translation errors and further work is therefore required to identify the main source(s) of error and to focus any research effort.

This problem is solved by first aligning the recognized word sequence with the reference (spoken) word sequence using dynamic string alignment. Examination of this issue is seen through a theory called the power law that states the correlation between perplexity and word error rate.

Word error rate can then be computed as:

WER = (S + D + I) / N = (S + D + I) / (S + D + C)

where

S is the number of substitutions,
D is the number of deletions,
I is the number of insertions,
C is the number of correct words,
N is the number of words in the reference (N=S+D+C).

This value indicates the average number of errors per reference word. The lower the value, the better the
performance of the ASR system with a WER of 0 being a perfect score.
'''

UpperCamelCase_							=					'''
Compute WER score of transcribed segments against references.

Args:
    references: List of references for each speech input.
    predictions: List of transcriptions to score.
    concatenate_texts (bool, default=False): Whether to concatenate all input texts or compute WER iteratively.

Returns:
    (float): the word error rate

Examples:

    >>> predictions = ["this is the prediction", "there is an other sample"]
    >>> references = ["this is the reference", "there is another one"]
    >>> wer = datasets.load_metric("wer")
    >>> wer_score = wer.compute(predictions=predictions, references=references)
    >>> print(wer_score)
    0.5
'''





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







   def 					UpperCAmelCase__     (      self)					->List[str]:
      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",
          ]    ,				)







   def 					UpperCAmelCase__     (      self    ,				__UpperCAmelCase=None    ,				__UpperCAmelCase=None    ,				__UpperCAmelCase=False)					->Union[str, Any]:


      if concatenate_texts:
         return compute_measures(__UpperCAmelCase    ,				__UpperCAmelCase)["wer"]
      else:
         a_							=			0
         a_							=			0
         for prediction, reference in zip(__UpperCAmelCase    ,				__UpperCAmelCase):
            a_							=			compute_measures(__UpperCAmelCase    ,				__UpperCAmelCase)
            incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
            total += measures["substitutions"] + measures["deletions"] + measures["hits"]
         return incorrect / total

import inspect
import unittest

from transformers import MobileNetVaConfig
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 transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel
			from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import MobileNetVaImageProcessor




class    SCREAMING_SNAKE_CASE__     (	UpperCAmelCase					):





	'''simple docstring'''



	def  A							(  self	:    List[str]	):


				'''simple docstring'''


				_snake_case		=			self.config_class(**self.inputs_dict	)
				self.parent.assertTrue(hasattr(lowercase			,  'tf_padding'	)	)
				self.parent.assertTrue(hasattr(lowercase			,  'depth_multiplier'	)	)




class    SCREAMING_SNAKE_CASE__     :





	'''simple docstring'''



	def __init__(  self	:    Dict			,  lowercase	:    List[str]			,  lowercase	:    Dict=13			,  lowercase	:    Optional[int]=3			,  lowercase	:    Any=32			,  lowercase	:    Any=0.25			,  lowercase	:    Union[str, Any]=8			,  lowercase	:    List[Any]=8			,  lowercase	:    List[Any]=6			,  lowercase	:    Dict=32			,  lowercase	:    Dict=True			,  lowercase	:    Optional[Any]=True			,  lowercase	:    Tuple=True			,  lowercase	:    Tuple="relu6"			,  lowercase	:    List[Any]=1_280			,  lowercase	:    Optional[Any]=0.1			,  lowercase	:    int=0.02			,  lowercase	:    Optional[Any]=True			,  lowercase	:    List[str]=True			,  lowercase	:    List[str]=10			,  lowercase	:    Optional[Any]=None			,  ):


				'''simple docstring'''


				_snake_case		=			parent
				_snake_case		=			batch_size
				_snake_case		=			num_channels
				_snake_case		=			image_size
				_snake_case		=			depth_multiplier
				_snake_case		=			depth_divisible_by
				_snake_case		=			min_depth
				_snake_case		=			expand_ratio
				_snake_case		=			tf_padding
				_snake_case		=			output_stride
				_snake_case		=			first_layer_is_expansion
				_snake_case		=			finegrained_output
				_snake_case		=			hidden_act
				_snake_case		=			last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier	)
				_snake_case		=			classifier_dropout_prob
				_snake_case		=			use_labels
				_snake_case		=			is_training
				_snake_case		=			num_labels
				_snake_case		=			initializer_range
				_snake_case		=			scope



	def  A							(  self	:    Union[str, Any]	):


				'''simple docstring'''


				_snake_case		=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

				_snake_case		=			None
				_snake_case		=			None
				if self.use_labels:
							_snake_case		=			ids_tensor([self.batch_size]			,  self.num_labels	)
							_snake_case		=			ids_tensor([self.batch_size, self.image_size, self.image_size]			,  self.num_labels	)

				_snake_case		=			self.get_config()

				return config, pixel_values, labels, pixel_labels



	def  A							(  self	:    str	):


				'''simple docstring'''


				return MobileNetVaConfig(
				    num_channels=self.num_channels			,  image_size=self.image_size			,  depth_multiplier=self.depth_multiplier			,  depth_divisible_by=self.depth_divisible_by			,  min_depth=self.min_depth			,  expand_ratio=self.expand_ratio			,  output_stride=self.output_stride			,  first_layer_is_expansion=self.first_layer_is_expansion			,  finegrained_output=self.finegrained_output			,  hidden_act=self.hidden_act			,  tf_padding=self.tf_padding			,  classifier_dropout_prob=self.classifier_dropout_prob			,  initializer_range=self.initializer_range			,  )



	def  A							(  self	:    Optional[Any]			,  lowercase	:    str			,  lowercase	:    List[str]			,  lowercase	:    str			,  lowercase	:    Dict	):


				'''simple docstring'''


				_snake_case		=			MobileNetVaModel(config=lowercase	)
				model.to(lowercase	)
				model.eval()
				_snake_case		=			model(lowercase	)
				self.parent.assertEqual(
				    result.last_hidden_state.shape			,  (
				        self.batch_size,
				        self.last_hidden_size,
				        self.image_size // self.output_stride,
				        self.image_size // self.output_stride,
				    )			,  )
				self.parent.assertEqual(
				    result.pooler_output.shape			,  (self.batch_size, self.last_hidden_size)			,  )



	def  A							(  self	:    List[Any]			,  lowercase	:    Optional[int]			,  lowercase	:    Optional[int]			,  lowercase	:    Optional[Any]			,  lowercase	:    List[Any]	):


				'''simple docstring'''


				_snake_case		=			self.num_labels
				_snake_case		=			MobileNetVaForImageClassification(lowercase	)
				model.to(lowercase	)
				model.eval()
				_snake_case		=			model(lowercase			,  labels=lowercase	)
				self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_labels)	)



	def  A							(  self	:    Any			,  lowercase	:    int			,  lowercase	:    Dict			,  lowercase	:    int			,  lowercase	:    List[Any]	):


				'''simple docstring'''


				_snake_case		=			self.num_labels
				_snake_case		=			MobileNetVaForSemanticSegmentation(lowercase	)
				model.to(lowercase	)
				model.eval()
				_snake_case		=			model(lowercase	)
				self.parent.assertEqual(
				    result.logits.shape			,  (
				        self.batch_size,
				        self.num_labels,
				        self.image_size // self.output_stride,
				        self.image_size // self.output_stride,
				    )			,  )
				_snake_case		=			model(lowercase			,  labels=lowercase	)
				self.parent.assertEqual(
				    result.logits.shape			,  (
				        self.batch_size,
				        self.num_labels,
				        self.image_size // self.output_stride,
				        self.image_size // self.output_stride,
				    )			,  )





	def  A							(  self	:    str	):


				'''simple docstring'''


				_snake_case		=			self.prepare_config_and_inputs()
				_snake_case	,					_snake_case	,					_snake_case	,					_snake_case		=			config_and_inputs
				_snake_case		=			{'pixel_values': pixel_values}
				return config, inputs_dict




@require_torch
class    SCREAMING_SNAKE_CASE__     (	UpperCAmelCase							,UpperCAmelCase							,unittest.TestCase					):





	'''simple docstring'''



	_UpperCAmelCase       :		str       =					(
	    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
	    if is_torch_available()
	    else ()
	)
	_UpperCAmelCase       :		str       =					(
	    {
	        "feature-extraction": MobileNetVaModel,
	        "image-classification": MobileNetVaForImageClassification,
	        "image-segmentation": MobileNetVaForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

	_UpperCAmelCase       :		Optional[int]       =					False
	_UpperCAmelCase       :		Dict       =					False
	_UpperCAmelCase       :		Dict       =					False
	_UpperCAmelCase       :		Union[str, Any]       =					False



	def  A							(  self	:    Any	):


				'''simple docstring'''


				_snake_case		=			MobileNetVaModelTester(self	)
				_snake_case		=			MobileNetVaConfigTester(self			,  config_class=lowercase			,  has_text_modality=lowercase	)



	def  A							(  self	:    Any	):


				'''simple docstring'''


				self.config_tester.run_common_tests()



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


				'''simple docstring'''


				pass



	@unittest.skip(reason='MobileNetV2 does not support input and output embeddings'	)
	def  A							(  self	:    int	):


				'''simple docstring'''


				pass



	@unittest.skip(reason='MobileNetV2 does not output attentions'	)
	def  A							(  self	:    Any	):


				'''simple docstring'''


				pass



	def  A							(  self	:    Optional[int]	):


				'''simple docstring'''


				_snake_case	,					_snake_case		=			self.model_tester.prepare_config_and_inputs_for_common()

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

							_snake_case		=			['pixel_values']
							self.assertListEqual(arg_names[:1]			,  lowercase	)



	def  A							(  self	:    List[str]	):


				'''simple docstring'''


				_snake_case		=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowercase	)



	def  A							(  self	:    List[Any]	):


				'''simple docstring'''


				def check_hidden_states_output(lowercase	:    List[Any]			,  lowercase	:    Union[str, Any]			,  lowercase	:    str	):
							_snake_case		=			model_class(lowercase	)
							model.to(lowercase	)
							model.eval()

							with torch.no_grad():
										_snake_case		=			model(**self._prepare_for_class(lowercase			,  lowercase	)	)

							_snake_case		=			outputs.hidden_states

							_snake_case		=			16
							self.assertEqual(len(lowercase	)			,  lowercase	)

				_snake_case	,					_snake_case		=			self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							_snake_case		=			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		=			True

							check_hidden_states_output(lowercase			,  lowercase			,  lowercase	)



	def  A							(  self	:    Tuple	):


				'''simple docstring'''


				_snake_case		=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*lowercase	)



	def  A							(  self	:    Dict	):


				'''simple docstring'''


				_snake_case		=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_semantic_segmentation(*lowercase	)





	@slow
	def  A							(  self	:    List[Any]	):


				'''simple docstring'''


				for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_snake_case		=			MobileNetVaModel.from_pretrained(lowercase	)
							self.assertIsNotNone(lowercase	)






def        a_		(       ) ->  Union[str, Any]:
			_snake_case		=			Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' )
			return image




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





	'''simple docstring'''



	@cached_property
	def  A							(  self	:    Optional[Any]	):


				'''simple docstring'''


				return (
				    MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v2_1.0_224'	) if is_vision_available() else None
				)



	@slow
	def  A							(  self	:    List[Any]	):


				'''simple docstring'''


				_snake_case		=			MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v2_1.0_224'	).to(lowercase	)

				_snake_case		=			self.default_image_processor
				_snake_case		=			prepare_img()
				_snake_case		=			image_processor(images=lowercase			,  return_tensors='pt'	).to(lowercase	)

				# forward pass
				with torch.no_grad():
							_snake_case		=			model(**lowercase	)

				# verify the logits
				_snake_case		=			torch.Size((1, 1_001)	)
				self.assertEqual(outputs.logits.shape			,  lowercase	)

				_snake_case		=			torch.tensor([0.2445, -1.1993, 0.1905]	).to(lowercase	)

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





	@slow
	def  A							(  self	:    Dict	):


				'''simple docstring'''


				_snake_case		=			MobileNetVaForSemanticSegmentation.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'	)
				_snake_case		=			model.to(lowercase	)

				_snake_case		=			MobileNetVaImageProcessor.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'	)

				_snake_case		=			prepare_img()
				_snake_case		=			image_processor(images=lowercase			,  return_tensors='pt'	).to(lowercase	)

				# forward pass
				with torch.no_grad():
							_snake_case		=			model(**lowercase	)
				_snake_case		=			outputs.logits

				# verify the logits
				_snake_case		=			torch.Size((1, 21, 65, 65)	)
				self.assertEqual(logits.shape			,  lowercase	)

				_snake_case		=			torch.tensor(
				    [
				        [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
				        [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]],
				        [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]],
				    ]			,  device=lowercase			,  )

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