





'''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      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					):







 '''simple docstring'''

 def 							SCREAMING_SNAKE_CASE_      (self       :     List[str]) ->List[str]:



   '''simple docstring'''



   lowerCamelCase__:   List[str]    						=self.config_class(**self.inputs_dict)
   self.parent.assertTrue(hasattr(UpperCAmelCase_ ,				"hidden_sizes"))
   self.parent.assertTrue(hasattr(UpperCAmelCase_ ,				"num_attention_heads"))
   self.parent.assertTrue(hasattr(UpperCAmelCase_ ,				"num_encoder_blocks"))



class      _SCREAMING_SNAKE_CASE		:







 '''simple docstring'''

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



   '''simple docstring'''



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

 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[Any]) ->Union[str, Any]:



   '''simple docstring'''



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

   lowerCamelCase__:   Dict    						=None
   if self.use_labels:
     lowerCamelCase__:   Optional[Any]    						=ids_tensor([self.batch_size, self.image_size, self.image_size] ,				self.num_labels)

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

 def 							SCREAMING_SNAKE_CASE_      (self       :     Any) ->List[str]:



   '''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 							SCREAMING_SNAKE_CASE_      (self       :     Any ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     Optional[Any] ,				UpperCAmelCase_       :     Optional[int]) ->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:   int    						=SegformerModel(config=UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.eval()
   lowerCamelCase__:   Any    						=model(UpperCAmelCase_)
   lowerCamelCase__:   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 							SCREAMING_SNAKE_CASE_      (self       :     Any ,				UpperCAmelCase_       :     Optional[Any] ,				UpperCAmelCase_       :     List[Any] ,				UpperCAmelCase_       :     Tuple) ->Any:



   '''simple docstring'''



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



   '''simple docstring'''



   lowerCamelCase__:   Optional[Any]    						=1
   lowerCamelCase__:   Tuple    						=SegformerForSemanticSegmentation(config=UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.eval()
   lowerCamelCase__:   List[str]    						=torch.randint(0 ,				1 ,				(self.batch_size, self.image_size, self.image_size)).to(UpperCAmelCase_)
   lowerCamelCase__:   List[str]    						=model(UpperCAmelCase_ ,				labels=UpperCAmelCase_)
   self.parent.assertGreater(result.loss ,				0.0)







 def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->Optional[int]:



   '''simple docstring'''



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



@require_torch
class      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					,							__SCREAMING_SNAKE_CASE					,							unittest.TestCase					):







 '''simple docstring'''
 lowercase_	=    (
     (
         SegformerModel,
         SegformerForSemanticSegmentation,
         SegformerForImageClassification,
     )
     if is_torch_available()
     else ()
 )
 lowercase_	=    (
     {
         "feature-extraction": SegformerModel,
         "image-classification": SegformerForImageClassification,
         "image-segmentation": SegformerForSemanticSegmentation,
     }
     if is_torch_available()
     else {}
 )

 lowercase_	=    True
 lowercase_	=    False
 lowercase_	=    False
 lowercase_	=    False

 def 							SCREAMING_SNAKE_CASE_      (self       :     List[str]) ->int:



   '''simple docstring'''



   lowerCamelCase__:   Any    						=SegformerModelTester(self)
   lowerCamelCase__:   Tuple    						=SegformerConfigTester(self ,				config_class=UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     Dict) ->Dict:



   '''simple docstring'''



   self.config_tester.run_common_tests()

 def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:   Tuple    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->Tuple:



   '''simple docstring'''



   lowerCamelCase__:   int    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_binary_image_segmentation(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     Tuple) ->Tuple:



   '''simple docstring'''



   lowerCamelCase__:   Tuple    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_segmentation(*UpperCAmelCase_)

 @unittest.skip("SegFormer does not use inputs_embeds")
 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int]) ->Union[str, Any]:



   '''simple docstring'''



   pass

 @unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods")
 def 							SCREAMING_SNAKE_CASE_      (self       :     Tuple) ->Dict:



   '''simple docstring'''



   pass

 def 							SCREAMING_SNAKE_CASE_      (self       :     List[str]) ->Dict:



   '''simple docstring'''



   lowerCamelCase__:   Union[str, Any]    						=self.model_tester.prepare_config_and_inputs_for_common()

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

     lowerCamelCase__:   Optional[int]    						=["pixel_values"]
     self.assertListEqual(arg_names[:1] ,				UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[Any]) ->List[Any]:



   '''simple docstring'''



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

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

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

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

     self.assertEqual(len(UpperCAmelCase_) ,				UpperCAmelCase_)

     # verify the first attentions (first block, first layer)
     lowerCamelCase__:   Tuple    						=(self.model_tester.image_size // 4) ** 2
     lowerCamelCase__:   Tuple    						=(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)
     lowerCamelCase__:   Any    						=(self.model_tester.image_size // 32) ** 2
     lowerCamelCase__:   Any    						=(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] ,				)
     lowerCamelCase__:   Dict    						=len(UpperCAmelCase_)

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

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

     lowerCamelCase__:   Optional[Any]    						=outputs.attentions

     self.assertEqual(len(UpperCAmelCase_) ,				UpperCAmelCase_)
     # verify the first attentions (first block, first layer)
     lowerCamelCase__:   Optional[Any]    						=(self.model_tester.image_size // 4) ** 2
     lowerCamelCase__:   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 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int]) ->int:



   '''simple docstring'''



   def check_hidden_states_output(UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     Optional[int] ,				UpperCAmelCase_       :     Optional[Any]):
     lowerCamelCase__:   Optional[Any]    						=model_class(UpperCAmelCase_)
     model.to(UpperCAmelCase_)
     model.eval()

     with torch.no_grad():
       lowerCamelCase__:   Union[str, Any]    						=model(**self._prepare_for_class(UpperCAmelCase_ ,				UpperCAmelCase_))

     lowerCamelCase__:   Optional[int]    						=outputs.hidden_states

     lowerCamelCase__:   int    						=self.model_tester.num_encoder_blocks
     self.assertEqual(len(UpperCAmelCase_) ,				UpperCAmelCase_)

     # 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,
         ] ,				)

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

   for model_class in self.all_model_classes:
     lowerCamelCase__:   Union[str, Any]    						=True
     check_hidden_states_output(UpperCAmelCase_ ,				UpperCAmelCase_ ,				UpperCAmelCase_)

     # check that output_hidden_states also work using config
     del inputs_dict["output_hidden_states"]
     lowerCamelCase__:   List[str]    						=True

     check_hidden_states_output(UpperCAmelCase_ ,				UpperCAmelCase_ ,				UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     Union[str, Any]) ->int:



   '''simple docstring'''



   if not self.model_tester.is_training:
     return

   lowerCamelCase__:   str    						=self.model_tester.prepare_config_and_inputs_for_common()
   lowerCamelCase__:   str    						=True

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

     lowerCamelCase__:   Union[str, Any]    						=model_class(UpperCAmelCase_)
     model.to(UpperCAmelCase_)
     model.train()
     lowerCamelCase__:   Union[str, Any]    						=self._prepare_for_class(UpperCAmelCase_ ,				UpperCAmelCase_ ,				return_labels=UpperCAmelCase_)
     lowerCamelCase__:   Optional[Any]    						=model(**UpperCAmelCase_).loss
     loss.backward()

 @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests.")
 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->Dict:



   '''simple docstring'''



   pass







 @slow
 def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->List[Any]:



   '''simple docstring'''



   for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     lowerCamelCase__:   Any    						=SegformerModel.from_pretrained(UpperCAmelCase_)
     self.assertIsNotNone(UpperCAmelCase_)





