






import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
       from PIL import Image

       from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
       import torch

if is_tf_available():
       import tensorflow as tf
@require_vision
@require_torchvision
class     a    ( unittest.TestCase   ):



      """simple docstring"""


      def 	UpperCAmelCase      (      self					:			Any					)							->      int:
        __UpperCAmelCase							:	List[str]           =  tempfile.mkdtemp()
        __UpperCAmelCase							:	Union[str, Any]           =  SamImageProcessor()
        __UpperCAmelCase							:	Tuple           =  SamProcessor(__lowercase					)
        processor.save_pretrained(self.tmpdirname					)
      def 	UpperCAmelCase      (      self					:			str		,      **__lowercase					:			List[str]					)							->      Any:
        return AutoProcessor.from_pretrained(self.tmpdirname		,      **__lowercase					).image_processor
      def 	UpperCAmelCase      (      self					:			str					)							->      Dict:
        shutil.rmtree(self.tmpdirname					)
      def 	UpperCAmelCase      (      self					:			List[str]					)							->      List[str]:
        __UpperCAmelCase							:	Tuple           =  [np.random.randint(255		,      size=(3, 30, 400)		,      dtype=np.uinta					)]

        __UpperCAmelCase							:	Dict           =  [Image.fromarray(np.moveaxis(__lowercase		,      0		,      -1					)					) for x in image_inputs]

        return image_inputs
      def 	UpperCAmelCase      (      self					:			Optional[int]					)							->      Optional[Any]:
        __UpperCAmelCase							:	Optional[int]           =  SamProcessor(image_processor=self.get_image_processor()					)
        processor.save_pretrained(self.tmpdirname					)

        __UpperCAmelCase							:	Optional[Any]           =  self.get_image_processor(do_normalize=__lowercase		,      padding_value=1.0					)

        __UpperCAmelCase							:	Union[str, Any]           =  SamProcessor.from_pretrained(self.tmpdirname		,      do_normalize=__lowercase		,      padding_value=1.0					)

        self.assertEqual(processor.image_processor.to_json_string()		,      image_processor_add_kwargs.to_json_string()					)
        self.assertIsInstance(processor.image_processor		,      __lowercase					)
      def 	UpperCAmelCase      (      self					:			Optional[Any]					)							->      str:
        __UpperCAmelCase							:	str           =  self.get_image_processor()

        __UpperCAmelCase							:	Optional[int]           =  SamProcessor(image_processor=__lowercase					)

        __UpperCAmelCase							:	Optional[Any]           =  self.prepare_image_inputs()

        __UpperCAmelCase							:	Tuple           =  image_processor(__lowercase		,      return_tensors="""np"""					)
        __UpperCAmelCase							:	str           =  processor(images=__lowercase		,      return_tensors="""np"""					)

        input_feat_extract.pop("""original_sizes"""					)  # pop original_sizes as it is popped in the processor
        input_feat_extract.pop("""reshaped_input_sizes"""					)  # pop original_sizes as it is popped in the processor

        for key in input_feat_extract.keys():
          self.assertAlmostEqual(input_feat_extract[key].sum()		,      input_processor[key].sum()		,      delta=1e-2					)




      @require_torch
      def 	UpperCAmelCase      (      self					:			Union[str, Any]					)							->      List[Any]:
        __UpperCAmelCase							:	Union[str, Any]           =  self.get_image_processor()

        __UpperCAmelCase							:	List[Any]           =  SamProcessor(image_processor=__lowercase					)
        __UpperCAmelCase							:	List[str]           =  [torch.ones((1, 3, 5, 5)					)]

        __UpperCAmelCase							:	List[Any]           =  [[1764, 2646]]

        __UpperCAmelCase							:	Dict           =  [[683, 1024]]
        __UpperCAmelCase							:	Tuple           =  processor.post_process_masks(__lowercase		,      __lowercase		,      __lowercase					)
        self.assertEqual(masks[0].shape		,      (1, 3, 1764, 2646)					)

        __UpperCAmelCase							:	int           =  processor.post_process_masks(
            __lowercase		,      torch.tensor(__lowercase					)		,      torch.tensor(__lowercase					)					)
        self.assertEqual(masks[0].shape		,      (1, 3, 1764, 2646)					)

        # should also work with np
        __UpperCAmelCase							:	Any           =  [np.ones((1, 3, 5, 5)					)]
        __UpperCAmelCase							:	Optional[int]           =  processor.post_process_masks(__lowercase		,      np.array(__lowercase					)		,      np.array(__lowercase					)					)

        self.assertEqual(masks[0].shape		,      (1, 3, 1764, 2646)					)

        __UpperCAmelCase							:	Optional[int]           =  [[1, 0], [0, 1]]
        with self.assertRaises(__lowercase					):
          __UpperCAmelCase							:	List[str]           =  processor.post_process_masks(__lowercase		,      np.array(__lowercase					)		,      np.array(__lowercase					)					)


