



'''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_						(			a_		):





    def 	__snake_case  (							self							:							Any      ):



       '''simple docstring'''







       snake_case       :				Optional[Any]		    =self.config_class(**self.inputs_dict      )
       self.parent.assertTrue(hasattr(_lowercase,							'''hidden_sizes'''      )      )
       self.parent.assertTrue(hasattr(_lowercase,							'''num_attention_heads'''      )      )
       self.parent.assertTrue(hasattr(_lowercase,							'''num_encoder_blocks'''      )      )





class 				lowerCAmelCase_						:





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



       '''simple docstring'''







       snake_case       :				List[str]		    =parent
       snake_case       :				int		    =batch_size
       snake_case       :				Dict		    =image_size
       snake_case       :				Any		    =num_channels
       snake_case       :				Optional[int]		    =num_encoder_blocks
       snake_case       :				Optional[Any]		    =sr_ratios
       snake_case       :				List[str]		    =depths
       snake_case       :				int		    =hidden_sizes
       snake_case       :				List[Any]		    =downsampling_rates
       snake_case       :				List[Any]		    =num_attention_heads
       snake_case       :				Any		    =is_training
       snake_case       :				List[Any]		    =use_labels
       snake_case       :				List[Any]		    =hidden_act
       snake_case       :				Dict		    =hidden_dropout_prob
       snake_case       :				Any		    =attention_probs_dropout_prob
       snake_case       :				List[str]		    =initializer_range
       snake_case       :				Dict		    =num_labels
       snake_case       :				Union[str, Any]		    =scope





    def 	__snake_case  (							self							:							Tuple      ):



       '''simple docstring'''







       snake_case       :				Dict		    =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

       snake_case       :				int		    =None
       if self.use_labels:
          snake_case       :				List[Any]		    =ids_tensor([self.batch_size, self.image_size, self.image_size],							self.num_labels      )

       snake_case       :				Dict		    =self.get_config()
       return config, pixel_values, labels





    def 	__snake_case  (							self							:							Tuple      ):



       '''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 	__snake_case  (							self							:							int,							_snake_case							:							Optional[int],							_snake_case							:							List[Any],							_snake_case							:							str      ):



       '''simple docstring'''







       snake_case       :				Optional[Any]		    =SegformerModel(config=_lowercase      )
       model.to(_lowercase      )
       model.eval()
       snake_case       :				List[str]		    =model(_lowercase      )
       snake_case       :				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 	__snake_case  (							self							:							Dict,							_snake_case							:							Optional[Any],							_snake_case							:							Dict,							_snake_case							:							Tuple      ):



       '''simple docstring'''







       snake_case       :				Any		    =self.num_labels
       snake_case       :				Union[str, Any]		    =SegformerForSemanticSegmentation(_lowercase      )
       model.to(_lowercase      )
       model.eval()
       snake_case       :				str		    =model(_lowercase      )
       self.parent.assertEqual(
           result.logits.shape,							(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)      )
       snake_case       :				Optional[Any]		    =model(_lowercase,							labels=_lowercase      )
       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 	__snake_case  (							self							:							int,							_snake_case							:							Optional[int],							_snake_case							:							Dict,							_snake_case							:							int      ):



       '''simple docstring'''







       snake_case       :				int		    =1
       snake_case       :				Union[str, Any]		    =SegformerForSemanticSegmentation(config=_lowercase      )
       model.to(_lowercase      )
       model.eval()
       snake_case       :				int		    =torch.randint(0,							1,							(self.batch_size, self.image_size, self.image_size)      ).to(_lowercase      )
       snake_case       :				Optional[int]		    =model(_lowercase,							labels=_lowercase      )
       self.parent.assertGreater(result.loss,							0.0      )






    def 	__snake_case  (							self							:							str      ):



       '''simple docstring'''







       snake_case       :				Union[str, Any]		    =self.prepare_config_and_inputs()
       snake_case       :				Optional[int]		    =config_and_inputs
       snake_case       :				int		    ={"""pixel_values""": pixel_values}
       return config, inputs_dict





