




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       _A				:






  def __init__(    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE=2						,		_SCREAMING_SNAKE_CASE=True						,		_SCREAMING_SNAKE_CASE=False						,		_SCREAMING_SNAKE_CASE=10						,		_SCREAMING_SNAKE_CASE=3						,		_SCREAMING_SNAKE_CASE=32 * 8						,		_SCREAMING_SNAKE_CASE=32 * 8						,		_SCREAMING_SNAKE_CASE=4						,		_SCREAMING_SNAKE_CASE=64						,		):
        _UpperCAmelCase      =			parent
        _UpperCAmelCase      =			batch_size
        _UpperCAmelCase      =			is_training
        _UpperCAmelCase      =			use_auxiliary_loss
        _UpperCAmelCase      =			num_queries
        _UpperCAmelCase      =			num_channels
        _UpperCAmelCase      =			min_size
        _UpperCAmelCase      =			max_size
        _UpperCAmelCase      =			num_labels
        _UpperCAmelCase      =			hidden_dim
        _UpperCAmelCase      =			hidden_dim






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase      =			floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]			).to(
            UpperCamelCase__			)

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

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

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






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase      =			MaskaFormerConfig(
            hidden_size=self.hidden_dim						,		)
        _UpperCAmelCase      =			self.num_queries
        _UpperCAmelCase      =			self.num_labels

        _UpperCAmelCase      =			[1, 1, 1, 1]
        _UpperCAmelCase      =			self.num_channels

        _UpperCAmelCase      =			64
        _UpperCAmelCase      =			128
        _UpperCAmelCase      =			self.hidden_dim
        _UpperCAmelCase      =			self.hidden_dim
        _UpperCAmelCase      =			self.hidden_dim
        return config






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase      =			self.prepare_config_and_inputs()
        _UpperCAmelCase      =			{"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask}
        return config, inputs_dict






  def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
        _UpperCAmelCase      =			output.encoder_hidden_states
        _UpperCAmelCase      =			output.pixel_decoder_hidden_states
        _UpperCAmelCase      =			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_layers			)






  def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE=False			):





        with torch.no_grad():
              _UpperCAmelCase      =			MaskaFormerModel(config=UpperCamelCase__			)
              model.to(UpperCamelCase__			)
              model.eval()

              _UpperCAmelCase      =			model(pixel_values=UpperCamelCase__						,		pixel_mask=UpperCamelCase__			)
              _UpperCAmelCase      =			model(UpperCamelCase__						,		output_hidden_states=UpperCamelCase__			)

        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(UpperCamelCase__						,		UpperCamelCase__			)






  def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
        _UpperCAmelCase      =			MaskaFormerForUniversalSegmentation(config=UpperCamelCase__			)
        model.to(UpperCamelCase__			)
        model.eval()

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

              comm_check_on_output(UpperCamelCase__			)

              _UpperCAmelCase      =			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       _A				(		__lowercase       ,     __lowercase       ,     unittest.TestCase     ):
  __a					    =       (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
  __a					    =       {"feature-extraction": MaskaFormerModel} if is_torch_available() else {}

  __a					    =       False
  __a					    =       False
  __a					    =       False
  __a					    =       False






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase      =			MaskaFormerModelTester(self			)
        _UpperCAmelCase      =			ConfigTester(self						,		config_class=UpperCamelCase__						,		has_text_modality=UpperCamelCase__			)






  def 				UpperCAmelCase (    self			):
        self.config_tester.run_common_tests()






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase    ,					_UpperCAmelCase      =			self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.create_and_check_maskaformer_model(UpperCamelCase__						,		**UpperCamelCase__						,		output_hidden_states=UpperCamelCase__			)






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase      =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*UpperCamelCase__			)






  @unittest.skip(reason="""Mask2Former does not use inputs_embeds"""			)
  def 				UpperCAmelCase (    self			):
        pass






  @unittest.skip(reason="""Mask2Former does not have a get_input_embeddings method"""			)
  def 				UpperCAmelCase (    self			):
        pass






  @unittest.skip(reason="""Mask2Former is not a generative model"""			)
  def 				UpperCAmelCase (    self			):
        pass






  @unittest.skip(reason="""Mask2Former does not use token embeddings"""			)
  def 				UpperCAmelCase (    self			):
        pass






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






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






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase    ,					_UpperCAmelCase      =			self.model_tester.prepare_config_and_inputs_for_common()

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

              _UpperCAmelCase      =			["""pixel_values"""]
              self.assertListEqual(arg_names[:1]						,		UpperCamelCase__			)






  @slow
  def 				UpperCAmelCase (    self			):





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






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase      =			(self.model_tester.min_size,) * 2
        _UpperCAmelCase      =			{
            """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(),
        }
        _UpperCAmelCase      =			self.model_tester.get_config()

        _UpperCAmelCase      =			MaskaFormerForUniversalSegmentation(UpperCamelCase__			).to(UpperCamelCase__			)
        _UpperCAmelCase      =			model(**UpperCamelCase__			)
        self.assertTrue(outputs.loss is not None			)






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase    ,					_UpperCAmelCase      =			self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.create_and_check_maskaformer_model(UpperCamelCase__						,		**UpperCamelCase__						,		output_hidden_states=UpperCamelCase__			)






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase    ,					_UpperCAmelCase      =			self.model_tester.prepare_config_and_inputs_for_common()

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






  def 				UpperCAmelCase (    self			):





        if not self.model_tester.is_training:
              return

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

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

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






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase      =			self.all_model_classes[1]
        _UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase      =			self.model_tester.prepare_config_and_inputs()
        _UpperCAmelCase      =			True
        _UpperCAmelCase      =			True

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

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

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

        _UpperCAmelCase      =			outputs.pixel_decoder_hidden_states[0]
        pixel_decoder_hidden_states.retain_grad()

        _UpperCAmelCase      =			outputs.transformer_decoder_hidden_states[0]
        transformer_decoder_hidden_states.retain_grad()

        _UpperCAmelCase      =			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			)



a													=			1E-4




def        _SCREAMING_SNAKE_CASE      (		)						->				List[str]:
      _UpperCAmelCase      =			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
      return image




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






  @cached_property
  def 				UpperCAmelCase (    self			):
        return "facebook/mask2former-swin-small-coco-instance"






  @cached_property
  def 				UpperCAmelCase (    self			):
        return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints			) if is_vision_available() else None






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase      =			MaskaFormerModel.from_pretrained(self.model_checkpoints			).to(UpperCamelCase__			)
        _UpperCAmelCase      =			self.default_image_processor
        _UpperCAmelCase      =			prepare_img()
        _UpperCAmelCase      =			image_processor(UpperCamelCase__						,		return_tensors="""pt"""			).to(UpperCamelCase__			)
        _UpperCAmelCase      =			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, 384, 384)			)

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

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

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

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






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase      =			MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints			).to(UpperCamelCase__			).eval()
        _UpperCAmelCase      =			self.default_image_processor
        _UpperCAmelCase      =			prepare_img()
        _UpperCAmelCase      =			image_processor(UpperCamelCase__						,		return_tensors="""pt"""			).to(UpperCamelCase__			)
        _UpperCAmelCase      =			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, 384, 384)			)

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






  def 				UpperCAmelCase (    self			):
        _UpperCAmelCase      =			MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints			).to(UpperCamelCase__			).eval()
        _UpperCAmelCase      =			self.default_image_processor

        _UpperCAmelCase      =			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"""						,		)

        _UpperCAmelCase      =			inputs["""pixel_values"""].to(UpperCamelCase__			)
        _UpperCAmelCase      =			[el.to(UpperCamelCase__			) for el in inputs["""mask_labels"""]]
        _UpperCAmelCase      =			[el.to(UpperCamelCase__			) for el in inputs["""class_labels"""]]

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

        self.assertTrue(outputs.loss is not None			)