@require_vision
@require_tf
class     a    ( unittest.TestCase   ):



      """simple docstring"""


      def 	UpperCAmelCase      (      self					:			str					)							->      str:
        __UpperCAmelCase							:	int           =  tempfile.mkdtemp()
        __UpperCAmelCase							:	Union[str, Any]           =  SamImageProcessor()
        __UpperCAmelCase							:	Dict           =  SamProcessor(__lowercase					)
        processor.save_pretrained(self.tmpdirname					)
      def 	UpperCAmelCase      (      self					:			Union[str, Any]		,      **__lowercase					:			Optional[int]					)							->      Any:
        return AutoProcessor.from_pretrained(self.tmpdirname		,      **__lowercase					).image_processor
      def 	UpperCAmelCase      (      self					:			List[str]					)							->      List[str]:
        shutil.rmtree(self.tmpdirname					)
      def 	UpperCAmelCase      (      self					:			Optional[int]					)							->      str:
        __UpperCAmelCase							:	Optional[int]           =  [np.random.randint(255		,      size=(3, 30, 400)		,      dtype=np.uinta					)]

        __UpperCAmelCase							:	Any           =  [Image.fromarray(np.moveaxis(__lowercase		,      0		,      -1					)					) for x in image_inputs]

        return image_inputs
      def 	UpperCAmelCase      (      self					:			int					)							->      str:
        __UpperCAmelCase							:	List[Any]           =  SamProcessor(image_processor=self.get_image_processor()					)
        processor.save_pretrained(self.tmpdirname					)

        __UpperCAmelCase							:	Optional[int]           =  self.get_image_processor(do_normalize=__lowercase		,      padding_value=1.0					)

        __UpperCAmelCase							:	Union[str, Any]           =  SamProcessor.from_pretrained(self.tmpdirname		,      do_normalize=__lowercase		,      padding_value=1.0					)

        self.assertEqual(processor.image_processor.to_json_string()		,      image_processor_add_kwargs.to_json_string()					)
        self.assertIsInstance(processor.image_processor		,      __lowercase					)
      def 	UpperCAmelCase      (      self					:			Any					)							->      Optional[Any]:
        __UpperCAmelCase							:	Union[str, Any]           =  self.get_image_processor()

        __UpperCAmelCase							:	Dict           =  SamProcessor(image_processor=__lowercase					)

        __UpperCAmelCase							:	int           =  self.prepare_image_inputs()

        __UpperCAmelCase							:	Optional[int]           =  image_processor(__lowercase		,      return_tensors="""np"""					)
        __UpperCAmelCase							:	Union[str, Any]           =  processor(images=__lowercase		,      return_tensors="""np"""					)

        input_feat_extract.pop("""original_sizes"""					)  # pop original_sizes as it is popped in the processor
        input_feat_extract.pop("""reshaped_input_sizes"""					)  # pop reshaped_input_sizes as it is popped in the processor

        for key in input_feat_extract.keys():
          self.assertAlmostEqual(input_feat_extract[key].sum()		,      input_processor[key].sum()		,      delta=1e-2					)




      @require_tf
      def 	UpperCAmelCase      (      self					:			List[str]					)							->      List[Any]:
        __UpperCAmelCase							:	Optional[int]           =  self.get_image_processor()

        __UpperCAmelCase							:	List[str]           =  SamProcessor(image_processor=__lowercase					)
        __UpperCAmelCase							:	Union[str, Any]           =  [tf.ones((1, 3, 5, 5)					)]

        __UpperCAmelCase							:	List[str]           =  [[1764, 2646]]

        __UpperCAmelCase							:	Union[str, Any]           =  [[683, 1024]]
        __UpperCAmelCase							:	Union[str, Any]           =  processor.post_process_masks(__lowercase		,      __lowercase		,      __lowercase		,      return_tensors="""tf"""					)
        self.assertEqual(masks[0].shape		,      (1, 3, 1764, 2646)					)

        __UpperCAmelCase							:	Optional[Any]           =  processor.post_process_masks(
            __lowercase		,      tf.convert_to_tensor(__lowercase					)		,      tf.convert_to_tensor(__lowercase					)		,      return_tensors="""tf"""		,      )
        self.assertEqual(masks[0].shape		,      (1, 3, 1764, 2646)					)

        # should also work with np
        __UpperCAmelCase							:	Optional[Any]           =  [np.ones((1, 3, 5, 5)					)]
        __UpperCAmelCase							:	int           =  processor.post_process_masks(
            __lowercase		,      np.array(__lowercase					)		,      np.array(__lowercase					)		,      return_tensors="""tf"""					)

        self.assertEqual(masks[0].shape		,      (1, 3, 1764, 2646)					)

        __UpperCAmelCase							:	Dict           =  [[1, 0], [0, 1]]
        with self.assertRaises(tf.errors.InvalidArgumentError					):
          __UpperCAmelCase							:	int           =  processor.post_process_masks(
              __lowercase		,      np.array(__lowercase					)		,      np.array(__lowercase					)		,      return_tensors="""tf"""					)


