



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




							def __init__(			self	:			Tuple							,							A_	:			Tuple							,							A_	:			List[str]=1_3							,							A_	:			str=3_2							,							A_	:			str=3							,							A_	:			Union[str, Any]=4							,							A_	:			Tuple=[1_0, 2_0, 3_0, 4_0]							,							A_	:			str=[2, 2, 3, 2]							,							A_	:			Tuple=True							,							A_	:			Dict=True							,							A_	:			Any=3_7							,							A_	:			List[Any]="gelu"							,							A_	:			List[str]=1_0							,							A_	:			Any=0.02							,							A_	:			Tuple=["stage2", "stage3", "stage4"]							,							A_	:			Tuple=3							,							A_	:			int=None							,							):
									lowerCAmelCase_     :	List[str]							   =					parent
									lowerCAmelCase_     :	List[str]							   =					batch_size
									lowerCAmelCase_     :	List[str]							   =					image_size
									lowerCAmelCase_     :	Union[str, Any]							   =					num_channels
									lowerCAmelCase_     :	Union[str, Any]							   =					num_stages
									lowerCAmelCase_     :	str							   =					hidden_sizes
									lowerCAmelCase_     :	Optional[Any]							   =					depths
									lowerCAmelCase_     :	Dict							   =					is_training
									lowerCAmelCase_     :	str							   =					use_labels
									lowerCAmelCase_     :	List[Any]							   =					intermediate_size
									lowerCAmelCase_     :	str							   =					hidden_act
									lowerCAmelCase_     :	str							   =					type_sequence_label_size
									lowerCAmelCase_     :	Union[str, Any]							   =					initializer_range
									lowerCAmelCase_     :	Tuple							   =					out_features
									lowerCAmelCase_     :	int							   =					num_labels
									lowerCAmelCase_     :	Optional[int]							   =					scope
									lowerCAmelCase_     :	Union[str, Any]							   =					num_stages




							def   UpperCAmelCase__     (			self	:			Tuple):
									lowerCAmelCase_     :	str							   =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

									lowerCAmelCase_     :	str							   =					None
									if self.use_labels:
											lowerCAmelCase_     :	List[Any]							   =					ids_tensor([self.batch_size]							,							self.type_sequence_label_size)

									lowerCAmelCase_     :	str							   =					self.get_config()

									return config, pixel_values, labels




							def   UpperCAmelCase__     (			self	:			int):
									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	:			Any):
									return UperNetConfig(
									    backbone_config=self.get_backbone_config()							,							hidden_size=5_1_2							,							pool_scales=[1, 2, 3, 6]							,							use_auxiliary_head=_lowercase							,							auxiliary_loss_weight=0.4							,							auxiliary_in_channels=4_0							,							auxiliary_channels=2_5_6							,							auxiliary_num_convs=1							,							auxiliary_concat_input=_lowercase							,							loss_ignore_index=2_5_5							,							num_labels=self.num_labels							,							)




							def   UpperCAmelCase__     (			self	:			Optional[int]							,							A_	:			Dict							,							A_	:			Optional[int]							,							A_	:			int):
									lowerCAmelCase_     :	List[str]							   =					UperNetForSemanticSegmentation(config=_lowercase)
									model.to(_lowercase)
									model.eval()
									lowerCAmelCase_     :	Union[str, Any]							   =					model(_lowercase)
									self.parent.assertEqual(
									    result.logits.shape							,							(self.batch_size, self.num_labels, self.image_size, self.image_size))







							def   UpperCAmelCase__     (			self	:			Union[str, Any]):
									lowerCAmelCase_     :	Optional[Any]							   =					self.prepare_config_and_inputs()
									(
									    (
									    lowerCAmelCase_       
									)   ,    (
									    lowerCAmelCase_       
									)   ,    (
									    lowerCAmelCase_       
									)   ,           
									)     :	Any							   =					config_and_inputs
									lowerCAmelCase_     :	int							   =					{'''pixel_values''': pixel_values}
									return config, inputs_dict





