
'''simple docstring'''

import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import MaskaFormerConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel

     if is_vision_available():
          from transformers import MaskaFormerImageProcessor

if is_vision_available():
     from PIL import Image




class    lowerCAmelCase       :


 def __init__(    self  : Optional[int]			,							a__  : str			,							a__  : Tuple=2			,							a__  : Dict=True			,							a__  : List[Any]=False			,							a__  : int=10			,							a__  : Dict=3			,							a__  : Any=32 * 8			,							a__  : Tuple=32 * 8			,							a__  : Dict=4			,							a__  : str=64			,							):
  '''simple docstring'''

  lowerCAmelCase__     :   str     		=	parent
  lowerCAmelCase__     :   Optional[int]     		=	batch_size
  lowerCAmelCase__     :   str     		=	is_training
  lowerCAmelCase__     :   Any     		=	use_auxiliary_loss
  lowerCAmelCase__     :   List[Any]     		=	num_queries
  lowerCAmelCase__     :   Optional[Any]     		=	num_channels
  lowerCAmelCase__     :   List[str]     		=	min_size
  lowerCAmelCase__     :   str     		=	max_size
  lowerCAmelCase__     :   Tuple     		=	num_labels
  lowerCAmelCase__     :   Union[str, Any]     		=	hidden_dim
  lowerCAmelCase__     :   Optional[Any]     		=	hidden_dim


 def    _A	(    self  : Optional[Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   int     		=	floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]    ).to(
      a__    )

  lowerCAmelCase__     :   Dict     		=	torch.ones([self.batch_size, self.min_size, self.max_size]			,							device=a__    )

  lowerCAmelCase__     :   Union[str, Any]     		=	(
      torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]			,							device=a__    ) > 0.5
  ).float()
  lowerCAmelCase__     :   List[Any]     		=	(torch.rand((self.batch_size, self.num_labels)			,							device=a__    ) > 0.5).long()

  lowerCAmelCase__     :   List[str]     		=	self.get_config()
  return config, pixel_values, pixel_mask, mask_labels, class_labels


 def    _A	(    self  : int    ):
  '''simple docstring'''

  lowerCAmelCase__     :   int     		=	MaskaFormerConfig(
      hidden_size=self.hidden_dim			,							)
  lowerCAmelCase__     :   str     		=	self.num_queries
  lowerCAmelCase__     :   Dict     		=	self.num_labels

  lowerCAmelCase__     :   Optional[Any]     		=	[1, 1, 1, 1]
  lowerCAmelCase__     :   List[str]     		=	self.num_channels

  lowerCAmelCase__     :   Any     		=	64
  lowerCAmelCase__     :   int     		=	128
  lowerCAmelCase__     :   List[Any]     		=	self.hidden_dim
  lowerCAmelCase__     :   Optional[int]     		=	self.hidden_dim
  lowerCAmelCase__     :   List[str]     		=	self.hidden_dim
  return config


 def    _A	(    self  : Optional[Any]    ):
  '''simple docstring'''

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


 def    _A	(    self  : Any			,							a__  : int			,							a__  : Tuple    ):
  '''simple docstring'''

  lowerCAmelCase__     :   int     		=	output.encoder_hidden_states
  lowerCAmelCase__     :   List[Any]     		=	output.pixel_decoder_hidden_states
  lowerCAmelCase__     :   List[Any]     		=	output.transformer_decoder_hidden_states

  self.parent.assertTrue(len(a__    )			,							len(config.backbone_config.depths    )    )
  self.parent.assertTrue(len(a__    )			,							len(config.backbone_config.depths    )    )
  self.parent.assertTrue(len(a__    )			,							config.decoder_layers    )


 def    _A	(    self  : Tuple			,							a__  : Any			,							a__  : Dict			,							a__  : int			,							a__  : Optional[Any]=False    ):
  '''simple docstring'''

  with torch.no_grad():
   lowerCAmelCase__     :   int     		=	MaskaFormerModel(config=a__    )
   model.to(a__    )
   model.eval()

   lowerCAmelCase__     :   Union[str, Any]     		=	model(pixel_values=a__			,							pixel_mask=a__    )
   lowerCAmelCase__     :   Optional[int]     		=	model(a__			,							output_hidden_states=a__    )

  self.parent.assertEqual(
      output.transformer_decoder_last_hidden_state.shape			,							(self.batch_size, self.num_queries, self.hidden_dim)			,							)
  # let's ensure the other two hidden state exists
  self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None    )
  self.parent.assertTrue(output.encoder_last_hidden_state is not None    )

  if output_hidden_states:
   self.check_output_hidden_state(a__			,							a__    )


 def    _A	(    self  : Tuple			,							a__  : str			,							a__  : str			,							a__  : Optional[Any]			,							a__  : Union[str, Any]			,							a__  : Union[str, Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Any     		=	MaskaFormerForUniversalSegmentation(config=a__    )
  model.to(a__    )
  model.eval()

  def comm_check_on_output(a__  : Union[str, Any]    ):
   # let's still check that all the required stuff is there
   self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None    )
   self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None    )
   self.parent.assertTrue(result.encoder_last_hidden_state is not None    )
   # okay, now we need to check the logits shape
   # due to the encoder compression, masks have a //4 spatial size
   self.parent.assertEqual(
       result.masks_queries_logits.shape			,							(self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)			,							)
   # + 1 for null class
   self.parent.assertEqual(
       result.class_queries_logits.shape			,							(self.batch_size, self.num_queries, self.num_labels + 1)    )

  with torch.no_grad():
   lowerCAmelCase__     :   int     		=	model(pixel_values=a__			,							pixel_mask=a__    )
   lowerCAmelCase__     :   List[str]     		=	model(a__    )

   comm_check_on_output(a__    )

   lowerCAmelCase__     :   Optional[Any]     		=	model(
       pixel_values=a__			,							pixel_mask=a__			,							mask_labels=a__			,							class_labels=a__    )

  comm_check_on_output(a__    )

  self.parent.assertTrue(result.loss is not None    )
  self.parent.assertEqual(result.loss.shape			,							torch.Size([1]    )    )




