


'''simple docstring'''



import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch
      from torch import nn

      from transformers import (
          MODEL_MAPPING,
          BeitForImageClassification,
          BeitForMaskedImageModeling,
          BeitForSemanticSegmentation,
          BeitModel,
      )
      from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      import PIL
      from PIL import Image

      from transformers import BeitImageProcessor





class    UpperCAmelCase_  :





    """simple docstring"""






    def __init__(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_=1_00			,     SCREAMING_SNAKE_CASE_=13			,     SCREAMING_SNAKE_CASE_=30			,     SCREAMING_SNAKE_CASE_=2			,     SCREAMING_SNAKE_CASE_=3			,     SCREAMING_SNAKE_CASE_=True			,     SCREAMING_SNAKE_CASE_=True			,     SCREAMING_SNAKE_CASE_=32			,     SCREAMING_SNAKE_CASE_=4			,     SCREAMING_SNAKE_CASE_=4			,     SCREAMING_SNAKE_CASE_=37			,     SCREAMING_SNAKE_CASE_="gelu"			,     SCREAMING_SNAKE_CASE_=0.1			,     SCREAMING_SNAKE_CASE_=0.1			,     SCREAMING_SNAKE_CASE_=10			,     SCREAMING_SNAKE_CASE_=0.0_2			,     SCREAMING_SNAKE_CASE_=3			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=[0, 1, 2, 3]			,     )				->     Any:
         __lowerCamelCase   :	List[str]								=		parent
         __lowerCamelCase   :	Optional[int]								=		1_00
         __lowerCamelCase   :	Dict								=		batch_size
         __lowerCamelCase   :	Optional[int]								=		image_size
         __lowerCamelCase   :	Optional[int]								=		patch_size
         __lowerCamelCase   :	Union[str, Any]								=		num_channels
         __lowerCamelCase   :	Optional[int]								=		is_training
         __lowerCamelCase   :	Any								=		use_labels
         __lowerCamelCase   :	Any								=		hidden_size
         __lowerCamelCase   :	Optional[Any]								=		num_hidden_layers
         __lowerCamelCase   :	List[str]								=		num_attention_heads
         __lowerCamelCase   :	Dict								=		intermediate_size
         __lowerCamelCase   :	List[Any]								=		hidden_act
         __lowerCamelCase   :	Optional[int]								=		hidden_dropout_prob
         __lowerCamelCase   :	List[str]								=		attention_probs_dropout_prob
         __lowerCamelCase   :	str								=		type_sequence_label_size
         __lowerCamelCase   :	Optional[Any]								=		initializer_range
         __lowerCamelCase   :	Union[str, Any]								=		scope
         __lowerCamelCase   :	Tuple								=		out_indices
         __lowerCamelCase   :	Any								=		num_labels

         # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
         __lowerCamelCase   :	Any								=		(image_size // patch_size) ** 2
         __lowerCamelCase   :	int								=		num_patches + 1







    def       lowercase_				(							self	)				->     Any:
         __lowerCamelCase   :	List[str]								=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

         __lowerCamelCase   :	Optional[Any]								=		None
         __lowerCamelCase   :	Optional[int]								=		None
         if self.use_labels:
              __lowerCamelCase   :	Optional[Any]								=		ids_tensor([self.batch_size]			,     self.type_sequence_label_size	)
              __lowerCamelCase   :	Any								=		ids_tensor([self.batch_size, self.image_size, self.image_size]			,     self.num_labels	)

         __lowerCamelCase   :	int								=		self.get_config()

         return config, pixel_values, labels, pixel_labels







    def       lowercase_				(							self	)				->     Union[str, Any]:
         return BeitConfig(
             vocab_size=self.vocab_size			,     image_size=self.image_size			,     patch_size=self.patch_size			,     num_channels=self.num_channels			,     hidden_size=self.hidden_size			,     num_hidden_layers=self.num_hidden_layers			,     num_attention_heads=self.num_attention_heads			,     intermediate_size=self.intermediate_size			,     hidden_act=self.hidden_act			,     hidden_dropout_prob=self.hidden_dropout_prob			,     attention_probs_dropout_prob=self.attention_probs_dropout_prob			,     is_decoder=SCREAMING_SNAKE_CASE_			,     initializer_range=self.initializer_range			,     out_indices=self.out_indices			,     )







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     List[Any]:
         __lowerCamelCase   :	Union[str, Any]								=		BeitModel(config=SCREAMING_SNAKE_CASE_	)
         model.to(SCREAMING_SNAKE_CASE_	)
         model.eval()
         __lowerCamelCase   :	Dict								=		model(SCREAMING_SNAKE_CASE_	)
         self.parent.assertEqual(result.last_hidden_state.shape			,     (self.batch_size, self.seq_length, self.hidden_size)	)







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     Tuple:
         __lowerCamelCase   :	int								=		BeitForMaskedImageModeling(config=SCREAMING_SNAKE_CASE_	)
         model.to(SCREAMING_SNAKE_CASE_	)
         model.eval()
         __lowerCamelCase   :	Tuple								=		model(SCREAMING_SNAKE_CASE_	)
         self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length - 1, self.vocab_size)	)







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     Union[str, Any]:
         __lowerCamelCase   :	str								=		self.type_sequence_label_size
         __lowerCamelCase   :	Any								=		BeitForImageClassification(SCREAMING_SNAKE_CASE_	)
         model.to(SCREAMING_SNAKE_CASE_	)
         model.eval()
         __lowerCamelCase   :	int								=		model(SCREAMING_SNAKE_CASE_			,     labels=SCREAMING_SNAKE_CASE_	)
         self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.type_sequence_label_size)	)

         # test greyscale images
         __lowerCamelCase   :	Optional[Any]								=		1
         __lowerCamelCase   :	Any								=		BeitForImageClassification(SCREAMING_SNAKE_CASE_	)
         model.to(SCREAMING_SNAKE_CASE_	)
         model.eval()

         __lowerCamelCase   :	Any								=		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
         __lowerCamelCase   :	int								=		model(SCREAMING_SNAKE_CASE_			,     labels=SCREAMING_SNAKE_CASE_	)
         self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.type_sequence_label_size)	)







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     int:
         __lowerCamelCase   :	List[Any]								=		self.num_labels
         __lowerCamelCase   :	List[Any]								=		BeitForSemanticSegmentation(SCREAMING_SNAKE_CASE_	)
         model.to(SCREAMING_SNAKE_CASE_	)
         model.eval()
         __lowerCamelCase   :	List[str]								=		model(SCREAMING_SNAKE_CASE_	)
         self.parent.assertEqual(
             result.logits.shape			,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)	)
         __lowerCamelCase   :	Any								=		model(SCREAMING_SNAKE_CASE_			,     labels=SCREAMING_SNAKE_CASE_	)
         self.parent.assertEqual(
             result.logits.shape			,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)	)







    def       lowercase_				(							self	)				->     Optional[Any]:
         __lowerCamelCase   :	Optional[int]								=		self.prepare_config_and_inputs()
         __lowerCamelCase				,       __lowerCamelCase				,       __lowerCamelCase				,       __lowerCamelCase   :	Optional[int]								=		config_and_inputs
         __lowerCamelCase   :	Any								=		{'pixel_values': pixel_values}
         return config, inputs_dict







