

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  __UpperCAmelCase					(	lowerCamelCase__						):



			def 	__magic_name__   (							self							:					List[Any]						):
					UpperCAmelCase	:							Union[str, Any]						=			self.config_class(**self.inputs_dict						)
					self.parent.assertTrue(hasattr(__A,		'''hidden_sizes'''						)						)
					self.parent.assertTrue(hasattr(__A,		'''num_attention_heads'''						)						)
					self.parent.assertTrue(hasattr(__A,		'''num_encoder_blocks'''						)						)






class  __UpperCAmelCase					:



			def __init__(							self							:					Dict,		__A							:					Union[str, Any],		__A							:					Any=1_3,		__A							:					List[str]=6_4,		__A							:					Any=3,		__A							:					int=4,		__A							:					List[Any]=[2, 2, 2, 2],		__A							:					List[Any]=[8, 4, 2, 1],		__A							:					Any=[1_6, 3_2, 6_4, 1_2_8],		__A							:					Optional[Any]=[1, 4, 8, 1_6],		__A							:					Tuple=[1, 2, 4, 8],		__A							:					List[str]=True,		__A							:					List[Any]=True,		__A							:					Optional[Any]="gelu",		__A							:					str=0.1,		__A							:					Optional[int]=0.1,		__A							:					Union[str, Any]=0.0_2,		__A							:					Optional[Any]=3,		__A							:					Union[str, Any]=None,		):
					UpperCAmelCase	:							Optional[int]						=			parent
					UpperCAmelCase	:							int						=			batch_size
					UpperCAmelCase	:							List[Any]						=			image_size
					UpperCAmelCase	:							List[str]						=			num_channels
					UpperCAmelCase	:							Union[str, Any]						=			num_encoder_blocks
					UpperCAmelCase	:							int						=			sr_ratios
					UpperCAmelCase	:							Tuple						=			depths
					UpperCAmelCase	:							List[Any]						=			hidden_sizes
					UpperCAmelCase	:							Tuple						=			downsampling_rates
					UpperCAmelCase	:							Union[str, Any]						=			num_attention_heads
					UpperCAmelCase	:							Union[str, Any]						=			is_training
					UpperCAmelCase	:							Optional[int]						=			use_labels
					UpperCAmelCase	:							Dict						=			hidden_act
					UpperCAmelCase	:							Optional[Any]						=			hidden_dropout_prob
					UpperCAmelCase	:							int						=			attention_probs_dropout_prob
					UpperCAmelCase	:							List[str]						=			initializer_range
					UpperCAmelCase	:							List[str]						=			num_labels
					UpperCAmelCase	:							Tuple						=			scope



			def 	__magic_name__   (							self							:					Any						):
					UpperCAmelCase	:							int						=			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	:							Optional[Any]						=			self.get_config()
					return config, pixel_values, labels



			def 	__magic_name__   (							self							:					str						):
					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 	__magic_name__   (							self							:					List[Any],		__A							:					Tuple,		__A							:					Tuple,		__A							:					Dict						):
					UpperCAmelCase	:							str						=			SegformerModel(config=__A						)
					model.to(__A						)
					model.eval()
					UpperCAmelCase	:							Optional[Any]						=			model(__A						)
					UpperCAmelCase	:							Tuple						=			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 	__magic_name__   (							self							:					int,		__A							:					List[Any],		__A							:					Optional[int],		__A							:					Optional[int]						):
					UpperCAmelCase	:							List[str]						=			self.num_labels
					UpperCAmelCase	:							Dict						=			SegformerForSemanticSegmentation(__A						)
					model.to(__A						)
					model.eval()
					UpperCAmelCase	:							Optional[Any]						=			model(__A						)
					self.parent.assertEqual(
					    result.logits.shape,		(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)						)
					UpperCAmelCase	:							Union[str, Any]						=			model(__A,		labels=__A						)
					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 	__magic_name__   (							self							:					Tuple,		__A							:					Tuple,		__A							:					Union[str, Any],		__A							:					Optional[int]						):
					UpperCAmelCase	:							int						=			1
					UpperCAmelCase	:							Dict						=			SegformerForSemanticSegmentation(config=__A						)
					model.to(__A						)
					model.eval()
					UpperCAmelCase	:							str						=			torch.randint(0,		1,		(self.batch_size, self.image_size, self.image_size)						).to(__A						)
					UpperCAmelCase	:							List[str]						=			model(__A,		labels=__A						)
					self.parent.assertGreater(result.loss,		0.0						)





			def 	__magic_name__   (							self							:					Any						):
					UpperCAmelCase	:							int						=			self.prepare_config_and_inputs()
					UpperCAmelCase	:							Optional[Any]						=			config_and_inputs
					UpperCAmelCase	:							List[Any]						=			{'''pixel_values''': pixel_values}
					return config, inputs_dict






