





import unittest

import numpy as np

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 MobileNetVaImageProcessor


class        __SCREAMING_SNAKE_CASE      (			unittest.TestCase ):







       def __init__(				self					:     Any		, A					:     int		, A					:     Optional[int]=7		, A					:     Dict=3		, A					:     Any=1_8		, A					:     List[str]=3_0		, A					:     int=4_0_0		, A					:     Any=True		, A					:     List[str]=None		, A					:     Optional[int]=True		, A					:     Union[str, Any]=None		, )       ->Optional[int]:
          lowerCamelCase__       :						Dict        =							size if size is not None else {'''shortest_edge''': 2_0}
          lowerCamelCase__       :						str        =							crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}
          lowerCamelCase__       :						Dict        =							parent
          lowerCamelCase__       :						int        =							batch_size
          lowerCamelCase__       :						List[str]        =							num_channels
          lowerCamelCase__       :						Dict        =							image_size
          lowerCamelCase__       :						Optional[Any]        =							min_resolution
          lowerCamelCase__       :						Optional[int]        =							max_resolution
          lowerCamelCase__       :						Union[str, Any]        =							do_resize
          lowerCamelCase__       :						Union[str, Any]        =							size
          lowerCamelCase__       :						Optional[int]        =							do_center_crop
          lowerCamelCase__       :						List[Any]        =							crop_size







       def 						__lowerCamelCase   (				self					:     Optional[int]      )       ->Optional[Any]:
          return {
              "do_resize": self.do_resize,
              "size": self.size,
              "do_center_crop": self.do_center_crop,
              "crop_size": self.crop_size,
          }


