






'''simple docstring'''







import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, 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 MaskFormerForInstanceSegmentation, MaskFormerModel

  if is_vision_available():
    from transformers import MaskFormerImageProcessor

if is_vision_available():
  from PIL import Image




class   __SCREAMING_SNAKE_CASE       :


  """simple docstring"""


  def __init__(			self   ,	UpperCamelCase__   ,	UpperCamelCase__=2   ,	UpperCamelCase__=True   ,	UpperCamelCase__=False   ,	UpperCamelCase__=10   ,	UpperCamelCase__=3   ,	UpperCamelCase__=32 * 4   ,	UpperCamelCase__=32 * 6   ,	UpperCamelCase__=4   ,	UpperCamelCase__=32   ,	):






         """simple docstring"""


         a_						     =			parent
         a_						     =			batch_size
         a_						     =			is_training
         a_						     =			use_auxiliary_loss
         a_						     =			num_queries
         a_						     =			num_channels
         a_						     =			min_size
         a_						     =			max_size
         a_						     =			num_labels
         a_						     =			mask_feature_size







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]	).to(
             UpperCamelCase__	)

         a_						     =			torch.ones([self.batch_size, self.min_size, self.max_size]   ,	device=UpperCamelCase__	)

         a_						     =			(
             torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]   ,	device=UpperCamelCase__	) > 0.5
         ).float()
         a_						     =			(torch.rand((self.batch_size, self.num_labels)   ,	device=UpperCamelCase__	) > 0.5).long()

         a_						     =			self.get_config()
         return config, pixel_values, pixel_mask, mask_labels, class_labels







  def    _a       (			self	):






         """simple docstring"""


         return MaskFormerConfig.from_backbone_and_decoder_configs(
             backbone_config=SwinConfig(
                 depths=[1, 1, 1, 1]   ,	)   ,	decoder_config=DetrConfig(
                 decoder_ffn_dim=128   ,	num_queries=self.num_queries   ,	decoder_attention_heads=2   ,	d_model=self.mask_feature_size   ,	)   ,	mask_feature_size=self.mask_feature_size   ,	fpn_feature_size=self.mask_feature_size   ,	num_channels=self.num_channels   ,	num_labels=self.num_labels   ,	)







  def    _a       (			self	):






         """simple docstring"""


         a_     ,	a_     ,	a_     ,	a_     ,	a_						     =			self.prepare_config_and_inputs()
         a_						     =			{'pixel_values': pixel_values, 'pixel_mask': pixel_mask}
         return config, inputs_dict







  def    _a       (			self   ,	UpperCamelCase__   ,	UpperCamelCase__	):






         """simple docstring"""


         a_						     =			output.encoder_hidden_states
         a_						     =			output.pixel_decoder_hidden_states
         a_						     =			output.transformer_decoder_hidden_states

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







  def    _a       (			self   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__=False	):






         """simple docstring"""


         with torch.no_grad():
                a_						     =			MaskFormerModel(config=UpperCamelCase__	)
                model.to(UpperCamelCase__	)
                model.eval()

                a_						     =			model(pixel_values=UpperCamelCase__   ,	pixel_mask=UpperCamelCase__	)
                a_						     =			model(UpperCamelCase__   ,	output_hidden_states=UpperCamelCase__	)
         # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
         # encoder and pixel decoder
         self.parent.assertEqual(
             output.transformer_decoder_last_hidden_state.shape   ,	(self.batch_size, self.num_queries, self.mask_feature_size)   ,	)
         # 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(UpperCamelCase__   ,	UpperCamelCase__	)







  def    _a       (			self   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__	):






         """simple docstring"""


         a_						     =			MaskFormerForInstanceSegmentation(config=UpperCamelCase__	)
         model.to(UpperCamelCase__	)
         model.eval()

         def comm_check_on_output(UpperCamelCase__	):
                # 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():
                a_						     =			model(pixel_values=UpperCamelCase__   ,	pixel_mask=UpperCamelCase__	)
                a_						     =			model(UpperCamelCase__	)

                comm_check_on_output(UpperCamelCase__	)

                a_						     =			model(
                    pixel_values=UpperCamelCase__   ,	pixel_mask=UpperCamelCase__   ,	mask_labels=UpperCamelCase__   ,	class_labels=UpperCamelCase__	)

         comm_check_on_output(UpperCamelCase__	)

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





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


  """simple docstring"""


  _a  : Optional[int]      			=     (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
  _a  : Optional[int]      			=     (
      {'''feature-extraction''': MaskFormerModel, '''image-segmentation''': MaskFormerForInstanceSegmentation}
      if is_torch_available()
      else {}
  )

  _a  : List[str]      			=     False
  _a  : Tuple      			=     False
  _a  : int      			=     False
  _a  : Dict      			=     False







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			MaskFormerModelTester(self	)
         a_						     =			ConfigTester(self   ,	config_class=UpperCamelCase__   ,	has_text_modality=UpperCamelCase__	)







  def    _a       (			self	):






         """simple docstring"""


         self.config_tester.run_common_tests()







  def    _a       (			self	):






         """simple docstring"""


         a_     ,	a_						     =			self.model_tester.prepare_config_and_inputs_for_common()
         self.model_tester.create_and_check_maskformer_model(UpperCamelCase__   ,	**UpperCamelCase__   ,	output_hidden_states=UpperCamelCase__	)







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*UpperCamelCase__	)







  @unittest.skip(reason='MaskFormer does not use inputs_embeds'	)
  def    _a       (			self	):






         """simple docstring"""


         pass







  @unittest.skip(reason='MaskFormer does not have a get_input_embeddings method'	)
  def    _a       (			self	):






         """simple docstring"""


         pass







  @unittest.skip(reason='MaskFormer is not a generative model'	)
  def    _a       (			self	):






         """simple docstring"""


         pass







  @unittest.skip(reason='MaskFormer does not use token embeddings'	)
  def    _a       (			self	):






         """simple docstring"""


         pass







  @require_torch_multi_gpu
  @unittest.skip(
      reason='MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'	)
  def    _a       (			self	):






         """simple docstring"""


         pass







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






         """simple docstring"""


         pass







  def    _a       (			self	):






         """simple docstring"""


         a_     ,	a_						     =			self.model_tester.prepare_config_and_inputs_for_common()

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

                a_						     =			['pixel_values']
                self.assertListEqual(arg_names[:1]   ,	UpperCamelCase__	)







  @slow
  def    _a       (			self	):






         """simple docstring"""


         for model_name in ["facebook/maskformer-swin-small-coco"]:
                a_						     =			MaskFormerModel.from_pretrained(UpperCamelCase__	)
                self.assertIsNotNone(UpperCamelCase__	)







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			(self.model_tester.min_size,) * 2
         a_						     =			{
             'pixel_values': torch.randn((2, 3, *size)   ,	device=UpperCamelCase__	),
             'mask_labels': torch.randn((2, 10, *size)   ,	device=UpperCamelCase__	),
             'class_labels': torch.zeros(2   ,	10   ,	device=UpperCamelCase__	).long(),
         }

         a_						     =			MaskFormerForInstanceSegmentation(MaskFormerConfig()	).to(UpperCamelCase__	)
         a_						     =			model(**UpperCamelCase__	)
         self.assertTrue(outputs.loss is not None	)







  def    _a       (			self	):






         """simple docstring"""


         a_     ,	a_						     =			self.model_tester.prepare_config_and_inputs_for_common()
         self.model_tester.create_and_check_maskformer_model(UpperCamelCase__   ,	**UpperCamelCase__   ,	output_hidden_states=UpperCamelCase__	)







  def    _a       (			self	):






         """simple docstring"""


         a_     ,	a_						     =			self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                a_						     =			model_class(UpperCamelCase__	).to(UpperCamelCase__	)
                a_						     =			model(**UpperCamelCase__   ,	output_attentions=UpperCamelCase__	)
                self.assertTrue(outputs.attentions is not None	)







  def    _a       (			self	):






         """simple docstring"""


         if not self.model_tester.is_training:
                return
         # only MaskFormerForInstanceSegmentation has the loss
         a_						     =			self.all_model_classes[1]
         a_     ,	a_     ,	a_     ,	a_     ,	a_						     =			self.model_tester.prepare_config_and_inputs()

         a_						     =			model_class(UpperCamelCase__	)
         model.to(UpperCamelCase__	)
         model.train()

         a_						     =			model(UpperCamelCase__   ,	mask_labels=UpperCamelCase__   ,	class_labels=UpperCamelCase__	).loss
         loss.backward()







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			self.all_model_classes[1]
         a_     ,	a_     ,	a_     ,	a_     ,	a_						     =			self.model_tester.prepare_config_and_inputs()
         a_						     =			True
         a_						     =			True

         a_						     =			model_class(UpperCamelCase__	)
         model.to(UpperCamelCase__	)
         model.train()

         a_						     =			model(UpperCamelCase__   ,	mask_labels=UpperCamelCase__   ,	class_labels=UpperCamelCase__	)

         a_						     =			outputs.encoder_hidden_states[0]
         encoder_hidden_states.retain_grad()

         a_						     =			outputs.pixel_decoder_hidden_states[0]
         pixel_decoder_hidden_states.retain_grad()
         # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
         a_						     =			outputs.transformer_decoder_hidden_states[0]
         transformer_decoder_hidden_states.retain_grad()

         a_						     =			outputs.attentions[0]
         attentions.retain_grad()

         outputs.loss.backward(retain_graph=UpperCamelCase__	)

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