@require_torch
class  __UpperCAmelCase					(	lowerCamelCase__      ,  lowerCamelCase__      ,  unittest.TestCase						):
			UpperCamelCase       					=					(
			    (
			        SegformerModel,
			        SegformerForSemanticSegmentation,
			        SegformerForImageClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase       					=					(
			    {
			        """feature-extraction""": SegformerModel,
			        """image-classification""": SegformerForImageClassification,
			        """image-segmentation""": SegformerForSemanticSegmentation,
			    }
			    if is_torch_available()
			    else {}
			)

			UpperCamelCase       					=					True
			UpperCamelCase       					=					False
			UpperCamelCase       					=					False
			UpperCamelCase       					=					False



			def 	__magic_name__   (							self							:					List[Any]						):
					UpperCAmelCase	:							Optional[int]						=			SegformerModelTester(self						)
					UpperCAmelCase	:							Optional[Any]						=			SegformerConfigTester(self,		config_class=__A						)



			def 	__magic_name__   (							self							:					str						):
					self.config_tester.run_common_tests()



			def 	__magic_name__   (							self							:					Optional[Any]						):
					UpperCAmelCase	:							List[Any]						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__A						)



			def 	__magic_name__   (							self							:					Any						):
					UpperCAmelCase	:							Any						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_binary_image_segmentation(*__A						)



			def 	__magic_name__   (							self							:					Optional[int]						):
					UpperCAmelCase	:							Optional[Any]						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_segmentation(*__A						)



			@unittest.skip('''SegFormer does not use inputs_embeds'''						)
			def 	__magic_name__   (							self							:					int						):
					pass



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



			def 	__magic_name__   (							self							:					str						):
					UpperCAmelCase	:							List[Any]						=			self.model_tester.prepare_config_and_inputs_for_common()

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

							UpperCAmelCase	:							Dict						=			['''pixel_values''']
							self.assertListEqual(arg_names[:1],		__A						)



			def 	__magic_name__   (							self							:					Any						):
					UpperCAmelCase	:							List[str]						=			self.model_tester.prepare_config_and_inputs_for_common()
					UpperCAmelCase	:							Tuple						=			True

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

							UpperCAmelCase	:							str						=			sum(self.model_tester.depths						)
							self.assertEqual(len(__A						),		__A						)

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

							self.assertEqual(len(__A						),		__A						)

							# verify the first attentions (first block, first layer)
							UpperCAmelCase	:							Optional[int]						=			(self.model_tester.image_size // 4) ** 2
							UpperCAmelCase	:							str						=			(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	:							Union[str, Any]						=			(self.model_tester.image_size // 3_2) ** 2
							UpperCAmelCase	:							Dict						=			(self.model_tester.image_size // (3_2 * 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	:							Dict						=			len(__A						)

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

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

							UpperCAmelCase	:							Optional[int]						=			outputs.attentions

							self.assertEqual(len(__A						),		__A						)
							# verify the first attentions (first block, first layer)
							UpperCAmelCase	:							Any						=			(self.model_tester.image_size // 4) ** 2
							UpperCAmelCase	:							Union[str, Any]						=			(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 	__magic_name__   (							self							:					str						):
					def check_hidden_states_output(__A							:					Union[str, Any],		__A							:					Optional[int],		__A							:					Dict						):
							UpperCAmelCase	:							int						=			model_class(__A						)
							model.to(__A						)
							model.eval()

							with torch.no_grad():
									UpperCAmelCase	:							Any						=			model(**self._prepare_for_class(__A,		__A						)						)

							UpperCAmelCase	:							Tuple						=			outputs.hidden_states

							UpperCAmelCase	:							Dict						=			self.model_tester.num_encoder_blocks
							self.assertEqual(len(__A						),		__A						)

							# 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	:							Dict						=			self.model_tester.prepare_config_and_inputs_for_common()

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

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

							check_hidden_states_output(__A,		__A,		__A						)



			def 	__magic_name__   (							self							:					int						):
					if not self.model_tester.is_training:
							return

					UpperCAmelCase	:							Dict						=			self.model_tester.prepare_config_and_inputs_for_common()
					UpperCAmelCase	:							str						=			True

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

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



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





			@slow
			def 	__magic_name__   (							self							:					int						):
					for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase	:							List[str]						=			SegformerModel.from_pretrained(__A						)
							self.assertIsNotNone(__A						)






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






@require_torch
class  __UpperCAmelCase					(	unittest.TestCase						):



			@slow
			def 	__magic_name__   (							self							:					int						):
					# only resize + normalize
					UpperCAmelCase	:							Optional[int]						=			SegformerImageProcessor(
					    image_scale=(5_1_2, 5_1_2),		keep_ratio=__A,		align=__A,		do_random_crop=__A						)
					UpperCAmelCase	:							Tuple						=			SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512'''						).to(
					    __A						)

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

					with torch.no_grad():
							UpperCAmelCase	:							Optional[int]						=			model(__A						)

					UpperCAmelCase	:							int						=			torch.Size((1, model.config.num_labels, 1_2_8, 1_2_8)						)
					self.assertEqual(outputs.logits.shape,		__A						)

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



			@slow
			def 	__magic_name__   (							self							:					Union[str, Any]						):
					# only resize + normalize
					UpperCAmelCase	:							Dict						=			SegformerImageProcessor(
					    image_scale=(5_1_2, 5_1_2),		keep_ratio=__A,		align=__A,		do_random_crop=__A						)
					UpperCAmelCase	:							List[str]						=			SegformerForSemanticSegmentation.from_pretrained(
					    '''nvidia/segformer-b1-finetuned-cityscapes-1024-1024'''						).to(__A						)

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

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

					UpperCAmelCase	:							Any						=			torch.Size((1, model.config.num_labels, 1_2_8, 1_2_8)						)
					self.assertEqual(outputs.logits.shape,		__A						)

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





			@slow
			def 	__magic_name__   (							self							:					List[str]						):
					# only resize + normalize
					UpperCAmelCase	:							str						=			SegformerImageProcessor(
					    image_scale=(5_1_2, 5_1_2),		keep_ratio=__A,		align=__A,		do_random_crop=__A						)
					UpperCAmelCase	:							Union[str, Any]						=			SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512'''						).to(
					    __A						)

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

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

					UpperCAmelCase	:							Tuple						=			outputs.logits.detach().cpu()

					UpperCAmelCase	:							str						=			image_processor.post_process_semantic_segmentation(outputs=__A,		target_sizes=[(5_0_0, 3_0_0)]						)
					UpperCAmelCase	:							List[Any]						=			torch.Size((5_0_0, 3_0_0)						)
					self.assertEqual(segmentation[0].shape,		__A						)

					UpperCAmelCase	:							str						=			image_processor.post_process_semantic_segmentation(outputs=__A						)
					UpperCAmelCase	:							List[str]						=			torch.Size((1_2_8, 1_2_8)						)
					self.assertEqual(segmentation[0].shape,		__A						)








def       a__					(		UpperCAmelCase							:      int      )  ->						bool:
  if not isinstance(UpperCAmelCase    ,			UpperCAmelCase      ):
    UpperCAmelCase	:							List[str]						=			f'''Input value of [number={number}] must be an integer'''
    raise TypeError(UpperCAmelCase      )
  if number < 0:
    return False
  UpperCAmelCase	:							List[str]						=			number * number
  while number > 0:
    if number % 10 != number_square % 10:
      return False
    number //= 10
    number_square //= 10
  return True


if __name__ == "__main__":
     import doctest

     doctest.testmod()