@require_torch
@require_vision
class        __SCREAMING_SNAKE_CASE      (			lowerCAmelCase_					,unittest.TestCase ):
       _UpperCAmelCase				:  str			   =   MobileNetVaImageProcessor if is_vision_available() else None







       def 						__lowerCamelCase   (				self					:     Any      )       ->Optional[Any]:
          lowerCamelCase__       :						Optional[int]        =							MobileNetVaImageProcessingTester(self      )







       @property
       def 						__lowerCamelCase   (				self					:     str      )       ->Optional[int]:
          return self.image_processor_tester.prepare_image_processor_dict()







       def 						__lowerCamelCase   (				self					:     str      )       ->Optional[Any]:
          lowerCamelCase__       :						Any        =							self.image_processing_class(**self.image_processor_dict      )
          self.assertTrue(hasattr(A		, '''do_resize'''      )      )
          self.assertTrue(hasattr(A		, '''size'''      )      )
          self.assertTrue(hasattr(A		, '''do_center_crop'''      )      )
          self.assertTrue(hasattr(A		, '''crop_size'''      )      )







       def 						__lowerCamelCase   (				self					:     Union[str, Any]      )       ->Dict:
          lowerCamelCase__       :						Optional[int]        =							self.image_processing_class.from_dict(self.image_processor_dict      )
          self.assertEqual(image_processor.size		, {'''shortest_edge''': 2_0}      )
          self.assertEqual(image_processor.crop_size		, {'''height''': 1_8, '''width''': 1_8}      )

          lowerCamelCase__       :						List[str]        =							self.image_processing_class.from_dict(self.image_processor_dict		, size=4_2		, crop_size=8_4      )
          self.assertEqual(image_processor.size		, {'''shortest_edge''': 4_2}      )
          self.assertEqual(image_processor.crop_size		, {'''height''': 8_4, '''width''': 8_4}      )







       def 						__lowerCamelCase   (				self					:     Tuple      )       ->Any:
          pass







       def 						__lowerCamelCase   (				self					:     Union[str, Any]      )       ->Tuple:
          # Initialize image_processing
          lowerCamelCase__       :						Optional[Any]        =							self.image_processing_class(**self.image_processor_dict      )
          # create random PIL images
          lowerCamelCase__       :						Union[str, Any]        =							prepare_image_inputs(self.image_processor_tester		, equal_resolution=A      )
          for image in image_inputs:
             self.assertIsInstance(A		, Image.Image      )

          # Test not batched input
          lowerCamelCase__       :						Dict        =							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
          lowerCamelCase__       :						Dict        =							image_processing(A		, 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 						__lowerCamelCase   (				self					:     List[str]      )       ->List[Any]:
          # Initialize image_processing
          lowerCamelCase__       :						List[Any]        =							self.image_processing_class(**self.image_processor_dict      )
          # create random numpy tensors
          lowerCamelCase__       :						Union[str, Any]        =							prepare_image_inputs(self.image_processor_tester		, equal_resolution=A		, numpify=A      )
          for image in image_inputs:
             self.assertIsInstance(A		, np.ndarray      )

          # Test not batched input
          lowerCamelCase__       :						List[Any]        =							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
          lowerCamelCase__       :						List[str]        =							image_processing(A		, 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 						__lowerCamelCase   (				self					:     Optional[Any]      )       ->Any:
          # Initialize image_processing
          lowerCamelCase__       :						Any        =							self.image_processing_class(**self.image_processor_dict      )
          # create random PyTorch tensors
          lowerCamelCase__       :						List[str]        =							prepare_image_inputs(self.image_processor_tester		, equal_resolution=A		, torchify=A      )
          for image in image_inputs:
             self.assertIsInstance(A		, torch.Tensor      )

          # Test not batched input
          lowerCamelCase__       :						Tuple        =							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
          lowerCamelCase__       :						str        =							image_processing(A		, 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'''],
              )		, )










from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_A :   Tuple  			=  logging.get_logger(__name__)

_A :   List[str]  			=  {
    '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        __SCREAMING_SNAKE_CASE      (			lowerCAmelCase_					,lowerCAmelCase_ ):
       _UpperCAmelCase				:  Dict			   =   "nat"

       _UpperCAmelCase				:  Tuple			   =   {
           "num_attention_heads": "num_heads",
           "num_hidden_layers": "num_layers",
       }







       def __init__(				self					:     List[Any]		, A					:     Union[str, Any]=4		, A					:     str=3		, A					:     List[Any]=6_4		, A					:     Optional[Any]=[3, 4, 6, 5]		, A					:     int=[2, 4, 8, 1_6]		, A					:     Optional[int]=7		, A					:     List[Any]=3.0		, A					:     str=True		, A					:     str=0.0		, A					:     Any=0.0		, A					:     int=0.1		, A					:     Tuple="gelu"		, A					:     List[Any]=0.02		, A					:     str=1e-5		, A					:     Optional[int]=0.0		, A					:     Optional[Any]=None		, A					:     Dict=None		, **A					:     str		, )       ->Union[str, Any]:
          super().__init__(**A      )

          lowerCamelCase__       :						Any        =							patch_size
          lowerCamelCase__       :						Optional[Any]        =							num_channels
          lowerCamelCase__       :						Any        =							embed_dim
          lowerCamelCase__       :						str        =							depths
          lowerCamelCase__       :						Union[str, Any]        =							len(A      )
          lowerCamelCase__       :						int        =							num_heads
          lowerCamelCase__       :						Optional[int]        =							kernel_size
          lowerCamelCase__       :						Optional[int]        =							mlp_ratio
          lowerCamelCase__       :						List[Any]        =							qkv_bias
          lowerCamelCase__       :						Tuple        =							hidden_dropout_prob
          lowerCamelCase__       :						List[str]        =							attention_probs_dropout_prob
          lowerCamelCase__       :						List[Any]        =							drop_path_rate
          lowerCamelCase__       :						Union[str, Any]        =							hidden_act
          lowerCamelCase__       :						Union[str, Any]        =							layer_norm_eps
          lowerCamelCase__       :						List[Any]        =							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
          lowerCamelCase__       :						str        =							int(embed_dim * 2 ** (len(A      ) - 1)      )
          lowerCamelCase__       :						Dict        =							layer_scale_init_value
          lowerCamelCase__       :						Dict        =							['''stem'''] + [F"stage{idx}" for idx in range(1		, len(A      ) + 1      )]
          lowerCamelCase__       ,							lowerCamelCase__       :						str        =							get_aligned_output_features_output_indices(
              out_features=A		, out_indices=A		, stage_names=self.stage_names      )



