
'''simple docstring'''
import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
				import torch

if is_vision_available():
				from PIL import Image

				from transformers import BeitImageProcessor


class       _lowerCAmelCase   (   unittest.TestCase				):







							'''simple docstring'''

							def __init__(self  ,  UpperCAmelCase  ,  UpperCAmelCase=7  ,  UpperCAmelCase=3  ,  UpperCAmelCase=18  ,  UpperCAmelCase=30  ,  UpperCAmelCase=400  ,  UpperCAmelCase=True  ,  UpperCAmelCase=None  ,  UpperCAmelCase=True  ,  UpperCAmelCase=None  ,  UpperCAmelCase=True  ,  UpperCAmelCase=[0.5, 0.5, 0.5]  ,  UpperCAmelCase=[0.5, 0.5, 0.5]  ,  UpperCAmelCase=False  ,  )					->       List[str]:
														_snake_case       =  size if size is not None else {"""height""": 20, """width""": 20}
														_snake_case       =  crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
														_snake_case       =  parent
														_snake_case       =  batch_size
														_snake_case       =  num_channels
														_snake_case       =  image_size
														_snake_case       =  min_resolution
														_snake_case       =  max_resolution
														_snake_case       =  do_resize
														_snake_case       =  size
														_snake_case       =  do_center_crop
														_snake_case       =  crop_size
														_snake_case       =  do_normalize
														_snake_case       =  image_mean
														_snake_case       =  image_std
														_snake_case       =  do_reduce_labels





							def 	lowercase							(self  )					->       Optional[int]:
														return {
														    "do_resize": self.do_resize,
														    "size": self.size,
														    "do_center_crop": self.do_center_crop,
														    "crop_size": self.crop_size,
														    "do_normalize": self.do_normalize,
														    "image_mean": self.image_mean,
														    "image_std": self.image_std,
														    "do_reduce_labels": self.do_reduce_labels,
														}






def  __SCREAMING_SNAKE_CASE				(  ):
							_snake_case       =  load_dataset("""hf-internal-testing/fixtures_ade20k"""							, split="""test"""				)

							_snake_case       =  Image.open(dataset[0]["""file"""]				)
							_snake_case       =  Image.open(dataset[1]["""file"""]				)

							return image, map






def  __SCREAMING_SNAKE_CASE				(  ):
							_snake_case       =  load_dataset("""hf-internal-testing/fixtures_ade20k"""							, split="""test"""				)

							_snake_case       =  Image.open(ds[0]["""file"""]				)
							_snake_case       =  Image.open(ds[1]["""file"""]				)
							_snake_case       =  Image.open(ds[2]["""file"""]				)
							_snake_case       =  Image.open(ds[3]["""file"""]				)

							return [imagea, imagea], [mapa, mapa]


