
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        lowercase      :



		def __init__(				self       ,   A_       ,   A_=13       ,   A_=32       ,   A_=3       ,   A_=4       ,   A_=[10, 20, 30, 40]       ,   A_=[2, 2, 3, 2]       ,   A_=True       ,   A_=True       ,   A_=37       ,   A_="gelu"       ,   A_=10       ,   A_=0.02       ,   A_=["stage2", "stage3", "stage4"]       ,   A_=3       ,   A_=None       ,   )						->      Optional[Any]:
				"""simple docstring"""
				UpperCamelCase						=  parent
				UpperCamelCase						=  batch_size
				UpperCamelCase						=  image_size
				UpperCamelCase						=  num_channels
				UpperCamelCase						=  num_stages
				UpperCamelCase						=  hidden_sizes
				UpperCamelCase						=  depths
				UpperCamelCase						=  is_training
				UpperCamelCase						=  use_labels
				UpperCamelCase						=  intermediate_size
				UpperCamelCase						=  hidden_act
				UpperCamelCase						=  type_sequence_label_size
				UpperCamelCase						=  initializer_range
				UpperCamelCase						=  out_features
				UpperCamelCase						=  num_labels
				UpperCamelCase						=  scope
				UpperCamelCase						=  num_stages



		def __UpperCamelCase				(				self )						->      List[Any]:
				"""simple docstring"""
				UpperCamelCase						=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

				UpperCamelCase						=  self.get_config()

				return config, pixel_values, labels



		def __UpperCamelCase				(				self )						->      List[str]:
				"""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 __UpperCamelCase				(				self )						->      List[Any]:
				"""simple docstring"""
				return UperNetConfig(
				    backbone_config=self.get_backbone_config()       ,   hidden_size=512       ,   pool_scales=[1, 2, 3, 6]       ,   use_auxiliary_head=_UpperCamelCase       ,   auxiliary_loss_weight=0.4       ,   auxiliary_in_channels=40       ,   auxiliary_channels=256       ,   auxiliary_num_convs=1       ,   auxiliary_concat_input=_UpperCamelCase       ,   loss_ignore_index=255       ,   num_labels=self.num_labels       ,   )



		def __UpperCamelCase				(				self       ,   A_       ,   A_       ,   A_ )						->      Tuple:
				"""simple docstring"""
				UpperCamelCase						=  UperNetForSemanticSegmentation(config=_UpperCamelCase )
				model.to(_UpperCamelCase )
				model.eval()
				UpperCamelCase						=  model(_UpperCamelCase )
				self.parent.assertEqual(
				    result.logits.shape       ,   (self.batch_size, self.num_labels, self.image_size, self.image_size) )



		def __UpperCamelCase				(				self )						->      Tuple:
				"""simple docstring"""
				UpperCamelCase						=  self.prepare_config_and_inputs()
				(
				    UpperCamelCase							
				)						=  config_and_inputs
				UpperCamelCase						=  {"""pixel_values""": pixel_values}
				return config, inputs_dict






@require_torch
class        lowercase      (	__lowerCAmelCase						,     __lowerCAmelCase						,     unittest.TestCase     ):
		__lowercase : Optional[int]			  =       (UperNetForSemanticSegmentation,) if is_torch_available() else ()
		__lowercase : Union[str, Any]			  =       {"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {}
		__lowercase : Union[str, Any]			  =       False
		__lowercase : int			  =       False
		__lowercase : Dict			  =       False
		__lowercase : Any			  =       False
		__lowercase : Any			  =       False
		__lowercase : Any			  =       False



		def __UpperCamelCase				(				self )						->      List[str]:
				"""simple docstring"""
				UpperCamelCase						=  UperNetModelTester(self )
				UpperCamelCase						=  ConfigTester(self       ,   config_class=_UpperCamelCase       ,   has_text_modality=_UpperCamelCase       ,   hidden_size=37 )



		def __UpperCamelCase				(				self )						->      Union[str, 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 __UpperCamelCase				(				self )						->      List[str]:
				"""simple docstring"""
				return



		def __UpperCamelCase				(				self )						->      Dict:
				"""simple docstring"""
				UpperCamelCase						=  self.model_tester.prepare_config_and_inputs_for_common()

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

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



		def __UpperCamelCase				(				self )						->      Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase						=  self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_semantic_segmentation(*_UpperCamelCase )



		@unittest.skip(reason='UperNet does not use inputs_embeds' )
		def __UpperCamelCase				(				self )						->      str:
				"""simple docstring"""
				pass



		@unittest.skip(reason='UperNet does not support input and output embeddings' )
		def __UpperCamelCase				(				self )						->      Union[str, Any]:
				"""simple docstring"""
				pass



		@unittest.skip(reason='UperNet does not have a base model' )
		def __UpperCamelCase				(				self )						->      Optional[Any]:
				"""simple docstring"""
				pass



		@unittest.skip(reason='UperNet does not have a base model' )
		def __UpperCamelCase				(				self )						->      Optional[Any]:
				"""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 __UpperCamelCase				(				self )						->      Tuple:
				"""simple docstring"""
				pass



		@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' )
		def __UpperCamelCase				(				self )						->      int:
				"""simple docstring"""
				pass



		def __UpperCamelCase				(				self )						->      str:
				"""simple docstring"""


				def check_hidden_states_output(A_       ,   A_       ,   A_ ):
						UpperCamelCase						=  model_class(_UpperCamelCase )
						model.to(_UpperCamelCase )
						model.eval()

						with torch.no_grad():
								UpperCamelCase						=  model(**self._prepare_for_class(_UpperCamelCase       ,   _UpperCamelCase ) )

						UpperCamelCase						=  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

						UpperCamelCase						=  self.model_tester.num_stages
						self.assertEqual(len(_UpperCamelCase )       ,   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]       ,   )

				UpperCamelCase						=  self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
						UpperCamelCase						=  True
						check_hidden_states_output(_UpperCamelCase       ,   _UpperCamelCase       ,   _UpperCamelCase )

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						UpperCamelCase						=  True

						check_hidden_states_output(_UpperCamelCase       ,   _UpperCamelCase       ,   _UpperCamelCase )



		def __UpperCamelCase				(				self )						->      List[Any]:
				"""simple docstring"""
				UpperCamelCase						=  self.model_tester.prepare_config_and_inputs_for_common()

				UpperCamelCase						=  _config_zero_init(_UpperCamelCase )
				UpperCamelCase						=  _config_zero_init(configs_no_init.backbone_config )
				for model_class in self.all_model_classes:
						UpperCamelCase						=  model_class(config=_UpperCamelCase )
						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 __UpperCamelCase				(				self )						->      Optional[Any]:
				"""simple docstring"""
				pass



		@slow
		def __UpperCamelCase				(				self )						->      Tuple:
				"""simple docstring"""


				for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						UpperCamelCase						=  UperNetForSemanticSegmentation.from_pretrained(_UpperCamelCase )
						self.assertIsNotNone(_UpperCamelCase )