@require_vision
@require_torchvision
class     a    ( unittest.TestCase   ):



      """simple docstring"""


      def 	UpperCAmelCase      (      self					:			Optional[int]					)							->      List[str]:
        __UpperCAmelCase							:	Tuple           =  tempfile.mkdtemp()
        __UpperCAmelCase							:	Optional[Any]           =  SamImageProcessor()
        __UpperCAmelCase							:	str           =  SamProcessor(__lowercase					)
        processor.save_pretrained(self.tmpdirname					)
      def 	UpperCAmelCase      (      self					:			Optional[int]		,      **__lowercase					:			Optional[int]					)							->      List[str]:
        return AutoProcessor.from_pretrained(self.tmpdirname		,      **__lowercase					).image_processor
      def 	UpperCAmelCase      (      self					:			Any					)							->      List[Any]:
        shutil.rmtree(self.tmpdirname					)
      def 	UpperCAmelCase      (      self					:			List[str]					)							->      List[Any]:
        __UpperCAmelCase							:	Dict           =  [np.random.randint(255		,      size=(3, 30, 400)		,      dtype=np.uinta					)]

        __UpperCAmelCase							:	str           =  [Image.fromarray(np.moveaxis(__lowercase		,      0		,      -1					)					) for x in image_inputs]

        return image_inputs
      @is_pt_tf_cross_test
      def 	UpperCAmelCase      (      self					:			Dict					)							->      Optional[int]:
        __UpperCAmelCase							:	Union[str, Any]           =  self.get_image_processor()

        __UpperCAmelCase							:	List[Any]           =  SamProcessor(image_processor=__lowercase					)
        __UpperCAmelCase							:	int           =  np.random.randint(0		,      2		,      size=(1, 3, 5, 5)					).astype(np.floataa					)
        __UpperCAmelCase							:	Optional[int]           =  [tf.convert_to_tensor(__lowercase					)]
        __UpperCAmelCase							:	Tuple           =  [torch.tensor(__lowercase					)]

        __UpperCAmelCase							:	int           =  [[1764, 2646]]

        __UpperCAmelCase							:	List[Any]           =  [[683, 1024]]
        __UpperCAmelCase							:	str           =  processor.post_process_masks(
            __lowercase		,      __lowercase		,      __lowercase		,      return_tensors="""tf"""					)
        __UpperCAmelCase							:	Any           =  processor.post_process_masks(
            __lowercase		,      __lowercase		,      __lowercase		,      return_tensors="""pt"""					)

        self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()					)					)




      @is_pt_tf_cross_test
      def 	UpperCAmelCase      (      self					:			Optional[int]					)							->      Dict:
        __UpperCAmelCase							:	Tuple           =  self.get_image_processor()

        __UpperCAmelCase							:	Dict           =  SamProcessor(image_processor=__lowercase					)

        __UpperCAmelCase							:	Union[str, Any]           =  self.prepare_image_inputs()

        __UpperCAmelCase							:	List[Any]           =  image_processor(__lowercase		,      return_tensors="""pt"""					)["""pixel_values"""].numpy()
        __UpperCAmelCase							:	Any           =  processor(images=__lowercase		,      return_tensors="""pt"""					)["""pixel_values"""].numpy()

        __UpperCAmelCase							:	List[Any]           =  image_processor(__lowercase		,      return_tensors="""tf"""					)["""pixel_values"""].numpy()
        __UpperCAmelCase							:	List[Any]           =  processor(images=__lowercase		,      return_tensors="""tf"""					)["""pixel_values"""].numpy()

        self.assertTrue(np.allclose(__lowercase		,      __lowercase					)					)
        self.assertTrue(np.allclose(__lowercase		,      __lowercase					)					)
        self.assertTrue(np.allclose(__lowercase		,      __lowercase					)					)










import copy
from typing import Dict, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING
from ..detr import DetrConfig
from ..swin import SwinConfig


a		:       Optional[Any]			     = {
    "facebook/maskformer-swin-base-ade": (
        "https://huggingface.co/facebook/maskformer-swin-base-ade/blob/main/config.json"
    )
    # See all MaskFormer models at https://huggingface.co/models?filter=maskformer
}

