

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 				lowercase       ( lowercase_    ):




				def 					a							(      self ):
											snake_case_           =					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 				lowercase       :




				def __init__(      self   ,     snake_case   ,     snake_case=13   ,     snake_case=64   ,     snake_case=3   ,     snake_case=4   ,     snake_case=[2, 2, 2, 2]   ,     snake_case=[8, 4, 2, 1]   ,     snake_case=[16, 32, 64, 128]   ,     snake_case=[1, 4, 8, 16]   ,     snake_case=[1, 2, 4, 8]   ,     snake_case=True   ,     snake_case=True   ,     snake_case="gelu"   ,     snake_case=0.1   ,     snake_case=0.1   ,     snake_case=0.02   ,     snake_case=3   ,     snake_case=None   ,     ):
											snake_case_           =					parent
											snake_case_           =					batch_size
											snake_case_           =					image_size
											snake_case_           =					num_channels
											snake_case_           =					num_encoder_blocks
											snake_case_           =					sr_ratios
											snake_case_           =					depths
											snake_case_           =					hidden_sizes
											snake_case_           =					downsampling_rates
											snake_case_           =					num_attention_heads
											snake_case_           =					is_training
											snake_case_           =					use_labels
											snake_case_           =					hidden_act
											snake_case_           =					hidden_dropout_prob
											snake_case_           =					attention_probs_dropout_prob
											snake_case_           =					initializer_range
											snake_case_           =					num_labels
											snake_case_           =					scope




				def 					a							(      self ):
											snake_case_           =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

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




				def 					a							(      self ):
											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   ,     snake_case   ,     snake_case   ,     snake_case ):
											snake_case_           =					SegformerModel(config=snake_case )
											model.to(snake_case )
											model.eval()
											snake_case_           =					model(snake_case )
											snake_case_           =					snake_case_           =					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   ,     snake_case   ,     snake_case   ,     snake_case ):
											snake_case_           =					self.num_labels
											snake_case_           =					SegformerForSemanticSegmentation(snake_case )
											model.to(snake_case )
											model.eval()
											snake_case_           =					model(snake_case )
											self.parent.assertEqual(
											    result.logits.shape   ,     (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) )
											snake_case_           =					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   ,     snake_case   ,     snake_case   ,     snake_case ):
											snake_case_           =					1
											snake_case_           =					SegformerForSemanticSegmentation(config=snake_case )
											model.to(snake_case )
											model.eval()
											snake_case_           =					torch.randint(0   ,     1   ,     (self.batch_size, self.image_size, self.image_size) ).to(snake_case )
											snake_case_           =					model(snake_case   ,     labels=snake_case )
											self.parent.assertGreater(result.loss   ,     0.0 )




				def 					a							(      self ):
											snake_case_           =					self.prepare_config_and_inputs()
											snake_case_       ,					snake_case_       ,					snake_case_           =					config_and_inputs
											snake_case_           =					{'pixel_values': pixel_values}
											return config, inputs_dict







