






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 							__a							(  __A						):





		'''simple docstring'''



		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Tuple													=   self.config_class(**self.inputs_dict       )
						self.parent.assertTrue(hasattr(UpperCamelCase__ ,    'tf_padding'       )       )
						self.parent.assertTrue(hasattr(UpperCamelCase__ ,    'depth_multiplier'       )       )






class 							__a							:





		'''simple docstring'''



		def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__=13 ,    UpperCamelCase__=3 ,    UpperCamelCase__=32 ,    UpperCamelCase__=0.25 ,    UpperCamelCase__=8 ,    UpperCamelCase__=8 ,    UpperCamelCase__=6 ,    UpperCamelCase__=32 ,    UpperCamelCase__=True ,    UpperCamelCase__=True ,    UpperCamelCase__=True ,    UpperCamelCase__="relu6" ,    UpperCamelCase__=1280 ,    UpperCamelCase__=0.1 ,    UpperCamelCase__=0.02 ,    UpperCamelCase__=True ,    UpperCamelCase__=True ,    UpperCamelCase__=10 ,    UpperCamelCase__=None ,    ):
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   parent
						SCREAMING_SNAKE_CASE_   :	Dict													=   batch_size
						SCREAMING_SNAKE_CASE_   :	int													=   num_channels
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   image_size
						SCREAMING_SNAKE_CASE_   :	Tuple													=   depth_multiplier
						SCREAMING_SNAKE_CASE_   :	Dict													=   depth_divisible_by
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   min_depth
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   expand_ratio
						SCREAMING_SNAKE_CASE_   :	List[str]													=   tf_padding
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   output_stride
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   first_layer_is_expansion
						SCREAMING_SNAKE_CASE_   :	Dict													=   finegrained_output
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   hidden_act
						SCREAMING_SNAKE_CASE_   :	str													=   last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier       )
						SCREAMING_SNAKE_CASE_   :	int													=   classifier_dropout_prob
						SCREAMING_SNAKE_CASE_   :	int													=   use_labels
						SCREAMING_SNAKE_CASE_   :	Any													=   is_training
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   num_labels
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   initializer_range
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   scope


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Tuple													=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   None
						SCREAMING_SNAKE_CASE_   :	Dict													=   None
						if self.use_labels:
										SCREAMING_SNAKE_CASE_   :	List[str]													=   ids_tensor([self.batch_size] ,    self.num_labels       )
										SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   ids_tensor([self.batch_size, self.image_size, self.image_size] ,    self.num_labels       )

						SCREAMING_SNAKE_CASE_   :	Tuple													=   self.get_config()

						return config, pixel_values, labels, pixel_labels


		def   __snake_case			(						self       ):
						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   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	int													=   MobileNetVaModel(config=UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()
						SCREAMING_SNAKE_CASE_   :	str													=   model(UpperCamelCase__       )
						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   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   self.num_labels
						SCREAMING_SNAKE_CASE_   :	int													=   MobileNetVaForImageClassification(UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()
						SCREAMING_SNAKE_CASE_   :	Tuple													=   model(UpperCamelCase__ ,    labels=UpperCamelCase__       )
						self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.num_labels)       )


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Any													=   self.num_labels
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   MobileNetVaForSemanticSegmentation(UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()
						SCREAMING_SNAKE_CASE_   :	Any													=   model(UpperCamelCase__       )
						self.parent.assertEqual(
						    result.logits.shape ,    (
						        self.batch_size,
						        self.num_labels,
						        self.image_size // self.output_stride,
						        self.image_size // self.output_stride,
						    ) ,    )
						SCREAMING_SNAKE_CASE_   :	Dict													=   model(UpperCamelCase__ ,    labels=UpperCamelCase__       )
						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   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   self.prepare_config_and_inputs()
						SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_   :	List[Any]													=   config_and_inputs
						SCREAMING_SNAKE_CASE_   :	List[str]													=   {'pixel_values': pixel_values}
						return config, inputs_dict






@require_torch
class 							__a							(  __A ,	__A ,	unittest.TestCase						):





		'''simple docstring'''



		UpperCAmelCase__      :				Any				    =						(
		    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
		    if is_torch_available()
		    else ()
		)
		UpperCAmelCase__      :				Optional[int]				    =						(
		    {
		        """feature-extraction""": MobileNetVaModel,
		        """image-classification""": MobileNetVaForImageClassification,
		        """image-segmentation""": MobileNetVaForSemanticSegmentation,
		    }
		    if is_torch_available()
		    else {}
		)

		UpperCAmelCase__      :				Dict				    =						False
		UpperCAmelCase__      :				Optional[Any]				    =						False
		UpperCAmelCase__      :				Tuple				    =						False
		UpperCAmelCase__      :				Tuple				    =						False


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Dict													=   MobileNetVaModelTester(self       )
						SCREAMING_SNAKE_CASE_   :	int													=   MobileNetVaConfigTester(self ,    config_class=UpperCamelCase__ ,    has_text_modality=UpperCamelCase__       )


		def   __snake_case			(						self       ):
						self.config_tester.run_common_tests()


		@unittest.skip(reason='MobileNetV2 does not use inputs_embeds'       )
		def   __snake_case			(						self       ):
						pass


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


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


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_   :	Tuple													=   self.model_tester.prepare_config_and_inputs_for_common()

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

										SCREAMING_SNAKE_CASE_   :	Optional[int]													=   ['pixel_values']
										self.assertListEqual(arg_names[:1] ,    UpperCamelCase__       )


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	str													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCamelCase__       )


		def   __snake_case			(						self       ):
						def check_hidden_states_output(UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ):
										SCREAMING_SNAKE_CASE_   :	int													=   model_class(UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()

										with torch.no_grad():
														SCREAMING_SNAKE_CASE_   :	Any													=   model(**self._prepare_for_class(UpperCamelCase__ ,    UpperCamelCase__       )       )

										SCREAMING_SNAKE_CASE_   :	List[Any]													=   outputs.hidden_states

										SCREAMING_SNAKE_CASE_   :	Dict													=   16
										self.assertEqual(len(UpperCamelCase__       ) ,    UpperCamelCase__       )

						SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_   :	List[str]													=   self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										SCREAMING_SNAKE_CASE_   :	List[Any]													=   True
										check_hidden_states_output(UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       )

										# check that output_hidden_states also work using config
										del inputs_dict["output_hidden_states"]
										SCREAMING_SNAKE_CASE_   :	List[str]													=   True

										check_hidden_states_output(UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       )


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Dict													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__       )


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_semantic_segmentation(*UpperCamelCase__       )






		@slow
		def   __snake_case			(						self       ):
						for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										SCREAMING_SNAKE_CASE_   :	Tuple													=   MobileNetVaModel.from_pretrained(UpperCamelCase__       )
										self.assertIsNotNone(UpperCamelCase__       )






