




"""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        __magic_name__				:






				'''simple docstring'''





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





							"""simple docstring"""
							lowerCamelCase      =   parent
							lowerCamelCase      =   batch_size
							lowerCamelCase      =   is_training
							lowerCamelCase      =   use_auxiliary_loss
							lowerCamelCase      =   num_queries
							lowerCamelCase      =   num_channels
							lowerCamelCase      =   min_size
							lowerCamelCase      =   max_size
							lowerCamelCase      =   num_labels
							lowerCamelCase      =   mask_feature_size





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]					).to(
							    _a					)

							lowerCamelCase      =   torch.ones([self.batch_size, self.min_size, self.max_size]						,					device=_a					)

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

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





				def   _lowerCAmelCase							(			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   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase ,  lowerCamelCase ,  lowerCamelCase ,  lowerCamelCase ,  lowerCamelCase      =   self.prepare_config_and_inputs()
							lowerCamelCase      =   {"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask}
							return config, inputs_dict





				def   _lowerCAmelCase							(			self						,					_a						,					_a					):





							"""simple docstring"""
							lowerCamelCase      =   output.encoder_hidden_states
							lowerCamelCase      =   output.pixel_decoder_hidden_states
							lowerCamelCase      =   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_config.decoder_layers					)





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





							"""simple docstring"""
							with torch.no_grad():
										lowerCamelCase      =   MaskFormerModel(config=_a					)
										model.to(_a					)
										model.eval()

										lowerCamelCase      =   model(pixel_values=_a						,					pixel_mask=_a					)
										lowerCamelCase      =   model(_a						,					output_hidden_states=_a					)
							# 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(_a						,					_a					)





				def   _lowerCAmelCase							(			self						,					_a						,					_a						,					_a						,					_a						,					_a					):





							"""simple docstring"""
							lowerCamelCase      =   MaskFormerForInstanceSegmentation(config=_a					)
							model.to(_a					)
							model.eval()

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

										comm_check_on_output(_a					)

										lowerCamelCase      =   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        __magic_name__				(			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					):





							"""simple docstring"""
							lowerCamelCase      =   MaskFormerModelTester(self					)
							lowerCamelCase      =   ConfigTester(self						,					config_class=_a						,					has_text_modality=_a					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							self.config_tester.run_common_tests()





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase ,  lowerCamelCase      =   self.model_tester.prepare_config_and_inputs_for_common()
							self.model_tester.create_and_check_maskformer_model(_a						,					**_a						,					output_hidden_states=_a					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*_a					)





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





							"""simple docstring"""
							pass





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





							"""simple docstring"""
							pass





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





							"""simple docstring"""
							pass





				@unittest.skip(reason="""MaskFormer does not use token embeddings"""					)
				def   _lowerCAmelCase							(			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   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							pass





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





							"""simple docstring"""
							pass





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase ,  lowerCamelCase      =   self.model_tester.prepare_config_and_inputs_for_common()

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

										lowerCamelCase      =   ["""pixel_values"""]
										self.assertListEqual(arg_names[:1]						,					_a					)





				@slow
				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							for model_name in ["facebook/maskformer-swin-small-coco"]:
										lowerCamelCase      =   MaskFormerModel.from_pretrained(_a					)
										self.assertIsNotNone(_a					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   (self.model_tester.min_size,) * 2
							lowerCamelCase      =   {
							    """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      =   MaskFormerForInstanceSegmentation(MaskFormerConfig()					).to(_a					)
							lowerCamelCase      =   model(**_a					)
							self.assertTrue(outputs.loss is not None					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase ,  lowerCamelCase      =   self.model_tester.prepare_config_and_inputs_for_common()
							self.model_tester.create_and_check_maskformer_model(_a						,					**_a						,					output_hidden_states=_a					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase ,  lowerCamelCase      =   self.model_tester.prepare_config_and_inputs_for_common()

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





				def   _lowerCAmelCase							(			self					):





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

							lowerCamelCase      =   model_class(_a					)
							model.to(_a					)
							model.train()

							lowerCamelCase      =   model(_a						,					mask_labels=_a						,					class_labels=_a					).loss
							loss.backward()





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							# only MaskFormerForInstanceSegmentation has the loss
							lowerCamelCase      =   self.all_model_classes[1]
							lowerCamelCase ,  lowerCamelCase ,  lowerCamelCase ,  lowerCamelCase ,  lowerCamelCase      =   self.model_tester.prepare_config_and_inputs()
							lowerCamelCase      =   True
							lowerCamelCase      =   True

							lowerCamelCase      =   model_class(_a					)
							model.to(_a					)
							model.train()

							lowerCamelCase      =   model(_a						,					mask_labels=_a						,					class_labels=_a					)

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

							lowerCamelCase      =   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
							lowerCamelCase      =   outputs.transformer_decoder_hidden_states[0]
							transformer_decoder_hidden_states.retain_grad()

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




lowerCAmelCase				:     Optional[int]					=				1e-4



def        a__					(							)		->   List[Any]:
			lowerCamelCase      =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""							)
			return image






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






				'''simple docstring'''





				@cached_property
				def   _lowerCAmelCase							(			self					):





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





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   MaskFormerModel.from_pretrained("""facebook/maskformer-swin-small-coco"""					).to(_a					)
							lowerCamelCase      =   self.default_image_processor
							lowerCamelCase      =   prepare_img()
							lowerCamelCase      =   image_processor(_a						,					return_tensors="""pt"""					).to(_a					)
							lowerCamelCase      =   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, 800, 1_088)					)

							with torch.no_grad():
										lowerCamelCase      =   model(**_a					)

							lowerCamelCase      =   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(_a					)
							self.assertTrue(
							    torch.allclose(
							        outputs.encoder_last_hidden_state[0, 0, :3, :3]						,					_a						,					atol=_a					)					)

							lowerCamelCase      =   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(_a					)
							self.assertTrue(
							    torch.allclose(
							        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]						,					_a						,					atol=_a					)					)

							lowerCamelCase      =   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(_a					)
							self.assertTrue(
							    torch.allclose(
							        outputs.transformer_decoder_last_hidden_state[0, :3, :3]						,					_a						,					atol=_a					)					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   (
							    MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-swin-small-coco"""					)
							    .to(_a					)
							    .eval()
							)
							lowerCamelCase      =   self.default_image_processor
							lowerCamelCase      =   prepare_img()
							lowerCamelCase      =   image_processor(_a						,					return_tensors="""pt"""					).to(_a					)
							lowerCamelCase      =   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, 800, 1_088)					)

							with torch.no_grad():
										lowerCamelCase      =   model(**_a					)
							# masks_queries_logits
							lowerCamelCase      =   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)						,					)
							lowerCamelCase      =   [
							    [-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],
							]
							lowerCamelCase      =   torch.tensor(_a					).to(_a					)
							self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]						,					_a						,					atol=_a					)					)
							# class_queries_logits
							lowerCamelCase      =   outputs.class_queries_logits
							self.assertEqual(
							    class_queries_logits.shape						,					(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)					)
							lowerCamelCase      =   torch.tensor(
							    [
							        [1.6_5_1_2e0_0, -5.2_5_7_2e0_0, -3.3_5_1_9e0_0],
							        [3.6_1_6_9e-0_2, -5.9_0_2_5e0_0, -2.9_3_1_3e0_0],
							        [1.0_7_6_6e-0_4, -7.7_6_3_0e0_0, -5.1_2_6_3e0_0],
							    ]					).to(_a					)
							self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]						,					_a						,					atol=_a					)					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   (
							    MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-resnet101-coco-stuff"""					)
							    .to(_a					)
							    .eval()
							)
							lowerCamelCase      =   self.default_image_processor
							lowerCamelCase      =   prepare_img()
							lowerCamelCase      =   image_processor(_a						,					return_tensors="""pt"""					).to(_a					)
							lowerCamelCase      =   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, 800, 1_088)					)

							with torch.no_grad():
										lowerCamelCase      =   model(**_a					)
							# masks_queries_logits
							lowerCamelCase      =   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)						,					)
							lowerCamelCase      =   [[-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]]
							lowerCamelCase      =   torch.tensor(_a					).to(_a					)
							self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]						,					_a						,					atol=_a					)					)
							# class_queries_logits
							lowerCamelCase      =   outputs.class_queries_logits
							self.assertEqual(
							    class_queries_logits.shape						,					(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)					)
							lowerCamelCase      =   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(_a					)
							self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]						,					_a						,					atol=_a					)					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   (
							    MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-swin-small-coco"""					)
							    .to(_a					)
							    .eval()
							)
							lowerCamelCase      =   self.default_image_processor

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

							lowerCamelCase      =   inputs["""pixel_values"""].to(_a					)
							lowerCamelCase      =   [el.to(_a					) for el in inputs["""mask_labels"""]]
							lowerCamelCase      =   [el.to(_a					) for el in inputs["""class_labels"""]]

							with torch.no_grad():
										lowerCamelCase      =   model(**_a					)

							self.assertTrue(outputs.loss is not None					)









"""simple docstring"""




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


lowerCAmelCase				:     Optional[int]					=				{"""configuration_swin""": ["""SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SwinConfig""", """SwinOnnxConfig"""]}


try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase				:     int					=				[
					    """SWIN_PRETRAINED_MODEL_ARCHIVE_LIST""",
					    """SwinForImageClassification""",
					    """SwinForMaskedImageModeling""",
					    """SwinModel""",
					    """SwinPreTrainedModel""",
					    """SwinBackbone""",
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase				:     str					=				[
					    """TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST""",
					    """TFSwinForImageClassification""",
					    """TFSwinForMaskedImageModeling""",
					    """TFSwinModel""",
					    """TFSwinPreTrainedModel""",
					]

if TYPE_CHECKING:
					from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_swin import (
										    SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
										    SwinBackbone,
										    SwinForImageClassification,
										    SwinForMaskedImageModeling,
										    SwinModel,
										    SwinPreTrainedModel,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_swin import (
										    TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFSwinForImageClassification,
										    TFSwinForMaskedImageModeling,
										    TFSwinModel,
										    TFSwinPreTrainedModel,
										)

else:
					import sys

					lowerCAmelCase				:     int					=				_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



