



"""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     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE  ):



   def    __UpperCAmelCase    (       self    :	List[Any]				):



     """simple docstring"""
     _snake_case   	=      self.config_class(**self.inputs_dict				)
     self.parent.assertTrue(hasattr(__lowerCamelCase							,       '''hidden_sizes'''				)				)
     self.parent.assertTrue(hasattr(__lowerCamelCase							,       '''num_attention_heads'''				)				)
     self.parent.assertTrue(hasattr(__lowerCamelCase							,       '''num_encoder_blocks'''				)				)



class     UpperCAmelCase    :



   def __init__(       self    :	str							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	Optional[int]=1_3							,       __lowerCamelCase    :	Optional[int]=6_4							,       __lowerCamelCase    :	str=3							,       __lowerCamelCase    :	Optional[int]=4							,       __lowerCamelCase    :	List[str]=[2, 2, 2, 2]							,       __lowerCamelCase    :	List[str]=[8, 4, 2, 1]							,       __lowerCamelCase    :	List[str]=[1_6, 3_2, 6_4, 1_2_8]							,       __lowerCamelCase    :	int=[1, 4, 8, 1_6]							,       __lowerCamelCase    :	Dict=[1, 2, 4, 8]							,       __lowerCamelCase    :	Union[str, Any]=True							,       __lowerCamelCase    :	int=True							,       __lowerCamelCase    :	Tuple="gelu"							,       __lowerCamelCase    :	Any=0.1							,       __lowerCamelCase    :	str=0.1							,       __lowerCamelCase    :	int=0.0_2							,       __lowerCamelCase    :	Any=3							,       __lowerCamelCase    :	str=None							,       ):



     """simple docstring"""
     _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    __UpperCAmelCase    (       self    :	List[Any]				):



     """simple docstring"""
     _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    __UpperCAmelCase    (       self    :	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    __UpperCAmelCase    (       self    :	Optional[Any]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	List[str]				):



     """simple docstring"""
     _snake_case   	=      SegformerModel(config=__lowerCamelCase				)
     model.to(__lowerCamelCase				)
     model.eval()
     _snake_case   	=      model(__lowerCamelCase				)
     _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    __UpperCAmelCase    (       self    :	Any							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	List[str]				):



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



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





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



     """simple docstring"""
     _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     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,unittest.TestCase  ):
   A__		:	str           =  (
       (
           SegformerModel,
           SegformerForSemanticSegmentation,
           SegformerForImageClassification,
       )
       if is_torch_available()
       else ()
   )
   A__		:	Optional[int]           =  (
       {
           '''feature-extraction''': SegformerModel,
           '''image-classification''': SegformerForImageClassification,
           '''image-segmentation''': SegformerForSemanticSegmentation,
       }
       if is_torch_available()
       else {}
   )

   A__		:	Optional[Any]           =  True
   A__		:	List[str]           =  False
   A__		:	Any           =  False
   A__		:	str           =  False



   def    __UpperCAmelCase    (       self    :	Optional[int]				):



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



   def    __UpperCAmelCase    (       self    :	List[str]				):



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



   def    __UpperCAmelCase    (       self    :	Optional[int]				):



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



   def    __UpperCAmelCase    (       self    :	Tuple				):



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



   def    __UpperCAmelCase    (       self    :	Optional[Any]				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_segmentation(*__lowerCamelCase				)



   @unittest.skip('''SegFormer does not use inputs_embeds'''				)
   def    __UpperCAmelCase    (       self    :	Any				):



     """simple docstring"""
     pass



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



     """simple docstring"""
     pass



   def    __UpperCAmelCase    (       self    :	Optional[Any]				):



     """simple docstring"""
     _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(__lowerCamelCase				)
       _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]							,       __lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Optional[Any]				):



     """simple docstring"""
     _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(__lowerCamelCase				)
       model.to(__lowerCamelCase				)
       model.eval()
       with torch.no_grad():
         _snake_case   	=      model(**self._prepare_for_class(__lowerCamelCase							,       __lowerCamelCase				)				)
       _snake_case   	=      outputs.attentions

       _snake_case   	=      sum(self.model_tester.depths				)
       self.assertEqual(len(__lowerCamelCase				)							,       __lowerCamelCase				)

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

       self.assertEqual(len(__lowerCamelCase				)							,       __lowerCamelCase				)

       # 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 // 3_2) ** 2
       _snake_case   	=      (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]							,       )
       _snake_case   	=      len(__lowerCamelCase				)

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

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

       _snake_case   	=      outputs.attentions

       self.assertEqual(len(__lowerCamelCase				)							,       __lowerCamelCase				)
       # 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    __UpperCAmelCase    (       self    :	Optional[Any]				):



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

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

       _snake_case   	=      outputs.hidden_states

       _snake_case   	=      self.model_tester.num_encoder_blocks
       self.assertEqual(len(__lowerCamelCase				)							,       __lowerCamelCase				)

       # 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(__lowerCamelCase							,       __lowerCamelCase							,       __lowerCamelCase				)

       # check that output_hidden_states also work using config
       del inputs_dict["output_hidden_states"]
       _snake_case   	=      True

       check_hidden_states_output(__lowerCamelCase							,       __lowerCamelCase							,       __lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Dict				):



     """simple docstring"""
     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(__lowerCamelCase				):
         continue

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



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



     """simple docstring"""
     pass





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



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




def 			snake_case		(		)						->				int:
  _snake_case   	=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
  return image







@require_torch
class     UpperCAmelCase    (	unittest.TestCase  ):



   @slow
   def    __UpperCAmelCase    (       self    :	int				):



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

     _snake_case   	=      prepare_img()
     _snake_case   	=      image_processor(images=__lowerCamelCase							,       return_tensors='''pt'''				)
     _snake_case   	=      encoded_inputs.pixel_values.to(__lowerCamelCase				)

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

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

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



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



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

     _snake_case   	=      prepare_img()
     _snake_case   	=      image_processor(images=__lowerCamelCase							,       return_tensors='''pt'''				)
     _snake_case   	=      encoded_inputs.pixel_values.to(__lowerCamelCase				)

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

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

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





   @slow
   def    __UpperCAmelCase    (       self    :	Union[str, Any]				):



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

     _snake_case   	=      prepare_img()
     _snake_case   	=      image_processor(images=__lowerCamelCase							,       return_tensors='''pt'''				)
     _snake_case   	=      encoded_inputs.pixel_values.to(__lowerCamelCase				)

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

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

     _snake_case   	=      image_processor.post_process_semantic_segmentation(outputs=__lowerCamelCase							,       target_sizes=[(5_0_0, 3_0_0)]				)
     _snake_case   	=      torch.Size((5_0_0, 3_0_0)				)
     self.assertEqual(segmentation[0].shape							,       __lowerCamelCase				)

     _snake_case   	=      image_processor.post_process_semantic_segmentation(outputs=__lowerCamelCase				)
     _snake_case   	=      torch.Size((1_2_8, 1_2_8)				)
     self.assertEqual(segmentation[0].shape							,       __lowerCamelCase				)








from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_UpperCAmelCase						:  Union[str, Any]									=			logging.get_logger(__name__)

_UpperCAmelCase						:  Dict									=			{
    """microsoft/resnet-50""": """https://huggingface.co/microsoft/resnet-50/blob/main/config.json""",
}