@require_torch
class    lowerCAmelCase       (     UpperCamelCase_    ,  UpperCamelCase_    ,  unittest.TestCase  ):
 A_				:				Any	=			(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
 A_				:				Optional[int]	=			{"""feature-extraction""": MaskaFormerModel} if is_torch_available() else {}

 A_				:				Any	=			False
 A_				:				List[str]	=			False
 A_				:				Union[str, Any]	=			False
 A_				:				Optional[int]	=			False


 def    _A	(    self  : Dict    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Union[str, Any]     		=	MaskaFormerModelTester(self    )
  lowerCAmelCase__     :   str     		=	ConfigTester(self			,							config_class=a__			,							has_text_modality=a__    )


 def    _A	(    self  : int    ):
  '''simple docstring'''

  self.config_tester.run_common_tests()


 def    _A	(    self  : Dict    ):
  '''simple docstring'''

  lowerCAmelCase__		,     lowerCAmelCase__     :   Union[str, Any]     		=	self.model_tester.prepare_config_and_inputs_for_common()
  self.model_tester.create_and_check_maskaformer_model(a__			,							**a__			,							output_hidden_states=a__    )


 def    _A	(    self  : List[Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   str     		=	self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*a__    )


 @unittest.skip(reason="Mask2Former does not use inputs_embeds"    )
 def    _A	(    self  : Optional[Any]    ):
  '''simple docstring'''

  pass


 @unittest.skip(reason="Mask2Former does not have a get_input_embeddings method"    )
 def    _A	(    self  : Optional[int]    ):
  '''simple docstring'''

  pass


 @unittest.skip(reason="Mask2Former is not a generative model"    )
 def    _A	(    self  : str    ):
  '''simple docstring'''

  pass


 @unittest.skip(reason="Mask2Former does not use token embeddings"    )
 def    _A	(    self  : Dict    ):
  '''simple docstring'''

  pass


 @require_torch_multi_gpu
 @unittest.skip(
     reason="Mask2Former has some layers using `add_module` which doesn't work well with `nn.DataParallel`"    )
 def    _A	(    self  : List[Any]    ):
  '''simple docstring'''

  pass


 @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."    )
 def    _A	(    self  : int    ):
  '''simple docstring'''

  pass


 def    _A	(    self  : Tuple    ):
  '''simple docstring'''

  lowerCAmelCase__		,     lowerCAmelCase__     :   Dict     		=	self.model_tester.prepare_config_and_inputs_for_common()

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

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


 @slow
 def    _A	(    self  : List[Any]    ):
  '''simple docstring'''

  for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
   lowerCAmelCase__     :   Dict     		=	MaskaFormerModel.from_pretrained(a__    )
   self.assertIsNotNone(a__    )


 def    _A	(    self  : List[Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   List[str]     		=	(self.model_tester.min_size,) * 2
  lowerCAmelCase__     :   str     		=	{
      "pixel_values": torch.randn((2, 3, *size)			,							device=a__    ),
      "mask_labels": torch.randn((2, 10, *size)			,							device=a__    ),
      "class_labels": torch.zeros(2			,							10			,							device=a__    ).long(),
  }
  lowerCAmelCase__     :   Union[str, Any]     		=	self.model_tester.get_config()

  lowerCAmelCase__     :   List[Any]     		=	MaskaFormerForUniversalSegmentation(a__    ).to(a__    )
  lowerCAmelCase__     :   Optional[int]     		=	model(**a__    )
  self.assertTrue(outputs.loss is not None    )


 def    _A	(    self  : Union[str, Any]    ):
  '''simple docstring'''

  lowerCAmelCase__		,     lowerCAmelCase__     :   Optional[Any]     		=	self.model_tester.prepare_config_and_inputs_for_common()
  self.model_tester.create_and_check_maskaformer_model(a__			,							**a__			,							output_hidden_states=a__    )


 def    _A	(    self  : Dict    ):
  '''simple docstring'''

  lowerCAmelCase__		,     lowerCAmelCase__     :   int     		=	self.model_tester.prepare_config_and_inputs_for_common()

  for model_class in self.all_model_classes:
   lowerCAmelCase__     :   Any     		=	model_class(a__    ).to(a__    )
   lowerCAmelCase__     :   int     		=	model(**a__			,							output_attentions=a__    )
   self.assertTrue(outputs.attentions is not None    )


 def    _A	(    self  : int    ):
  '''simple docstring'''

  if not self.model_tester.is_training:
   return

  lowerCAmelCase__     :   Optional[int]     		=	self.all_model_classes[1]
  lowerCAmelCase__		,     lowerCAmelCase__		,     lowerCAmelCase__		,     lowerCAmelCase__		,     lowerCAmelCase__     :   List[Any]     		=	self.model_tester.prepare_config_and_inputs()

  lowerCAmelCase__     :   Dict     		=	model_class(a__    )
  model.to(a__    )
  model.train()

  lowerCAmelCase__     :   Optional[Any]     		=	model(a__			,							mask_labels=a__			,							class_labels=a__    ).loss
  loss.backward()


 def    _A	(    self  : str    ):
  '''simple docstring'''

  lowerCAmelCase__     :   List[str]     		=	self.all_model_classes[1]
  lowerCAmelCase__		,     lowerCAmelCase__		,     lowerCAmelCase__		,     lowerCAmelCase__		,     lowerCAmelCase__     :   Optional[Any]     		=	self.model_tester.prepare_config_and_inputs()
  lowerCAmelCase__     :   Optional[Any]     		=	True
  lowerCAmelCase__     :   Optional[int]     		=	True

  lowerCAmelCase__     :   str     		=	model_class(a__    ).to(a__    )
  model.train()

  lowerCAmelCase__     :   Optional[int]     		=	model(a__			,							mask_labels=a__			,							class_labels=a__    )

  lowerCAmelCase__     :   Tuple     		=	outputs.encoder_hidden_states[0]
  encoder_hidden_states.retain_grad()

  lowerCAmelCase__     :   Union[str, Any]     		=	outputs.pixel_decoder_hidden_states[0]
  pixel_decoder_hidden_states.retain_grad()

  lowerCAmelCase__     :   Optional[Any]     		=	outputs.transformer_decoder_hidden_states[0]
  transformer_decoder_hidden_states.retain_grad()

  lowerCAmelCase__     :   Optional[int]     		=	outputs.attentions[0]
  attentions.retain_grad()

  outputs.loss.backward(retain_graph=a__    )

  self.assertIsNotNone(encoder_hidden_states.grad    )
  self.assertIsNotNone(pixel_decoder_hidden_states.grad    )
  self.assertIsNotNone(transformer_decoder_hidden_states.grad    )
  self.assertIsNotNone(attentions.grad    )



snake_case									=    1e-4


def        UpperCAmelCase_ (  ):







 """simple docstring"""


 lowerCAmelCase__     :   Dict     		=	Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
 return image







@require_vision
@slow
class    lowerCAmelCase       (     unittest.TestCase  ):


 @cached_property
 def    _A	(    self  : Union[str, Any]    ):
  '''simple docstring'''

  return "facebook/mask2former-swin-small-coco-instance"


 @cached_property
 def    _A	(    self  : str    ):
  '''simple docstring'''

  return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints    ) if is_vision_available() else None


 def    _A	(    self  : Union[str, Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Union[str, Any]     		=	MaskaFormerModel.from_pretrained(self.model_checkpoints    ).to(a__    )
  lowerCAmelCase__     :   Tuple     		=	self.default_image_processor
  lowerCAmelCase__     :   Dict     		=	prepare_img()
  lowerCAmelCase__     :   int     		=	image_processor(a__			,							return_tensors="pt"    ).to(a__    )
  lowerCAmelCase__     :   Optional[int]     		=	inputs["pixel_values"].shape
  # check size is divisible by 32
  self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0    )
  # check size
  self.assertEqual(a__			,							(1, 3, 384, 384)    )

  with torch.no_grad():
   lowerCAmelCase__     :   List[Any]     		=	model(**a__    )

  lowerCAmelCase__     :   int     		=	torch.tensor(
      [[-0.2790, -1.0717, -1.1668], [-0.5128, -0.3128, -0.4987], [-0.5832, 0.1971, -0.0197]]    ).to(a__    )
  self.assertTrue(
      torch.allclose(
          outputs.encoder_last_hidden_state[0, 0, :3, :3]			,							a__			,							atol=a__    )    )

  lowerCAmelCase__     :   Tuple     		=	torch.tensor(
      [[0.8973, 1.1847, 1.1776], [1.1934, 1.5040, 1.5128], [1.1153, 1.4486, 1.4951]]    ).to(a__    )
  self.assertTrue(
      torch.allclose(
          outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]			,							a__			,							atol=a__    )    )

  lowerCAmelCase__     :   List[str]     		=	torch.tensor(
      [[2.1152, 1.7000, -0.8603], [1.5808, 1.8004, -0.9353], [1.6043, 1.7495, -0.5999]]    ).to(a__    )
  self.assertTrue(
      torch.allclose(
          outputs.transformer_decoder_last_hidden_state[0, :3, :3]			,							a__			,							atol=a__    )    )


 def    _A	(    self  : str    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Any     		=	MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints    ).to(a__    ).eval()
  lowerCAmelCase__     :   Tuple     		=	self.default_image_processor
  lowerCAmelCase__     :   Union[str, Any]     		=	prepare_img()
  lowerCAmelCase__     :   int     		=	image_processor(a__			,							return_tensors="pt"    ).to(a__    )
  lowerCAmelCase__     :   Any     		=	inputs["pixel_values"].shape
  # check size is divisible by 32
  self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0    )
  # check size
  self.assertEqual(a__			,							(1, 3, 384, 384)    )

  with torch.no_grad():
   lowerCAmelCase__     :   Union[str, Any]     		=	model(**a__    )
  # masks_queries_logits
  lowerCAmelCase__     :   Any     		=	outputs.masks_queries_logits
  self.assertEqual(
      masks_queries_logits.shape			,							(1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)    )
  lowerCAmelCase__     :   Dict     		=	[
      [-8.7839, -9.0056, -8.8121],
      [-7.4104, -7.0313, -6.5401],
      [-6.6105, -6.3427, -6.4675],
  ]
  lowerCAmelCase__     :   Any     		=	torch.tensor(a__    ).to(a__    )
  self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]			,							a__			,							atol=a__    )    )
  # class_queries_logits
  lowerCAmelCase__     :   int     		=	outputs.class_queries_logits
  self.assertEqual(class_queries_logits.shape			,							(1, model.config.num_queries, model.config.num_labels + 1)    )
  lowerCAmelCase__     :   Any     		=	torch.tensor(
      [
          [1.8324, -8.0835, -4.1922],
          [0.8450, -9.0050, -3.6053],
          [0.3045, -7.7293, -3.0275],
      ]    ).to(a__    )
  self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]			,							a__			,							atol=a__    )    )


 def    _A	(    self  : List[Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   List[Any]     		=	MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints    ).to(a__    ).eval()
  lowerCAmelCase__     :   Dict     		=	self.default_image_processor

  lowerCAmelCase__     :   Optional[Any]     		=	image_processor(
      [np.zeros((3, 800, 1333)    ), np.zeros((3, 800, 1333)    )]			,							segmentation_maps=[np.zeros((384, 384)    ).astype(np.floataa    ), np.zeros((384, 384)    ).astype(np.floataa    )]			,							return_tensors="pt"			,							)

  lowerCAmelCase__     :   Union[str, Any]     		=	inputs["pixel_values"].to(a__    )
  lowerCAmelCase__     :   List[Any]     		=	[el.to(a__    ) for el in inputs["mask_labels"]]
  lowerCAmelCase__     :   Dict     		=	[el.to(a__    ) for el in inputs["class_labels"]]

  with torch.no_grad():
   lowerCAmelCase__     :   Dict     		=	model(**a__    )

  self.assertTrue(outputs.loss is not None    )







'''simple docstring'''

from cva import destroyAllWindows, imread, imshow, waitKey


def        UpperCAmelCase_ (  lowerCamelCase_			):







 """simple docstring"""


 lowerCAmelCase__		,     lowerCAmelCase__     :   Tuple     		=	img.shape[0], img.shape[1]

 # converting each pixel's color to its negative
 for i in range(lowerCamelCase_			):
  for j in range(lowerCamelCase_			):
   lowerCAmelCase__     :   Optional[int]     		=	[2_5_5, 2_5_5, 2_5_5] - img[i][j]

 return img


if __name__ == "__main__":
     # read original image
     snake_case									=    imread("""image_data/lena.jpg""", 1)

     # convert to its negative
     snake_case									=    convert_to_negative(img)

     # show result image
     imshow("""negative of original image""", img)
     waitKey(0)
     destroyAllWindows()