def    A (      )     ->						Tuple:
		'''simple docstring'''

		UpperCamelCase						=  hf_hub_download(
		    repo_id='hf-internal-testing/fixtures_ade20k'					, repo_type='dataset'					, filename='ADE_val_00000001.jpg'				)
		UpperCamelCase						=  Image.open(__A				).convert('RGB'				)
		return image


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



		def __UpperCamelCase				(				self )						->      Dict:
				"""simple docstring"""
				UpperCamelCase						=  AutoImageProcessor.from_pretrained('openmmlab/upernet-swin-tiny' )
				UpperCamelCase						=  UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-swin-tiny' ).to(_UpperCamelCase )

				UpperCamelCase						=  prepare_img()
				UpperCamelCase						=  processor(images=_UpperCamelCase       ,   return_tensors='pt' ).to(_UpperCamelCase )

				with torch.no_grad():
						UpperCamelCase						=  model(**_UpperCamelCase )

				UpperCamelCase						=  torch.Size((1, model.config.num_labels, 512, 512) )
				self.assertEqual(outputs.logits.shape       ,   _UpperCamelCase )

				UpperCamelCase						=  torch.tensor(
				    [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]] ).to(_UpperCamelCase )
				self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]       ,   _UpperCamelCase       ,   atol=1e-4 ) )



		def __UpperCamelCase				(				self )						->      Optional[int]:
				"""simple docstring"""
				UpperCamelCase						=  AutoImageProcessor.from_pretrained('openmmlab/upernet-convnext-tiny' )
				UpperCamelCase						=  UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-convnext-tiny' ).to(_UpperCamelCase )

				UpperCamelCase						=  prepare_img()
				UpperCamelCase						=  processor(images=_UpperCamelCase       ,   return_tensors='pt' ).to(_UpperCamelCase )

				with torch.no_grad():
						UpperCamelCase						=  model(**_UpperCamelCase )

				UpperCamelCase						=  torch.Size((1, model.config.num_labels, 512, 512) )
				self.assertEqual(outputs.logits.shape       ,   _UpperCamelCase )

				UpperCamelCase						=  torch.tensor(
				    [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]] ).to(_UpperCamelCase )
				self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]       ,   _UpperCamelCase       ,   atol=1e-4 ) )


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