@require_torch
class 				lowerCAmelCase_						(			a_    ,     a_    ,     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 	__snake_case  (							self							:							int      ):



       '''simple docstring'''







       snake_case       :				str		    =SegformerModelTester(self      )
       snake_case       :				Optional[int]		    =SegformerConfigTester(self,							config_class=_lowercase      )





    def 	__snake_case  (							self							:							Union[str, Any]      ):



       '''simple docstring'''







       self.config_tester.run_common_tests()





    def 	__snake_case  (							self							:							List[str]      ):



       '''simple docstring'''







       snake_case       :				str		    =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_lowercase      )





    def 	__snake_case  (							self							:							List[Any]      ):



       '''simple docstring'''







       snake_case       :				int		    =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_binary_image_segmentation(*_lowercase      )





    def 	__snake_case  (							self							:							Tuple      ):



       '''simple docstring'''







       snake_case       :				int		    =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_segmentation(*_lowercase      )





    @unittest.skip('''SegFormer does not use inputs_embeds'''      )
    def 	__snake_case  (							self							:							List[str]      ):



       '''simple docstring'''







       pass





    @unittest.skip('''SegFormer does not have get_input_embeddings method and get_output_embeddings methods'''      )
    def 	__snake_case  (							self							:							List[Any]      ):



       '''simple docstring'''







       pass





    def 	__snake_case  (							self							:							Optional[int]      ):



       '''simple docstring'''







       snake_case       :				Any		    =self.model_tester.prepare_config_and_inputs_for_common()

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

          snake_case       :				List[str]		    =["""pixel_values"""]
          self.assertListEqual(arg_names[:1],							_lowercase      )





    def 	__snake_case  (							self							:							Any      ):



       '''simple docstring'''







       snake_case       :				Any		    =self.model_tester.prepare_config_and_inputs_for_common()
       snake_case       :				Optional[int]		    =True

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

          snake_case       :				Optional[int]		    =sum(self.model_tester.depths      )
          self.assertEqual(len(_lowercase      ),							_lowercase      )

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

          self.assertEqual(len(_lowercase      ),							_lowercase      )

          # verify the first attentions (first block, first layer)
          snake_case       :				List[str]		    =(self.model_tester.image_size // 4) ** 2
          snake_case       :				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)
          snake_case       :				int		    =(self.model_tester.image_size // 32) ** 2
          snake_case       :				Optional[int]		    =(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       :				Dict		    =len(_lowercase      )

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

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

          snake_case       :				Optional[int]		    =outputs.attentions

          self.assertEqual(len(_lowercase      ),							_lowercase      )
          # verify the first attentions (first block, first layer)
          snake_case       :				int		    =(self.model_tester.image_size // 4) ** 2
          snake_case       :				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 	__snake_case  (							self							:							Tuple      ):



       '''simple docstring'''







       def check_hidden_states_output(_snake_case							:							Tuple,							_snake_case							:							Tuple,							_snake_case							:							Optional[Any]      ):
          snake_case       :				Tuple		    =model_class(_lowercase      )
          model.to(_lowercase      )
          model.eval()

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

          snake_case       :				Optional[Any]		    =outputs.hidden_states

          snake_case       :				Optional[Any]		    =self.model_tester.num_encoder_blocks
          self.assertEqual(len(_lowercase      ),							_lowercase      )

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

       for model_class in self.all_model_classes:
          snake_case       :				List[Any]		    =True
          check_hidden_states_output(_lowercase,							_lowercase,							_lowercase      )

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

          check_hidden_states_output(_lowercase,							_lowercase,							_lowercase      )





    def 	__snake_case  (							self							:							Dict      ):



       '''simple docstring'''







       if not self.model_tester.is_training:
          return

       snake_case       :				Dict		    =self.model_tester.prepare_config_and_inputs_for_common()
       snake_case       :				List[Any]		    =True

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

          snake_case       :				Union[str, Any]		    =model_class(_lowercase      )
          model.to(_lowercase      )
          model.train()
          snake_case       :				List[str]		    =self._prepare_for_class(_lowercase,							_lowercase,							return_labels=_lowercase      )
          snake_case       :				List[Any]		    =model(**_lowercase      ).loss
          loss.backward()





    @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''      )
    def 	__snake_case  (							self							:							Optional[int]      ):



       '''simple docstring'''







       pass






    @slow
    def 	__snake_case  (							self							:							Optional[Any]      ):



       '''simple docstring'''







       for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          snake_case       :				Union[str, Any]		    =SegformerModel.from_pretrained(_lowercase      )
          self.assertIsNotNone(_lowercase      )




def 						_a       (    ):
   snake_case       :				List[str]		    =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
   return image





@require_torch
class 				lowerCAmelCase_						(			unittest.TestCase		):





    @slow
    def 	__snake_case  (							self							:							Tuple      ):



       '''simple docstring'''







       snake_case       :				Tuple		    =SegformerImageProcessor(
           image_scale=(512, 512),							keep_ratio=_lowercase,							align=_lowercase,							do_random_crop=_lowercase      )
       snake_case       :				Dict		    =SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512'''      ).to(
           _lowercase      )

       snake_case       :				str		    =prepare_img()
       snake_case       :				str		    =image_processor(images=_lowercase,							return_tensors='''pt'''      )
       snake_case       :				List[str]		    =encoded_inputs.pixel_values.to(_lowercase      )

       with torch.no_grad():
          snake_case       :				Union[str, Any]		    =model(_lowercase      )

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

       snake_case       :				Any		    =torch.tensor(
           [
               [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]],
               [[-12.1_391, -13.3_122, -13.9_554], [-12.8_732, -13.9_352, -14.3_563], [-12.9_438, -13.8_226, -14.2_513]],
               [[-12.5_134, -13.4_686, -14.4_915], [-12.8_669, -14.4_343, -14.7_758], [-13.2_523, -14.5_819, -15.0_694]],
           ]      ).to(_lowercase      )
       self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3],							_lowercase,							atol=1E-4      )      )





    @slow
    def 	__snake_case  (							self							:							Optional[int]      ):



       '''simple docstring'''







       snake_case       :				Optional[Any]		    =SegformerImageProcessor(
           image_scale=(512, 512),							keep_ratio=_lowercase,							align=_lowercase,							do_random_crop=_lowercase      )
       snake_case       :				int		    =SegformerForSemanticSegmentation.from_pretrained(
           '''nvidia/segformer-b1-finetuned-cityscapes-1024-1024'''      ).to(_lowercase      )

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

       with torch.no_grad():
          snake_case       :				List[str]		    =model(_lowercase      )

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

       snake_case       :				Any		    =torch.tensor(
           [
               [[-13.5_748, -13.9_111, -12.6_500], [-14.3_500, -15.3_683, -14.2_328], [-14.7_532, -16.0_424, -15.6_087]],
               [[-17.1_651, -15.8_725, -12.9_653], [-17.2_580, -17.3_718, -14.8_223], [-16.6_058, -16.8_783, -16.7_452]],
               [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]],
           ]      ).to(_lowercase      )
       self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3],							_lowercase,							atol=1E-1      )      )






    @slow
    def 	__snake_case  (							self							:							List[Any]      ):



       '''simple docstring'''







       snake_case       :				Tuple		    =SegformerImageProcessor(
           image_scale=(512, 512),							keep_ratio=_lowercase,							align=_lowercase,							do_random_crop=_lowercase      )
       snake_case       :				str		    =SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512'''      ).to(
           _lowercase      )

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

       with torch.no_grad():
          snake_case       :				Any		    =model(_lowercase      )

       snake_case       :				str		    =outputs.logits.detach().cpu()

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

       snake_case       :				Tuple		    =image_processor.post_process_semantic_segmentation(outputs=_lowercase      )
       snake_case       :				Optional[Any]		    =torch.Size((128, 128)      )
       self.assertEqual(segmentation[0].shape,							_lowercase      )








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        A__(a_							):
						"""simple docstring"""






						def 			UpperCamelCase__					(    self    )     -> List[str]:
									a_						:						Optional[Any] 							= self.config_class(**self.inputs_dict    )
									self.parent.assertTrue(hasattr(_lowercase  ,      """hidden_sizes"""    )    )
									self.parent.assertTrue(hasattr(_lowercase  ,      """num_attention_heads"""    )    )
									self.parent.assertTrue(hasattr(_lowercase  ,      """num_encoder_blocks"""    )    )







class        A__:
						"""simple docstring"""






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





						def 			UpperCamelCase__					(    self    )     -> List[str]:
									a_						:						Dict 							= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

									a_						:						int 							= None
									if self.use_labels:
												a_						:						List[Any] 							= ids_tensor([self.batch_size, self.image_size, self.image_size]  ,      self.num_labels    )

									a_						:						Dict 							= self.get_config()
									return config, pixel_values, labels





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





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_						:						Union[str, Any] 							= self.prepare_config_and_inputs()
									a_							,					a_							,					a_						:						Optional[int] 							= config_and_inputs
									a_						:						int 							= {"""pixel_values""": pixel_values}
									return config, inputs_dict







@require_torch
class        A__(a_,  a_,  unittest.TestCase							):
						"""simple docstring"""






						_A    :				Dict       					=			(
						    (
						        SegformerModel,
						        SegformerForSemanticSegmentation,
						        SegformerForImageClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						_A    :				List[Any]       					=			(
						    {
						        '''feature-extraction''': SegformerModel,
						        '''image-classification''': SegformerForImageClassification,
						        '''image-segmentation''': SegformerForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

						_A    :				Optional[int]       					=			True
						_A    :				Optional[Any]       					=			False
						_A    :				int       					=			False
						_A    :				Optional[int]       					=			False





						def 			UpperCamelCase__					(    self    )     -> Dict:
									a_						:						str 							= SegformerModelTester(self    )
									a_						:						Optional[int] 							= SegformerConfigTester(self  ,      config_class=_lowercase    )





						def 			UpperCamelCase__					(    self    )     -> Union[str, Any]:
									self.config_tester.run_common_tests()





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									a_						:						str 							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_lowercase    )





						def 			UpperCamelCase__					(    self    )     -> int:
									a_						:						int 							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_binary_image_segmentation(*_lowercase    )





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_						:						int 							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_segmentation(*_lowercase    )





						@unittest.skip("""SegFormer does not use inputs_embeds"""    )
						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									pass





						@unittest.skip("""SegFormer does not have get_input_embeddings method and get_output_embeddings methods"""    )
						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									pass





						def 			UpperCamelCase__					(    self    )     -> Dict:
									a_							,					a_						:						Any 							= self.model_tester.prepare_config_and_inputs_for_common()

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

												a_						:						List[str] 							= ["""pixel_values"""]
												self.assertListEqual(arg_names[:1]  ,      _lowercase    )





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_							,					a_						:						Any 							= self.model_tester.prepare_config_and_inputs_for_common()
									a_						:						Optional[int] 							= True

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

												a_						:						Optional[int] 							= sum(self.model_tester.depths    )
												self.assertEqual(len(_lowercase    )  ,      _lowercase    )

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

												self.assertEqual(len(_lowercase    )  ,      _lowercase    )

												# verify the first attentions (first block, first layer)
												a_						:						List[str] 							= (self.model_tester.image_size // 4) ** 2
												a_						:						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)
												a_						:						int 							= (self.model_tester.image_size // 32) ** 2
												a_						:						Optional[int] 							= (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_						:						Dict 							= len(_lowercase    )

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

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

												a_						:						Optional[int] 							= outputs.attentions

												self.assertEqual(len(_lowercase    )  ,      _lowercase    )
												# verify the first attentions (first block, first layer)
												a_						:						int 							= (self.model_tester.image_size // 4) ** 2
												a_						:						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 			UpperCamelCase__					(    self    )     -> Optional[int]:
									def check_hidden_states_output(_lowercase  ,      _lowercase  ,      _lowercase    ):
												a_						:						Tuple 							= model_class(_lowercase    )
												model.to(_lowercase    )
												model.eval()

												with torch.no_grad():
															a_						:						Tuple 							= model(**self._prepare_for_class(_lowercase  ,      _lowercase    )    )

												a_						:						Optional[Any] 							= outputs.hidden_states

												a_						:						Optional[Any] 							= self.model_tester.num_encoder_blocks
												self.assertEqual(len(_lowercase    )  ,      _lowercase    )

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

									for model_class in self.all_model_classes:
												a_						:						List[Any] 							= True
												check_hidden_states_output(_lowercase  ,      _lowercase  ,      _lowercase    )

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

												check_hidden_states_output(_lowercase  ,      _lowercase  ,      _lowercase    )





						def 			UpperCamelCase__					(    self    )     -> Optional[Any]:
									if not self.model_tester.is_training:
												return

									a_							,					a_						:						Dict 							= self.model_tester.prepare_config_and_inputs_for_common()
									a_						:						List[Any] 							= True

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

												a_						:						Union[str, Any] 							= model_class(_lowercase    )
												model.to(_lowercase    )
												model.train()
												a_						:						List[str] 							= self._prepare_for_class(_lowercase  ,      _lowercase  ,      return_labels=_lowercase    )
												a_						:						List[Any] 							= model(**_lowercase    ).loss
												loss.backward()





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





						@slow
						def 			UpperCamelCase__					(    self    )     -> Optional[Any]:
									for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												a_						:						Union[str, Any] 							= SegformerModel.from_pretrained(_lowercase    )
												self.assertIsNotNone(_lowercase    )







def 					_UpperCAmelCase				(						):

			'''simple docstring'''



			a_						:						List[str] 							= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
			return image



@require_torch
class        A__(unittest.TestCase							):
						"""simple docstring"""






						@slow
						def 			UpperCamelCase__					(    self    )     -> List[str]:
									# only resize + normalize
									a_						:						Tuple 							= SegformerImageProcessor(
									    image_scale=(512, 512)  ,      keep_ratio=_lowercase  ,      align=_lowercase  ,      do_random_crop=_lowercase    )
									a_						:						Dict 							= SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""    ).to(
									    _lowercase    )

									a_						:						str 							= prepare_img()
									a_						:						str 							= image_processor(images=_lowercase  ,      return_tensors="""pt"""    )
									a_						:						List[str] 							= encoded_inputs.pixel_values.to(_lowercase    )

									with torch.no_grad():
												a_						:						Union[str, Any] 							= model(_lowercase    )

									a_						:						Tuple 							= torch.Size((1, model.config.num_labels, 128, 128)    )
									self.assertEqual(outputs.logits.shape  ,      _lowercase    )

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





						@slow
						def 			UpperCamelCase__					(    self    )     -> Optional[Any]:
									# only resize + normalize
									a_						:						Optional[Any] 							= SegformerImageProcessor(
									    image_scale=(512, 512)  ,      keep_ratio=_lowercase  ,      align=_lowercase  ,      do_random_crop=_lowercase    )
									a_						:						int 							= SegformerForSemanticSegmentation.from_pretrained(
									    """nvidia/segformer-b1-finetuned-cityscapes-1024-1024"""    ).to(_lowercase    )

									a_						:						Any 							= prepare_img()
									a_						:						List[Any] 							= image_processor(images=_lowercase  ,      return_tensors="""pt"""    )
									a_						:						Optional[int] 							= encoded_inputs.pixel_values.to(_lowercase    )

									with torch.no_grad():
												a_						:						List[str] 							= model(_lowercase    )

									a_						:						Tuple 							= torch.Size((1, model.config.num_labels, 128, 128)    )
									self.assertEqual(outputs.logits.shape  ,      _lowercase    )

									a_						:						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(_lowercase    )
									self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]  ,      _lowercase  ,      atol=1e-1    )    )





						@slow
						def 			UpperCamelCase__					(    self    )     -> List[str]:
									# only resize + normalize
									a_						:						Tuple 							= SegformerImageProcessor(
									    image_scale=(512, 512)  ,      keep_ratio=_lowercase  ,      align=_lowercase  ,      do_random_crop=_lowercase    )
									a_						:						str 							= SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""    ).to(
									    _lowercase    )

									a_						:						int 							= prepare_img()
									a_						:						Any 							= image_processor(images=_lowercase  ,      return_tensors="""pt"""    )
									a_						:						Optional[int] 							= encoded_inputs.pixel_values.to(_lowercase    )

									with torch.no_grad():
												a_						:						Any 							= model(_lowercase    )

									a_						:						str 							= outputs.logits.detach().cpu()

									a_						:						int 							= image_processor.post_process_semantic_segmentation(outputs=_lowercase  ,      target_sizes=[(500, 300)]    )
									a_						:						Any 							= torch.Size((500, 300)    )
									self.assertEqual(segmentation[0].shape  ,      _lowercase    )

									a_						:						Tuple 							= image_processor.post_process_semantic_segmentation(outputs=_lowercase    )
									a_						:						Optional[Any] 							= torch.Size((128, 128)    )
									self.assertEqual(segmentation[0].shape  ,      _lowercase    )


