
"""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 				lowercase__:
	'''simple docstring'''

	def __init__(						self    :Dict		, lowerCamelCase_    :str		, lowerCamelCase_    :Dict=2		, lowerCamelCase_    :Optional[int]=True		, lowerCamelCase_    :Any=False		, lowerCamelCase_    :int=10		, lowerCamelCase_    :Tuple=3		, lowerCamelCase_    :str=32 * 4		, lowerCamelCase_    :Union[str, Any]=32 * 6		, lowerCamelCase_    :Union[str, Any]=4		, lowerCamelCase_    :Tuple=32		, )							->			Optional[int]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     str				= parent
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= batch_size
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= is_training
			SCREAMING_SNAKE_CASE       :     List[Any]				= use_auxiliary_loss
			SCREAMING_SNAKE_CASE       :     Dict				= num_queries
			SCREAMING_SNAKE_CASE       :     Optional[int]				= num_channels
			SCREAMING_SNAKE_CASE       :     List[str]				= min_size
			SCREAMING_SNAKE_CASE       :     Dict				= max_size
			SCREAMING_SNAKE_CASE       :     Dict				= num_labels
			SCREAMING_SNAKE_CASE       :     List[str]				= mask_feature_size





	def   __lowerCAmelCase (						self    :Any					)							->			Any:







			'''simple docstring'''




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

			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= torch.ones([self.batch_size, self.min_size, self.max_size]		, device=lowerCamelCase_					)

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

			SCREAMING_SNAKE_CASE       :     Any				= self.get_config()
			return config, pixel_values, pixel_mask, mask_labels, class_labels





	def   __lowerCAmelCase (						self    :Optional[Any]					)							->			str:







			'''simple docstring'''




			return MaskFormerConfig.from_backbone_and_decoder_configs(
			    backbone_config=SwinConfig(
			        depths=[1, 1, 1, 1]		, )		, decoder_config=DetrConfig(
			        decoder_ffn_dim=1_28		, 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   __lowerCAmelCase (						self    :List[str]					)							->			Dict:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE       :     Tuple				= self.prepare_config_and_inputs()
			SCREAMING_SNAKE_CASE       :     List[str]				= {'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
			return config, inputs_dict





	def   __lowerCAmelCase (						self    :int		, lowerCamelCase_    :Union[str, Any]		, lowerCamelCase_    :List[Any]					)							->			Optional[int]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Tuple				= output.encoder_hidden_states
			SCREAMING_SNAKE_CASE       :     int				= output.pixel_decoder_hidden_states
			SCREAMING_SNAKE_CASE       :     str				= output.transformer_decoder_hidden_states

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





	def   __lowerCAmelCase (						self    :int		, lowerCamelCase_    :int		, lowerCamelCase_    :Optional[int]		, lowerCamelCase_    :Optional[int]		, lowerCamelCase_    :List[str]=False					)							->			str:







			'''simple docstring'''




			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     Tuple				= MaskFormerModel(config=lowerCamelCase_					)
					model.to(lowerCamelCase_					)
					model.eval()

					SCREAMING_SNAKE_CASE       :     List[str]				= model(pixel_values=lowerCamelCase_		, pixel_mask=lowerCamelCase_					)
					SCREAMING_SNAKE_CASE       :     Dict				= model(lowerCamelCase_		, output_hidden_states=lowerCamelCase_					)
			# 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(lowerCamelCase_		, lowerCamelCase_					)







	def   __lowerCAmelCase (						self    :Optional[Any]		, lowerCamelCase_    :Dict		, lowerCamelCase_    :Optional[Any]		, lowerCamelCase_    :Optional[Any]		, lowerCamelCase_    :int		, lowerCamelCase_    :int					)							->			List[Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= MaskFormerForInstanceSegmentation(config=lowerCamelCase_					)
			model.to(lowerCamelCase_					)
			model.eval()

			def comm_check_on_output(lowerCamelCase_    :List[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():
					SCREAMING_SNAKE_CASE       :     List[Any]				= model(pixel_values=lowerCamelCase_		, pixel_mask=lowerCamelCase_					)
					SCREAMING_SNAKE_CASE       :     List[Any]				= model(lowerCamelCase_					)

					comm_check_on_output(lowerCamelCase_					)

					SCREAMING_SNAKE_CASE       :     List[str]				= model(
					    pixel_values=lowerCamelCase_		, pixel_mask=lowerCamelCase_		, mask_labels=lowerCamelCase_		, class_labels=lowerCamelCase_					)

			comm_check_on_output(lowerCamelCase_					)

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





@require_torch
class 				lowercase__(				_UpperCAmelCase		,						_UpperCAmelCase		,						unittest.TestCase   ):
	'''simple docstring'''

	UpperCamelCase				=  (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
	UpperCamelCase				=  (
	    {"""feature-extraction""": MaskFormerModel, """image-segmentation""": MaskFormerForInstanceSegmentation}
	    if is_torch_available()
	    else {}
	)

	UpperCamelCase				=  False
	UpperCamelCase				=  False
	UpperCamelCase				=  False
	UpperCamelCase				=  False





	def   __lowerCAmelCase (						self    :Any					)							->			int:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     List[Any]				= MaskFormerModelTester(self					)
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= ConfigTester(self		, config_class=lowerCamelCase_		, has_text_modality=lowerCamelCase_					)





	def   __lowerCAmelCase (						self    :Optional[int]					)							->			Optional[Any]:







			'''simple docstring'''




			self.config_tester.run_common_tests()





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







			'''simple docstring'''




			SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE       :     Tuple				= self.model_tester.prepare_config_and_inputs_for_common()
			self.model_tester.create_and_check_maskformer_model(lowerCamelCase_		, **lowerCamelCase_		, output_hidden_states=lowerCamelCase_					)





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







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Optional[int]				= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*lowerCamelCase_					)





	@unittest.skip(reason='''MaskFormer does not use inputs_embeds'''					)
	def   __lowerCAmelCase (						self    :Any					)							->			str:







			'''simple docstring'''




			pass





	@unittest.skip(reason='''MaskFormer does not have a get_input_embeddings method'''					)
	def   __lowerCAmelCase (						self    :Any					)							->			Tuple:







			'''simple docstring'''




			pass





	@unittest.skip(reason='''MaskFormer is not a generative model'''					)
	def   __lowerCAmelCase (						self    :List[Any]					)							->			Any:







			'''simple docstring'''




			pass





	@unittest.skip(reason='''MaskFormer does not use token embeddings'''					)
	def   __lowerCAmelCase (						self    :Optional[Any]					)							->			List[Any]:







			'''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   __lowerCAmelCase (						self    :str					)							->			Dict:







			'''simple docstring'''




			pass





	@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''					)
	def   __lowerCAmelCase (						self    :Optional[int]					)							->			Union[str, Any]:







			'''simple docstring'''




			pass





	def   __lowerCAmelCase (						self    :str					)							->			str:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE       :     str				= self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					SCREAMING_SNAKE_CASE       :     Union[str, Any]				= model_class(lowerCamelCase_					)
					SCREAMING_SNAKE_CASE       :     Tuple				= inspect.signature(model.forward					)
					# signature.parameters is an OrderedDict => so arg_names order is deterministic
					SCREAMING_SNAKE_CASE       :     int				= [*signature.parameters.keys()]

					SCREAMING_SNAKE_CASE       :     Optional[Any]				= ['''pixel_values''']
					self.assertListEqual(arg_names[:1]		, lowerCamelCase_					)





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







			'''simple docstring'''




			for model_name in ["facebook/maskformer-swin-small-coco"]:
					SCREAMING_SNAKE_CASE       :     Optional[int]				= MaskFormerModel.from_pretrained(lowerCamelCase_					)
					self.assertIsNotNone(lowerCamelCase_					)





	def   __lowerCAmelCase (						self    :Union[str, Any]					)							->			Tuple:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= (self.model_tester.min_size,) * 2
			SCREAMING_SNAKE_CASE       :     Optional[int]				= {
			    '''pixel_values''': torch.randn((2, 3, *size)		, device=lowerCamelCase_					),
			    '''mask_labels''': torch.randn((2, 10, *size)		, device=lowerCamelCase_					),
			    '''class_labels''': torch.zeros(2		, 10		, device=lowerCamelCase_					).long(),
			}

			SCREAMING_SNAKE_CASE       :     Dict				= MaskFormerForInstanceSegmentation(MaskFormerConfig()					).to(lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     Dict				= model(**lowerCamelCase_					)
			self.assertTrue(outputs.loss is not None					)





	def   __lowerCAmelCase (						self    :Optional[int]					)							->			Optional[Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE       :     Any				= self.model_tester.prepare_config_and_inputs_for_common()
			self.model_tester.create_and_check_maskformer_model(lowerCamelCase_		, **lowerCamelCase_		, output_hidden_states=lowerCamelCase_					)





	def   __lowerCAmelCase (						self    :str					)							->			Any:







			'''simple docstring'''




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

			for model_class in self.all_model_classes:
					SCREAMING_SNAKE_CASE       :     Union[str, Any]				= model_class(lowerCamelCase_					).to(lowerCamelCase_					)
					SCREAMING_SNAKE_CASE       :     Dict				= model(**lowerCamelCase_		, output_attentions=lowerCamelCase_					)
					self.assertTrue(outputs.attentions is not None					)





	def   __lowerCAmelCase (						self    :Any					)							->			Dict:







			'''simple docstring'''




			if not self.model_tester.is_training:
					return
			# only MaskFormerForInstanceSegmentation has the loss
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= self.all_model_classes[1]
			SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE       :     List[Any]				= self.model_tester.prepare_config_and_inputs()

			SCREAMING_SNAKE_CASE       :     Optional[Any]				= model_class(lowerCamelCase_					)
			model.to(lowerCamelCase_					)
			model.train()

			SCREAMING_SNAKE_CASE       :     Tuple				= model(lowerCamelCase_		, mask_labels=lowerCamelCase_		, class_labels=lowerCamelCase_					).loss
			loss.backward()







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







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     int				= self.all_model_classes[1]
			SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE       :     List[Any]				= self.model_tester.prepare_config_and_inputs()
			SCREAMING_SNAKE_CASE       :     Tuple				= True
			SCREAMING_SNAKE_CASE       :     Dict				= True

			SCREAMING_SNAKE_CASE       :     Any				= model_class(lowerCamelCase_					)
			model.to(lowerCamelCase_					)
			model.train()

			SCREAMING_SNAKE_CASE       :     List[str]				= model(lowerCamelCase_		, mask_labels=lowerCamelCase_		, class_labels=lowerCamelCase_					)

			SCREAMING_SNAKE_CASE       :     Dict				= outputs.encoder_hidden_states[0]
			encoder_hidden_states.retain_grad()

			SCREAMING_SNAKE_CASE       :     Optional[int]				= 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
			SCREAMING_SNAKE_CASE       :     int				= outputs.transformer_decoder_hidden_states[0]
			transformer_decoder_hidden_states.retain_grad()

			SCREAMING_SNAKE_CASE       :     List[str]				= outputs.attentions[0]
			attentions.retain_grad()

			outputs.loss.backward(retain_graph=lowerCamelCase_					)

			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__ :       Dict						     =			1e-4





def __A						(   )->							Tuple:




		'''simple docstring'''






		SCREAMING_SNAKE_CASE       :     List[Any]				= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
		return image




@require_vision
@slow
class 				lowercase__(				unittest.TestCase   ):
	'''simple docstring'''

	@cached_property
	def   __lowerCAmelCase (						self    :Any					)							->			Optional[int]:







			'''simple docstring'''




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





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







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Optional[Any]				= MaskFormerModel.from_pretrained('''facebook/maskformer-swin-small-coco'''					).to(lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     List[str]				= self.default_image_processor
			SCREAMING_SNAKE_CASE       :     List[Any]				= prepare_img()
			SCREAMING_SNAKE_CASE       :     Optional[int]				= image_processor(lowerCamelCase_		, return_tensors='''pt'''					).to(lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     Union[str, 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(lowerCamelCase_		, (1, 3, 8_00, 10_88)					)

			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     Dict				= model(**lowerCamelCase_					)

			SCREAMING_SNAKE_CASE       :     List[str]				= torch.tensor(
			    [[-0.0_4_8_2, 0.9_2_2_8, 0.4_9_5_1], [-0.2_5_4_7, 0.8_0_1_7, 0.8_5_2_7], [-0.0_0_6_9, 0.3_3_8_5, -0.0_0_8_9]]					).to(lowerCamelCase_					)
			self.assertTrue(
			    torch.allclose(
			        outputs.encoder_last_hidden_state[0, 0, :3, :3]		, lowerCamelCase_		, atol=lowerCamelCase_					)					)

			SCREAMING_SNAKE_CASE       :     Any				= torch.tensor(
			    [[-0.8_4_2_2, -0.8_4_3_4, -0.9_7_1_8], [-1.0_1_4_4, -0.5_5_6_5, -0.4_1_9_5], [-1.0_0_3_8, -0.4_4_8_4, -0.1_9_6_1]]					).to(lowerCamelCase_					)
			self.assertTrue(
			    torch.allclose(
			        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]		, lowerCamelCase_		, atol=lowerCamelCase_					)					)

			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= torch.tensor(
			    [[0.2_8_5_2, -0.0_1_5_9, 0.9_7_3_5], [0.6_2_5_4, 0.1_8_5_8, 0.8_5_2_9], [-0.0_6_8_0, -0.4_1_1_6, 1.8_4_1_3]]					).to(lowerCamelCase_					)
			self.assertTrue(
			    torch.allclose(
			        outputs.transformer_decoder_last_hidden_state[0, :3, :3]		, lowerCamelCase_		, atol=lowerCamelCase_					)					)





	def   __lowerCAmelCase (						self    :Any					)							->			Any:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     List[Any]				= (
			    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''					)
			    .to(lowerCamelCase_					)
			    .eval()
			)
			SCREAMING_SNAKE_CASE       :     int				= self.default_image_processor
			SCREAMING_SNAKE_CASE       :     List[str]				= prepare_img()
			SCREAMING_SNAKE_CASE       :     Any				= image_processor(lowerCamelCase_		, return_tensors='''pt'''					).to(lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     Dict				= 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(lowerCamelCase_		, (1, 3, 8_00, 10_88)					)

			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     str				= model(**lowerCamelCase_					)
			# masks_queries_logits
			SCREAMING_SNAKE_CASE       :     int				= 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)		, )
			SCREAMING_SNAKE_CASE       :     List[str]				= [
			    [-1.3_7_3_7_1_2_4, -1.7_7_2_4_9_3_7, -1.9_3_6_4_2_3_3],
			    [-1.5_9_7_7_2_8_1, -1.9_8_6_7_9_3_9, -2.1_5_2_3_6_9_5],
			    [-1.5_7_9_5_3_9_8, -1.9_2_6_9_8_3_2, -2.0_9_3_9_4_2],
			]
			SCREAMING_SNAKE_CASE       :     int				= torch.tensor(lowerCamelCase_					).to(lowerCamelCase_					)
			self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]		, lowerCamelCase_		, atol=lowerCamelCase_					)					)
			# class_queries_logits
			SCREAMING_SNAKE_CASE       :     int				= outputs.class_queries_logits
			self.assertEqual(
			    class_queries_logits.shape		, (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)					)
			SCREAMING_SNAKE_CASE       :     Tuple				= torch.tensor(
			    [
			        [1.6512E00, -5.2572E00, -3.3519E00],
			        [3.6169E-02, -5.9025E00, -2.9313E00],
			        [1.0766E-04, -7.7630E00, -5.1263E00],
			    ]					).to(lowerCamelCase_					)
			self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]		, lowerCamelCase_		, atol=lowerCamelCase_					)					)





	def   __lowerCAmelCase (						self    :str					)							->			str:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Optional[Any]				= (
			    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-resnet101-coco-stuff'''					)
			    .to(lowerCamelCase_					)
			    .eval()
			)
			SCREAMING_SNAKE_CASE       :     int				= self.default_image_processor
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= prepare_img()
			SCREAMING_SNAKE_CASE       :     List[str]				= image_processor(lowerCamelCase_		, return_tensors='''pt'''					).to(lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     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(lowerCamelCase_		, (1, 3, 8_00, 10_88)					)

			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     Tuple				= model(**lowerCamelCase_					)
			# masks_queries_logits
			SCREAMING_SNAKE_CASE       :     Tuple				= 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)		, )
			SCREAMING_SNAKE_CASE       :     int				= [[-0.9_0_4_6, -2.6_3_6_6, -4.6_0_6_2], [-3.4_1_7_9, -5.7_8_9_0, -8.8_0_5_7], [-4.9_1_7_9, -7.6_5_6_0, -1_0.7_7_1_1]]
			SCREAMING_SNAKE_CASE       :     Tuple				= torch.tensor(lowerCamelCase_					).to(lowerCamelCase_					)
			self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]		, lowerCamelCase_		, atol=lowerCamelCase_					)					)
			# class_queries_logits
			SCREAMING_SNAKE_CASE       :     int				= outputs.class_queries_logits
			self.assertEqual(
			    class_queries_logits.shape		, (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)					)
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= torch.tensor(
			    [[4.7_1_8_8, -3.2_5_8_5, -2.8_8_5_7], [6.6_8_7_1, -2.9_1_8_1, -1.2_4_8_7], [7.2_4_4_9, -2.2_7_6_4, -2.1_8_7_4]]					).to(lowerCamelCase_					)
			self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]		, lowerCamelCase_		, atol=lowerCamelCase_					)					)







	def   __lowerCAmelCase (						self    :Any					)							->			Tuple:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Any				= (
			    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''					)
			    .to(lowerCamelCase_					)
			    .eval()
			)
			SCREAMING_SNAKE_CASE       :     Optional[int]				= self.default_image_processor

			SCREAMING_SNAKE_CASE       :     List[str]				= image_processor(
			    [np.zeros((3, 8_00, 13_33)					), np.zeros((3, 8_00, 13_33)					)]		, segmentation_maps=[np.zeros((3_84, 3_84)					).astype(np.floataa					), np.zeros((3_84, 3_84)					).astype(np.floataa					)]		, return_tensors='''pt'''		, )

			SCREAMING_SNAKE_CASE       :     List[Any]				= inputs['''pixel_values'''].to(lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     Any				= [el.to(lowerCamelCase_					) for el in inputs['''mask_labels''']]
			SCREAMING_SNAKE_CASE       :     Tuple				= [el.to(lowerCamelCase_					) for el in inputs['''class_labels''']]

			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     int				= model(**lowerCamelCase_					)

			self.assertTrue(outputs.loss is not None					)







"""simple docstring"""






from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowerCamelCase__ :       int						     =			logging.get_logger(__name__)




class 				lowercase__(				_UpperCAmelCase		,						_UpperCAmelCase   ):
	'''simple docstring'''

	UpperCamelCase				=  """maskformer-swin"""

	UpperCamelCase				=  {
	    """num_attention_heads""": """num_heads""",
	    """num_hidden_layers""": """num_layers""",
	}





	def __init__(						self    :Optional[int]		, lowerCamelCase_    :List[Any]=2_24		, lowerCamelCase_    :Tuple=4		, lowerCamelCase_    :Optional[Any]=3		, lowerCamelCase_    :List[str]=96		, lowerCamelCase_    :int=[2, 2, 6, 2]		, lowerCamelCase_    :Union[str, Any]=[3, 6, 12, 24]		, lowerCamelCase_    :Optional[int]=7		, lowerCamelCase_    :Tuple=4.0		, lowerCamelCase_    :Tuple=True		, lowerCamelCase_    :Dict=0.0		, lowerCamelCase_    :Any=0.0		, lowerCamelCase_    :List[Any]=0.1		, lowerCamelCase_    :Dict="gelu"		, lowerCamelCase_    :Optional[int]=False		, lowerCamelCase_    :List[str]=0.0_2		, lowerCamelCase_    :Any=1E-5		, lowerCamelCase_    :Union[str, Any]=None		, lowerCamelCase_    :List[str]=None		, **lowerCamelCase_    :Union[str, Any]		, )							->			Dict:







			'''simple docstring'''




			super().__init__(**lowerCamelCase_					)

			SCREAMING_SNAKE_CASE       :     int				= image_size
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= patch_size
			SCREAMING_SNAKE_CASE       :     str				= num_channels
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= embed_dim
			SCREAMING_SNAKE_CASE       :     List[Any]				= depths
			SCREAMING_SNAKE_CASE       :     List[str]				= len(lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     str				= num_heads
			SCREAMING_SNAKE_CASE       :     Any				= window_size
			SCREAMING_SNAKE_CASE       :     List[str]				= mlp_ratio
			SCREAMING_SNAKE_CASE       :     str				= qkv_bias
			SCREAMING_SNAKE_CASE       :     Tuple				= hidden_dropout_prob
			SCREAMING_SNAKE_CASE       :     int				= attention_probs_dropout_prob
			SCREAMING_SNAKE_CASE       :     int				= drop_path_rate
			SCREAMING_SNAKE_CASE       :     Tuple				= hidden_act
			SCREAMING_SNAKE_CASE       :     Any				= use_absolute_embeddings
			SCREAMING_SNAKE_CASE       :     List[str]				= layer_norm_eps
			SCREAMING_SNAKE_CASE       :     List[str]				= initializer_range
			# we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
			# this indicates the channel dimension after the last stage of the model
			SCREAMING_SNAKE_CASE       :     int				= int(embed_dim * 2 ** (len(lowerCamelCase_					) - 1)					)
			SCREAMING_SNAKE_CASE       :     Dict				= ['''stem'''] + [f"stage{idx}" for idx in range(1		, len(lowerCamelCase_					) + 1					)]
			SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE       :     int				= get_aligned_output_features_output_indices(
			    out_features=lowerCamelCase_		, out_indices=lowerCamelCase_		, stage_names=self.stage_names					)