a		:       Optional[Any]			     = logging.get_logger(__name__)
class     a    ( lowercase__   ):



      """simple docstring"""


      a  : str							       =	'maskformer'
      a  : Dict							       =	{'hidden_size': 'mask_feature_size'}
      a  : Optional[Any]							       =	['resnet', 'swin']
      a  : List[Any]							       =	['detr']
      def __init__(      self					:			Optional[int]		,      __lowercase					:			int = 256		,      __lowercase					:			int = 256		,      __lowercase					:			float = 0.1		,      __lowercase					:			bool = False		,      __lowercase					:			Optional[Dict] = None		,      __lowercase					:			Optional[Dict] = None		,      __lowercase					:			float = 0.02		,      __lowercase					:			float = 1.0		,      __lowercase					:			float = 1.0		,      __lowercase					:			float = 1.0		,      __lowercase					:			float = 20.0		,      __lowercase					:			Optional[bool] = None		,      **__lowercase					:			Tuple		,      )							->      str:
        if backbone_config is None:
          # fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k
          __UpperCAmelCase							:	List[str]           =  SwinConfig(
              image_size=384		,      in_channels=3		,      patch_size=4		,      embed_dim=128		,      depths=[2, 2, 18, 2]		,      num_heads=[4, 8, 16, 32]		,      window_size=12		,      drop_path_rate=0.3		,      out_features=["""stage1""", """stage2""", """stage3""", """stage4"""]		,      )

        if isinstance(__lowercase		,      __lowercase					):
          __UpperCAmelCase							:	Dict           =  backbone_config.pop("""model_type"""					)
          __UpperCAmelCase							:	Tuple           =  CONFIG_MAPPING[backbone_model_type]
          __UpperCAmelCase							:	List[str]           =  config_class.from_dict(__lowercase					)

        # verify that the backbone is supported
        if backbone_config.model_type not in self.backbones_supported:
          logger.warning_once(
              f"""Backbone {backbone_config.model_type} is not a supported model and may not be compatible with MaskFormer. """
              f"""Supported model types: {",".join(self.backbones_supported					)}"""					)

        if decoder_config is None:
          # fall back to https://huggingface.co/facebook/detr-resnet-50
          __UpperCAmelCase							:	List[Any]           =  DetrConfig()
        else:
          # verify that the decoder is supported
          __UpperCAmelCase							:	List[Any]           =  (
              decoder_config.pop("""model_type"""					) if isinstance(__lowercase		,      __lowercase					) else decoder_config.model_type
          )
          if decoder_type not in self.decoders_supported:
            raise ValueError(
                f"""Transformer Decoder {decoder_type} not supported, please use one of"""
                f""" {",".join(self.decoders_supported					)}"""					)
          if isinstance(__lowercase		,      __lowercase					):
            __UpperCAmelCase							:	Dict           =  CONFIG_MAPPING[decoder_type]
            __UpperCAmelCase							:	Union[str, Any]           =  config_class.from_dict(__lowercase					)

        __UpperCAmelCase							:	Optional[Any]           =  backbone_config
        __UpperCAmelCase							:	List[str]           =  decoder_config
        # main feature dimension for the model
        __UpperCAmelCase							:	Union[str, Any]           =  fpn_feature_size
        __UpperCAmelCase							:	Optional[Any]           =  mask_feature_size
        # initializer
        __UpperCAmelCase							:	int           =  init_std
        __UpperCAmelCase							:	Any           =  init_xavier_std
        # Hungarian matcher && loss
        __UpperCAmelCase							:	Any           =  cross_entropy_weight
        __UpperCAmelCase							:	Optional[Any]           =  dice_weight
        __UpperCAmelCase							:	List[str]           =  mask_weight
        __UpperCAmelCase							:	Union[str, Any]           =  use_auxiliary_loss
        __UpperCAmelCase							:	int           =  no_object_weight
        __UpperCAmelCase							:	int           =  output_auxiliary_logits

        __UpperCAmelCase							:	Optional[Any]           =  self.decoder_config.encoder_attention_heads
        __UpperCAmelCase							:	Dict           =  self.decoder_config.num_hidden_layers
        super().__init__(**__lowercase					)
      @classmethod
      def 	UpperCAmelCase      (      cls					:			int		,      __lowercase					:			PretrainedConfig		,      __lowercase					:			PretrainedConfig		,      **__lowercase					:			str					)							->      Tuple:
        return cls(
            backbone_config=__lowercase		,      decoder_config=__lowercase		,      **__lowercase		,      )




      def 	UpperCAmelCase      (      self					:			List[Any]					)							->      Dict[str, any]:
        __UpperCAmelCase							:	Optional[Any]           =  copy.deepcopy(self.__dict__					)
        __UpperCAmelCase							:	Optional[int]           =  self.backbone_config.to_dict()
        __UpperCAmelCase							:	Any           =  self.decoder_config.to_dict()
        __UpperCAmelCase							:	Tuple           =  self.__class__.model_type
        return output


