

'''simple docstring'''





import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ConvNextConfig, UperNetConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import UperNetForSemanticSegmentation
							from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor





class 		__SCREAMING_SNAKE_CASE							:







				def __init__(       self     :		Union[str, Any]		,					UpperCAmelCase__     :		Optional[Any]		,					UpperCAmelCase__     :		Optional[Any]=13		,					UpperCAmelCase__     :		List[str]=32		,					UpperCAmelCase__     :		int=3		,					UpperCAmelCase__     :		Dict=4		,					UpperCAmelCase__     :		Optional[Any]=[10, 20, 30, 40]		,					UpperCAmelCase__     :		Tuple=[2, 2, 3, 2]		,					UpperCAmelCase__     :		Dict=True		,					UpperCAmelCase__     :		List[Any]=True		,					UpperCAmelCase__     :		List[str]=37		,					UpperCAmelCase__     :		Optional[int]="gelu"		,					UpperCAmelCase__     :		str=10		,					UpperCAmelCase__     :		Dict=0.02		,					UpperCAmelCase__     :		Optional[int]=["stage2", "stage3", "stage4"]		,					UpperCAmelCase__     :		Dict=3		,					UpperCAmelCase__     :		str=None		,					):
										'''simple docstring'''
										lowercase :							str    		=parent
										lowercase :							List[Any]    		=batch_size
										lowercase :							List[Any]    		=image_size
										lowercase :							Union[str, Any]    		=num_channels
										lowercase :							str    		=num_stages
										lowercase :							Optional[int]    		=hidden_sizes
										lowercase :							Tuple    		=depths
										lowercase :							Optional[int]    		=is_training
										lowercase :							Dict    		=use_labels
										lowercase :							str    		=intermediate_size
										lowercase :							List[str]    		=hidden_act
										lowercase :							Union[str, Any]    		=type_sequence_label_size
										lowercase :							Tuple    		=initializer_range
										lowercase :							List[str]    		=out_features
										lowercase :							Dict    		=num_labels
										lowercase :							List[Any]    		=scope
										lowercase :							Union[str, Any]    		=num_stages







				def  lowerCamelCase_	(       self     :		Union[str, Any]   ):
										'''simple docstring'''
										lowercase :							Optional[int]    		=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

										lowercase :							Any    		=self.get_config()

										return config, pixel_values, labels







				def  lowerCamelCase_	(       self     :		int   ):
										'''simple docstring'''
										return ConvNextConfig(
										    num_channels=self.num_channels		,					num_stages=self.num_stages		,					hidden_sizes=self.hidden_sizes		,					depths=self.depths		,					is_training=self.is_training		,					intermediate_size=self.intermediate_size		,					hidden_act=self.hidden_act		,					out_features=self.out_features		,					)







				def  lowerCamelCase_	(       self     :		List[str]   ):
										'''simple docstring'''
										return UperNetConfig(
										    backbone_config=self.get_backbone_config()		,					hidden_size=512		,					pool_scales=[1, 2, 3, 6]		,					use_auxiliary_head=__A		,					auxiliary_loss_weight=0.4		,					auxiliary_in_channels=40		,					auxiliary_channels=256		,					auxiliary_num_convs=1		,					auxiliary_concat_input=__A		,					loss_ignore_index=255		,					num_labels=self.num_labels		,					)







				def  lowerCamelCase_	(       self     :		str		,					UpperCAmelCase__     :		Optional[int]		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		List[str]   ):
										'''simple docstring'''
										lowercase :							List[str]    		=UperNetForSemanticSegmentation(config=__A   )
										model.to(__A   )
										model.eval()
										lowercase :							Union[str, Any]    		=model(__A   )
										self.parent.assertEqual(
										    result.logits.shape		,					(self.batch_size, self.num_labels, self.image_size, self.image_size)   )







				def  lowerCamelCase_	(       self     :		Union[str, Any]   ):
										'''simple docstring'''
										lowercase :							int    		=self.prepare_config_and_inputs()
										(
										    lowercase     
										) :							List[str]    		=config_and_inputs
										lowercase :							List[str]    		={'''pixel_values''': pixel_values}
										return config, inputs_dict