@require_torch
class 				lowercase       ( lowercase_			,						lowercase_			,						unittest.TestCase    ):
				__SCREAMING_SNAKE_CASE      :				str     =    (
				    (
				        SegformerModel,
				        SegformerForSemanticSegmentation,
				        SegformerForImageClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				__SCREAMING_SNAKE_CASE      :				Dict     =    (
				    {
				        '''feature-extraction''': SegformerModel,
				        '''image-classification''': SegformerForImageClassification,
				        '''image-segmentation''': SegformerForSemanticSegmentation,
				    }
				    if is_torch_available()
				    else {}
				)

				__SCREAMING_SNAKE_CASE      :				int     =    True
				__SCREAMING_SNAKE_CASE      :				Dict     =    False
				__SCREAMING_SNAKE_CASE      :				List[str]     =    False
				__SCREAMING_SNAKE_CASE      :				List[Any]     =    False




				def 					a							(      self ):
											snake_case_           =					SegformerModelTester(self )
											snake_case_           =					SegformerConfigTester(self   ,     config_class=snake_case )




				def 					a							(      self ):
											self.config_tester.run_common_tests()




				def 					a							(      self ):
											snake_case_           =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*snake_case )




				def 					a							(      self ):
											snake_case_           =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_binary_image_segmentation(*snake_case )




				def 					a							(      self ):
											snake_case_           =					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 ):
											pass




				@unittest.skip('SegFormer does not have get_input_embeddings method and get_output_embeddings methods' )
				def 					a							(      self ):
											pass




				def 					a							(      self ):
											snake_case_       ,					snake_case_           =					self.model_tester.prepare_config_and_inputs_for_common()

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

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




				def 					a							(      self ):
											snake_case_       ,					snake_case_           =					self.model_tester.prepare_config_and_inputs_for_common()
											snake_case_           =					True

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

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

																		self.assertEqual(len(snake_case )   ,     snake_case )

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

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

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

																		snake_case_           =					outputs.attentions

																		self.assertEqual(len(snake_case )   ,     snake_case )
																		# verify the first attentions (first block, first layer)
																		snake_case_           =					(self.model_tester.image_size // 4) ** 2
																		snake_case_           =					(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 ):
											def check_hidden_states_output(snake_case   ,     snake_case   ,     snake_case ):
																		snake_case_           =					model_class(snake_case )
																		model.to(snake_case )
																		model.eval()

																		with torch.no_grad():
																									snake_case_           =					model(**self._prepare_for_class(snake_case   ,     snake_case ) )

																		snake_case_           =					outputs.hidden_states

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

											snake_case_       ,					snake_case_           =					self.model_tester.prepare_config_and_inputs_for_common()

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

																		check_hidden_states_output(snake_case   ,     snake_case   ,     snake_case )




				def 					a							(      self ):
											if not self.model_tester.is_training:
																		return

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

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

																		snake_case_           =					model_class(snake_case )
																		model.to(snake_case )
																		model.train()
																		snake_case_           =					self._prepare_for_class(snake_case   ,     snake_case   ,     return_labels=snake_case )
																		snake_case_           =					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 ):
											pass




				@slow
				def 					a							(      self ):
											for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		snake_case_           =					SegformerModel.from_pretrained(snake_case )
																		self.assertIsNotNone(snake_case )





def       __lowerCamelCase			(				):







							'''simple docstring'''






							snake_case_           =					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'   )
							return image







@require_torch
class 				lowercase       ( unittest.TestCase    ):




				@slow
				def 					a							(      self ):
											# only resize + normalize
											snake_case_           =					SegformerImageProcessor(
											    image_scale=(512, 512)   ,     keep_ratio=snake_case   ,     align=snake_case   ,     do_random_crop=snake_case )
											snake_case_           =					SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to(
											    snake_case )

											snake_case_           =					prepare_img()
											snake_case_           =					image_processor(images=snake_case   ,     return_tensors='pt' )
											snake_case_           =					encoded_inputs.pixel_values.to(snake_case )

											with torch.no_grad():
																		snake_case_           =					model(snake_case )

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

											snake_case_           =					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 ):
											# only resize + normalize
											snake_case_           =					SegformerImageProcessor(
											    image_scale=(512, 512)   ,     keep_ratio=snake_case   ,     align=snake_case   ,     do_random_crop=snake_case )
											snake_case_           =					SegformerForSemanticSegmentation.from_pretrained(
											    'nvidia/segformer-b1-finetuned-cityscapes-1024-1024' ).to(snake_case )

											snake_case_           =					prepare_img()
											snake_case_           =					image_processor(images=snake_case   ,     return_tensors='pt' )
											snake_case_           =					encoded_inputs.pixel_values.to(snake_case )

											with torch.no_grad():
																		snake_case_           =					model(snake_case )

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

											snake_case_           =					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 ):
											# only resize + normalize
											snake_case_           =					SegformerImageProcessor(
											    image_scale=(512, 512)   ,     keep_ratio=snake_case   ,     align=snake_case   ,     do_random_crop=snake_case )
											snake_case_           =					SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to(
											    snake_case )

											snake_case_           =					prepare_img()
											snake_case_           =					image_processor(images=snake_case   ,     return_tensors='pt' )
											snake_case_           =					encoded_inputs.pixel_values.to(snake_case )

											with torch.no_grad():
																		snake_case_           =					model(snake_case )

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

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

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







import warnings

from ...utils import logging
from .image_processing_deformable_detr import DeformableDetrImageProcessor


_UpperCAmelCase	:     Any		       = logging.get_logger(__name__)







class 				lowercase       ( lowercase_    ):




				def __init__(      self   ,     *snake_case   ,     **snake_case ):
											warnings.warn(
											    'The class DeformableDetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'
											    ' Please use DeformableDetrImageProcessor instead.'   ,     snake_case   ,     )
											super().__init__(*snake_case   ,     **snake_case )