@require_torch
class 							__snake_case		(	_SCREAMING_SNAKE_CASE		,_SCREAMING_SNAKE_CASE		,unittest.TestCase		):
							_a      =					(UperNetForSemanticSegmentation,) if is_torch_available() else ()
							_a      =					{'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
							_a      =					False
							_a      =					False
							_a      =					False
							_a      =					False
							_a      =					False
							_a      =					False




							def   UpperCAmelCase__     (			self	:			Tuple):
									lowerCAmelCase_     :	str							   =					UperNetModelTester(self)
									lowerCAmelCase_     :	List[Any]							   =					ConfigTester(self							,							config_class=_lowercase							,							has_text_modality=_lowercase							,							hidden_size=3_7)




							def   UpperCAmelCase__     (			self	:			Any):
									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	:			Optional[int]):
									return




							def   UpperCAmelCase__     (			self	:			Tuple):
									lowerCAmelCase_   ,    lowerCAmelCase_     :	Optional[int]							   =					self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											lowerCAmelCase_     :	Optional[int]							   =					model_class(_lowercase)
											lowerCAmelCase_     :	Tuple							   =					inspect.signature(model.forward)
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											lowerCAmelCase_     :	List[str]							   =					[*signature.parameters.keys()]

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




							def   UpperCAmelCase__     (			self	:			Dict):
									lowerCAmelCase_     :	Optional[Any]							   =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_semantic_segmentation(*_lowercase)




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




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




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




							@unittest.skip(reason='''UperNet does not have a base model''')
							def   UpperCAmelCase__     (			self	:			int):
									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	:			int):
									pass




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




							def   UpperCAmelCase__     (			self	:			Tuple):



									def check_hidden_states_output(A_	:			int							,							A_	:			List[Any]							,							A_	:			List[str]):
											lowerCAmelCase_     :	Union[str, Any]							   =					model_class(_lowercase)
											model.to(_lowercase)
											model.eval()

											with torch.no_grad():
													lowerCAmelCase_     :	Tuple							   =					model(**self._prepare_for_class(_lowercase							,							_lowercase))

											lowerCAmelCase_     :	Union[str, Any]							   =					outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

											lowerCAmelCase_     :	Optional[int]							   =					self.model_tester.num_stages
											self.assertEqual(len(_lowercase)							,							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]							,							)

									lowerCAmelCase_   ,    lowerCAmelCase_     :	List[Any]							   =					self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											lowerCAmelCase_     :	Optional[Any]							   =					True
											check_hidden_states_output(_lowercase							,							_lowercase							,							_lowercase)

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

											check_hidden_states_output(_lowercase							,							_lowercase							,							_lowercase)




							def   UpperCAmelCase__     (			self	:			Any):
									lowerCAmelCase_   ,    lowerCAmelCase_     :	Union[str, Any]							   =					self.model_tester.prepare_config_and_inputs_for_common()

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







							@slow
							def   UpperCAmelCase__     (			self	:			Any):



									for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowerCAmelCase_     :	Any							   =					UperNetForSemanticSegmentation.from_pretrained(_lowercase)
											self.assertIsNotNone(_lowercase)





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





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




							def   UpperCAmelCase__     (			self	:			Any):
									lowerCAmelCase_     :	Optional[Any]							   =					AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny''')
									lowerCAmelCase_     :	Optional[int]							   =					UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny''').to(_lowercase)

									lowerCAmelCase_     :	Optional[int]							   =					prepare_img()
									lowerCAmelCase_     :	int							   =					processor(images=_lowercase							,							return_tensors='''pt''').to(_lowercase)

									with torch.no_grad():
											lowerCAmelCase_     :	Any							   =					model(**_lowercase)

									lowerCAmelCase_     :	Optional[Any]							   =					torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2))
									self.assertEqual(outputs.logits.shape							,							_lowercase)

									lowerCAmelCase_     :	Union[str, Any]							   =					torch.tensor(
									    [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]]).to(_lowercase)
									self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]							,							_lowercase							,							atol=1e-4))







							def   UpperCAmelCase__     (			self	:			Union[str, Any]):
									lowerCAmelCase_     :	Optional[Any]							   =					AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny''')
									lowerCAmelCase_     :	List[str]							   =					UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny''').to(_lowercase)

									lowerCAmelCase_     :	Dict							   =					prepare_img()
									lowerCAmelCase_     :	Union[str, Any]							   =					processor(images=_lowercase							,							return_tensors='''pt''').to(_lowercase)

									with torch.no_grad():
											lowerCAmelCase_     :	str							   =					model(**_lowercase)

									lowerCAmelCase_     :	Optional[Any]							   =					torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2))
									self.assertEqual(outputs.logits.shape							,							_lowercase)

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







'''simple docstring'''



_lowercase          =						"""
# Transformers 설치 방법
! pip install transformers datasets
# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.
# ! pip install git+https://github.com/huggingface/transformers.git
"""

_lowercase          =						[{"""type""": """code""", """content""": INSTALL_CONTENT}]
_lowercase          =						{
    """{processor_class}""": """FakeProcessorClass""",
    """{model_class}""": """FakeModelClass""",
    """{object_class}""": """FakeObjectClass""",
}