def   lowerCAmelCase_				( )  ->	Optional[int]:







  """simple docstring"""





  lowerCamelCase__:   Optional[Any]    						=Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"	)
  return image






@require_torch
class      _SCREAMING_SNAKE_CASE		(			unittest.TestCase					):







 '''simple docstring'''

 @slow
 def 							SCREAMING_SNAKE_CASE_      (self       :     Any) ->List[str]:



   '''simple docstring'''



   lowerCamelCase__:   Union[str, Any]    						=SegformerImageProcessor(
       image_scale=(512, 512) ,				keep_ratio=UpperCAmelCase_ ,				align=UpperCAmelCase_ ,				do_random_crop=UpperCAmelCase_)
   lowerCamelCase__:   List[Any]    						=SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512").to(
       UpperCAmelCase_)

   lowerCamelCase__:   Dict    						=prepare_img()
   lowerCamelCase__:   str    						=image_processor(images=UpperCAmelCase_ ,				return_tensors="pt")
   lowerCamelCase__:   Tuple    						=encoded_inputs.pixel_values.to(UpperCAmelCase_)

   with torch.no_grad():
     lowerCamelCase__:   List[Any]    						=model(UpperCAmelCase_)

   lowerCamelCase__:   Optional[Any]    						=torch.Size((1, model.config.num_labels, 128, 128))
   self.assertEqual(outputs.logits.shape ,				UpperCAmelCase_)

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

 @slow
 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int]) ->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:   Tuple    						=SegformerImageProcessor(
       image_scale=(512, 512) ,				keep_ratio=UpperCAmelCase_ ,				align=UpperCAmelCase_ ,				do_random_crop=UpperCAmelCase_)
   lowerCamelCase__:   Any    						=SegformerForSemanticSegmentation.from_pretrained(
       "nvidia/segformer-b1-finetuned-cityscapes-1024-1024").to(UpperCAmelCase_)

   lowerCamelCase__:   Union[str, Any]    						=prepare_img()
   lowerCamelCase__:   Optional[int]    						=image_processor(images=UpperCAmelCase_ ,				return_tensors="pt")
   lowerCamelCase__:   List[str]    						=encoded_inputs.pixel_values.to(UpperCAmelCase_)

   with torch.no_grad():
     lowerCamelCase__:   int    						=model(UpperCAmelCase_)

   lowerCamelCase__:   Any    						=torch.Size((1, model.config.num_labels, 128, 128))
   self.assertEqual(outputs.logits.shape ,				UpperCAmelCase_)

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







 @slow
 def 							SCREAMING_SNAKE_CASE_      (self       :     List[Any]) ->Optional[Any]:



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=SegformerImageProcessor(
       image_scale=(512, 512) ,				keep_ratio=UpperCAmelCase_ ,				align=UpperCAmelCase_ ,				do_random_crop=UpperCAmelCase_)
   lowerCamelCase__:   Dict    						=SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512").to(
       UpperCAmelCase_)

   lowerCamelCase__:   Optional[int]    						=prepare_img()
   lowerCamelCase__:   Dict    						=image_processor(images=UpperCAmelCase_ ,				return_tensors="pt")
   lowerCamelCase__:   int    						=encoded_inputs.pixel_values.to(UpperCAmelCase_)

   with torch.no_grad():
     lowerCamelCase__:   List[str]    						=model(UpperCAmelCase_)

   lowerCamelCase__:   int    						=outputs.logits.detach().cpu()

   lowerCamelCase__:   Any    						=image_processor.post_process_semantic_segmentation(outputs=UpperCAmelCase_ ,				target_sizes=[(500, 300)])
   lowerCamelCase__:   Dict    						=torch.Size((500, 300))
   self.assertEqual(segmentation[0].shape ,				UpperCAmelCase_)

   lowerCamelCase__:   Tuple    						=image_processor.post_process_semantic_segmentation(outputs=UpperCAmelCase_)
   lowerCamelCase__:   Optional[int]    						=torch.Size((128, 128))
   self.assertEqual(segmentation[0].shape ,				UpperCAmelCase_)








def   lowerCAmelCase_				( __a			,					__a	)  ->	float:







  """simple docstring"""





  if density <= 0:
    raise ValueError("Impossible fluid density"	)
  if bulk_modulus <= 0:
    raise ValueError("Impossible bulk modulus"	)

  return (bulk_modulus / density) ** 0.5


if __name__ == "__main__":
     import doctest

     doctest.testmod()