@require_torch
class 		__SCREAMING_SNAKE_CASE							(			lowercase__						,							lowercase__						,							unittest.TestCase      ):
				lowerCamelCase_		       =    (UperNetForSemanticSegmentation,) if is_torch_available() else ()
				lowerCamelCase_		       =    {'image-segmentation': UperNetForSemanticSegmentation} if is_torch_available() else {}
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    False







				def  lowerCamelCase_	(       self     :		Optional[Any]   ):
										'''simple docstring'''
										lowercase :							Tuple    		=UperNetModelTester(self   )
										lowercase :							Union[str, Any]    		=ConfigTester(self		,					config_class=__A		,					has_text_modality=__A		,					hidden_size=37   )







				def  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										self.create_and_test_config_common_properties()
										self.config_tester.create_and_test_config_to_json_string()
										self.config_tester.create_and_test_config_to_json_file()
										self.config_tester.create_and_test_config_from_and_save_pretrained()
										self.config_tester.create_and_test_config_with_num_labels()
										self.config_tester.check_config_can_be_init_without_params()
										self.config_tester.check_config_arguments_init()







				def  lowerCamelCase_	(       self     :		Dict   ):
										'''simple docstring'''
										return







				def  lowerCamelCase_	(       self     :		Union[str, Any]   ):
										'''simple docstring'''
										lowercase :							int    		=self.model_tester.prepare_config_and_inputs_for_common()

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

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







				def  lowerCamelCase_	(       self     :		Optional[int]   ):
										'''simple docstring'''
										lowercase :							Any    		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_semantic_segmentation(*__A   )







				@unittest.skip(reason='''UperNet does not use inputs_embeds'''   )
				def  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										pass







				@unittest.skip(reason='''UperNet does not support input and output embeddings'''   )
				def  lowerCamelCase_	(       self     :		Optional[Any]   ):
										'''simple docstring'''
										pass







				@unittest.skip(reason='''UperNet does not have a base model'''   )
				def  lowerCamelCase_	(       self     :		Dict   ):
										'''simple docstring'''
										pass







				@unittest.skip(reason='''UperNet does not have a base model'''   )
				def  lowerCamelCase_	(       self     :		Tuple   ):
										'''simple docstring'''
										pass







				@require_torch_multi_gpu
				@unittest.skip(reason='''UperNet has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''   )
				def  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										pass







				@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''   )
				def  lowerCamelCase_	(       self     :		List[str]   ):
										'''simple docstring'''
										pass







				def  lowerCamelCase_	(       self     :		Optional[Any]   ):
										'''simple docstring'''
										def check_hidden_states_output(UpperCAmelCase__     :		Tuple		,					UpperCAmelCase__     :		Dict		,					UpperCAmelCase__     :		List[str]   ):
																lowercase :							List[str]    		=model_class(__A   )
																model.to(__A   )
																model.eval()

																with torch.no_grad():
																						lowercase :							Optional[Any]    		=model(**self._prepare_for_class(__A		,					__A   )   )

																lowercase :							Optional[int]    		=outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																lowercase :							List[Any]    		=self.model_tester.num_stages
																self.assertEqual(len(__A   )		,					expected_num_stages + 1   )

																# ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
																self.assertListEqual(
																    list(hidden_states[0].shape[-2:]   )		,					[self.model_tester.image_size // 4, self.model_tester.image_size // 4]		,					)

										lowercase :							Tuple    		=self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																lowercase :							Optional[int]    		=True
																check_hidden_states_output(__A		,					__A		,					__A   )

																# check that output_hidden_states also work using config
																del inputs_dict["output_hidden_states"]
																lowercase :							Union[str, Any]    		=True

																check_hidden_states_output(__A		,					__A		,					__A   )







				def  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										lowercase :							List[str]    		=self.model_tester.prepare_config_and_inputs_for_common()

										lowercase :							Dict    		=_config_zero_init(__A   )
										lowercase :							Any    		=_config_zero_init(configs_no_init.backbone_config   )
										for model_class in self.all_model_classes:
																lowercase :							Optional[Any]    		=model_class(config=__A   )
																for name, param in model.named_parameters():
																						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='''UperNet does not have tied weights'''   )
				def  lowerCamelCase_	(       self     :		Optional[Any]   ):
										'''simple docstring'''
										pass







				@slow
				def  lowerCamelCase_	(       self     :		Optional[Any]   ):
										'''simple docstring'''
										for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowercase :							Any    		=UperNetForSemanticSegmentation.from_pretrained(__A   )
																self.assertIsNotNone(__A   )






def 				_lowerCAmelCase			(   )				->  List[str]:
						lowercase :							Union[str, Any]    		=hf_hub_download(
						    repo_id='''hf-internal-testing/fixtures_ade20k'''   , repo_type='''dataset'''   , filename='''ADE_val_00000001.jpg'''						)
						lowercase :							Union[str, Any]    		=Image.open(UpperCAmelCase_						).convert('''RGB'''						)
						return image







@require_torch
@require_vision
@slow
class 		__SCREAMING_SNAKE_CASE							(			unittest.TestCase      ):







				def  lowerCamelCase_	(       self     :		Tuple   ):
										'''simple docstring'''
										lowercase :							List[str]    		=AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny'''   )
										lowercase :							Optional[int]    		=UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny'''   ).to(__A   )

										lowercase :							Optional[int]    		=prepare_img()
										lowercase :							Union[str, Any]    		=processor(images=__A		,					return_tensors='''pt'''   ).to(__A   )

										with torch.no_grad():
																lowercase :							Optional[int]    		=model(**__A   )

										lowercase :							Dict    		=torch.Size((1, model.config.num_labels, 512, 512)   )
										self.assertEqual(outputs.logits.shape		,					__A   )

										lowercase :							Any    		=torch.tensor(
										    [[-7.59_58, -7.59_58, -7.43_02], [-7.59_58, -7.59_58, -7.43_02], [-7.47_97, -7.47_97, -7.30_68]]   ).to(__A   )
										self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]		,					__A		,					atol=1E-4   )   )







				def  lowerCamelCase_	(       self     :		List[Any]   ):
										'''simple docstring'''
										lowercase :							Optional[Any]    		=AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny'''   )
										lowercase :							str    		=UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny'''   ).to(__A   )

										lowercase :							Union[str, Any]    		=prepare_img()
										lowercase :							List[Any]    		=processor(images=__A		,					return_tensors='''pt'''   ).to(__A   )

										with torch.no_grad():
																lowercase :							List[str]    		=model(**__A   )

										lowercase :							List[str]    		=torch.Size((1, model.config.num_labels, 512, 512)   )
										self.assertEqual(outputs.logits.shape		,					__A   )

										lowercase :							List[str]    		=torch.tensor(
										    [[-8.81_10, -8.81_10, -8.65_21], [-8.81_10, -8.81_10, -8.65_21], [-8.77_46, -8.77_46, -8.61_30]]   ).to(__A   )
										self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]		,					__A		,					atol=1E-4   )   )







from scipy.stats import pearsonr, spearmanr
from sklearn.metrics import fa_score, matthews_corrcoef

import datasets


_lowercase											=    """\
@inproceedings{wang2019glue,
  title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding},
  author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.},
  note={In the Proceedings of ICLR.},
  year={2019}
}
"""

_lowercase											=    """\
GLUE, the General Language Understanding Evaluation benchmark
(https://gluebenchmark.com/) is a collection of resources for training,
evaluating, and analyzing natural language understanding systems.
"""

_lowercase											=    """
Compute GLUE evaluation metric associated to each GLUE dataset.
Args:
    predictions: list of predictions to score.
        Each translation should be tokenized into a list of tokens.
    references: list of lists of references for each translation.
        Each reference should be tokenized into a list of tokens.
Returns: depending on the GLUE subset, one or several of:
    \"accuracy\": Accuracy
    \"f1\": F1 score
    \"pearson\": Pearson Correlation
    \"spearmanr\": Spearman Correlation
    \"matthews_correlation\": Matthew Correlation
Examples:

    >>> glue_metric = datasets.load_metric('glue', 'sst2')  # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"]
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0}

    >>> glue_metric = datasets.load_metric('glue', 'mrpc')  # 'mrpc' or 'qqp'
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0, 'f1': 1.0}

    >>> glue_metric = datasets.load_metric('glue', 'stsb')
    >>> references = [0., 1., 2., 3., 4., 5.]
    >>> predictions = [0., 1., 2., 3., 4., 5.]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)})
    {'pearson': 1.0, 'spearmanr': 1.0}

    >>> glue_metric = datasets.load_metric('glue', 'cola')
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'matthews_correlation': 1.0}
"""
def 							SCREAMING_SNAKE_CASE_	(					UpperCAmelCase_	:     List[Any]			,							UpperCAmelCase_	:     Union[str, Any]				)			->						int:
       return float((preds == labels).mean()				)
def 							SCREAMING_SNAKE_CASE_	(					UpperCAmelCase_	:     int			,							UpperCAmelCase_	:     str				)			->						Union[str, Any]:
       SCREAMING_SNAKE_CASE_							:				Tuple		=simple_accuracy(UpperCAmelCase_			,							UpperCAmelCase_				)
       SCREAMING_SNAKE_CASE_							:				Union[str, Any]		=float(fa_score(y_true=UpperCAmelCase_			,							y_pred=UpperCAmelCase_				)				)
       return {
           "accuracy": acc,
           "f1": fa,
       }
def 							SCREAMING_SNAKE_CASE_	(					UpperCAmelCase_	:     Optional[int]			,							UpperCAmelCase_	:     Dict				)			->						List[str]:
       SCREAMING_SNAKE_CASE_							:				str		=float(pearsonr(UpperCAmelCase_			,							UpperCAmelCase_				)[0]				)
       SCREAMING_SNAKE_CASE_							:				List[str]		=float(spearmanr(UpperCAmelCase_			,							UpperCAmelCase_				)[0]				)
       return {
           "pearson": pearson_corr,
           "spearmanr": spearman_corr,
       }


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


       def 	_snake_case				(    self				)						->    str:
              if self.config_name not in [
                  "sst2",
                  "mnli",
                  "mnli_mismatched",
                  "mnli_matched",
                  "cola",
                  "stsb",
                  "mrpc",
                  "qqp",
                  "qnli",
                  "rte",
                  "wnli",
                  "hans",
              ]:
                     raise KeyError(
                         '''You should supply a configuration name selected in '''
                         '''["sst2", "mnli", "mnli_mismatched", "mnli_matched", '''
                         '''"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]'''				)
              return datasets.MetricInfo(
                  description=_DESCRIPTION						,       citation=_CITATION						,       inputs_description=_KWARGS_DESCRIPTION						,       features=datasets.Features(
                      {
                          '''predictions''': datasets.Value('''int64''' if self.config_name != '''stsb''' else '''float32'''				),
                          '''references''': datasets.Value('''int64''' if self.config_name != '''stsb''' else '''float32'''				),
                      }				)						,       codebase_urls=[]						,       reference_urls=[]						,       format='''numpy'''						,       )





       def 	_snake_case				(    self						,       __A						,       __A				)						->    List[Any]:
              if self.config_name == "cola":
                     return {"matthews_correlation": matthews_corrcoef(__A						,       __A				)}
              elif self.config_name == "stsb":
                     return pearson_and_spearman(__A						,       __A				)
              elif self.config_name in ["mrpc", "qqp"]:
                     return acc_and_fa(__A						,       __A				)
              elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]:
                     return {"accuracy": simple_accuracy(__A						,       __A				)}
              else:
                     raise KeyError(
                         '''You should supply a configuration name selected in '''
                         '''["sst2", "mnli", "mnli_mismatched", "mnli_matched", '''
                         '''"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]'''				)
