



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    :
						'''simple docstring'''


						def __init__(  self    :				List[Any]       ,	snake_case    :				str       ,	snake_case    :				Tuple=13       ,	snake_case    :				Optional[Any]=32       ,	snake_case    :				List[Any]=3       ,	snake_case    :				str=4       ,	snake_case    :				Optional[Any]=[10, 20, 30, 40]       ,	snake_case    :				List[Any]=[2, 2, 3, 2]       ,	snake_case    :				int=True       ,	snake_case    :				Optional[int]=True       ,	snake_case    :				str=37       ,	snake_case    :				Union[str, Any]="gelu"       ,	snake_case    :				Optional[int]=10       ,	snake_case    :				List[str]=0.02       ,	snake_case    :				Dict=["stage2", "stage3", "stage4"]       ,	snake_case    :				List[str]=3       ,	snake_case    :				Union[str, Any]=None       ,	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     int						=				parent
											SCREAMING_SNAKE_CASE							:     int						=				batch_size
											SCREAMING_SNAKE_CASE							:     List[str]						=				image_size
											SCREAMING_SNAKE_CASE							:     Optional[int]						=				num_channels
											SCREAMING_SNAKE_CASE							:     Any						=				num_stages
											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				hidden_sizes
											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				depths
											SCREAMING_SNAKE_CASE							:     int						=				is_training
											SCREAMING_SNAKE_CASE							:     int						=				use_labels
											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				intermediate_size
											SCREAMING_SNAKE_CASE							:     Dict						=				hidden_act
											SCREAMING_SNAKE_CASE							:     Tuple						=				type_sequence_label_size
											SCREAMING_SNAKE_CASE							:     List[Any]						=				initializer_range
											SCREAMING_SNAKE_CASE							:     str						=				out_features
											SCREAMING_SNAKE_CASE							:     List[Any]						=				num_labels
											SCREAMING_SNAKE_CASE							:     Optional[int]						=				scope
											SCREAMING_SNAKE_CASE							:     str						=				num_stages







						def       lowerCamelCase_				(  self    :				Tuple	):



											'''simple docstring'''


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

											SCREAMING_SNAKE_CASE							:     List[str]						=				None
											if self.use_labels:
																SCREAMING_SNAKE_CASE							:     Optional[int]						=				ids_tensor([self.batch_size]       ,	self.type_sequence_label_size	)

											SCREAMING_SNAKE_CASE							:     Tuple						=				self.get_config()

											return config, pixel_values, labels







						def       lowerCamelCase_				(  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       lowerCamelCase_				(  self    :				Tuple	):



											'''simple docstring'''


											return UperNetConfig(
											    backbone_config=self.get_backbone_config()       ,	hidden_size=512       ,	pool_scales=[1, 2, 3, 6]       ,	use_auxiliary_head=snake_case       ,	auxiliary_loss_weight=0.4       ,	auxiliary_in_channels=40       ,	auxiliary_channels=256       ,	auxiliary_num_convs=1       ,	auxiliary_concat_input=snake_case       ,	loss_ignore_index=255       ,	num_labels=self.num_labels       ,	)







						def       lowerCamelCase_				(  self    :				Any       ,	snake_case    :				Optional[int]       ,	snake_case    :				Optional[Any]       ,	snake_case    :				List[str]	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Any						=				UperNetForSemanticSegmentation(config=snake_case	)
											model.to(snake_case	)
											model.eval()
											SCREAMING_SNAKE_CASE							:     str						=				model(snake_case	)
											self.parent.assertEqual(
											    result.logits.shape       ,	(self.batch_size, self.num_labels, self.image_size, self.image_size)	)







						def       lowerCamelCase_				(  self    :				Any	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     List[Any]						=				self.prepare_config_and_inputs()
											(
											    (
											    SCREAMING_SNAKE_CASE
											)					,  (
											    SCREAMING_SNAKE_CASE
											)					,  (
											    SCREAMING_SNAKE_CASE
											)					,  
											)							:     Tuple						=				config_and_inputs
											SCREAMING_SNAKE_CASE							:     Any						=				{'pixel_values': pixel_values}
											return config, inputs_dict


@require_torch
class 						lowercase    (							SCREAMING_SNAKE_CASE_      ,    SCREAMING_SNAKE_CASE_      ,    unittest.TestCase):
						'''simple docstring'''


						UpperCAmelCase			:		int		=		(UperNetForSemanticSegmentation,) if is_torch_available() else ()
						UpperCAmelCase			:		Optional[Any]		=		{'image-segmentation': UperNetForSemanticSegmentation} if is_torch_available() else {}
						UpperCAmelCase			:		Union[str, Any]		=		False
						UpperCAmelCase			:		str		=		False
						UpperCAmelCase			:		str		=		False
						UpperCAmelCase			:		Any		=		False
						UpperCAmelCase			:		List[str]		=		False
						UpperCAmelCase			:		List[Any]		=		False







						def       lowerCamelCase_				(  self    :				Tuple	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Tuple						=				UperNetModelTester(self	)
											SCREAMING_SNAKE_CASE							:     List[str]						=				ConfigTester(self       ,	config_class=snake_case       ,	has_text_modality=snake_case       ,	hidden_size=37	)







						def       lowerCamelCase_				(  self    :				Optional[int]	):



											'''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    :				str	):



											'''simple docstring'''


											return







						def       lowerCamelCase_				(  self    :				Optional[int]	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE					,  SCREAMING_SNAKE_CASE							:     Any						=				self.model_tester.prepare_config_and_inputs_for_common()

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

																SCREAMING_SNAKE_CASE							:     int						=				['pixel_values']
																self.assertListEqual(arg_names[:1]       ,	snake_case	)







						def       lowerCamelCase_				(  self    :				str	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Any						=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_semantic_segmentation(*snake_case	)







						@unittest.skip(reason='UperNet does not use inputs_embeds'	)
						def       lowerCamelCase_				(  self    :				Dict	):



											'''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    :				List[str]	):



											'''simple docstring'''


											pass







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



											'''simple docstring'''


											pass







						@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'	)
						def       lowerCamelCase_				(  self    :				Tuple	):



											'''simple docstring'''


											pass







						def       lowerCamelCase_				(  self    :				Dict	):



											'''simple docstring'''


											def check_hidden_states_output(snake_case    :				str       ,	snake_case    :				Dict       ,	snake_case    :				Optional[Any]	):
																SCREAMING_SNAKE_CASE							:     List[str]						=				model_class(snake_case	)
																model.to(snake_case	)
																model.eval()

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

																SCREAMING_SNAKE_CASE							:     Optional[Any]						=				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																SCREAMING_SNAKE_CASE							:     Any						=				self.model_tester.num_stages
																self.assertEqual(len(snake_case	)       ,	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]       ,	)

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

											for model_class in self.all_model_classes:
																SCREAMING_SNAKE_CASE							:     Any						=				True
																check_hidden_states_output(snake_case       ,	snake_case       ,	snake_case	)

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

																check_hidden_states_output(snake_case       ,	snake_case       ,	snake_case	)







						def       lowerCamelCase_				(  self    :				Optional[Any]	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE					,  SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				self.model_tester.prepare_config_and_inputs_for_common()

											SCREAMING_SNAKE_CASE							:     Dict						=				_config_zero_init(snake_case	)
											SCREAMING_SNAKE_CASE							:     Dict						=				_config_zero_init(configs_no_init.backbone_config	)
											for model_class in self.all_model_classes:
																SCREAMING_SNAKE_CASE							:     Optional[Any]						=				model_class(config=snake_case	)
																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    :				Union[str, Any]	):



											'''simple docstring'''


											pass







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



											'''simple docstring'''


											for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																SCREAMING_SNAKE_CASE							:     Tuple						=				UperNetForSemanticSegmentation.from_pretrained(snake_case	)
																self.assertIsNotNone(snake_case	)


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



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


						def       lowerCamelCase_				(  self    :				str	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     List[Any]						=				AutoImageProcessor.from_pretrained('openmmlab/upernet-swin-tiny'	)
											SCREAMING_SNAKE_CASE							:     Optional[int]						=				UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-swin-tiny'	).to(snake_case	)

											SCREAMING_SNAKE_CASE							:     int						=				prepare_img()
											SCREAMING_SNAKE_CASE							:     str						=				processor(images=snake_case       ,	return_tensors='pt'	).to(snake_case	)

											with torch.no_grad():
																SCREAMING_SNAKE_CASE							:     Tuple						=				model(**snake_case	)

											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				torch.Size((1, model.config.num_labels, 512, 512)	)
											self.assertEqual(outputs.logits.shape       ,	snake_case	)

											SCREAMING_SNAKE_CASE							:     Dict						=				torch.tensor(
											    [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]]	).to(snake_case	)
											self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]       ,	snake_case       ,	atol=1E-4	)	)







						def       lowerCamelCase_				(  self    :				List[str]	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Optional[Any]						=				AutoImageProcessor.from_pretrained('openmmlab/upernet-convnext-tiny'	)
											SCREAMING_SNAKE_CASE							:     Optional[int]						=				UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-convnext-tiny'	).to(snake_case	)

											SCREAMING_SNAKE_CASE							:     List[str]						=				prepare_img()
											SCREAMING_SNAKE_CASE							:     str						=				processor(images=snake_case       ,	return_tensors='pt'	).to(snake_case	)

											with torch.no_grad():
																SCREAMING_SNAKE_CASE							:     List[str]						=				model(**snake_case	)

											SCREAMING_SNAKE_CASE							:     str						=				torch.Size((1, model.config.num_labels, 512, 512)	)
											self.assertEqual(outputs.logits.shape       ,	snake_case	)

											SCREAMING_SNAKE_CASE							:     str						=				torch.tensor(
											    [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]]	).to(snake_case	)
											self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]       ,	snake_case       ,	atol=1E-4	)	)




