






'''simple docstring'''
import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

     if is_vision_available():
          from transformers import MaskFormerImageProcessor

if is_vision_available():
     from PIL import Image






class      __SCREAMING_SNAKE_CASE      :




 def __init__(      self	:							List[Any]					,      __lowercase	:							Dict					,      __lowercase	:							List[str]=2					,      __lowercase	:							List[Any]=True					,      __lowercase	:							Union[str, Any]=False					,      __lowercase	:							int=10					,      __lowercase	:							Tuple=3					,      __lowercase	:							Any=32 * 4					,      __lowercase	:							str=32 * 6					,      __lowercase	:							str=4					,      __lowercase	:							Union[str, Any]=32					,      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=parent
       SCREAMING_SNAKE_CASE__     :				str									=batch_size
       SCREAMING_SNAKE_CASE__     :				str									=is_training
       SCREAMING_SNAKE_CASE__     :				List[Any]									=use_auxiliary_loss
       SCREAMING_SNAKE_CASE__     :				str									=num_queries
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=num_channels
       SCREAMING_SNAKE_CASE__     :				str									=min_size
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=max_size
       SCREAMING_SNAKE_CASE__     :				Any									=num_labels
       SCREAMING_SNAKE_CASE__     :				Dict									=mask_feature_size




 def      __magic_name__	(      self	:							int      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=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__     :				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__     :				Dict									=self.get_config()
       return config, pixel_values, pixel_mask, mask_labels, class_labels




 def      __magic_name__	(      self	:							Tuple      )	->						Tuple:
       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      __magic_name__	(      self	:							Tuple      )	->						int:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE__     :				Tuple									={'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
       return config, inputs_dict




 def      __magic_name__	(      self	:							List[Any]					,      __lowercase	:							int					,      __lowercase	:							Dict      )	->						int:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=output.encoder_hidden_states
       SCREAMING_SNAKE_CASE__     :				List[str]									=output.pixel_decoder_hidden_states
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=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      __magic_name__	(      self	:							Optional[Any]					,      __lowercase	:							Optional[Any]					,      __lowercase	:							Dict					,      __lowercase	:							Any					,      __lowercase	:							List[Any]=False      )	->						Tuple:







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

             SCREAMING_SNAKE_CASE__     :				int									=model(pixel_values=_lowerCAmelCase					,      pixel_mask=_lowerCAmelCase      )
             SCREAMING_SNAKE_CASE__     :				Any									=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      __magic_name__	(      self	:							Optional[Any]					,      __lowercase	:							int					,      __lowercase	:							Any					,      __lowercase	:							int					,      __lowercase	:							Any					,      __lowercase	:							int      )	->						int:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=MaskFormerForInstanceSegmentation(config=_lowerCAmelCase      )
       model.to(_lowerCAmelCase      )
       model.eval()

       def comm_check_on_output(__lowercase	:							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__     :				Any									=model(pixel_values=_lowerCAmelCase					,      pixel_mask=_lowerCAmelCase      )
             SCREAMING_SNAKE_CASE__     :				Dict									=model(_lowerCAmelCase      )

             comm_check_on_output(_lowerCAmelCase      )

             SCREAMING_SNAKE_CASE__     :				Optional[int]									=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      __SCREAMING_SNAKE_CASE      (			_lowerCAmelCase	,				_lowerCAmelCase	,				unittest.TestCase				):
 snake_case_  							=							(MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
 snake_case_  							=							(
     {'feature-extraction': MaskFormerModel, 'image-segmentation': MaskFormerForInstanceSegmentation}
     if is_torch_available()
     else {}
 )

 snake_case_  							=							False
 snake_case_  							=							False
 snake_case_  							=							False
 snake_case_  							=							False




 def      __magic_name__	(      self	:							Dict      )	->						Dict:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=MaskFormerModelTester(self      )
       SCREAMING_SNAKE_CASE__     :				List[Any]									=ConfigTester(self					,      config_class=_lowerCAmelCase					,      has_text_modality=_lowerCAmelCase      )




 def      __magic_name__	(      self	:							Tuple      )	->						Optional[Any]:
       self.config_tester.run_common_tests()




 def      __magic_name__	(      self	:							str      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				int									=self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.create_and_check_maskformer_model(_lowerCAmelCase					,      **_lowerCAmelCase					,      output_hidden_states=_lowerCAmelCase      )




 def      __magic_name__	(      self	:							List[Any]      )	->						Union[str, Any]:
       SCREAMING_SNAKE_CASE__     :				Dict									=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      __magic_name__	(      self	:							List[str]      )	->						Union[str, Any]:
       pass




 @unittest.skip(reason='''MaskFormer does not have a get_input_embeddings method'''      )
 def      __magic_name__	(      self	:							List[str]      )	->						Dict:
       pass




 @unittest.skip(reason='''MaskFormer is not a generative model'''      )
 def      __magic_name__	(      self	:							Optional[int]      )	->						Optional[int]:
       pass




 @unittest.skip(reason='''MaskFormer does not use token embeddings'''      )
 def      __magic_name__	(      self	:							Union[str, Any]      )	->						List[str]:
       pass




 @require_torch_multi_gpu
 @unittest.skip(
     reason='''MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''      )
 def      __magic_name__	(      self	:							Any      )	->						List[Any]:
       pass




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




 def      __magic_name__	(      self	:							int      )	->						str:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				Any									=self.model_tester.prepare_config_and_inputs_for_common()

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

             SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=['''pixel_values''']
             self.assertListEqual(arg_names[:1]					,      _lowerCAmelCase      )




 @slow
 def      __magic_name__	(      self	:							Any      )	->						Any:







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




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




 def      __magic_name__	(      self	:							Optional[Any]      )	->						str:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				int									=self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.create_and_check_maskformer_model(_lowerCAmelCase					,      **_lowerCAmelCase					,      output_hidden_states=_lowerCAmelCase      )




 def      __magic_name__	(      self	:							Union[str, Any]      )	->						Any:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				List[Any]									=self.model_tester.prepare_config_and_inputs_for_common()

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




 def      __magic_name__	(      self	:							Dict      )	->						Dict:







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

       SCREAMING_SNAKE_CASE__     :				Any									=model_class(_lowerCAmelCase      )
       model.to(_lowerCAmelCase      )
       model.train()

       SCREAMING_SNAKE_CASE__     :				str									=model(_lowerCAmelCase					,      mask_labels=_lowerCAmelCase					,      class_labels=_lowerCAmelCase      ).loss
       loss.backward()




 def      __magic_name__	(      self	:							Union[str, Any]      )	->						Union[str, Any]:
       SCREAMING_SNAKE_CASE__     :				int									=self.all_model_classes[1]
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				Any									=self.model_tester.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE__     :				Any									=True
       SCREAMING_SNAKE_CASE__     :				str									=True

       SCREAMING_SNAKE_CASE__     :				str									=model_class(_lowerCAmelCase      )
       model.to(_lowerCAmelCase      )
       model.train()

       SCREAMING_SNAKE_CASE__     :				Any									=model(_lowerCAmelCase					,      mask_labels=_lowerCAmelCase					,      class_labels=_lowerCAmelCase      )

       SCREAMING_SNAKE_CASE__     :				List[str]									=outputs.encoder_hidden_states[0]
       encoder_hidden_states.retain_grad()

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

       SCREAMING_SNAKE_CASE__     :				Any									=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      )


a_					= 1E-4





def  _a(  ):




      '''simple docstring'''






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







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




 @cached_property
 def      __magic_name__	(      self	:							Dict      )	->						int:
       return (
           MaskFormerImageProcessor.from_pretrained('''facebook/maskformer-swin-small-coco'''      )
           if is_vision_available()
           else None
       )




 def      __magic_name__	(      self	:							str      )	->						str:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=MaskFormerModel.from_pretrained('''facebook/maskformer-swin-small-coco'''      ).to(_lowerCAmelCase      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=self.default_image_processor
       SCREAMING_SNAKE_CASE__     :				int									=prepare_img()
       SCREAMING_SNAKE_CASE__     :				Dict									=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__     :				Optional[Any]									=model(**_lowerCAmelCase      )

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=torch.tensor(
           [[-0.0482, 0.9228, 0.4951], [-0.2547, 0.8017, 0.8527], [-0.0069, 0.3385, -0.0089]]      ).to(_lowerCAmelCase      )
       self.assertTrue(
           torch.allclose(
               outputs.encoder_last_hidden_state[0, 0, :3, :3]					,      _lowerCAmelCase					,      atol=_lowerCAmelCase      )      )

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

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




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

       with torch.no_grad():
             SCREAMING_SNAKE_CASE__     :				List[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[Any]									=[
           [-1.3737124, -1.7724937, -1.9364233],
           [-1.5977281, -1.9867939, -2.1523695],
           [-1.5795398, -1.9269832, -2.093942],
       ]
       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__     :				List[Any]									=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__     :				Optional[int]									=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      __magic_name__	(      self	:							Union[str, Any]      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				List[str]									=(
           MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-resnet101-coco-stuff'''      )
           .to(_lowerCAmelCase      )
           .eval()
       )
       SCREAMING_SNAKE_CASE__     :				Any									=self.default_image_processor
       SCREAMING_SNAKE_CASE__     :				List[Any]									=prepare_img()
       SCREAMING_SNAKE_CASE__     :				Dict									=image_processor(_lowerCAmelCase					,      return_tensors='''pt'''      ).to(_lowerCAmelCase      )
       SCREAMING_SNAKE_CASE__     :				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(_lowerCAmelCase					,      (1, 3, 8_00, 10_88)      )

       with torch.no_grad():
             SCREAMING_SNAKE_CASE__     :				List[str]									=model(**_lowerCAmelCase      )
       # masks_queries_logits
       SCREAMING_SNAKE_CASE__     :				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)					,      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=[[-0.9046, -2.6366, -4.6062], [-3.4179, -5.7890, -8.8057], [-4.9179, -7.6560, -10.7711]]
       SCREAMING_SNAKE_CASE__     :				List[str]									=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__     :				Dict									=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__     :				str									=torch.tensor(
           [[4.7188, -3.2585, -2.8857], [6.6871, -2.9181, -1.2487], [7.2449, -2.2764, -2.1874]]      ).to(_lowerCAmelCase      )
       self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]					,      _lowerCAmelCase					,      atol=_lowerCAmelCase      )      )




 def      __magic_name__	(      self	:							List[Any]      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=(
           MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''      )
           .to(_lowerCAmelCase      )
           .eval()
       )
       SCREAMING_SNAKE_CASE__     :				Dict									=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__     :				Dict									=[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__     :				List[Any]									=model(**_lowerCAmelCase      )

       self.assertTrue(outputs.loss is not None      )







'''simple docstring'''
import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
  from PIL import Image
else:






  class      __SCREAMING_SNAKE_CASE      :




   @staticmethod
   def      __magic_name__	(      *__lowercase	:							int					,      **__lowercase	:							Optional[Any]      )	->						Optional[Any]:
         pass






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




 @require_torch
 def      __magic_name__	(      self	:							Optional[int]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''					,      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Any									=image_classifier(__lowercase					,      candidate_labels=['''a''', '''b''', '''c''']      )

       # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
       # python and torch versions.
       self.assertIn(
           nested_simplify(__lowercase      )					,      [
               [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}],
               [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''c'''}, {'''score''': 0.333, '''label''': '''b'''}],
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				int									=image_classifier([image] * 5					,      candidate_labels=['''A''', '''B''', '''C''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
           ]					,      )




 @require_tf
 def      __magic_name__	(      self	:							Union[str, Any]      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''					,      framework='''tf'''      )
       SCREAMING_SNAKE_CASE__     :				Dict									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Tuple									=image_classifier(__lowercase					,      candidate_labels=['''a''', '''b''', '''c''']      )

       self.assertEqual(
           nested_simplify(__lowercase      )					,      [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}]					,      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=image_classifier([image] * 5					,      candidate_labels=['''A''', '''B''', '''C''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
           ]					,      )




 @slow
 @require_torch
 def      __magic_name__	(      self	:							List[Any]      )	->						Dict:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=pipeline(
           task='''zero-shot-image-classification'''					,      model='''openai/clip-vit-base-patch32'''					,      )
       # This is an image of 2 cats with remotes and no planes
       SCREAMING_SNAKE_CASE__     :				Any									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=image_classifier(__lowercase					,      candidate_labels=['''cat''', '''plane''', '''remote''']      )

       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               {'''score''': 0.511, '''label''': '''remote'''},
               {'''score''': 0.485, '''label''': '''cat'''},
               {'''score''': 0.004, '''label''': '''plane'''},
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=image_classifier([image] * 5					,      candidate_labels=['''cat''', '''plane''', '''remote''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.511, '''label''': '''remote'''},
                   {'''score''': 0.485, '''label''': '''cat'''},
                   {'''score''': 0.004, '''label''': '''plane'''},
               ],
           ]
           * 5					,      )




 @slow
 @require_tf
 def      __magic_name__	(      self	:							Union[str, Any]      )	->						int:
       SCREAMING_SNAKE_CASE__     :				str									=pipeline(
           task='''zero-shot-image-classification'''					,      model='''openai/clip-vit-base-patch32'''					,      framework='''tf'''      )
       # This is an image of 2 cats with remotes and no planes
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Any									=image_classifier(__lowercase					,      candidate_labels=['''cat''', '''plane''', '''remote''']      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               {'''score''': 0.511, '''label''': '''remote'''},
               {'''score''': 0.485, '''label''': '''cat'''},
               {'''score''': 0.004, '''label''': '''plane'''},
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=image_classifier([image] * 5					,      candidate_labels=['''cat''', '''plane''', '''remote''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.511, '''label''': '''remote'''},
                   {'''score''': 0.485, '''label''': '''cat'''},
                   {'''score''': 0.004, '''label''': '''plane'''},
               ],
           ]
           * 5					,      )