



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







	"""simple docstring"""






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







	def 	A      (		self     :	Union[str, Any]						)     ->						str:
								UpperCAmelCase					:			List[str]									=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

								UpperCAmelCase					:			Any									=						self.get_config()

								return config, pixel_values, labels







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







	def 	A      (		self     :	Union[str, Any]						,     __snake_case     :	List[Any]						,     __snake_case     :	Any						,     __snake_case     :	Tuple						)     ->						Union[str, Any]:
								UpperCAmelCase					:			Optional[int]									=						UperNetForSemanticSegmentation(config=SCREAMING_SNAKE_CASE_						)
								model.to(SCREAMING_SNAKE_CASE_						)
								model.eval()
								UpperCAmelCase					:			Any									=						model(SCREAMING_SNAKE_CASE_						)
								self.parent.assertEqual(
								    result.logits.shape						,     (self.batch_size, self.num_labels, self.image_size, self.image_size)						)







	def 	A      (		self     :	Optional[Any]						)     ->						Optional[Any]:
								UpperCAmelCase					:			List[str]									=						self.prepare_config_and_inputs()
								(
								    UpperCAmelCase							
								)					:			List[Any]									=						config_and_inputs
								UpperCAmelCase					:			List[Any]									=						{"""pixel_values""": pixel_values}
								return config, inputs_dict