@require_torch
class    UpperCAmelCase_  (_UpperCAmelCase					,		_UpperCAmelCase					,		unittest.TestCase  ):





    """simple docstring"""






    lowerCamelCase  :					Optional[int]							     =			(
        (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
        if is_torch_available()
        else ()
    )
    lowerCamelCase  :					str							     =			(
        {
            'feature-extraction': BeitModel,
            'image-classification': BeitForImageClassification,
            'image-segmentation': BeitForSemanticSegmentation,
        }
        if is_torch_available()
        else {}
    )

    lowerCamelCase  :					str							     =			False
    lowerCamelCase  :					List[str]							     =			False
    lowerCamelCase  :					int							     =			False







    def       lowercase_				(							self	)				->     Optional[int]:
         __lowerCamelCase   :	int								=		BeitModelTester(self	)
         __lowerCamelCase   :	Tuple								=		ConfigTester(self			,     config_class=SCREAMING_SNAKE_CASE_			,     has_text_modality=SCREAMING_SNAKE_CASE_			,     hidden_size=37	)







    def       lowercase_				(							self	)				->     List[str]:
         self.config_tester.run_common_tests()







    @unittest.skip(reason='BEiT does not use inputs_embeds'	)
    def       lowercase_				(							self	)				->     List[Any]:
         pass







    @require_torch_multi_gpu
    @unittest.skip(reason='BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'	)
    def       lowercase_				(							self	)				->     int:
         pass







    def       lowercase_				(							self	)				->     str:
         __lowerCamelCase				,       __lowerCamelCase   :	List[str]								=		self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
              __lowerCamelCase   :	Optional[int]								=		model_class(SCREAMING_SNAKE_CASE_	)
              self.assertIsInstance(model.get_input_embeddings()			,     (nn.Module)	)
              __lowerCamelCase   :	List[Any]								=		model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE_			,     nn.Linear	)	)







    def       lowercase_				(							self	)				->     Any:
         __lowerCamelCase				,       __lowerCamelCase   :	Union[str, Any]								=		self.model_tester.prepare_config_and_inputs_for_common()

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

              __lowerCamelCase   :	Dict								=		['pixel_values']
              self.assertListEqual(arg_names[:1]			,     SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self	)				->     str:
         __lowerCamelCase   :	Optional[Any]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self	)				->     Union[str, Any]:
         __lowerCamelCase   :	List[str]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self	)				->     Tuple:
         __lowerCamelCase   :	Tuple								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self	)				->     Tuple:
         __lowerCamelCase   :	List[str]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_semantic_segmentation(*SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self	)				->     Union[str, Any]:
         if not self.model_tester.is_training:
              return

         __lowerCamelCase				,       __lowerCamelCase   :	List[Any]								=		self.model_tester.prepare_config_and_inputs_for_common()
         __lowerCamelCase   :	Dict								=		True

         for model_class in self.all_model_classes:
              # we don't test BeitForMaskedImageModeling
              if model_class in [*get_values(SCREAMING_SNAKE_CASE_	), BeitForMaskedImageModeling]:
                   continue

              __lowerCamelCase   :	int								=		model_class(SCREAMING_SNAKE_CASE_	)
              model.to(SCREAMING_SNAKE_CASE_	)
              model.train()
              __lowerCamelCase   :	Optional[int]								=		self._prepare_for_class(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     return_labels=SCREAMING_SNAKE_CASE_	)
              __lowerCamelCase   :	Optional[Any]								=		model(**SCREAMING_SNAKE_CASE_	).loss
              loss.backward()







    def       lowercase_				(							self	)				->     List[str]:
         __lowerCamelCase				,       __lowerCamelCase   :	int								=		self.model_tester.prepare_config_and_inputs_for_common()
         if not self.model_tester.is_training:
              return

         __lowerCamelCase   :	Any								=		False
         __lowerCamelCase   :	List[str]								=		True

         for model_class in self.all_model_classes:
              # we don't test BeitForMaskedImageModeling
              if (
                  model_class in [*get_values(SCREAMING_SNAKE_CASE_	), BeitForMaskedImageModeling]
                  or not model_class.supports_gradient_checkpointing
              ):
                   continue

              __lowerCamelCase   :	List[Any]								=		model_class(SCREAMING_SNAKE_CASE_	)
              model.gradient_checkpointing_enable()
              model.to(SCREAMING_SNAKE_CASE_	)
              model.train()
              __lowerCamelCase   :	str								=		self._prepare_for_class(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     return_labels=SCREAMING_SNAKE_CASE_	)
              __lowerCamelCase   :	str								=		model(**SCREAMING_SNAKE_CASE_	).loss
              loss.backward()







    def       lowercase_				(							self	)				->     List[str]:
         __lowerCamelCase				,       __lowerCamelCase   :	str								=		self.model_tester.prepare_config_and_inputs_for_common()

         __lowerCamelCase   :	Union[str, Any]								=		_config_zero_init(SCREAMING_SNAKE_CASE_	)
         for model_class in self.all_model_classes:
              __lowerCamelCase   :	str								=		model_class(config=SCREAMING_SNAKE_CASE_	)
              for name, param in model.named_parameters():
                   # we skip lambda parameters as these require special initial values
                   # determined by config.layer_scale_init_value
                   if "lambda" in name:
                        continue
                   if param.requires_grad:
                        self.assertIn(
                            ((param.data.mean() * 1E9).round() / 1E9).item()			,     [0.0, 1.0]			,     msg=f'Parameter {name} of model {model_class} seems not properly initialized'			,     )







    @slow
    def       lowercase_				(							self	)				->     str:
         for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              __lowerCamelCase   :	Optional[Any]								=		BeitModel.from_pretrained(SCREAMING_SNAKE_CASE_	)
              self.assertIsNotNone(SCREAMING_SNAKE_CASE_	)







