





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




      """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				:  Tuple						=		parent
             _lowerCamelCase				:  Optional[Any]						=		batch_size
             _lowerCamelCase				:  Any						=		is_training
             _lowerCamelCase				:  Tuple						=		use_auxiliary_loss
             _lowerCamelCase				:  Optional[Any]						=		num_queries
             _lowerCamelCase				:  int						=		num_channels
             _lowerCamelCase				:  List[str]						=		min_size
             _lowerCamelCase				:  Optional[int]						=		max_size
             _lowerCamelCase				:  Union[str, Any]						=		num_labels
             _lowerCamelCase				:  Tuple						=		mask_feature_size


      def 					__snake_case      (		self):



             """simple docstring"""





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

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

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

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


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



             """simple docstring"""





             _lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase				:  List[Any]						=		self.prepare_config_and_inputs()
             _lowerCamelCase				:  Dict						=		{'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
             return config, inputs_dict


      def 					__snake_case      (		self   ,				a__   ,				a__):



             """simple docstring"""





             _lowerCamelCase				:  str						=		output.encoder_hidden_states
             _lowerCamelCase				:  int						=		output.pixel_decoder_hidden_states
             _lowerCamelCase				:  str						=		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 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__=False):



             """simple docstring"""





             with torch.no_grad():
                    _lowerCamelCase				:  Union[str, Any]						=		MaskFormerModel(config=a__)
                    model.to(a__)
                    model.eval()

                    _lowerCamelCase				:  Optional[Any]						=		model(pixel_values=a__   ,				pixel_mask=a__)
                    _lowerCamelCase				:  Optional[Any]						=		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 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__):



             """simple docstring"""





             _lowerCamelCase				:  str						=		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				:  str						=		model(pixel_values=a__   ,				pixel_mask=a__)
                    _lowerCamelCase				:  Optional[Any]						=		model(a__)

                    comm_check_on_output(a__)

                    _lowerCamelCase				:  int						=		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       __A							(   lowerCamelCase__			,lowerCamelCase__			,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 					__snake_case      (		self):



             """simple docstring"""





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


      def 					__snake_case      (		self):



             """simple docstring"""





             self.config_tester.run_common_tests()


      def 					__snake_case      (		self):



             """simple docstring"""





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


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		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 					__snake_case      (		self):



             """simple docstring"""





             pass


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



             """simple docstring"""





             pass


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



             """simple docstring"""





             pass


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



             """simple docstring"""





             pass


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



             """simple docstring"""





             pass


      def 					__snake_case      (		self):



             """simple docstring"""





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

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

                    _lowerCamelCase				:  Dict						=		['''pixel_values''']
                    self.assertListEqual(arg_names[:1]   ,				a__)


      @slow
      def 					__snake_case      (		self):



             """simple docstring"""





             for model_name in ["facebook/maskformer-swin-small-coco"]:
                    _lowerCamelCase				:  Optional[Any]						=		MaskFormerModel.from_pretrained(a__)
                    self.assertIsNotNone(a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		(self.model_tester.min_size,) * 2
             _lowerCamelCase				:  Union[str, Any]						=		{
                 '''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				:  Optional[Any]						=		MaskFormerForInstanceSegmentation(MaskFormerConfig()).to(a__)
             _lowerCamelCase				:  Tuple						=		model(**a__)
             self.assertTrue(outputs.loss is not None)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase,	_lowerCamelCase				:  Dict						=		self.model_tester.prepare_config_and_inputs_for_common()
             self.model_tester.create_and_check_maskformer_model(a__   ,				**a__   ,				output_hidden_states=a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase,	_lowerCamelCase				:  Optional[Any]						=		self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    _lowerCamelCase				:  str						=		model_class(a__).to(a__)
                    _lowerCamelCase				:  Optional[Any]						=		model(**a__   ,				output_attentions=a__)
                    self.assertTrue(outputs.attentions is not None)


      def 					__snake_case      (		self):



             """simple docstring"""





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

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

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



      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		self.all_model_classes[1]
             _lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase				:  str						=		self.model_tester.prepare_config_and_inputs()
             _lowerCamelCase				:  List[Any]						=		True
             _lowerCamelCase				:  List[str]						=		True

             _lowerCamelCase				:  List[str]						=		model_class(a__)
             model.to(a__)
             model.train()

             _lowerCamelCase				:  Union[str, Any]						=		model(a__   ,				mask_labels=a__   ,				class_labels=a__)

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

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

             _lowerCamelCase				:  List[Any]						=		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      							=   1E-4




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







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




      """simple docstring"""







      @cached_property
      def 					__snake_case      (		self):



             """simple docstring"""





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


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Union[str, Any]						=		MaskFormerModel.from_pretrained('''facebook/maskformer-swin-small-coco''').to(a__)
             _lowerCamelCase				:  Optional[Any]						=		self.default_image_processor
             _lowerCamelCase				:  Optional[int]						=		prepare_img()
             _lowerCamelCase				:  Tuple						=		image_processor(a__   ,				return_tensors='''pt''').to(a__)
             _lowerCamelCase				:  str						=		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, 1088))

             with torch.no_grad():
                    _lowerCamelCase				:  List[str]						=		model(**a__)

             _lowerCamelCase				:  int						=		torch.tensor(
                 [[-0.0482, 0.9228, 0.4951], [-0.2547, 0.8017, 0.8527], [-0.0069, 0.3385, -0.0089]]).to(a__)
             self.assertTrue(
                 torch.allclose(
                     outputs.encoder_last_hidden_state[0, 0, :3, :3]   ,				a__   ,				atol=a__))

             _lowerCamelCase				:  List[Any]						=		torch.tensor(
                 [[-0.8422, -0.8434, -0.9718], [-1.0144, -0.5565, -0.4195], [-1.0038, -0.4484, -0.1961]]).to(a__)
             self.assertTrue(
                 torch.allclose(
                     outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]   ,				a__   ,				atol=a__))

             _lowerCamelCase				:  Any						=		torch.tensor(
                 [[0.2852, -0.0159, 0.9735], [0.6254, 0.1858, 0.8529], [-0.0680, -0.4116, 1.8413]]).to(a__)
             self.assertTrue(
                 torch.allclose(
                     outputs.transformer_decoder_last_hidden_state[0, :3, :3]   ,				a__   ,				atol=a__))


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Optional[Any]						=		(
                 MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco''')
                 .to(a__)
                 .eval()
             )
             _lowerCamelCase				:  Tuple						=		self.default_image_processor
             _lowerCamelCase				:  Optional[int]						=		prepare_img()
             _lowerCamelCase				:  List[Any]						=		image_processor(a__   ,				return_tensors='''pt''').to(a__)
             _lowerCamelCase				:  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(a__   ,				(1, 3, 800, 1088))

             with torch.no_grad():
                    _lowerCamelCase				:  List[str]						=		model(**a__)
             # masks_queries_logits
             _lowerCamelCase				:  Dict						=		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				:  Dict						=		[
                 [-1.373_7124, -1.772_4937, -1.936_4233],
                 [-1.597_7281, -1.986_7939, -2.152_3695],
                 [-1.579_5398, -1.926_9832, -2.09_3942],
             ]
             _lowerCamelCase				:  Union[str, Any]						=		torch.tensor(a__).to(a__)
             self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]   ,				a__   ,				atol=a__))
             # class_queries_logits
             _lowerCamelCase				:  Tuple						=		outputs.class_queries_logits
             self.assertEqual(
                 class_queries_logits.shape   ,				(1, model.config.decoder_config.num_queries, model.config.num_labels + 1))
             _lowerCamelCase				:  Union[str, Any]						=		torch.tensor(
                 [
                     [1.6512e00, -5.2572e00, -3.3519e00],
                     [3.6169e-02, -5.9025e00, -2.9313e00],
                     [1.0766e-04, -7.7630e00, -5.1263e00],
                 ]).to(a__)
             self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]   ,				a__   ,				atol=a__))


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  str						=		(
                 MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-resnet101-coco-stuff''')
                 .to(a__)
                 .eval()
             )
             _lowerCamelCase				:  Optional[int]						=		self.default_image_processor
             _lowerCamelCase				:  int						=		prepare_img()
             _lowerCamelCase				:  Union[str, Any]						=		image_processor(a__   ,				return_tensors='''pt''').to(a__)
             _lowerCamelCase				:  Tuple						=		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, 1088))

             with torch.no_grad():
                    _lowerCamelCase				:  int						=		model(**a__)
             # masks_queries_logits
             _lowerCamelCase				:  Union[str, Any]						=		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				:  Tuple						=		[[-0.9046, -2.6366, -4.6062], [-3.4179, -5.7890, -8.8057], [-4.9179, -7.6560, -10.7711]]
             _lowerCamelCase				:  int						=		torch.tensor(a__).to(a__)
             self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]   ,				a__   ,				atol=a__))
             # class_queries_logits
             _lowerCamelCase				:  Optional[int]						=		outputs.class_queries_logits
             self.assertEqual(
                 class_queries_logits.shape   ,				(1, model.config.decoder_config.num_queries, model.config.num_labels + 1))
             _lowerCamelCase				:  Any						=		torch.tensor(
                 [[4.7188, -3.2585, -2.8857], [6.6871, -2.9181, -1.2487], [7.2449, -2.2764, -2.1874]]).to(a__)
             self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]   ,				a__   ,				atol=a__))



      def 					__snake_case      (		self):



             """simple docstring"""





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

             _lowerCamelCase				:  int						=		image_processor(
                 [np.zeros((3, 800, 1333)), np.zeros((3, 800, 1333))]   ,				segmentation_maps=[np.zeros((384, 384)).astype(np.floataa), np.zeros((384, 384)).astype(np.floataa)]   ,				return_tensors='''pt'''   ,				)

             _lowerCamelCase				:  List[str]						=		inputs['''pixel_values'''].to(a__)
             _lowerCamelCase				:  Tuple						=		[el.to(a__) for el in inputs['''mask_labels''']]
             _lowerCamelCase				:  Tuple						=		[el.to(a__) for el in inputs['''class_labels''']]

             with torch.no_grad():
                    _lowerCamelCase				:  int						=		model(**a__)

             self.assertTrue(outputs.loss is not None)








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_lowerCamelCase      							=   {
    'configuration_clipseg': [
        'CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'CLIPSegConfig',
        'CLIPSegTextConfig',
        'CLIPSegVisionConfig',
    ],
    'processing_clipseg': ['CLIPSegProcessor'],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       _lowerCamelCase      							=   [
           'CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST',
           'CLIPSegModel',
           'CLIPSegPreTrainedModel',
           'CLIPSegTextModel',
           'CLIPSegVisionModel',
           'CLIPSegForImageSegmentation',
       ]

if TYPE_CHECKING:
       from .configuration_clipseg import (
           CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP,
           CLIPSegConfig,
           CLIPSegTextConfig,
           CLIPSegVisionConfig,
       )
       from .processing_clipseg import CLIPSegProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_clipseg import (
                  CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST,
                  CLIPSegForImageSegmentation,
                  CLIPSegModel,
                  CLIPSegPreTrainedModel,
                  CLIPSegTextModel,
                  CLIPSegVisionModel,
              )

else:
       import sys

       _lowerCamelCase      							=   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

