
"""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 __lowerCAmelCase					(       _lowercase   ):




			"""simple docstring"""







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







								"""simple docstring"""
								A_								=    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 __lowerCAmelCase					:




			"""simple docstring"""







			def __init__(					self    :       int							, _snake_case    :       str							, _snake_case    :       List[str]=13							, _snake_case    :       Tuple=64							, _snake_case    :       Dict=3							, _snake_case    :       str=4							, _snake_case    :       List[Any]=[2, 2, 2, 2]							, _snake_case    :       int=[8, 4, 2, 1]							, _snake_case    :       Union[str, Any]=[16, 32, 64, 128]							, _snake_case    :       int=[1, 4, 8, 16]							, _snake_case    :       Any=[1, 2, 4, 8]							, _snake_case    :       int=True							, _snake_case    :       Dict=True							, _snake_case    :       Any="gelu"							, _snake_case    :       Dict=0.1							, _snake_case    :       List[Any]=0.1							, _snake_case    :       Optional[Any]=0.0_2							, _snake_case    :       Dict=3							, _snake_case    :       Tuple=None							, )   ->			Any:







								"""simple docstring"""
								A_								=    parent
								A_								=    batch_size
								A_								=    image_size
								A_								=    num_channels
								A_								=    num_encoder_blocks
								A_								=    sr_ratios
								A_								=    depths
								A_								=    hidden_sizes
								A_								=    downsampling_rates
								A_								=    num_attention_heads
								A_								=    is_training
								A_								=    use_labels
								A_								=    hidden_act
								A_								=    hidden_dropout_prob
								A_								=    attention_probs_dropout_prob
								A_								=    initializer_range
								A_								=    num_labels
								A_								=    scope




			def   lowerCamelCase__  (					self    :       Tuple						)   ->			Tuple:







								"""simple docstring"""
								A_								=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

								A_								=    None
								if self.use_labels:
													A_								=    ids_tensor([self.batch_size, self.image_size, self.image_size]							, self.num_labels						)

								A_								=    self.get_config()
								return config, pixel_values, labels




			def   lowerCamelCase__  (					self    :       List[str]						)   ->			Optional[int]:







								"""simple docstring"""
								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   lowerCamelCase__  (					self    :       Optional[Any]							, _snake_case    :       Dict							, _snake_case    :       Tuple							, _snake_case    :       Union[str, Any]						)   ->			List[Any]:







								"""simple docstring"""
								A_								=    SegformerModel(config=_snake_case						)
								model.to(_snake_case						)
								model.eval()
								A_								=    model(_snake_case						)
								A_								=    A_								=    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   lowerCamelCase__  (					self    :       str							, _snake_case    :       List[str]							, _snake_case    :       List[Any]							, _snake_case    :       Tuple						)   ->			Optional[int]:







								"""simple docstring"""
								A_								=    self.num_labels
								A_								=    SegformerForSemanticSegmentation(_snake_case						)
								model.to(_snake_case						)
								model.eval()
								A_								=    model(_snake_case						)
								self.parent.assertEqual(
								    result.logits.shape							, (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)						)
								A_								=    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   lowerCamelCase__  (					self    :       Dict							, _snake_case    :       Optional[Any]							, _snake_case    :       Optional[Any]							, _snake_case    :       List[str]						)   ->			int:







								"""simple docstring"""
								A_								=    1
								A_								=    SegformerForSemanticSegmentation(config=_snake_case						)
								model.to(_snake_case						)
								model.eval()
								A_								=    torch.randint(0							, 1							, (self.batch_size, self.image_size, self.image_size)						).to(_snake_case						)
								A_								=    model(_snake_case							, labels=_snake_case						)
								self.parent.assertGreater(result.loss							, 0.0						)






			def   lowerCamelCase__  (					self    :       Optional[int]						)   ->			Union[str, Any]:







								"""simple docstring"""
								A_								=    self.prepare_config_and_inputs()
								A_			,     A_			,     A_								=    config_and_inputs
								A_								=    {"pixel_values": pixel_values}
								return config, inputs_dict





@require_torch
class __lowerCAmelCase					(       _lowercase  ,					_lowercase  ,					unittest.TestCase   ):




			"""simple docstring"""







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

			snake_case						   =					True
			snake_case						   =					False
			snake_case						   =					False
			snake_case						   =					False




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







								"""simple docstring"""
								A_								=    SegformerModelTester(self						)
								A_								=    SegformerConfigTester(self							, config_class=_snake_case						)




			def   lowerCamelCase__  (					self    :       Optional[Any]						)   ->			Union[str, Any]:







								"""simple docstring"""
								self.config_tester.run_common_tests()




			def   lowerCamelCase__  (					self    :       List[Any]						)   ->			Union[str, Any]:







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_snake_case						)




			def   lowerCamelCase__  (					self    :       Any						)   ->			Optional[Any]:







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_binary_image_segmentation(*_snake_case						)




			def   lowerCamelCase__  (					self    :       List[Any]						)   ->			int:







								"""simple docstring"""
								A_								=    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   lowerCamelCase__  (					self    :       Dict						)   ->			Optional[Any]:







								"""simple docstring"""
								pass




			@unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods"						)
			def   lowerCamelCase__  (					self    :       int						)   ->			Any:







								"""simple docstring"""
								pass




			def   lowerCamelCase__  (					self    :       Tuple						)   ->			Optional[Any]:







								"""simple docstring"""
								A_			,     A_								=    self.model_tester.prepare_config_and_inputs_for_common()

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

													A_								=    ["pixel_values"]
													self.assertListEqual(arg_names[:1]							, _snake_case						)




			def   lowerCamelCase__  (					self    :       Tuple						)   ->			int:







								"""simple docstring"""
								A_			,     A_								=    self.model_tester.prepare_config_and_inputs_for_common()
								A_								=    True

								for model_class in self.all_model_classes:
													A_								=    True
													A_								=    False
													A_								=    True
													A_								=    model_class(_snake_case						)
													model.to(_snake_case						)
													model.eval()
													with torch.no_grad():
																		A_								=    model(**self._prepare_for_class(_snake_case							, _snake_case						)						)
													A_								=    outputs.attentions

													A_								=    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"]
													A_								=    True
													A_								=    model_class(_snake_case						)
													model.to(_snake_case						)
													model.eval()
													with torch.no_grad():
																		A_								=    model(**self._prepare_for_class(_snake_case							, _snake_case						)						)
													A_								=    outputs.attentions

													self.assertEqual(len(_snake_case						)							, _snake_case						)

													# verify the first attentions (first block, first layer)
													A_								=    (self.model_tester.image_size // 4) ** 2
													A_								=    (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)
													A_								=    (self.model_tester.image_size // 32) ** 2
													A_								=    (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]							, )
													A_								=    len(_snake_case						)

													# Check attention is always last and order is fine
													A_								=    True
													A_								=    True
													A_								=    model_class(_snake_case						)
													model.to(_snake_case						)
													model.eval()
													with torch.no_grad():
																		A_								=    model(**self._prepare_for_class(_snake_case							, _snake_case						)						)

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

													A_								=    outputs.attentions

													self.assertEqual(len(_snake_case						)							, _snake_case						)
													# verify the first attentions (first block, first layer)
													A_								=    (self.model_tester.image_size // 4) ** 2
													A_								=    (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   lowerCamelCase__  (					self    :       Optional[int]						)   ->			Tuple:







								"""simple docstring"""
								def check_hidden_states_output(_snake_case    :       List[str]							, _snake_case    :       str							, _snake_case    :       Optional[int]						):
													A_								=    model_class(_snake_case						)
													model.to(_snake_case						)
													model.eval()

													with torch.no_grad():
																		A_								=    model(**self._prepare_for_class(_snake_case							, _snake_case						)						)

													A_								=    outputs.hidden_states

													A_								=    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,
													    ]							, )

								A_			,     A_								=    self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													A_								=    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"]
													A_								=    True

													check_hidden_states_output(_snake_case							, _snake_case							, _snake_case						)




			def   lowerCamelCase__  (					self    :       str						)   ->			Union[str, Any]:







								"""simple docstring"""
								if not self.model_tester.is_training:
													return

								A_			,     A_								=    self.model_tester.prepare_config_and_inputs_for_common()
								A_								=    True

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

													A_								=    model_class(_snake_case						)
													model.to(_snake_case						)
													model.train()
													A_								=    self._prepare_for_class(_snake_case							, _snake_case							, return_labels=_snake_case						)
													A_								=    model(**_snake_case						).loss
													loss.backward()




			@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
			def   lowerCamelCase__  (					self    :       Tuple						)   ->			Any:







								"""simple docstring"""
								pass






			@slow
			def   lowerCamelCase__  (					self    :       List[Any]						)   ->			Dict:







								"""simple docstring"""
								for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													A_								=    SegformerModel.from_pretrained(_snake_case						)
													self.assertIsNotNone(_snake_case						)





def 				A_  ():

					'''simple docstring'''





					A_								=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
					return image




@require_torch
class __lowerCAmelCase					(       unittest.TestCase   ):




			"""simple docstring"""







			@slow
			def   lowerCamelCase__  (					self    :       Union[str, Any]						)   ->			List[str]:







								"""simple docstring"""
								# only resize + normalize
								A_								=    SegformerImageProcessor(
								    image_scale=(512, 512)							, keep_ratio=_snake_case							, align=_snake_case							, do_random_crop=_snake_case						)
								A_								=    SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"						).to(
								    _snake_case						)

								A_								=    prepare_img()
								A_								=    image_processor(images=_snake_case							, return_tensors="pt"						)
								A_								=    encoded_inputs.pixel_values.to(_snake_case						)

								with torch.no_grad():
													A_								=    model(_snake_case						)

								A_								=    torch.Size((1, model.config.num_labels, 128, 128)						)
								self.assertEqual(outputs.logits.shape							, _snake_case						)

								A_								=    torch.tensor(
								    [
								        [[-4.6_3_1_0, -5.5_2_3_2, -6.2_3_5_6], [-5.1_9_2_1, -6.1_4_4_4, -6.5_9_9_6], [-5.4_4_2_4, -6.2_7_9_0, -6.7_5_7_4]],
								        [[-1_2.1_3_9_1, -1_3.3_1_2_2, -1_3.9_5_5_4], [-1_2.8_7_3_2, -1_3.9_3_5_2, -1_4.3_5_6_3], [-1_2.9_4_3_8, -1_3.8_2_2_6, -1_4.2_5_1_3]],
								        [[-1_2.5_1_3_4, -1_3.4_6_8_6, -1_4.4_9_1_5], [-1_2.8_6_6_9, -1_4.4_3_4_3, -1_4.7_7_5_8], [-1_3.2_5_2_3, -1_4.5_8_1_9, -1_5.0_6_9_4]],
								    ]						).to(_snake_case						)
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]							, _snake_case							, atol=1e-4						)						)




			@slow
			def   lowerCamelCase__  (					self    :       List[str]						)   ->			List[Any]:







								"""simple docstring"""
								# only resize + normalize
								A_								=    SegformerImageProcessor(
								    image_scale=(512, 512)							, keep_ratio=_snake_case							, align=_snake_case							, do_random_crop=_snake_case						)
								A_								=    SegformerForSemanticSegmentation.from_pretrained(
								    "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"						).to(_snake_case						)

								A_								=    prepare_img()
								A_								=    image_processor(images=_snake_case							, return_tensors="pt"						)
								A_								=    encoded_inputs.pixel_values.to(_snake_case						)

								with torch.no_grad():
													A_								=    model(_snake_case						)

								A_								=    torch.Size((1, model.config.num_labels, 128, 128)						)
								self.assertEqual(outputs.logits.shape							, _snake_case						)

								A_								=    torch.tensor(
								    [
								        [[-1_3.5_7_4_8, -1_3.9_1_1_1, -1_2.6_5_0_0], [-1_4.3_5_0_0, -1_5.3_6_8_3, -1_4.2_3_2_8], [-1_4.7_5_3_2, -1_6.0_4_2_4, -1_5.6_0_8_7]],
								        [[-1_7.1_6_5_1, -1_5.8_7_2_5, -1_2.9_6_5_3], [-1_7.2_5_8_0, -1_7.3_7_1_8, -1_4.8_2_2_3], [-1_6.6_0_5_8, -1_6.8_7_8_3, -1_6.7_4_5_2]],
								        [[-3.6_4_5_6, -3.0_2_0_9, -1.4_2_0_3], [-3.0_7_9_7, -3.1_9_5_9, -2.0_0_0_0], [-1.8_7_5_7, -1.9_2_1_7, -1.6_9_9_7]],
								    ]						).to(_snake_case						)
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]							, _snake_case							, atol=1e-1						)						)






			@slow
			def   lowerCamelCase__  (					self    :       List[str]						)   ->			Union[str, Any]:







								"""simple docstring"""
								# only resize + normalize
								A_								=    SegformerImageProcessor(
								    image_scale=(512, 512)							, keep_ratio=_snake_case							, align=_snake_case							, do_random_crop=_snake_case						)
								A_								=    SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"						).to(
								    _snake_case						)

								A_								=    prepare_img()
								A_								=    image_processor(images=_snake_case							, return_tensors="pt"						)
								A_								=    encoded_inputs.pixel_values.to(_snake_case						)

								with torch.no_grad():
													A_								=    model(_snake_case						)

								A_								=    outputs.logits.detach().cpu()

								A_								=    image_processor.post_process_semantic_segmentation(outputs=_snake_case							, target_sizes=[(500, 300)]						)
								A_								=    torch.Size((500, 300)						)
								self.assertEqual(segmentation[0].shape							, _snake_case						)

								A_								=    image_processor.post_process_semantic_segmentation(outputs=_snake_case						)
								A_								=    torch.Size((128, 128)						)
								self.assertEqual(segmentation[0].shape							, _snake_case						)






"""simple docstring"""





from typing import TYPE_CHECKING

from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


UpperCamelCase_       :    Any						      =			{'''configuration_van''': ['''VAN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''VanConfig''']}


try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCamelCase_       :    str						      =			[
		    '''VAN_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''VanForImageClassification''',
		    '''VanModel''',
		    '''VanPreTrainedModel''',
		]

if TYPE_CHECKING:
		from .configuration_van import VAN_PRETRAINED_CONFIG_ARCHIVE_MAP, VanConfig

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_van import (
				    VAN_PRETRAINED_MODEL_ARCHIVE_LIST,
				    VanForImageClassification,
				    VanModel,
				    VanPreTrainedModel,
				)

else:
		import sys

		UpperCamelCase_       :    Optional[Any]						      =			_LazyModule(__name__, globals()['''__file__'''], _import_structure)