def 			UpperCAmelCase__					(	)     ->		Optional[int]:
     __lowerCamelCase   :	str								=		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							)
     return image





@require_torch
@require_vision
class    UpperCAmelCase_  (unittest.TestCase  ):





    """simple docstring"""






    @cached_property
    def       lowercase_				(							self	)				->     List[str]:
         return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224'	) if is_vision_available() else None







    @slow
    def       lowercase_				(							self	)				->     List[str]:
         __lowerCamelCase   :	Optional[Any]								=		BeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k'	).to(SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	str								=		self.default_image_processor
         __lowerCamelCase   :	Any								=		prepare_img()
         __lowerCamelCase   :	Union[str, Any]								=		image_processor(images=SCREAMING_SNAKE_CASE_			,     return_tensors='pt'	).pixel_values.to(SCREAMING_SNAKE_CASE_	)

         # prepare bool_masked_pos
         __lowerCamelCase   :	List[Any]								=		torch.ones((1, 1_96)			,     dtype=torch.bool	).to(SCREAMING_SNAKE_CASE_	)

         # forward pass
         with torch.no_grad():
              __lowerCamelCase   :	Optional[Any]								=		model(pixel_values=SCREAMING_SNAKE_CASE_			,     bool_masked_pos=SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Any								=		outputs.logits

         # verify the logits
         __lowerCamelCase   :	List[str]								=		torch.Size((1, 1_96, 81_92)	)
         self.assertEqual(logits.shape			,     SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	Optional[Any]								=		torch.tensor(
             [[-3.2_4_3_7, 0.5_0_7_2, -1_3.9_1_7_4], [-3.2_4_5_6, 0.4_9_4_8, -1_3.9_4_0_1], [-3.2_0_3_3, 0.5_1_2_1, -1_3.8_5_5_0]]	).to(SCREAMING_SNAKE_CASE_	)

         self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3]			,     SCREAMING_SNAKE_CASE_			,     atol=1E-2	)	)







    @slow
    def       lowercase_				(							self	)				->     Tuple:
         __lowerCamelCase   :	Any								=		BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224'	).to(SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	Union[str, Any]								=		self.default_image_processor
         __lowerCamelCase   :	Tuple								=		prepare_img()
         __lowerCamelCase   :	Dict								=		image_processor(images=SCREAMING_SNAKE_CASE_			,     return_tensors='pt'	).to(SCREAMING_SNAKE_CASE_	)

         # forward pass
         with torch.no_grad():
              __lowerCamelCase   :	Optional[int]								=		model(**SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Tuple								=		outputs.logits

         # verify the logits
         __lowerCamelCase   :	List[Any]								=		torch.Size((1, 10_00)	)
         self.assertEqual(logits.shape			,     SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	int								=		torch.tensor([-1.2_3_8_5, -1.0_9_8_7, -1.0_1_0_8]	).to(SCREAMING_SNAKE_CASE_	)

         self.assertTrue(torch.allclose(logits[0, :3]			,     SCREAMING_SNAKE_CASE_			,     atol=1E-4	)	)

         __lowerCamelCase   :	Any								=		2_81
         self.assertEqual(logits.argmax(-1	).item()			,     SCREAMING_SNAKE_CASE_	)







    @slow
    def       lowercase_				(							self	)				->     Optional[int]:
         __lowerCamelCase   :	str								=		BeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k'	).to(
             SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	Tuple								=		self.default_image_processor
         __lowerCamelCase   :	Dict								=		prepare_img()
         __lowerCamelCase   :	Optional[int]								=		image_processor(images=SCREAMING_SNAKE_CASE_			,     return_tensors='pt'	).to(SCREAMING_SNAKE_CASE_	)

         # forward pass
         with torch.no_grad():
              __lowerCamelCase   :	Any								=		model(**SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Optional[int]								=		outputs.logits

         # verify the logits
         __lowerCamelCase   :	List[str]								=		torch.Size((1, 2_18_41)	)
         self.assertEqual(logits.shape			,     SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	Union[str, Any]								=		torch.tensor([1.6_8_8_1, -0.2_7_8_7, 0.5_9_0_1]	).to(SCREAMING_SNAKE_CASE_	)

         self.assertTrue(torch.allclose(logits[0, :3]			,     SCREAMING_SNAKE_CASE_			,     atol=1E-4	)	)

         __lowerCamelCase   :	Tuple								=		23_96
         self.assertEqual(logits.argmax(-1	).item()			,     SCREAMING_SNAKE_CASE_	)







    @slow
    def       lowercase_				(							self	)				->     str:
         __lowerCamelCase   :	Any								=		BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640'	)
         __lowerCamelCase   :	List[Any]								=		model.to(SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	List[str]								=		BeitImageProcessor(do_resize=SCREAMING_SNAKE_CASE_			,     size=6_40			,     do_center_crop=SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	List[Any]								=		load_dataset('hf-internal-testing/fixtures_ade20k'			,     split='test'	)
         __lowerCamelCase   :	Tuple								=		Image.open(ds[0]['file']	)
         __lowerCamelCase   :	Any								=		image_processor(images=SCREAMING_SNAKE_CASE_			,     return_tensors='pt'	).to(SCREAMING_SNAKE_CASE_	)

         # forward pass
         with torch.no_grad():
              __lowerCamelCase   :	List[str]								=		model(**SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	int								=		outputs.logits

         # verify the logits
         __lowerCamelCase   :	Any								=		torch.Size((1, 1_50, 1_60, 1_60)	)
         self.assertEqual(logits.shape			,     SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	Union[str, Any]								=		version.parse(PIL.__version__	) < version.parse('9.0.0'	)

         if is_pillow_less_than_a:
              __lowerCamelCase   :	str								=		torch.tensor(
                  [
                      [[-4.9_2_2_5, -2.3_9_5_4, -3.0_5_2_2], [-2.8_8_2_2, -1.0_0_4_6, -1.7_5_6_1], [-2.9_5_4_9, -1.3_2_2_8, -2.1_3_4_7]],
                      [[-5.8_1_6_8, -3.4_1_2_9, -4.0_7_7_8], [-3.8_6_5_1, -2.2_2_1_4, -3.0_2_7_7], [-3.8_3_5_6, -2.4_6_4_3, -3.3_5_3_5]],
                      [[-0.0_0_7_8, 3.9_9_5_2, 4.0_7_5_4], [2.9_8_5_6, 4.6_9_4_4, 5.0_0_3_5], [3.2_4_1_3, 4.7_8_1_3, 4.9_9_6_9]],
                  ]			,     device=SCREAMING_SNAKE_CASE_			,     )
         else:
              __lowerCamelCase   :	List[Any]								=		torch.tensor(
                  [
                      [[-4.8_9_6_0, -2.3_6_8_8, -3.0_3_5_5], [-2.8_4_7_8, -0.9_8_3_6, -1.7_4_1_8], [-2.9_4_4_9, -1.3_3_3_2, -2.1_4_5_6]],
                      [[-5.8_0_8_1, -3.4_1_2_4, -4.1_0_0_6], [-3.8_5_6_1, -2.2_0_8_1, -3.0_3_2_3], [-3.8_3_6_5, -2.4_6_0_1, -3.3_6_6_9]],
                      [[-0.0_3_0_9, 3.9_8_6_8, 4.0_5_4_0], [2.9_6_4_0, 4.6_8_7_7, 4.9_9_7_6], [3.2_0_8_1, 4.7_6_9_0, 4.9_9_4_2]],
                  ]			,     device=SCREAMING_SNAKE_CASE_			,     )

         self.assertTrue(torch.allclose(logits[0, :3, :3, :3]			,     SCREAMING_SNAKE_CASE_			,     atol=1E-4	)	)







    @slow
    def       lowercase_				(							self	)				->     Tuple:
         __lowerCamelCase   :	List[str]								=		BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640'	)
         __lowerCamelCase   :	List[str]								=		model.to(SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	Optional[Any]								=		BeitImageProcessor(do_resize=SCREAMING_SNAKE_CASE_			,     size=6_40			,     do_center_crop=SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	Optional[Any]								=		load_dataset('hf-internal-testing/fixtures_ade20k'			,     split='test'	)
         __lowerCamelCase   :	List[Any]								=		Image.open(ds[0]['file']	)
         __lowerCamelCase   :	Union[str, Any]								=		image_processor(images=SCREAMING_SNAKE_CASE_			,     return_tensors='pt'	).to(SCREAMING_SNAKE_CASE_	)

         # forward pass
         with torch.no_grad():
              __lowerCamelCase   :	Any								=		model(**SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	List[Any]								=		outputs.logits.detach().cpu()

         __lowerCamelCase   :	str								=		image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE_			,     target_sizes=[(5_00, 3_00)]	)
         __lowerCamelCase   :	Any								=		torch.Size((5_00, 3_00)	)
         self.assertEqual(segmentation[0].shape			,     SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	Union[str, Any]								=		image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	str								=		torch.Size((1_60, 1_60)	)
         self.assertEqual(segmentation[0].shape			,     SCREAMING_SNAKE_CASE_	)










'''simple docstring'''



from copy import deepcopy
from typing import Optional, Union

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_tf_available, is_torch_available


if is_torch_available():
      import torch

if is_tf_available():
      import tensorflow as tf





class    UpperCAmelCase_  (_UpperCAmelCase  ):





    """simple docstring"""






    lowerCamelCase  :					Dict							     =			['image_processor']
    lowerCamelCase  :					str							     =			'SamImageProcessor'







    def __init__(							self			,     SCREAMING_SNAKE_CASE_	)				->     List[str]:
         super().__init__(SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Any								=		self.image_processor
         __lowerCamelCase   :	str								=		-10
         __lowerCamelCase   :	List[str]								=		self.image_processor.size['longest_edge']







    def __call__(							self			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_ = None			,     **SCREAMING_SNAKE_CASE_			,     )				->     BatchEncoding:
         __lowerCamelCase   :	Union[str, Any]								=		self.image_processor(
             SCREAMING_SNAKE_CASE_			,     return_tensors=SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_			,     )

         # pop arguments that are not used in the foward but used nevertheless
         __lowerCamelCase   :	List[Any]								=		encoding_image_processor['original_sizes']

         if hasattr(SCREAMING_SNAKE_CASE_			,     'numpy'	):  # Checks if Torch or TF tensor
              __lowerCamelCase   :	Dict								=		original_sizes.numpy()

         __lowerCamelCase				,       __lowerCamelCase				,       __lowerCamelCase   :	str								=		self._check_and_preprocess_points(
             input_points=SCREAMING_SNAKE_CASE_			,     input_labels=SCREAMING_SNAKE_CASE_			,     input_boxes=SCREAMING_SNAKE_CASE_			,     )

         __lowerCamelCase   :	Tuple								=		self._normalize_and_convert(
             SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     input_points=SCREAMING_SNAKE_CASE_			,     input_labels=SCREAMING_SNAKE_CASE_			,     input_boxes=SCREAMING_SNAKE_CASE_			,     return_tensors=SCREAMING_SNAKE_CASE_			,     )

         return encoding_image_processor







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_="pt"			,     )				->     Optional[int]:
         if input_points is not None:
              if len(SCREAMING_SNAKE_CASE_	) != len(SCREAMING_SNAKE_CASE_	):
                   __lowerCamelCase   :	Union[str, Any]								=		[
                       self._normalize_coordinates(self.target_size			,     SCREAMING_SNAKE_CASE_			,     original_sizes[0]	) for point in input_points
                   ]
              else:
                   __lowerCamelCase   :	List[Any]								=		[
                       self._normalize_coordinates(self.target_size			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)
                       for point, original_size in zip(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)
                   ]
              # check that all arrays have the same shape
              if not all(point.shape == input_points[0].shape for point in input_points	):
                   if input_labels is not None:
                        __lowerCamelCase				,       __lowerCamelCase   :	Tuple								=		self._pad_points_and_labels(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)

              __lowerCamelCase   :	Optional[int]								=		np.array(SCREAMING_SNAKE_CASE_	)

         if input_labels is not None:
              __lowerCamelCase   :	Optional[int]								=		np.array(SCREAMING_SNAKE_CASE_	)

         if input_boxes is not None:
              if len(SCREAMING_SNAKE_CASE_	) != len(SCREAMING_SNAKE_CASE_	):
                   __lowerCamelCase   :	Union[str, Any]								=		[
                       self._normalize_coordinates(self.target_size			,     SCREAMING_SNAKE_CASE_			,     original_sizes[0]			,     is_bounding_box=SCREAMING_SNAKE_CASE_	)
                       for box in input_boxes
                   ]
              else:
                   __lowerCamelCase   :	Optional[int]								=		[
                       self._normalize_coordinates(self.target_size			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     is_bounding_box=SCREAMING_SNAKE_CASE_	)
                       for box, original_size in zip(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)
                   ]
              __lowerCamelCase   :	Optional[int]								=		np.array(SCREAMING_SNAKE_CASE_	)

         if input_boxes is not None:
              if return_tensors == "pt":
                   __lowerCamelCase   :	Any								=		torch.from_numpy(SCREAMING_SNAKE_CASE_	)
                   # boxes batch size of 1 by default
                   __lowerCamelCase   :	Union[str, Any]								=		input_boxes.unsqueeze(1	) if len(input_boxes.shape	) != 3 else input_boxes
              elif return_tensors == "tf":
                   __lowerCamelCase   :	List[str]								=		tf.convert_to_tensor(SCREAMING_SNAKE_CASE_	)
                   # boxes batch size of 1 by default
                   __lowerCamelCase   :	str								=		tf.expand_dims(SCREAMING_SNAKE_CASE_			,     1	) if len(input_boxes.shape	) != 3 else input_boxes
              encoding_image_processor.update({'input_boxes': input_boxes}	)
         if input_points is not None:
              if return_tensors == "pt":
                   __lowerCamelCase   :	int								=		torch.from_numpy(SCREAMING_SNAKE_CASE_	)
                   # point batch size of 1 by default
                   __lowerCamelCase   :	Dict								=		input_points.unsqueeze(1	) if len(input_points.shape	) != 4 else input_points
              elif return_tensors == "tf":
                   __lowerCamelCase   :	Dict								=		tf.convert_to_tensor(SCREAMING_SNAKE_CASE_	)
                   # point batch size of 1 by default
                   __lowerCamelCase   :	Tuple								=		tf.expand_dims(SCREAMING_SNAKE_CASE_			,     1	) if len(input_points.shape	) != 4 else input_points
              encoding_image_processor.update({'input_points': input_points}	)
         if input_labels is not None:
              if return_tensors == "pt":
                   __lowerCamelCase   :	List[Any]								=		torch.from_numpy(SCREAMING_SNAKE_CASE_	)
                   # point batch size of 1 by default
                   __lowerCamelCase   :	Dict								=		input_labels.unsqueeze(1	) if len(input_labels.shape	) != 3 else input_labels
              elif return_tensors == "tf":
                   __lowerCamelCase   :	str								=		tf.convert_to_tensor(SCREAMING_SNAKE_CASE_	)
                   # point batch size of 1 by default
                   __lowerCamelCase   :	Dict								=		tf.expand_dims(SCREAMING_SNAKE_CASE_			,     1	) if len(input_labels.shape	) != 3 else input_labels
              encoding_image_processor.update({'input_labels': input_labels}	)

         return encoding_image_processor







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     Tuple:
         __lowerCamelCase   :	List[str]								=		max([point.shape[0] for point in input_points]	)
         __lowerCamelCase   :	Union[str, Any]								=		[]
         for i, point in enumerate(SCREAMING_SNAKE_CASE_	):
              if point.shape[0] != expected_nb_points:
                   __lowerCamelCase   :	Optional[int]								=		np.concatenate(
                       [point, np.zeros((expected_nb_points - point.shape[0], 2)	) + self.point_pad_value]			,     axis=0	)
                   __lowerCamelCase   :	List[Any]								=		np.append(input_labels[i]			,     [self.point_pad_value]	)
              processed_input_points.append(SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	List[Any]								=		processed_input_points
         return input_points, input_labels







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_=False	)				->     np.ndarray:
         __lowerCamelCase				,       __lowerCamelCase   :	Tuple								=		original_size
         __lowerCamelCase				,       __lowerCamelCase   :	Optional[Any]								=		self.image_processor._get_preprocess_shape(SCREAMING_SNAKE_CASE_			,     longest_edge=SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Dict								=		deepcopy(SCREAMING_SNAKE_CASE_	).astype(SCREAMING_SNAKE_CASE_	)

         if is_bounding_box:
              __lowerCamelCase   :	Optional[int]								=		coords.reshape(-1			,     2			,     2	)

         __lowerCamelCase   :	List[Any]								=		coords[..., 0] * (new_w / old_w)
         __lowerCamelCase   :	Dict								=		coords[..., 1] * (new_h / old_h)

         if is_bounding_box:
              __lowerCamelCase   :	Tuple								=		coords.reshape(-1			,     4	)

         return coords







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     )				->     Optional[Any]:
         if input_points is not None:
              if hasattr(SCREAMING_SNAKE_CASE_			,     'numpy'	):  # Checks for TF or Torch tensor
                   __lowerCamelCase   :	List[str]								=		input_points.numpy().tolist()

              if not isinstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	) or not isinstance(input_points[0]			,     SCREAMING_SNAKE_CASE_	):
                   raise ValueError('Input points must be a list of list of floating points.'	)
              __lowerCamelCase   :	str								=		[np.array(SCREAMING_SNAKE_CASE_	) for input_point in input_points]
         else:
              __lowerCamelCase   :	Optional[Any]								=		None

         if input_labels is not None:
              if hasattr(SCREAMING_SNAKE_CASE_			,     'numpy'	):
                   __lowerCamelCase   :	Union[str, Any]								=		input_labels.numpy().tolist()

              if not isinstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	) or not isinstance(input_labels[0]			,     SCREAMING_SNAKE_CASE_	):
                   raise ValueError('Input labels must be a list of list integers.'	)
              __lowerCamelCase   :	Any								=		[np.array(SCREAMING_SNAKE_CASE_	) for label in input_labels]
         else:
              __lowerCamelCase   :	Dict								=		None

         if input_boxes is not None:
              if hasattr(SCREAMING_SNAKE_CASE_			,     'numpy'	):
                   __lowerCamelCase   :	int								=		input_boxes.numpy().tolist()

              if (
                  not isinstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)
                  or not isinstance(input_boxes[0]			,     SCREAMING_SNAKE_CASE_	)
                  or not isinstance(input_boxes[0][0]			,     SCREAMING_SNAKE_CASE_	)
              ):
                   raise ValueError('Input boxes must be a list of list of list of floating points.'	)
              __lowerCamelCase   :	List[Any]								=		[np.array(SCREAMING_SNAKE_CASE_	).astype(np.floataa	) for box in input_boxes]
         else:
              __lowerCamelCase   :	Tuple								=		None

         return input_points, input_labels, input_boxes







    @property
    def       lowercase_				(							self	)				->     Dict:
         __lowerCamelCase   :	Any								=		self.image_processor.model_input_names
         return list(dict.fromkeys(SCREAMING_SNAKE_CASE_	)	)







    def       lowercase_				(							self			,     *SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_	)				->     Optional[Any]:
         return self.image_processor.post_process_masks(*SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_	)