import datasets


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

_UpperCAmelCase      :				str          = "\\nGLUE, the General Language Understanding Evaluation benchmark\n(https://gluebenchmark.com/) is a collection of resources for training,\nevaluating, and analyzing natural language understanding systems.\n"

_UpperCAmelCase      :				List[str]          = "\nCompute GLUE evaluation metric associated to each GLUE dataset.\nArgs:\n    predictions: list of predictions to score.\n        Each translation should be tokenized into a list of tokens.\n    references: list of lists of references for each translation.\n        Each reference should be tokenized into a list of tokens.\nReturns: depending on the GLUE subset, one or several of:\n    \"accuracy\": Accuracy\n    \"f1\": F1 score\n    \"pearson\": Pearson Correlation\n    \"spearmanr\": Spearman Correlation\n    \"matthews_correlation\": Matthew Correlation\nExamples:\n\n    >>> glue_metric = datasets.load_metric('glue', 'sst2')  # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"]\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'accuracy': 1.0}\n\n    >>> glue_metric = datasets.load_metric('glue', 'mrpc')  # 'mrpc' or 'qqp'\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'accuracy': 1.0, 'f1': 1.0}\n\n    >>> glue_metric = datasets.load_metric('glue', 'stsb')\n    >>> references = [0., 1., 2., 3., 4., 5.]\n    >>> predictions = [0., 1., 2., 3., 4., 5.]\n    >>> results = glue_metric.compute(predictions=predictions, references=references)\n    >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)})\n    {'pearson': 1.0, 'spearmanr': 1.0}\n\n    >>> glue_metric = datasets.load_metric('glue', 'cola')\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'matthews_correlation': 1.0}\n"


def    A (      lowercase					, lowercase				)     ->						List[str]:
		'''simple docstring'''

		return float((preds == labels).mean()				)


def    A (      lowercase					, lowercase				)     ->						Tuple:
		'''simple docstring'''

		UpperCamelCase						=  simple_accuracy(lowercase					, lowercase				)
		UpperCamelCase						=  float(fa_score(y_true=lowercase					, y_pred=lowercase				)				)
		return {
		    "accuracy": acc,
		    "f1": fa,
		}


def    A (      lowercase					, lowercase				)     ->						Optional[int]:
		'''simple docstring'''

		UpperCamelCase						=  float(pearsonr(lowercase					, lowercase				)[0]				)
		UpperCamelCase						=  float(spearmanr(lowercase					, lowercase				)[0]				)
		return {
		    "pearson": pearson_corr,
		    "spearmanr": spearman_corr,
		}


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



		def __UpperCamelCase				(				self )						->      Optional[Any]:
				"""simple docstring"""
				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 __UpperCamelCase				(				self       ,   A_       ,   A_ )						->      Any:
				"""simple docstring"""
				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"]' )
