


'''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 					a			(				snake_case__  ):
 '''simple docstring'''




 def   __UpperCamelCase   (    self      )      ->    int:
    _a					:  Tuple						=  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 					a			:
 '''simple docstring'''




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




 def   __UpperCamelCase   (    self      )      ->    List[Any]:
    _a					:  Tuple						=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

    _a					:  List[str]						=  None
    if self.use_labels:
       _a					:  Union[str, Any]						=  ids_tensor([self.batch_size, self.image_size, self.image_size]		,      self.num_labels      )

    _a					:  str						=  self.get_config()
    return config, pixel_values, labels




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




 def   __UpperCamelCase   (    self      )      ->    Tuple:
    _a					:  Any						=  self.prepare_config_and_inputs()
    _a		,						_a		,						_a					:  Union[str, Any]						=  config_and_inputs
    _a					:  Union[str, Any]						=  {'pixel_values': pixel_values}
    return config, inputs_dict







@require_torch
class 					a			(				snake_case__			,							snake_case__			,							unittest.TestCase  ):
 '''simple docstring'''




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

 __lowerCAmelCase						: Any							  =		True
 __lowerCAmelCase						: List[str]							  =		False
 __lowerCAmelCase						: int							  =		False
 __lowerCAmelCase						: int							  =		False




 def   __UpperCamelCase   (    self      )      ->    Union[str, Any]:
    _a					:  Tuple						=  SegformerModelTester(self      )
    _a					:  List[Any]						=  SegformerConfigTester(self		,      config_class=lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    int:
    self.config_tester.run_common_tests()




 def   __UpperCamelCase   (    self      )      ->    Tuple:
    _a					:  int						=  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    int:
    _a					:  List[Any]						=  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_binary_image_segmentation(*lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    int:
    _a					:  Union[str, Any]						=  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      )      ->    str:
    pass




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




 def   __UpperCamelCase   (    self      )      ->    Any:
    _a		,						_a					:  List[str]						=  self.model_tester.prepare_config_and_inputs_for_common()

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

       _a					:  Dict						=  ['pixel_values']
       self.assertListEqual(arg_names[:1]		,      lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    int:
    _a		,						_a					:  Optional[int]						=  self.model_tester.prepare_config_and_inputs_for_common()
    _a					:  int						=  True

    for model_class in self.all_model_classes:
       _a					:  Optional[Any]						=  True
       _a					:  Optional[Any]						=  False
       _a					:  Any						=  True
       _a					:  List[Any]						=  model_class(lowerCamelCase_      )
       model.to(lowerCamelCase_      )
       model.eval()
       with torch.no_grad():
          _a					:  Optional[int]						=  model(**self._prepare_for_class(lowerCamelCase_		,      lowerCamelCase_      )      )
       _a					:  Any						=  outputs.attentions

       _a					:  Any						=  sum(self.model_tester.depths      )
       self.assertEqual(len(lowerCamelCase_      )		,      lowerCamelCase_      )

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

       self.assertEqual(len(lowerCamelCase_      )		,      lowerCamelCase_      )

       # verify the first attentions (first block, first layer)
       _a					:  Optional[Any]						=  (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(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 // 3_2) ** 2
       _a					:  Any						=  (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]		,      )
       _a					:  List[str]						=  len(lowerCamelCase_      )

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

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

       _a					:  Tuple						=  outputs.attentions

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

       with torch.no_grad():
          _a					:  Any						=  model(**self._prepare_for_class(lowerCamelCase_		,      lowerCamelCase_      )      )

       _a					:  List[str]						=  outputs.hidden_states

       _a					:  Dict						=  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,
           ]		,      )

    _a		,						_a					:  List[Any]						=  self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       _a					:  Any						=  True
       check_hidden_states_output(lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_      )

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

       check_hidden_states_output(lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    Any:
    if not self.model_tester.is_training:
       return

    _a		,						_a					:  List[str]						=  self.model_tester.prepare_config_and_inputs_for_common()
    _a					:  Dict						=  True

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

       _a					:  str						=  model_class(lowerCamelCase_      )
       model.to(lowerCamelCase_      )
       model.train()
       _a					:  Optional[Any]						=  self._prepare_for_class(lowerCamelCase_		,      lowerCamelCase_		,      return_labels=lowerCamelCase_      )
       _a					:  Optional[Any]						=  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]:
    pass




 @slow
 def   __UpperCamelCase   (    self      )      ->    int:
    for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _a					:  str						=  SegformerModel.from_pretrained(lowerCamelCase_      )
       self.assertIsNotNone(lowerCamelCase_      )





def    UpperCAmelCase_  (       ):
   '''simple docstring'''





   _a					:  Optional[int]						=  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					:  Any						=  SegformerImageProcessor(
        image_scale=(5_1_2, 5_1_2)		,      keep_ratio=lowerCamelCase_		,      align=lowerCamelCase_		,      do_random_crop=lowerCamelCase_      )
    _a					:  Any						=  SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512'      ).to(
        lowerCamelCase_      )

    _a					:  Any						=  prepare_img()
    _a					:  Tuple						=  image_processor(images=lowerCamelCase_		,      return_tensors='pt'      )
    _a					:  Tuple						=  encoded_inputs.pixel_values.to(lowerCamelCase_      )

    with torch.no_grad():
       _a					:  Union[str, Any]						=  model(lowerCamelCase_      )

    _a					:  Optional[int]						=  torch.Size((1, model.config.num_labels, 1_2_8, 1_2_8)      )
    self.assertEqual(outputs.logits.shape		,      lowerCamelCase_      )

    _a					:  List[Any]						=  torch.tensor(
        [
            [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]],
            [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]],
            [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]],
        ]      ).to(lowerCamelCase_      )
    self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]		,      lowerCamelCase_		,      atol=1e-4      )      )




 @slow
 def   __UpperCamelCase   (    self      )      ->    Dict:
    # only resize + normalize
    _a					:  Optional[Any]						=  SegformerImageProcessor(
        image_scale=(5_1_2, 5_1_2)		,      keep_ratio=lowerCamelCase_		,      align=lowerCamelCase_		,      do_random_crop=lowerCamelCase_      )
    _a					:  str						=  SegformerForSemanticSegmentation.from_pretrained(
        'nvidia/segformer-b1-finetuned-cityscapes-1024-1024'      ).to(lowerCamelCase_      )

    _a					:  str						=  prepare_img()
    _a					:  Union[str, Any]						=  image_processor(images=lowerCamelCase_		,      return_tensors='pt'      )
    _a					:  Tuple						=  encoded_inputs.pixel_values.to(lowerCamelCase_      )

    with torch.no_grad():
       _a					:  List[Any]						=  model(lowerCamelCase_      )

    _a					:  str						=  torch.Size((1, model.config.num_labels, 1_2_8, 1_2_8)      )
    self.assertEqual(outputs.logits.shape		,      lowerCamelCase_      )

    _a					:  Optional[Any]						=  torch.tensor(
        [
            [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]],
            [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]],
            [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]],
        ]      ).to(lowerCamelCase_      )
    self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]		,      lowerCamelCase_		,      atol=1e-1      )      )




 @slow
 def   __UpperCamelCase   (    self      )      ->    Optional[int]:
    # only resize + normalize
    _a					:  List[str]						=  SegformerImageProcessor(
        image_scale=(5_1_2, 5_1_2)		,      keep_ratio=lowerCamelCase_		,      align=lowerCamelCase_		,      do_random_crop=lowerCamelCase_      )
    _a					:  Dict						=  SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512'      ).to(
        lowerCamelCase_      )

    _a					:  List[Any]						=  prepare_img()
    _a					:  Union[str, Any]						=  image_processor(images=lowerCamelCase_		,      return_tensors='pt'      )
    _a					:  str						=  encoded_inputs.pixel_values.to(lowerCamelCase_      )

    with torch.no_grad():
       _a					:  Any						=  model(lowerCamelCase_      )

    _a					:  Union[str, Any]						=  outputs.logits.detach().cpu()

    _a					:  Optional[int]						=  image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase_		,      target_sizes=[(5_0_0, 3_0_0)]      )
    _a					:  List[Any]						=  torch.Size((5_0_0, 3_0_0)      )
    self.assertEqual(segmentation[0].shape		,      lowerCamelCase_      )

    _a					:  Optional[int]						=  image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase_      )
    _a					:  Optional[Any]						=  torch.Size((1_2_8, 1_2_8)      )
    self.assertEqual(segmentation[0].shape		,      lowerCamelCase_      )






'''simple docstring'''




from ..utils import DummyObject, requires_backends







class 					a			(				metaclass=snake_case__  ):
 '''simple docstring'''




 __lowerCAmelCase						: Union[str, Any]							  =		["""note_seq"""]




 def __init__(    self		,      *lowerCamelCase_		,      **lowerCamelCase_      )      ->    int:
    requires_backends(self		,      ['note_seq']      )




 @classmethod
 def   __UpperCamelCase   (    cls		,      *lowerCamelCase_		,      **lowerCamelCase_      )      ->    Union[str, Any]:
    requires_backends(cls		,      ['note_seq']      )




 @classmethod
 def   __UpperCamelCase   (    cls		,      *lowerCamelCase_		,      **lowerCamelCase_      )      ->    List[str]:
    requires_backends(cls		,      ['note_seq']      )


