




import numpy as np
from nltk.translate import meteor_score

import datasets
from datasets.config import importlib_metadata, version


__lowerCamelCase	:					Dict			  =					version.parse(importlib_metadata.version("""nltk"""))
if NLTK_VERSION >= version.Version("""3.6.4"""):
    from nltk import word_tokenize


__lowerCamelCase	:					Union[str, Any]			  =					"""\
@inproceedings{banarjee2005,
  title     = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},
  author    = {Banerjee, Satanjeev  and Lavie, Alon},
  booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},
  month     = jun,
  year      = {2005},
  address   = {Ann Arbor, Michigan},
  publisher = {Association for Computational Linguistics},
  url       = {https://www.aclweb.org/anthology/W05-0909},
  pages     = {65--72},
}
"""

__lowerCamelCase	:					Optional[Any]			  =					"""\
METEOR, an automatic metric for machine translation evaluation
that is based on a generalized concept of unigram matching between the
machine-produced translation and human-produced reference translations.
Unigrams can be matched based on their surface forms, stemmed forms,
and meanings; furthermore, METEOR can be easily extended to include more
advanced matching strategies. Once all generalized unigram matches
between the two strings have been found, METEOR computes a score for
this matching using a combination of unigram-precision, unigram-recall, and
a measure of fragmentation that is designed to directly capture how
well-ordered the matched words in the machine translation are in relation
to the reference.

METEOR gets an R correlation value of 0.347 with human evaluation on the Arabic
data and 0.331 on the Chinese data. This is shown to be an improvement on
using simply unigram-precision, unigram-recall and their harmonic F1
combination.
"""

__lowerCamelCase	:					Optional[int]			  =					"""
Computes METEOR score of translated segments against one or more references.
Args:
    predictions: list of predictions to score. Each prediction
        should be a string with tokens separated by spaces.
    references: list of reference for each prediction. Each
        reference should be a string with tokens separated by spaces.
    alpha: Parameter for controlling relative weights of precision and recall. default: 0.9
    beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3
    gamma: Relative weight assigned to fragmentation penalty. default: 0.5
Returns:
    'meteor': meteor score.
Examples:

    >>> meteor = datasets.load_metric('meteor')
    >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]
    >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]
    >>> results = meteor.compute(predictions=predictions, references=references)
    >>> print(round(results[\"meteor\"], 4))
    0.6944
"""



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





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       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/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"]	,							reference_urls=[
               "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score",
               "https://en.wikipedia.org/wiki/METEOR",
           ]	,							)





      def 	__UpperCamelCase(			self	,							A_       ):
       '''simple docstring'''




       import nltk

       nltk.download("wordnet"       )
       if NLTK_VERSION >= version.Version("3.6.5"       ):
        nltk.download("punkt"       )
       if NLTK_VERSION >= version.Version("3.6.6"       ):
        nltk.download("omw-1.4"       )





      def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_=0.9	,							A_=3	,							A_=0.5       ):
       '''simple docstring'''




       if NLTK_VERSION >= version.Version("3.6.5"       ):
        UpperCamelCase     : List[str]	     =       [
            meteor_score.single_meteor_score(
                word_tokenize(A_       )	,							word_tokenize(A_       )	,							alpha=A_	,							beta=A_	,							gamma=A_       )
            for ref, pred in zip(A_	,							A_       )
        ]
       else:
        UpperCamelCase     : Tuple	     =       [
            meteor_score.single_meteor_score(A_	,							A_	,							alpha=A_	,							beta=A_	,							gamma=A_       )
            for ref, pred in zip(A_	,							A_       )
        ]

       return {"meteor": np.mean(A_       )}







from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers import (
        TFDeiTForImageClassification,
        TFDeiTForImageClassificationWithTeacher,
        TFDeiTForMaskedImageModeling,
        TFDeiTModel,
    )
    from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import DeiTImageProcessor