def 					_lowerCamelCase(			):





				'''simple docstring'''







				SCREAMING_SNAKE_CASE_   :	List[str]													=   Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      )
				return image


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





		'''simple docstring'''



		@cached_property
		def   __snake_case			(						self       ):
						return (
						    MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v2_1.0_224'       ) if is_vision_available() else None
						)


		@slow
		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	str													=   MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v2_1.0_224'       ).to(UpperCamelCase__       )

						SCREAMING_SNAKE_CASE_   :	Tuple													=   self.default_image_processor
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   prepare_img()
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   image_processor(images=UpperCamelCase__ ,    return_tensors='pt'       ).to(UpperCamelCase__       )

						# forward pass
						with torch.no_grad():
										SCREAMING_SNAKE_CASE_   :	int													=   model(**UpperCamelCase__       )

						# verify the logits
						SCREAMING_SNAKE_CASE_   :	List[str]													=   torch.Size((1, 1001)       )
						self.assertEqual(outputs.logits.shape ,    UpperCamelCase__       )

						SCREAMING_SNAKE_CASE_   :	Dict													=   torch.tensor([0.24_45, -1.19_93, 0.19_05]       ).to(UpperCamelCase__       )

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






		@slow
		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   MobileNetVaForSemanticSegmentation.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'       )
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   model.to(UpperCamelCase__       )

						SCREAMING_SNAKE_CASE_   :	int													=   MobileNetVaImageProcessor.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'       )

						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   prepare_img()
						SCREAMING_SNAKE_CASE_   :	int													=   image_processor(images=UpperCamelCase__ ,    return_tensors='pt'       ).to(UpperCamelCase__       )

						# forward pass
						with torch.no_grad():
										SCREAMING_SNAKE_CASE_   :	List[str]													=   model(**UpperCamelCase__       )
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   outputs.logits

						# verify the logits
						SCREAMING_SNAKE_CASE_   :	str													=   torch.Size((1, 21, 65, 65)       )
						self.assertEqual(logits.shape ,    UpperCamelCase__       )

						SCREAMING_SNAKE_CASE_   :	Any													=   torch.tensor(
						    [
						        [[17.57_90, 17.75_81, 18.33_55], [18.32_57, 18.42_30, 18.89_73], [18.61_69, 18.86_50, 19.21_87]],
						        [[-2.15_95, -2.09_77, -2.37_41], [-2.42_26, -2.30_28, -2.68_35], [-2.78_19, -2.59_91, -2.77_06]],
						        [[4.20_58, 4.83_17, 4.76_38], [4.41_36, 5.03_61, 4.93_83], [4.50_28, 4.96_44, 4.87_34]],
						    ] ,    device=UpperCamelCase__ ,    )

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