from __future__ import annotations

from scipy.special import comb  # type: ignore




class       _A				:






		def __init__(    self						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			list_of_points
								# Degree determines the flexibility of the curve.
								# Degree = 1 will produce a straight line.
								_UpperCAmelCase      =			len(_SCREAMING_SNAKE_CASE			) - 1






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								assert 0 <= t <= 1, "Time t must be between 0 and 1."
								_UpperCAmelCase      =			[]
								for i in range(len(self.list_of_points			)			):
														# basis function for each i
														output_values.append(
														    comb(self.degree						,		_SCREAMING_SNAKE_CASE			) * ((1 - t) ** (self.degree - i)) * (t**i)			)
								# the basis must sum up to 1 for it to produce a valid Bezier curve.
								assert round(sum(_SCREAMING_SNAKE_CASE			)						,		5			) == 1
								return output_values






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								assert 0 <= t <= 1, "Time t must be between 0 and 1."

								_UpperCAmelCase      =			self.basis_function(_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			0.0
								_UpperCAmelCase      =			0.0
								for i in range(len(self.list_of_points			)			):
														# For all points, sum up the product of i-th basis function and i-th point.
														x += basis_function[i] * self.list_of_points[i][0]
														y += basis_function[i] * self.list_of_points[i][1]
								return (x, y)






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE = 0.01			):
								from matplotlib import pyplot as plt  # type: ignore

								_UpperCAmelCase      =			[]  # x coordinates of points to plot
								_UpperCAmelCase      =			[]  # y coordinates of points to plot

								_UpperCAmelCase      =			0.0
								while t <= 1:
														_UpperCAmelCase      =			self.bezier_curve_function(_SCREAMING_SNAKE_CASE			)
														to_plot_x.append(value[0]			)
														to_plot_y.append(value[1]			)
														t += step_size

								_UpperCAmelCase      =			[i[0] for i in self.list_of_points]
								_UpperCAmelCase      =			[i[1] for i in self.list_of_points]

								plt.plot(
								    _SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		color="""blue"""						,		label="""Curve of Degree """ + str(self.degree			)						,		)
								plt.scatter(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		color="""red"""						,		label="""Control Points"""			)
								plt.legend()
								plt.show()


if __name__ == "__main__":
				import doctest

				doctest.testmod()

				BezierCurve([(1, 2), (3, 5)]).plot_curve()  # degree 1
				BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve()  # degree 2
				BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve()  # degree 3