class 					lowercase							(			lowercase_      ,    lowercase_     ):
				__SCREAMING_SNAKE_CASE   :					List[str]			     =     '''resnet'''
				__SCREAMING_SNAKE_CASE   :					Dict			     =     ['''basic''', '''bottleneck''']




				def __init__(   self ,  snake_case=3 ,  snake_case=64 ,  snake_case=[256, 512, 1024, 2048] ,  snake_case=[3, 4, 6, 3] ,  snake_case="bottleneck" ,  snake_case="relu" ,  snake_case=False ,  snake_case=None ,  snake_case=None ,  **snake_case ,  ):
											super().__init__(**snake_case			)
											if layer_type not in self.layer_types:
																		raise ValueError(F'''layer_type={layer_type} is not one of {",".join(self.layer_types			)}'''			)
											snake_case_     							=       num_channels
											snake_case_     							=       embedding_size
											snake_case_     							=       hidden_sizes
											snake_case_     							=       depths
											snake_case_     							=       layer_type
											snake_case_     							=       hidden_act
											snake_case_     							=       downsample_in_first_stage
											snake_case_     							=       ['stem'] + [F'''stage{idx}''' for idx in range(1 ,  len(snake_case			) + 1			)]
											snake_case_							,       snake_case_     							=       get_aligned_output_features_output_indices(
											    out_features=snake_case ,  out_indices=snake_case ,  stage_names=self.stage_names			)







class 					lowercase							(			lowercase_     ):
				__SCREAMING_SNAKE_CASE   :					List[Any]			     =     version.parse('''1.11'''     )




				@property
				def        a				(   self			):
											return OrderedDict(
											    [
											        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
											    ]			)




				@property
				def        a				(   self			):
											return 1e-3