@require_torch
class 							SCREAMING_SNAKE_CASE(				_lowerCAmelCase				,						_lowerCAmelCase				,						unittest.TestCase     ):







	"""simple docstring"""






	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 	A      (		self     :	Any						)     ->						Optional[Any]:
								UpperCAmelCase					:			List[str]									=						UperNetModelTester(self						)
								UpperCAmelCase					:			List[str]									=						ConfigTester(self						,     config_class=SCREAMING_SNAKE_CASE_						,     has_text_modality=SCREAMING_SNAKE_CASE_						,     hidden_size=37						)







	def 	A      (		self     :	Any						)     ->						List[str]:
								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 	A      (		self     :	List[Any]						)     ->						Any:
								return







	def 	A      (		self     :	Dict						)     ->						Union[str, Any]:
								UpperCAmelCase					:			Dict									=						self.model_tester.prepare_config_and_inputs_for_common()

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

															UpperCAmelCase					:			Dict									=						["""pixel_values"""]
															self.assertListEqual(arg_names[:1]						,     SCREAMING_SNAKE_CASE_						)







	def 	A      (		self     :	Optional[int]						)     ->						Tuple:
								UpperCAmelCase					:			Union[str, Any]									=						self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*SCREAMING_SNAKE_CASE_						)







	@unittest.skip(reason='''UperNet does not use inputs_embeds'''						)
	def 	A      (		self     :	List[Any]						)     ->						Union[str, Any]:
								pass







	@unittest.skip(reason='''UperNet does not support input and output embeddings'''						)
	def 	A      (		self     :	str						)     ->						Dict:
								pass







	@unittest.skip(reason='''UperNet does not have a base model'''						)
	def 	A      (		self     :	Dict						)     ->						int:
								pass







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







	@require_torch_multi_gpu
	@unittest.skip(reason='''UperNet has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''						)
	def 	A      (		self     :	List[str]						)     ->						Any:
								pass







	@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''						)
	def 	A      (		self     :	Union[str, Any]						)     ->						Any:
								pass







	def 	A      (		self     :	int						)     ->						List[Any]:
								def check_hidden_states_output(__snake_case     :	Optional[Any]						,     __snake_case     :	List[Any]						,     __snake_case     :	str						):
															UpperCAmelCase					:			Any									=						model_class(SCREAMING_SNAKE_CASE_						)
															model.to(SCREAMING_SNAKE_CASE_						)
															model.eval()

															with torch.no_grad():
																						UpperCAmelCase					:			int									=						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_						,     SCREAMING_SNAKE_CASE_						)						)

															UpperCAmelCase					:			int									=						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

															UpperCAmelCase					:			int									=						self.model_tester.num_stages
															self.assertEqual(len(SCREAMING_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]						,     )

								UpperCAmelCase					:			Optional[Any]									=						self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
															UpperCAmelCase					:			Any									=						True
															check_hidden_states_output(SCREAMING_SNAKE_CASE_						,     SCREAMING_SNAKE_CASE_						,     SCREAMING_SNAKE_CASE_						)

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

															check_hidden_states_output(SCREAMING_SNAKE_CASE_						,     SCREAMING_SNAKE_CASE_						,     SCREAMING_SNAKE_CASE_						)







	def 	A      (		self     :	Optional[int]						)     ->						List[str]:
								UpperCAmelCase					:			int									=						self.model_tester.prepare_config_and_inputs_for_common()

								UpperCAmelCase					:			List[Any]									=						_config_zero_init(SCREAMING_SNAKE_CASE_						)
								UpperCAmelCase					:			Optional[Any]									=						_config_zero_init(configs_no_init.backbone_config						)
								for model_class in self.all_model_classes:
															UpperCAmelCase					:			Dict									=						model_class(config=SCREAMING_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 	A      (		self     :	Any						)     ->						str:
								pass







	@slow
	def 	A      (		self     :	Tuple						)     ->						Dict:
								for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															UpperCAmelCase					:			Any									=						UperNetForSemanticSegmentation.from_pretrained(SCREAMING_SNAKE_CASE_						)
															self.assertIsNotNone(SCREAMING_SNAKE_CASE_						)


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




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







	"""simple docstring"""






	def 	A      (		self     :	Optional[Any]						)     ->						Any:
								UpperCAmelCase					:			List[Any]									=						AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny'''						)
								UpperCAmelCase					:			Any									=						UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny'''						).to(SCREAMING_SNAKE_CASE_						)

								UpperCAmelCase					:			Dict									=						prepare_img()
								UpperCAmelCase					:			str									=						processor(images=SCREAMING_SNAKE_CASE_						,     return_tensors='''pt'''						).to(SCREAMING_SNAKE_CASE_						)

								with torch.no_grad():
															UpperCAmelCase					:			str									=						model(**SCREAMING_SNAKE_CASE_						)

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

								UpperCAmelCase					:			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(SCREAMING_SNAKE_CASE_						)
								self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]						,     SCREAMING_SNAKE_CASE_						,     atol=1E-4						)						)







	def 	A      (		self     :	List[str]						)     ->						Optional[int]:
								UpperCAmelCase					:			Any									=						AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny'''						)
								UpperCAmelCase					:			Tuple									=						UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny'''						).to(SCREAMING_SNAKE_CASE_						)

								UpperCAmelCase					:			List[str]									=						prepare_img()
								UpperCAmelCase					:			Optional[Any]									=						processor(images=SCREAMING_SNAKE_CASE_						,     return_tensors='''pt'''						).to(SCREAMING_SNAKE_CASE_						)

								with torch.no_grad():
															UpperCAmelCase					:			List[str]									=						model(**SCREAMING_SNAKE_CASE_						)

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

								UpperCAmelCase					:			Optional[Any]									=						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(SCREAMING_SNAKE_CASE_						)
								self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]						,     SCREAMING_SNAKE_CASE_						,     atol=1E-4						)						)










import numpy as np
import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModelWithProjection, PreTrainedModel

from ...utils import logging


a      :  Union[str, Any]			     =			logging.get_logger(__name__)







class 				_a				(   _lowerCAmelCase					):
			A      		=	CLIPConfig

			A      		=	['''CLIPEncoderLayer''']

			def __init__(self,   SCREAMING_SNAKE_CASE_					)       ->   Optional[Any]:
						super().__init__(SCREAMING_SNAKE_CASE_					)

						UpperCAmelCase_:		Tuple							      =     CLIPVisionModelWithProjection(config.vision_config					)

						UpperCAmelCase_:		Tuple							      =     nn.Linear(config.vision_config.projection_dim,   1					)
						UpperCAmelCase_:		Tuple							      =     nn.Linear(config.vision_config.projection_dim,   1					)

			@torch.no_grad()
			def      __snake_case       (self,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_=0.5,   SCREAMING_SNAKE_CASE_=0.5					)       ->   Tuple:
						UpperCAmelCase_:		Optional[Any]							      =     self.vision_model(SCREAMING_SNAKE_CASE_					)[0]

						UpperCAmelCase_:		List[str]							      =     self.p_head(SCREAMING_SNAKE_CASE_					)
						UpperCAmelCase_:		Any							      =     nsfw_detected.flatten()
						UpperCAmelCase_:		List[Any]							      =     nsfw_detected > p_threshold
						UpperCAmelCase_:		Any							      =     nsfw_detected.tolist()

						if any(SCREAMING_SNAKE_CASE_					):
									logger.warning(
									    """Potential NSFW content was detected in one or more images. A black image will be returned instead."""
									    """ Try again with a different prompt and/or seed."""					)

						for idx, nsfw_detected_ in enumerate(SCREAMING_SNAKE_CASE_					):
									if nsfw_detected_:
												UpperCAmelCase_:		Tuple							      =     np.zeros(images[idx].shape					)

						UpperCAmelCase_:		str							      =     self.w_head(SCREAMING_SNAKE_CASE_					)
						UpperCAmelCase_:		List[Any]							      =     watermark_detected.flatten()
						UpperCAmelCase_:		Tuple							      =     watermark_detected > w_threshold
						UpperCAmelCase_:		int							      =     watermark_detected.tolist()

						if any(SCREAMING_SNAKE_CASE_					):
									logger.warning(
									    """Potential watermarked content was detected in one or more images. A black image will be returned instead."""
									    """ Try again with a different prompt and/or seed."""					)

						for idx, watermark_detected_ in enumerate(SCREAMING_SNAKE_CASE_					):
									if watermark_detected_:
												UpperCAmelCase_:		Any							      =     np.zeros(images[idx].shape					)

						return images, nsfw_detected, watermark_detected