from datetime import datetime

import matplotlib.pyplot as plt
import torch

def        __a			(   __lowerCAmelCase   )     ->     int:
					for param in module.parameters():
										SCREAMING_SNAKE_CASE							:     List[Any]						=				False

def        __a			(   )     ->     List[str]:
					SCREAMING_SNAKE_CASE							:     List[Any]						=				'cuda' if torch.cuda.is_available() else 'cpu'
					if torch.backends.mps.is_available() and torch.backends.mps.is_built():
										SCREAMING_SNAKE_CASE							:     List[str]						=				'mps'
					if device == "mps":
										print(
										    'WARNING: MPS currently doesn\'t seem to work, and messes up backpropagation without any visible torch'
										    ' errors. I recommend using CUDA on a colab notebook or CPU instead if you\'re facing inexplicable issues'
										    ' with generations.'   )
					return device

def        __a			(   __lowerCAmelCase   )     ->     List[Any]:
					SCREAMING_SNAKE_CASE							:     Optional[int]						=				plt.imshow(__lowerCAmelCase   )
					fig.axes.get_xaxis().set_visible(__lowerCAmelCase   )
					fig.axes.get_yaxis().set_visible(__lowerCAmelCase   )
					plt.show()



def        __a			(   )     ->     Optional[Any]:
					SCREAMING_SNAKE_CASE							:     str						=				datetime.now()
					SCREAMING_SNAKE_CASE							:     Optional[int]						=				current_time.strftime('%H:%M:%S'   )
					return timestamp