



'''simple docstring'''






import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import (
						    MODEL_MAPPING,
						    SegformerForImageClassification,
						    SegformerForSemanticSegmentation,
						    SegformerModel,
						)
						from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import SegformerImageProcessor

class        SCREAMING_SNAKE_CASE(							__UpperCAmelCase    ):







	"""simple docstring"""






	def   A			(		self    :  Any	)    ->	str:
								UpperCAmelCase				: Any											=	self.config_class(**self.inputs_dict	)
								self.parent.assertTrue(hasattr(__snake_case	,    '''hidden_sizes'''	)	)
								self.parent.assertTrue(hasattr(__snake_case	,    '''num_attention_heads'''	)	)
								self.parent.assertTrue(hasattr(__snake_case	,    '''num_encoder_blocks'''	)	)

class        SCREAMING_SNAKE_CASE:







	"""simple docstring"""






	def __init__(		self    :  List[str]	,    __snake_case    :  str	,    __snake_case    :  int=13	,    __snake_case    :  Optional[Any]=64	,    __snake_case    :  int=3	,    __snake_case    :  List[Any]=4	,    __snake_case    :  Dict=[2, 2, 2, 2]	,    __snake_case    :  Union[str, Any]=[8, 4, 2, 1]	,    __snake_case    :  List[str]=[16, 32, 64, 128]	,    __snake_case    :  Optional[int]=[1, 4, 8, 16]	,    __snake_case    :  Union[str, Any]=[1, 2, 4, 8]	,    __snake_case    :  Dict=True	,    __snake_case    :  Optional[int]=True	,    __snake_case    :  Optional[Any]="gelu"	,    __snake_case    :  Tuple=0.1	,    __snake_case    :  List[str]=0.1	,    __snake_case    :  Union[str, Any]=0.02	,    __snake_case    :  Dict=3	,    __snake_case    :  Dict=None	,    )    ->	Optional[Any]:
								UpperCAmelCase				: Tuple											=	parent
								UpperCAmelCase				: List[str]											=	batch_size
								UpperCAmelCase				: Tuple											=	image_size
								UpperCAmelCase				: Tuple											=	num_channels
								UpperCAmelCase				: int											=	num_encoder_blocks
								UpperCAmelCase				: Union[str, Any]											=	sr_ratios
								UpperCAmelCase				: Any											=	depths
								UpperCAmelCase				: List[Any]											=	hidden_sizes
								UpperCAmelCase				: Tuple											=	downsampling_rates
								UpperCAmelCase				: List[str]											=	num_attention_heads
								UpperCAmelCase				: Tuple											=	is_training
								UpperCAmelCase				: str											=	use_labels
								UpperCAmelCase				: int											=	hidden_act
								UpperCAmelCase				: List[str]											=	hidden_dropout_prob
								UpperCAmelCase				: str											=	attention_probs_dropout_prob
								UpperCAmelCase				: Any											=	initializer_range
								UpperCAmelCase				: List[str]											=	num_labels
								UpperCAmelCase				: str											=	scope







	def   A			(		self    :  Optional[Any]	)    ->	Optional[int]:
								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.image_size, self.image_size]	,    self.num_labels	)

								UpperCAmelCase				: Any											=	self.get_config()
								return config, pixel_values, labels







	def   A			(		self    :  Union[str, Any]	)    ->	Optional[int]:
								return SegformerConfig(
								    image_size=self.image_size	,    num_channels=self.num_channels	,    num_encoder_blocks=self.num_encoder_blocks	,    depths=self.depths	,    hidden_sizes=self.hidden_sizes	,    num_attention_heads=self.num_attention_heads	,    hidden_act=self.hidden_act	,    hidden_dropout_prob=self.hidden_dropout_prob	,    attention_probs_dropout_prob=self.attention_probs_dropout_prob	,    initializer_range=self.initializer_range	,    )







	def   A			(		self    :  Union[str, Any]	,    __snake_case    :  Any	,    __snake_case    :  int	,    __snake_case    :  Union[str, Any]	)    ->	List[Any]:
								UpperCAmelCase				: int											=	SegformerModel(config=__snake_case	)
								model.to(__snake_case	)
								model.eval()
								UpperCAmelCase				: List[str]											=	model(__snake_case	)
								UpperCAmelCase				: int											=	self.image_size // (self.downsampling_rates[-1] * 2)
								self.parent.assertEqual(
								    result.last_hidden_state.shape	,    (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)	)







	def   A			(		self    :  int	,    __snake_case    :  Optional[Any]	,    __snake_case    :  Tuple	,    __snake_case    :  int	)    ->	Any:
								UpperCAmelCase				: int											=	self.num_labels
								UpperCAmelCase				: Optional[int]											=	SegformerForSemanticSegmentation(__snake_case	)
								model.to(__snake_case	)
								model.eval()
								UpperCAmelCase				: Tuple											=	model(__snake_case	)
								self.parent.assertEqual(
								    result.logits.shape	,    (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)	)
								UpperCAmelCase				: Tuple											=	model(__snake_case	,    labels=__snake_case	)
								self.parent.assertEqual(
								    result.logits.shape	,    (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)	)
								self.parent.assertGreater(result.loss	,    0.0	)







	def   A			(		self    :  Optional[int]	,    __snake_case    :  Optional[int]	,    __snake_case    :  Tuple	,    __snake_case    :  Optional[int]	)    ->	Optional[Any]:
								UpperCAmelCase				: Dict											=	1
								UpperCAmelCase				: Any											=	SegformerForSemanticSegmentation(config=__snake_case	)
								model.to(__snake_case	)
								model.eval()
								UpperCAmelCase				: List[Any]											=	torch.randint(0	,    1	,    (self.batch_size, self.image_size, self.image_size)	).to(__snake_case	)
								UpperCAmelCase				: Tuple											=	model(__snake_case	,    labels=__snake_case	)
								self.parent.assertGreater(result.loss	,    0.0	)







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

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







	"""simple docstring"""






	lowerCamelCase__    				= (
	    (
	        SegformerModel,
	        SegformerForSemanticSegmentation,
	        SegformerForImageClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	lowerCamelCase__    				= (
	    {
	        """feature-extraction""": SegformerModel,
	        """image-classification""": SegformerForImageClassification,
	        """image-segmentation""": SegformerForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

	lowerCamelCase__    				= True
	lowerCamelCase__    				= False
	lowerCamelCase__    				= False
	lowerCamelCase__    				= False







	def   A			(		self    :  Tuple	)    ->	List[Any]:
								UpperCAmelCase				: List[Any]											=	SegformerModelTester(self	)
								UpperCAmelCase				: Union[str, Any]											=	SegformerConfigTester(self	,    config_class=__snake_case	)







	def   A			(		self    :  str	)    ->	List[Any]:
								self.config_tester.run_common_tests()







	def   A			(		self    :  str	)    ->	Union[str, Any]:
								UpperCAmelCase				: List[str]											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__snake_case	)







	def   A			(		self    :  Dict	)    ->	Optional[Any]:
								UpperCAmelCase				: Tuple											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_binary_image_segmentation(*__snake_case	)







	def   A			(		self    :  List[Any]	)    ->	Optional[Any]:
								UpperCAmelCase				: Dict											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_segmentation(*__snake_case	)







	@unittest.skip('''SegFormer does not use inputs_embeds'''	)
	def   A			(		self    :  List[str]	)    ->	int:
								pass







	@unittest.skip('''SegFormer does not have get_input_embeddings method and get_output_embeddings methods'''	)
	def   A			(		self    :  List[str]	)    ->	Dict:
								pass







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

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

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







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

								for model_class in self.all_model_classes:
															UpperCAmelCase				: List[str]											=	True
															UpperCAmelCase				: Optional[int]											=	False
															UpperCAmelCase				: str											=	True
															UpperCAmelCase				: List[Any]											=	model_class(__snake_case	)
															model.to(__snake_case	)
															model.eval()
															with torch.no_grad():
																						UpperCAmelCase				: Tuple											=	model(**self._prepare_for_class(__snake_case	,    __snake_case	)	)
															UpperCAmelCase				: int											=	outputs.attentions

															UpperCAmelCase				: Dict											=	sum(self.model_tester.depths	)
															self.assertEqual(len(__snake_case	)	,    __snake_case	)

															# check that output_attentions also work using config
															del inputs_dict["output_attentions"]
															UpperCAmelCase				: Any											=	True
															UpperCAmelCase				: str											=	model_class(__snake_case	)
															model.to(__snake_case	)
															model.eval()
															with torch.no_grad():
																						UpperCAmelCase				: Optional[Any]											=	model(**self._prepare_for_class(__snake_case	,    __snake_case	)	)
															UpperCAmelCase				: Optional[Any]											=	outputs.attentions

															self.assertEqual(len(__snake_case	)	,    __snake_case	)

															# verify the first attentions (first block, first layer)
															UpperCAmelCase				: int											=	(self.model_tester.image_size // 4) ** 2
															UpperCAmelCase				: Optional[int]											=	(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
															self.assertListEqual(
															    list(attentions[0].shape[-3:]	)	,    [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]	,    )

															# verify the last attentions (last block, last layer)
															UpperCAmelCase				: Dict											=	(self.model_tester.image_size // 32) ** 2
															UpperCAmelCase				: str											=	(self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
															self.assertListEqual(
															    list(attentions[-1].shape[-3:]	)	,    [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]	,    )
															UpperCAmelCase				: Union[str, Any]											=	len(__snake_case	)

															# Check attention is always last and order is fine
															UpperCAmelCase				: Any											=	True
															UpperCAmelCase				: List[Any]											=	True
															UpperCAmelCase				: Optional[Any]											=	model_class(__snake_case	)
															model.to(__snake_case	)
															model.eval()
															with torch.no_grad():
																						UpperCAmelCase				: Tuple											=	model(**self._prepare_for_class(__snake_case	,    __snake_case	)	)

															self.assertEqual(out_len + 1	,    len(__snake_case	)	)

															UpperCAmelCase				: int											=	outputs.attentions

															self.assertEqual(len(__snake_case	)	,    __snake_case	)
															# verify the first attentions (first block, first layer)
															UpperCAmelCase				: Any											=	(self.model_tester.image_size // 4) ** 2
															UpperCAmelCase				: Optional[int]											=	(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
															self.assertListEqual(
															    list(self_attentions[0].shape[-3:]	)	,    [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]	,    )







	def   A			(		self    :  Optional[int]	)    ->	int:

								def check_hidden_states_output(__snake_case    :  Any	,    __snake_case    :  int	,    __snake_case    :  List[str]	):
															UpperCAmelCase				: Tuple											=	model_class(__snake_case	)
															model.to(__snake_case	)
															model.eval()

															with torch.no_grad():
																						UpperCAmelCase				: List[Any]											=	model(**self._prepare_for_class(__snake_case	,    __snake_case	)	)

															UpperCAmelCase				: int											=	outputs.hidden_states

															UpperCAmelCase				: List[str]											=	self.model_tester.num_encoder_blocks
															self.assertEqual(len(__snake_case	)	,    __snake_case	)

															# verify the first hidden states (first block)
															self.assertListEqual(
															    list(hidden_states[0].shape[-3:]	)	,    [
															        self.model_tester.hidden_sizes[0],
															        self.model_tester.image_size // 4,
															        self.model_tester.image_size // 4,
															    ]	,    )

								UpperCAmelCase							,	UpperCAmelCase				: Tuple											=	self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
															UpperCAmelCase				: Union[str, 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"]
															UpperCAmelCase				: List[Any]											=	True

															check_hidden_states_output(__snake_case	,    __snake_case	,    __snake_case	)







	def   A			(		self    :  Optional[Any]	)    ->	List[str]:

								if not self.model_tester.is_training:
															return

								UpperCAmelCase							,	UpperCAmelCase				: Union[str, Any]											=	self.model_tester.prepare_config_and_inputs_for_common()
								UpperCAmelCase				: Optional[int]											=	True

								for model_class in self.all_model_classes:
															if model_class in get_values(__snake_case	):
																						continue

															UpperCAmelCase				: str											=	model_class(__snake_case	)
															model.to(__snake_case	)
															model.train()
															UpperCAmelCase				: str											=	self._prepare_for_class(__snake_case	,    __snake_case	,    return_labels=__snake_case	)
															UpperCAmelCase				: Optional[int]											=	model(**__snake_case	).loss
															loss.backward()







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







	@slow
	def   A			(		self    :  Optional[Any]	)    ->	str:

								for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															UpperCAmelCase				: int											=	SegformerModel.from_pretrained(__snake_case	)
															self.assertIsNotNone(__snake_case	)


def 				snake_case_  (      )   ->     Union[str, Any]:
							UpperCAmelCase				: Dict											=	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
							return image




@require_torch
class        SCREAMING_SNAKE_CASE(							unittest.TestCase    ):







	"""simple docstring"""






	@slow
	def   A			(		self    :  List[str]	)    ->	Any:
								UpperCAmelCase				: Tuple											=	SegformerImageProcessor(
								    image_scale=(512, 512)	,    keep_ratio=__snake_case	,    align=__snake_case	,    do_random_crop=__snake_case	)
								UpperCAmelCase				: int											=	SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512'''	).to(
								    __snake_case	)

								UpperCAmelCase				: str											=	prepare_img()
								UpperCAmelCase				: str											=	image_processor(images=__snake_case	,    return_tensors='''pt'''	)
								UpperCAmelCase				: List[Any]											=	encoded_inputs.pixel_values.to(__snake_case	)

								with torch.no_grad():
															UpperCAmelCase				: str											=	model(__snake_case	)

								UpperCAmelCase				: Dict											=	torch.Size((1, model.config.num_labels, 128, 128)	)
								self.assertEqual(outputs.logits.shape	,    __snake_case	)

								UpperCAmelCase				: Any											=	torch.tensor(
								    [
								        [[-4.63_10, -5.52_32, -6.23_56], [-5.19_21, -6.14_44, -6.59_96], [-5.44_24, -6.27_90, -6.75_74]],
								        [[-12.13_91, -13.31_22, -13.95_54], [-12.87_32, -13.93_52, -14.35_63], [-12.94_38, -13.82_26, -14.25_13]],
								        [[-12.51_34, -13.46_86, -14.49_15], [-12.86_69, -14.43_43, -14.77_58], [-13.25_23, -14.58_19, -15.06_94]],
								    ]	).to(__snake_case	)
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]	,    __snake_case	,    atol=1E-4	)	)







	@slow
	def   A			(		self    :  Optional[Any]	)    ->	List[str]:
								UpperCAmelCase				: Union[str, Any]											=	SegformerImageProcessor(
								    image_scale=(512, 512)	,    keep_ratio=__snake_case	,    align=__snake_case	,    do_random_crop=__snake_case	)
								UpperCAmelCase				: int											=	SegformerForSemanticSegmentation.from_pretrained(
								    '''nvidia/segformer-b1-finetuned-cityscapes-1024-1024'''	).to(__snake_case	)

								UpperCAmelCase				: List[str]											=	prepare_img()
								UpperCAmelCase				: int											=	image_processor(images=__snake_case	,    return_tensors='''pt'''	)
								UpperCAmelCase				: int											=	encoded_inputs.pixel_values.to(__snake_case	)

								with torch.no_grad():
															UpperCAmelCase				: Any											=	model(__snake_case	)

								UpperCAmelCase				: List[Any]											=	torch.Size((1, model.config.num_labels, 128, 128)	)
								self.assertEqual(outputs.logits.shape	,    __snake_case	)

								UpperCAmelCase				: Union[str, Any]											=	torch.tensor(
								    [
								        [[-13.57_48, -13.91_11, -12.65_00], [-14.35_00, -15.36_83, -14.23_28], [-14.75_32, -16.04_24, -15.60_87]],
								        [[-17.16_51, -15.87_25, -12.96_53], [-17.25_80, -17.37_18, -14.82_23], [-16.60_58, -16.87_83, -16.74_52]],
								        [[-3.64_56, -3.02_09, -1.42_03], [-3.07_97, -3.19_59, -2.00_00], [-1.87_57, -1.92_17, -1.69_97]],
								    ]	).to(__snake_case	)
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]	,    __snake_case	,    atol=1E-1	)	)







	@slow
	def   A			(		self    :  str	)    ->	str:
								UpperCAmelCase				: Optional[int]											=	SegformerImageProcessor(
								    image_scale=(512, 512)	,    keep_ratio=__snake_case	,    align=__snake_case	,    do_random_crop=__snake_case	)
								UpperCAmelCase				: Union[str, Any]											=	SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512'''	).to(
								    __snake_case	)

								UpperCAmelCase				: str											=	prepare_img()
								UpperCAmelCase				: Optional[int]											=	image_processor(images=__snake_case	,    return_tensors='''pt'''	)
								UpperCAmelCase				: Union[str, Any]											=	encoded_inputs.pixel_values.to(__snake_case	)

								with torch.no_grad():
															UpperCAmelCase				: List[Any]											=	model(__snake_case	)

								UpperCAmelCase				: Optional[Any]											=	outputs.logits.detach().cpu()

								UpperCAmelCase				: Dict											=	image_processor.post_process_semantic_segmentation(outputs=__snake_case	,    target_sizes=[(500, 300)]	)
								UpperCAmelCase				: Optional[Any]											=	torch.Size((500, 300)	)
								self.assertEqual(segmentation[0].shape	,    __snake_case	)

								UpperCAmelCase				: Optional[int]											=	image_processor.post_process_semantic_segmentation(outputs=__snake_case	)
								UpperCAmelCase				: int											=	torch.Size((128, 128)	)
								self.assertEqual(segmentation[0].shape	,    __snake_case	)




'''simple docstring'''






import warnings

from ...utils import logging
from .image_processing_beit import BeitImageProcessor


UpperCamelCase__:     Dict										=    logging.get_logger(__name__)

class        SCREAMING_SNAKE_CASE(							A__    ):







	"""simple docstring"""






	def __init__(		self    :  Optional[Any]	,    *__snake_case    :  List[str]	,    **__snake_case    :  str	)    ->	None:
								warnings.warn(
								    '''The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'''
								    ''' use BeitImageProcessor instead.'''	,    __snake_case	,    )
								super().__init__(*__snake_case	,    **__snake_case	)