@require_torch
@require_vision
class       _lowerCAmelCase   (   __snake_case					,							unittest.TestCase				):







							'''simple docstring'''

							lowerCAmelCase_ 						=						BeitImageProcessor if is_vision_available() else None

							def 	lowercase							(self  )					->       int:
														_snake_case       =  BeitImageProcessingTester(self  )

							@property
							def 	lowercase							(self  )					->       List[Any]:
														return self.image_processor_tester.prepare_image_processor_dict()

							def 	lowercase							(self  )					->       Optional[Any]:
														_snake_case       =  self.image_processing_class(**self.image_processor_dict  )
														self.assertTrue(hasattr(UpperCAmelCase  ,  """do_resize"""  )  )
														self.assertTrue(hasattr(UpperCAmelCase  ,  """size"""  )  )
														self.assertTrue(hasattr(UpperCAmelCase  ,  """do_center_crop"""  )  )
														self.assertTrue(hasattr(UpperCAmelCase  ,  """center_crop"""  )  )
														self.assertTrue(hasattr(UpperCAmelCase  ,  """do_normalize"""  )  )
														self.assertTrue(hasattr(UpperCAmelCase  ,  """image_mean"""  )  )
														self.assertTrue(hasattr(UpperCAmelCase  ,  """image_std"""  )  )

							def 	lowercase							(self  )					->       Optional[Any]:
														_snake_case       =  self.image_processing_class.from_dict(self.image_processor_dict  )
														self.assertEqual(image_processor.size  ,  {"""height""": 20, """width""": 20}  )
														self.assertEqual(image_processor.crop_size  ,  {"""height""": 18, """width""": 18}  )
														self.assertEqual(image_processor.do_reduce_labels  ,  UpperCAmelCase  )

														_snake_case       =  self.image_processing_class.from_dict(
														    self.image_processor_dict  ,  size=42  ,  crop_size=84  ,  reduce_labels=UpperCAmelCase  )
														self.assertEqual(image_processor.size  ,  {"""height""": 42, """width""": 42}  )
														self.assertEqual(image_processor.crop_size  ,  {"""height""": 84, """width""": 84}  )
														self.assertEqual(image_processor.do_reduce_labels  ,  UpperCAmelCase  )

							def 	lowercase							(self  )					->       int:
														pass

							def 	lowercase							(self  )					->       Tuple:
														# Initialize image_processing
														_snake_case       =  self.image_processing_class(**self.image_processor_dict  )
														# create random PIL images
														_snake_case       =  prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=UpperCAmelCase  )
														for image in image_inputs:
																					self.assertIsInstance(UpperCAmelCase  ,  Image.Image  )

														# Test not batched input
														_snake_case       =  image_processing(image_inputs[0]  ,  return_tensors="""pt"""  ).pixel_values
														self.assertEqual(
														    encoded_images.shape  ,  (
														        1,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )

														# Test batched
														_snake_case       =  image_processing(UpperCAmelCase  ,  return_tensors="""pt"""  ).pixel_values
														self.assertEqual(
														    encoded_images.shape  ,  (
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )

							def 	lowercase							(self  )					->       int:
														# Initialize image_processing
														_snake_case       =  self.image_processing_class(**self.image_processor_dict  )
														# create random numpy tensors
														_snake_case       =  prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=UpperCAmelCase  ,  numpify=UpperCAmelCase  )
														for image in image_inputs:
																					self.assertIsInstance(UpperCAmelCase  ,  np.ndarray  )

														# Test not batched input
														_snake_case       =  image_processing(image_inputs[0]  ,  return_tensors="""pt"""  ).pixel_values
														self.assertEqual(
														    encoded_images.shape  ,  (
														        1,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )

														# Test batched
														_snake_case       =  image_processing(UpperCAmelCase  ,  return_tensors="""pt"""  ).pixel_values
														self.assertEqual(
														    encoded_images.shape  ,  (
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )

							def 	lowercase							(self  )					->       int:
														# Initialize image_processing
														_snake_case       =  self.image_processing_class(**self.image_processor_dict  )
														# create random PyTorch tensors
														_snake_case       =  prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=UpperCAmelCase  ,  torchify=UpperCAmelCase  )
														for image in image_inputs:
																					self.assertIsInstance(UpperCAmelCase  ,  torch.Tensor  )

														# Test not batched input
														_snake_case       =  image_processing(image_inputs[0]  ,  return_tensors="""pt"""  ).pixel_values
														self.assertEqual(
														    encoded_images.shape  ,  (
														        1,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )

														# Test batched
														_snake_case       =  image_processing(UpperCAmelCase  ,  return_tensors="""pt"""  ).pixel_values
														self.assertEqual(
														    encoded_images.shape  ,  (
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )

							def 	lowercase							(self  )					->       Tuple:
														# Initialize image_processing
														_snake_case       =  self.image_processing_class(**self.image_processor_dict  )
														# create random PyTorch tensors
														_snake_case       =  prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=UpperCAmelCase  ,  torchify=UpperCAmelCase  )
														_snake_case       =  []
														for image in image_inputs:
																					self.assertIsInstance(UpperCAmelCase  ,  torch.Tensor  )
																					maps.append(torch.zeros(image.shape[-2:]  ).long()  )

														# Test not batched input
														_snake_case       =  image_processing(image_inputs[0]  ,  maps[0]  ,  return_tensors="""pt"""  )
														self.assertEqual(
														    encoding["""pixel_values"""].shape  ,  (
														        1,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )
														self.assertEqual(
														    encoding["""labels"""].shape  ,  (
														        1,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )
														self.assertEqual(encoding["""labels"""].dtype  ,  torch.long  )
														self.assertTrue(encoding["""labels"""].min().item() >= 0  )
														self.assertTrue(encoding["""labels"""].max().item() <= 255  )

														# Test batched
														_snake_case       =  image_processing(UpperCAmelCase  ,  UpperCAmelCase  ,  return_tensors="""pt"""  )
														self.assertEqual(
														    encoding["""pixel_values"""].shape  ,  (
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )
														self.assertEqual(
														    encoding["""labels"""].shape  ,  (
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )
														self.assertEqual(encoding["""labels"""].dtype  ,  torch.long  )
														self.assertTrue(encoding["""labels"""].min().item() >= 0  )
														self.assertTrue(encoding["""labels"""].max().item() <= 255  )

														# Test not batched input (PIL images)
														_snake_case,							_snake_case       =  prepare_semantic_single_inputs()

														_snake_case       =  image_processing(UpperCAmelCase  ,  UpperCAmelCase  ,  return_tensors="""pt"""  )
														self.assertEqual(
														    encoding["""pixel_values"""].shape  ,  (
														        1,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )
														self.assertEqual(
														    encoding["""labels"""].shape  ,  (
														        1,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )
														self.assertEqual(encoding["""labels"""].dtype  ,  torch.long  )
														self.assertTrue(encoding["""labels"""].min().item() >= 0  )
														self.assertTrue(encoding["""labels"""].max().item() <= 255  )

														# Test batched input (PIL images)
														_snake_case,							_snake_case       =  prepare_semantic_batch_inputs()

														_snake_case       =  image_processing(UpperCAmelCase  ,  UpperCAmelCase  ,  return_tensors="""pt"""  )
														self.assertEqual(
														    encoding["""pixel_values"""].shape  ,  (
														        2,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )
														self.assertEqual(
														    encoding["""labels"""].shape  ,  (
														        2,
														        self.image_processor_tester.crop_size["""height"""],
														        self.image_processor_tester.crop_size["""width"""],
														    )  ,  )
														self.assertEqual(encoding["""labels"""].dtype  ,  torch.long  )
														self.assertTrue(encoding["""labels"""].min().item() >= 0  )
														self.assertTrue(encoding["""labels"""].max().item() <= 255  )





							def 	lowercase							(self  )					->       Tuple:
														# Initialize image_processing
														_snake_case       =  self.image_processing_class(**self.image_processor_dict  )

														# ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
														_snake_case,							_snake_case       =  prepare_semantic_single_inputs()
														_snake_case       =  image_processing(UpperCAmelCase  ,  UpperCAmelCase  ,  return_tensors="""pt"""  )
														self.assertTrue(encoding["""labels"""].min().item() >= 0  )
														self.assertTrue(encoding["""labels"""].max().item() <= 150  )

														_snake_case       =  True
														_snake_case       =  image_processing(UpperCAmelCase  ,  UpperCAmelCase  ,  return_tensors="""pt"""  )
														self.assertTrue(encoding["""labels"""].min().item() >= 0  )
														self.assertTrue(encoding["""labels"""].max().item() <= 255  )

'''simple docstring'''
from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__lowerCAmelCase												=	logging.get_logger(__name__)

__lowerCAmelCase												=	{
    'shi-labs/nat-mini-in1k-224': 'https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json',
    # See all Nat models at https://huggingface.co/models?filter=nat
}


class       _lowerCAmelCase   (   __snake_case					,							__snake_case				):







							'''simple docstring'''

							lowerCAmelCase_ 						=						"nat"

							lowerCAmelCase_ 						=						{
							    "num_attention_heads": "num_heads",
							    "num_hidden_layers": "num_layers",
							}

							def __init__(self  ,  UpperCAmelCase=4  ,  UpperCAmelCase=3  ,  UpperCAmelCase=64  ,  UpperCAmelCase=[3, 4, 6, 5]  ,  UpperCAmelCase=[2, 4, 8, 16]  ,  UpperCAmelCase=7  ,  UpperCAmelCase=3.0  ,  UpperCAmelCase=True  ,  UpperCAmelCase=0.0  ,  UpperCAmelCase=0.0  ,  UpperCAmelCase=0.1  ,  UpperCAmelCase="gelu"  ,  UpperCAmelCase=0.02  ,  UpperCAmelCase=1e-5  ,  UpperCAmelCase=0.0  ,  UpperCAmelCase=None  ,  UpperCAmelCase=None  ,  **UpperCAmelCase  ,  )					->       str:
														super().__init__(**UpperCAmelCase  )

														_snake_case       =  patch_size
														_snake_case       =  num_channels
														_snake_case       =  embed_dim
														_snake_case       =  depths
														_snake_case       =  len(UpperCAmelCase  )
														_snake_case       =  num_heads
														_snake_case       =  kernel_size
														_snake_case       =  mlp_ratio
														_snake_case       =  qkv_bias
														_snake_case       =  hidden_dropout_prob
														_snake_case       =  attention_probs_dropout_prob
														_snake_case       =  drop_path_rate
														_snake_case       =  hidden_act
														_snake_case       =  layer_norm_eps
														_snake_case       =  initializer_range
														# we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel
														# this indicates the channel dimension after the last stage of the model
														_snake_case       =  int(embed_dim * 2 ** (len(UpperCAmelCase  ) - 1)  )
														_snake_case       =  layer_scale_init_value
														_snake_case       =  ["""stem"""] + [f"""stage{idx}""" for idx in range(1  ,  len(UpperCAmelCase  ) + 1  )]
														_snake_case,							_snake_case       =  get_aligned_output_features_output_indices(
														    out_features=UpperCAmelCase  ,  out_indices=UpperCAmelCase  ,  stage_names=self.stage_names  )