import os

from bleurt import score  # From: git+https://github.com/google-research/bleurt.git

import datasets


A        =			datasets.logging.get_logger(__name__)


A        =			'\\n@inproceedings{bleurt,\n  title={BLEURT: Learning Robust Metrics for Text Generation},\n  author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh},\n  booktitle={ACL},\n  year={2020},\n  url={https://arxiv.org/abs/2004.04696}\n}\n'

A        =			'\\nBLEURT a learnt evaluation metric for Natural Language Generation. It is built using multiple phases of transfer learning starting from a pretrained BERT model (Devlin et al. 2018)\nand then employing another pre-training phrase using synthetic data. Finally it is trained on WMT human annotations. You may run BLEURT out-of-the-box or fine-tune\nit for your specific application (the latter is expected to perform better).\n\nSee the project\'s README at https://github.com/google-research/bleurt#readme for more information.\n'

A        =			'\nBLEURT score.\n\nArgs:\n    `predictions` (list of str): prediction/candidate sentences\n    `references` (list of str): reference sentences\n    `checkpoint` BLEURT checkpoint. Will default to BLEURT-tiny if None.\n\nReturns:\n    \'scores\': List of scores.\nExamples:\n\n    >>> predictions = ["hello there", "general kenobi"]\n    >>> references = ["hello there", "general kenobi"]\n    >>> bleurt = datasets.load_metric("bleurt")\n    >>> results = bleurt.compute(predictions=predictions, references=references)\n    >>> print([round(v, 2) for v in results["scores"]])\n    [1.03, 1.04]\n'

A        =			{
    'bleurt-tiny-128': 'https://storage.googleapis.com/bleurt-oss/bleurt-tiny-128.zip',
    'bleurt-tiny-512': 'https://storage.googleapis.com/bleurt-oss/bleurt-tiny-512.zip',
    'bleurt-base-128': 'https://storage.googleapis.com/bleurt-oss/bleurt-base-128.zip',
    'bleurt-base-512': 'https://storage.googleapis.com/bleurt-oss/bleurt-base-512.zip',
    'bleurt-large-128': 'https://storage.googleapis.com/bleurt-oss/bleurt-large-128.zip',
    'bleurt-large-512': 'https://storage.googleapis.com/bleurt-oss/bleurt-large-512.zip',
    'BLEURT-20-D3': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D3.zip',
    'BLEURT-20-D6': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D6.zip',
    'BLEURT-20-D12': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip',
    'BLEURT-20': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20.zip',
}


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





		'''simple docstring'''



		def   __snake_case			(						self       ):
						return datasets.MetricInfo(
						    description=_DESCRIPTION ,    citation=_CITATION ,    homepage='https://github.com/google-research/bleurt' ,    inputs_description=_KWARGS_DESCRIPTION ,    features=datasets.Features(
						        {
						            'predictions': datasets.Value('string' ,    id='sequence'       ),
						            'references': datasets.Value('string' ,    id='sequence'       ),
						        }       ) ,    codebase_urls=['https://github.com/google-research/bleurt'] ,    reference_urls=['https://github.com/google-research/bleurt', 'https://arxiv.org/abs/2004.04696'] ,    )


		def   __snake_case			(						self ,    UpperCamelCase__       ):
						# check that config name specifies a valid BLEURT model
						if self.config_name == "default":
										logger.warning(
										    'Using default BLEURT-Base checkpoint for sequence maximum length 128. '
										    'You can use a bigger model for better results with e.g.: datasets.load_metric(\'bleurt\', \'bleurt-large-512\').'       )
										SCREAMING_SNAKE_CASE_   :	List[Any]													=   'bleurt-base-128'

						if self.config_name.lower() in CHECKPOINT_URLS:
										SCREAMING_SNAKE_CASE_   :	Tuple													=   self.config_name.lower()

						elif self.config_name.upper() in CHECKPOINT_URLS:
										SCREAMING_SNAKE_CASE_   :	Any													=   self.config_name.upper()

						else:
										raise KeyError(
										    F'''{self.config_name} model not found. You should supply the name of a model checkpoint for bleurt in {CHECKPOINT_URLS.keys()}'''       )

						# download the model checkpoint specified by self.config_name and set up the scorer
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name]       )
						SCREAMING_SNAKE_CASE_   :	int													=   score.BleurtScorer(os.path.join(UpperCamelCase__ ,    UpperCamelCase__       )       )






		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Tuple													=   self.scorer.score(references=UpperCamelCase__ ,    candidates=UpperCamelCase__       )
						return {"scores": scores}