__lowerCAmelCase        =							1e-4


def       __UpperCamelCase					(      ):
       """simple docstring"""




       a_						     =			Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'   )
       return image




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


  """simple docstring"""


  @cached_property
  def    _a       (			self	):






         """simple docstring"""


         return (
             MaskFormerImageProcessor.from_pretrained('facebook/maskformer-swin-small-coco'	)
             if is_vision_available()
             else None
         )







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			MaskFormerModel.from_pretrained('facebook/maskformer-swin-small-coco'	).to(UpperCamelCase__	)
         a_						     =			self.default_image_processor
         a_						     =			prepare_img()
         a_						     =			image_processor(UpperCamelCase__   ,	return_tensors='pt'	).to(UpperCamelCase__	)
         a_						     =			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(UpperCamelCase__   ,	(1, 3, 800, 1_088)	)

         with torch.no_grad():
                a_						     =			model(**UpperCamelCase__	)

         a_						     =			torch.tensor(
             [[-0.0_482, 0.9_228, 0.4_951], [-0.2_547, 0.8_017, 0.8_527], [-0.0_069, 0.3_385, -0.0_089]]	).to(UpperCamelCase__	)
         self.assertTrue(
             torch.allclose(
                 outputs.encoder_last_hidden_state[0, 0, :3, :3]   ,	UpperCamelCase__   ,	atol=UpperCamelCase__	)	)

         a_						     =			torch.tensor(
             [[-0.8_422, -0.8_434, -0.9_718], [-1.0_144, -0.5_565, -0.4_195], [-1.0_038, -0.4_484, -0.1_961]]	).to(UpperCamelCase__	)
         self.assertTrue(
             torch.allclose(
                 outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]   ,	UpperCamelCase__   ,	atol=UpperCamelCase__	)	)

         a_						     =			torch.tensor(
             [[0.2_852, -0.0_159, 0.9_735], [0.6_254, 0.1_858, 0.8_529], [-0.0_680, -0.4_116, 1.8_413]]	).to(UpperCamelCase__	)
         self.assertTrue(
             torch.allclose(
                 outputs.transformer_decoder_last_hidden_state[0, :3, :3]   ,	UpperCamelCase__   ,	atol=UpperCamelCase__	)	)







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			(
             MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-swin-small-coco'	)
             .to(UpperCamelCase__	)
             .eval()
         )
         a_						     =			self.default_image_processor
         a_						     =			prepare_img()
         a_						     =			image_processor(UpperCamelCase__   ,	return_tensors='pt'	).to(UpperCamelCase__	)
         a_						     =			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(UpperCamelCase__   ,	(1, 3, 800, 1_088)	)

         with torch.no_grad():
                a_						     =			model(**UpperCamelCase__	)
         # masks_queries_logits
         a_						     =			outputs.masks_queries_logits
         self.assertEqual(
             masks_queries_logits.shape   ,	(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)   ,	)
         a_						     =			[
             [-1.3_737_124, -1.7_724_937, -1.9_364_233],
             [-1.5_977_281, -1.9_867_939, -2.1_523_695],
             [-1.5_795_398, -1.9_269_832, -2.093_942],
         ]
         a_						     =			torch.tensor(UpperCamelCase__	).to(UpperCamelCase__	)
         self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]   ,	UpperCamelCase__   ,	atol=UpperCamelCase__	)	)
         # class_queries_logits
         a_						     =			outputs.class_queries_logits
         self.assertEqual(
             class_queries_logits.shape   ,	(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)	)
         a_						     =			torch.tensor(
             [
                 [1.6512e00, -5.2572e00, -3.3519e00],
                 [3.6169e-02, -5.9025e00, -2.9313e00],
                 [1.0766e-04, -7.7630e00, -5.1263e00],
             ]	).to(UpperCamelCase__	)
         self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]   ,	UpperCamelCase__   ,	atol=UpperCamelCase__	)	)







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			(
             MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-resnet101-coco-stuff'	)
             .to(UpperCamelCase__	)
             .eval()
         )
         a_						     =			self.default_image_processor
         a_						     =			prepare_img()
         a_						     =			image_processor(UpperCamelCase__   ,	return_tensors='pt'	).to(UpperCamelCase__	)
         a_						     =			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(UpperCamelCase__   ,	(1, 3, 800, 1_088)	)

         with torch.no_grad():
                a_						     =			model(**UpperCamelCase__	)
         # masks_queries_logits
         a_						     =			outputs.masks_queries_logits
         self.assertEqual(
             masks_queries_logits.shape   ,	(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)   ,	)
         a_						     =			[[-0.9_046, -2.6_366, -4.6_062], [-3.4_179, -5.7_890, -8.8_057], [-4.9_179, -7.6_560, -10.7_711]]
         a_						     =			torch.tensor(UpperCamelCase__	).to(UpperCamelCase__	)
         self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]   ,	UpperCamelCase__   ,	atol=UpperCamelCase__	)	)
         # class_queries_logits
         a_						     =			outputs.class_queries_logits
         self.assertEqual(
             class_queries_logits.shape   ,	(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)	)
         a_						     =			torch.tensor(
             [[4.7_188, -3.2_585, -2.8_857], [6.6_871, -2.9_181, -1.2_487], [7.2_449, -2.2_764, -2.1_874]]	).to(UpperCamelCase__	)
         self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]   ,	UpperCamelCase__   ,	atol=UpperCamelCase__	)	)







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			(
             MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-swin-small-coco'	)
             .to(UpperCamelCase__	)
             .eval()
         )
         a_						     =			self.default_image_processor

         a_						     =			image_processor(
             [np.zeros((3, 800, 1_333)	), np.zeros((3, 800, 1_333)	)]   ,	segmentation_maps=[np.zeros((384, 384)	).astype(np.floataa	), np.zeros((384, 384)	).astype(np.floataa	)]   ,	return_tensors='pt'   ,	)

         a_						     =			inputs['pixel_values'].to(UpperCamelCase__	)
         a_						     =			[el.to(UpperCamelCase__	) for el in inputs['mask_labels']]
         a_						     =			[el.to(UpperCamelCase__	) for el in inputs['class_labels']]

         with torch.no_grad():
                a_						     =			model(**UpperCamelCase__	)

         self.assertTrue(outputs.loss is not None	)












'''simple docstring'''







# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import sys
import warnings
from os.path import abspath, dirname, join


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
__lowerCAmelCase        =							abspath(join(dirname(dirname(dirname(__file__))), "src"))
sys.path.insert(1, git_repo_path)


# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action="ignore", category=FutureWarning)


def       __UpperCamelCase					(      lowercase_       :  Dict   ):
       """simple docstring"""




       from transformers.testing_utils import pytest_addoption_shared

       pytest_addoption_shared(lowercase_   )


def       __UpperCamelCase					(      lowercase_       :  List[Any]   ):
       """simple docstring"""




       from transformers.testing_utils import pytest_terminal_summary_main

       a_						     =			terminalreporter.config.getoption('--make-reports'   )
       if make_reports:
              pytest_terminal_summary_main(lowercase_   ,       id=lowercase_   )