class  A__       :





      def __init__(			self	,							A_	,							A_=13	,							A_=30	,							A_=2	,							A_=3	,							A_=True	,							A_=True	,							A_=32	,							A_=2	,							A_=4	,							A_=37	,							A_="gelu"	,							A_=0.1	,							A_=0.1	,							A_=10	,							A_=0.02	,							A_=3	,							A_=None	,							A_=2	,							):
       '''simple docstring'''




       UpperCamelCase     : List[str]	     =       parent
       UpperCamelCase     : Tuple	     =       batch_size
       UpperCamelCase     : Union[str, Any]	     =       image_size
       UpperCamelCase     : Optional[int]	     =       patch_size
       UpperCamelCase     : List[str]	     =       num_channels
       UpperCamelCase     : Any	     =       is_training
       UpperCamelCase     : Dict	     =       use_labels
       UpperCamelCase     : List[str]	     =       hidden_size
       UpperCamelCase     : Dict	     =       num_hidden_layers
       UpperCamelCase     : Union[str, Any]	     =       num_attention_heads
       UpperCamelCase     : str	     =       intermediate_size
       UpperCamelCase     : Optional[int]	     =       hidden_act
       UpperCamelCase     : List[Any]	     =       hidden_dropout_prob
       UpperCamelCase     : Dict	     =       attention_probs_dropout_prob
       UpperCamelCase     : List[Any]	     =       type_sequence_label_size
       UpperCamelCase     : List[str]	     =       initializer_range
       UpperCamelCase     : Union[str, Any]	     =       scope
       UpperCamelCase     : Union[str, Any]	     =       encoder_stride

       # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
       UpperCamelCase     : Optional[Any]	     =       (image_size // patch_size) ** 2
       UpperCamelCase     : int	     =       num_patches + 2





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Tuple	     =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

       UpperCamelCase     : Tuple	     =       None
       if self.use_labels:
        UpperCamelCase     : Optional[Any]	     =       ids_tensor([self.batch_size]	,							self.type_sequence_label_size       )

       UpperCamelCase     : Union[str, Any]	     =       self.get_config()

       return config, pixel_values, labels





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       return DeiTConfig(
           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	,							is_decoder=A_	,							initializer_range=self.initializer_range	,							encoder_stride=self.encoder_stride	,							)





      def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : Optional[Any]	     =       TFDeiTModel(config=A_       )
       UpperCamelCase     : Tuple	     =       model(A_       )
       self.parent.assertEqual(result.last_hidden_state.shape	,							(self.batch_size, self.seq_length, self.hidden_size)       )





      def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : List[str]	     =       TFDeiTForMaskedImageModeling(config=A_       )
       UpperCamelCase     : Optional[Any]	     =       model(A_       )
       self.parent.assertEqual(
           result.reconstruction.shape	,							(self.batch_size, self.num_channels, self.image_size, self.image_size)       )

       # test greyscale images
       UpperCamelCase     : Dict	     =       1
       UpperCamelCase     : Optional[Any]	     =       TFDeiTForMaskedImageModeling(A_       )

       UpperCamelCase     : Optional[Any]	     =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
       UpperCamelCase     : Any	     =       model(A_       )
       self.parent.assertEqual(result.reconstruction.shape	,							(self.batch_size, 1, self.image_size, self.image_size)       )





      def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : Optional[Any]	     =       self.type_sequence_label_size
       UpperCamelCase     : List[Any]	     =       TFDeiTForImageClassification(A_       )
       UpperCamelCase     : Optional[int]	     =       model(A_	,							labels=A_       )
       self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.type_sequence_label_size)       )

       # test greyscale images
       UpperCamelCase     : List[Any]	     =       1
       UpperCamelCase     : Optional[Any]	     =       TFDeiTForImageClassification(A_       )

       UpperCamelCase     : Optional[int]	     =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
       UpperCamelCase     : List[Any]	     =       model(A_	,							labels=A_       )
       self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.type_sequence_label_size)       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Union[str, Any]	     =       self.prepare_config_and_inputs()
       UpperCamelCase     ,   UpperCamelCase     ,   UpperCamelCase     : int	     =       config_and_inputs
       UpperCamelCase     : Union[str, Any]	     =       {"pixel_values": pixel_values}
       return config, inputs_dict



@require_tf
class  A__       (	__snake_case	,       __snake_case	,       unittest.TestCase      ):
      _UpperCAmelCase     :str						   =       (
          (
              TFDeiTModel,
              TFDeiTForImageClassification,
              TFDeiTForImageClassificationWithTeacher,
              TFDeiTForMaskedImageModeling,
          )
          if is_tf_available()
          else ()
      )
      _UpperCAmelCase     :Tuple						   =       (
          {
              'feature-extraction': TFDeiTModel,
              'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
          }
          if is_tf_available()
          else {}
      )

      _UpperCAmelCase     :Dict						   =       False
      _UpperCAmelCase     :List[str]						   =       False
      _UpperCAmelCase     :Optional[Any]						   =       False
      _UpperCAmelCase     :Optional[int]						   =       False





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Union[str, Any]	     =       TFDeiTModelTester(self       )
       UpperCamelCase     : Optional[Any]	     =       ConfigTester(self	,							config_class=A_	,							has_text_modality=A_	,							hidden_size=37       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       self.config_tester.run_common_tests()





      @unittest.skip(reason="DeiT does not use inputs_embeds"       )
      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       pass





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     ,   UpperCamelCase     : str	     =       self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCamelCase     : Optional[int]	     =       model_class(A_       )
        self.assertIsInstance(model.get_input_embeddings()	,							(tf.keras.layers.Layer)       )
        UpperCamelCase     : Union[str, Any]	     =       model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(A_	,							tf.keras.layers.Dense       )       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     ,   UpperCamelCase     : Optional[int]	     =       self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCamelCase     : str	     =       model_class(A_       )
        UpperCamelCase     : List[str]	     =       inspect.signature(model.call       )
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        UpperCamelCase     : Optional[Any]	     =       [*signature.parameters.keys()]

        UpperCamelCase     : Any	     =       ["pixel_values"]
        self.assertListEqual(arg_names[:1]	,							A_       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Optional[Any]	     =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : int	     =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*A_       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : List[str]	     =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*A_       )





      def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_=False       ):
       '''simple docstring'''




       UpperCamelCase     : List[str]	     =       super()._prepare_for_class(A_	,							A_	,							return_labels=A_       )

       if return_labels:
        if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call       ).parameters:
         del inputs_dict["labels"]

       return inputs_dict





      @slow
      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCamelCase     : str	     =       TFDeiTModel.from_pretrained(A_       )
        self.assertIsNotNone(A_       )



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



@require_tf
@require_vision
class  A__       (	unittest.TestCase      ):





      @cached_property
      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       return (
           DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224"       )
           if is_vision_available()
           else None
       )





      @slow
      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : str	     =       TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"       )

       UpperCamelCase     : List[Any]	     =       self.default_image_processor
       UpperCamelCase     : Union[str, Any]	     =       prepare_img()
       UpperCamelCase     : Union[str, Any]	     =       image_processor(images=A_	,							return_tensors="tf"       )

       # forward pass
       UpperCamelCase     : str	     =       model(**A_       )

       # verify the logits
       UpperCamelCase     : Dict	     =       tf.TensorShape((1, 1000)       )
       self.assertEqual(outputs.logits.shape	,							A_       )

       UpperCamelCase     : Tuple	     =       tf.constant([-1.02_66, 0.19_12, -1.28_61]       )

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