code: 
  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 ViltImageProcessor
  
  
  
  
  
  
  
  class 					UpperCAmelCase_				(							unittest.TestCase    ):
  
  
  
  
      def __init__(					self  :   List[str]      ,      lowerCAmelCase  :   Union[str, Any]      ,      lowerCAmelCase  :   Any=7      ,      lowerCAmelCase  :   Any=3      ,      lowerCAmelCase  :   Tuple=18      ,      lowerCAmelCase  :   Optional[int]=30      ,      lowerCAmelCase  :   Dict=400      ,      lowerCAmelCase  :   Optional[int]=True      ,      lowerCAmelCase  :   Dict=None      ,      lowerCAmelCase  :   Dict=2      ,      lowerCAmelCase  :   Dict=True      ,      lowerCAmelCase  :   Dict=[0.5, 0.5, 0.5]      ,      lowerCAmelCase  :   Any=[0.5, 0.5, 0.5]      ,      )	-> int:
        a__     		=     size if size is not None else {"shortest_edge": 30}
        a__     		=     parent
        a__     		=     batch_size
        a__     		=     num_channels
        a__     		=     image_size
        a__     		=     min_resolution
        a__     		=     max_resolution
        a__     		=     do_resize
        a__     		=     size
        a__     		=     size_divisor
        a__     		=     do_normalize
        a__     		=     image_mean
        a__     		=     image_std
  
  
  
  
      def 						__lowerCamelCase (					self  :   Optional[int]   )	-> Optional[int]:
        return {
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_normalize": self.do_normalize,
            "do_resize": self.do_resize,
            "size": self.size,
            "size_divisor": self.size_divisor,
        }
  
  
  
  
      def 						__lowerCamelCase (					self  :   List[str]      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   Tuple=False   )	-> str:
        if not batched:
          a__     		=     self.size["shortest_edge"]
          a__     		=     image_inputs[0]
          if isinstance(lowerCAmelCase      ,      Image.Image   ):
            a__		,a__     		=     image.size
          else:
            a__		,a__     		=     image.shape[1], image.shape[2]
          a__     		=     size / min(lowerCAmelCase      ,      lowerCAmelCase   )
          if h < w:
            a__		,a__     		=     size, scale * w
          else:
            a__		,a__     		=     scale * h, size
  
          a__     		=     int((1333 / 800) * size   )
          if max(lowerCAmelCase      ,      lowerCAmelCase   ) > max_size:
            a__     		=     max_size / max(lowerCAmelCase      ,      lowerCAmelCase   )
            a__     		=     newh * scale
            a__     		=     neww * scale
  
          a__		,a__     		=     int(newh + 0.5   ), int(neww + 0.5   )
          a__		,a__     		=     (
              newh // self.size_divisor * self.size_divisor,
              neww // self.size_divisor * self.size_divisor,
          )
  
        else:
          a__     		=     []
          for image in image_inputs:
            a__		,a__     		=     self.get_expected_values([image]   )
            expected_values.append((expected_height, expected_width)   )
          a__     		=     max(lowerCAmelCase      ,      key=lambda lowerCAmelCase   : item[0]   )[0]
          a__     		=     max(lowerCAmelCase      ,      key=lambda lowerCAmelCase   : item[1]   )[1]
  
        return expected_height, expected_width
  
  
  
  
  
  
  
  @require_torch
  @require_vision
  class 					UpperCAmelCase_				(							_a     ,     unittest.TestCase    ):
      UpperCAmelCase_			:       List[Any]						 =							ViltImageProcessor if is_vision_available() else None
  
  
  
  
      def 						__lowerCamelCase (					self  :   Optional[int]   )	-> List[str]:
        a__     		=     ViltImageProcessingTester(self   )
  
  
  
  
      @property
      def 						__lowerCamelCase (					self  :   List[str]   )	-> List[str]:
        return self.image_processor_tester.prepare_image_processor_dict()
  
  
  
  
      def 						__lowerCamelCase (					self  :   Tuple   )	-> List[str]:
        a__     		=     self.image_processing_class(**self.image_processor_dict   )
        self.assertTrue(hasattr(lowerCAmelCase      ,      "image_mean"   )   )
        self.assertTrue(hasattr(lowerCAmelCase      ,      "image_std"   )   )
        self.assertTrue(hasattr(lowerCAmelCase      ,      "do_normalize"   )   )
        self.assertTrue(hasattr(lowerCAmelCase      ,      "do_resize"   )   )
        self.assertTrue(hasattr(lowerCAmelCase      ,      "size"   )   )
        self.assertTrue(hasattr(lowerCAmelCase      ,      "size_divisor"   )   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   List[Any]   )	-> Union[str, Any]:
        a__     		=     self.image_processing_class.from_dict(self.image_processor_dict   )
        self.assertEqual(image_processor.size      ,      {"shortest_edge": 30}   )
  
        a__     		=     self.image_processing_class.from_dict(self.image_processor_dict      ,      size=42   )
        self.assertEqual(image_processor.size      ,      {"shortest_edge": 42}   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   Any   )	-> Dict:
        pass
  
  
  
  
      def 						__lowerCamelCase (					self  :   Optional[Any]   )	-> Optional[Any]:
        # Initialize image_processing
        a__     		=     self.image_processing_class(**self.image_processor_dict   )
        # create random PIL images
        a__     		=     prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowerCAmelCase   )
        for image in image_inputs:
          self.assertIsInstance(lowerCAmelCase      ,      Image.Image   )
  
        # Test not batched input
        a__     		=     image_processing(image_inputs[0]      ,      return_tensors="pt"   ).pixel_values
  
        a__		,a__     		=     self.image_processor_tester.get_expected_values(lowerCAmelCase   )
        self.assertEqual(
            encoded_images.shape      ,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)      ,      )
  
        # Test batched
        a__     		=     image_processing(lowerCAmelCase      ,      return_tensors="pt"   ).pixel_values
  
        a__		,a__     		=     self.image_processor_tester.get_expected_values(lowerCAmelCase      ,      batched=lowerCAmelCase   )
        self.assertEqual(
            encoded_images.shape      ,      (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )      ,      )
  
  
  
  
      def 						__lowerCamelCase (					self  :   Any   )	-> Union[str, Any]:
        # Initialize image_processing
        a__     		=     self.image_processing_class(**self.image_processor_dict   )
        # create random numpy tensors
        a__     		=     prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowerCAmelCase      ,      numpify=lowerCAmelCase   )
        for image in image_inputs:
          self.assertIsInstance(lowerCAmelCase      ,      np.ndarray   )
  
        # Test not batched input
        a__     		=     image_processing(image_inputs[0]      ,      return_tensors="pt"   ).pixel_values
  
        a__		,a__     		=     self.image_processor_tester.get_expected_values(lowerCAmelCase   )
        self.assertEqual(
            encoded_images.shape      ,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)      ,      )
  
        # Test batched
        a__     		=     image_processing(lowerCAmelCase      ,      return_tensors="pt"   ).pixel_values
  
        a__		,a__     		=     self.image_processor_tester.get_expected_values(lowerCAmelCase      ,      batched=lowerCAmelCase   )
        self.assertEqual(
            encoded_images.shape      ,      (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )      ,      )
  
  
  
  
      def 						__lowerCamelCase (					self  :   Tuple   )	-> Any:
        # Initialize image_processing
        a__     		=     self.image_processing_class(**self.image_processor_dict   )
        # create random PyTorch tensors
        a__     		=     prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowerCAmelCase      ,      torchify=lowerCAmelCase   )
        for image in image_inputs:
          self.assertIsInstance(lowerCAmelCase      ,      torch.Tensor   )
  
        # Test not batched input
        a__     		=     image_processing(image_inputs[0]      ,      return_tensors="pt"   ).pixel_values
  
        a__		,a__     		=     self.image_processor_tester.get_expected_values(lowerCAmelCase   )
        self.assertEqual(
            encoded_images.shape      ,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)      ,      )
  
        # Test batched
        a__     		=     image_processing(lowerCAmelCase      ,      return_tensors="pt"   ).pixel_values
  
        a__		,a__     		=     self.image_processor_tester.get_expected_values(lowerCAmelCase      ,      batched=lowerCAmelCase   )
        self.assertEqual(
            encoded_images.shape      ,      (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )      ,      )
  
  
style_context: import copy
  
  from ...configuration_utils import PretrainedConfig
  from ...utils import logging
  
  
  _SCREAMING_SNAKE_CASE   						=    logging.get_logger(__name__)
  
  _SCREAMING_SNAKE_CASE   						=    {
      """facebook/sam-vit-huge""": """https://huggingface.co/facebook/sam-vit-huge/resolve/main/config.json""",
      """facebook/sam-vit-large""": """https://huggingface.co/facebook/sam-vit-large/resolve/main/config.json""",
      """facebook/sam-vit-base""": """https://huggingface.co/facebook/sam-vit-base/resolve/main/config.json""",
  }
  
  
  
  
  
  
  
  class 					UpperCAmelCase_				(							_a    ):
  
  
  
  
      def __init__(					self  :   List[str]      ,      lowerCAmelCase  :   str=256      ,      lowerCAmelCase  :   Any=1024      ,      lowerCAmelCase  :   List[str]=16      ,      lowerCAmelCase  :   Union[str, Any]=16      ,      lowerCAmelCase  :   int=4      ,      lowerCAmelCase  :   List[Any]="gelu"      ,      lowerCAmelCase  :   str=1e-6      ,      **lowerCAmelCase  :   Union[str, Any]      ,      )	-> Union[str, Any]:
        super().__init__(**lowerCAmelCase   )
        a__     		=     hidden_size
        a__     		=     image_size
        a__     		=     patch_size
        a__     		=     image_size // patch_size
        a__     		=     mask_input_channels
        a__     		=     num_point_embeddings
        a__     		=     hidden_act
        a__     		=     layer_norm_eps
  
  
  
  
  
  
  
  class 					UpperCAmelCase_				(							_a    ):
  
  
  
  
      def __init__(					self  :   Dict      ,      lowerCAmelCase  :   str=256      ,      lowerCAmelCase  :   Union[str, Any]="relu"      ,      lowerCAmelCase  :   Optional[int]=2048      ,      lowerCAmelCase  :   Any=2      ,      lowerCAmelCase  :   Any=8      ,      lowerCAmelCase  :   Optional[Any]=2      ,      lowerCAmelCase  :   Optional[Any]=3      ,      lowerCAmelCase  :   Union[str, Any]=3      ,      lowerCAmelCase  :   List[str]=256      ,      lowerCAmelCase  :   Optional[Any]=1e-6      ,      **lowerCAmelCase  :   Any      ,      )	-> int:
        super().__init__(**lowerCAmelCase   )
        a__     		=     hidden_size
        a__     		=     hidden_act
        a__     		=     mlp_dim
        a__     		=     num_hidden_layers
        a__     		=     num_attention_heads
        a__     		=     attention_downsample_rate
        a__     		=     num_multimask_outputs
        a__     		=     iou_head_depth
        a__     		=     iou_head_hidden_dim
        a__     		=     layer_norm_eps
  
  
  
  
  
  
  
  class 					UpperCAmelCase_				(							_a    ):
  
  
  
  
      def __init__(					self  :   Optional[Any]      ,      lowerCAmelCase  :   Optional[Any]=768      ,      lowerCAmelCase  :   int=256      ,      lowerCAmelCase  :   Tuple=12      ,      lowerCAmelCase  :   List[str]=12      ,      lowerCAmelCase  :   Tuple=3      ,      lowerCAmelCase  :   Any=1024      ,      lowerCAmelCase  :   Dict=16      ,      lowerCAmelCase  :   Dict="gelu"      ,      lowerCAmelCase  :   List[Any]=1e-0_6      ,      lowerCAmelCase  :   Any=0.0      ,      lowerCAmelCase  :   Optional[Any]=1e-1_0      ,      lowerCAmelCase  :   Tuple=True      ,      lowerCAmelCase  :   Tuple=4.0      ,      lowerCAmelCase  :   Optional[Any]=True      ,      lowerCAmelCase  :   List[str]=True      ,      lowerCAmelCase  :   Dict=14      ,      lowerCAmelCase  :   Optional[int]=[2, 5, 8, 11]      ,      lowerCAmelCase  :   List[str]=128      ,      lowerCAmelCase  :   List[str]=None      ,      **lowerCAmelCase  :   str      ,      )	-> Optional[int]:
        super().__init__(**lowerCAmelCase   )
  
        a__     		=     hidden_size
        a__     		=     output_channels
        a__     		=     num_hidden_layers
        a__     		=     num_attention_heads
        a__     		=     num_channels
        a__     		=     image_size
        a__     		=     patch_size
        a__     		=     hidden_act
        a__     		=     layer_norm_eps
        a__     		=     attention_dropout
        a__     		=     initializer_range
        a__     		=     qkv_bias
        a__     		=     mlp_ratio
        a__     		=     use_abs_pos
        a__     		=     use_rel_pos
        a__     		=     window_size
        a__     		=     global_attn_indexes
        a__     		=     num_pos_feats
        a__     		=     int(hidden_size * mlp_ratio   ) if mlp_dim is None else mlp_dim
  
  
  
  
  
  
  
  class 					UpperCAmelCase_				(							_a    ):
      UpperCAmelCase_			:       Optional[Any]						 =							'sam'
      UpperCAmelCase_			:       List[Any]						 =							True
  
  
  
  
      def __init__(					self  :   Dict      ,      lowerCAmelCase  :   Any=None      ,      lowerCAmelCase  :   Optional[Any]=None      ,      lowerCAmelCase  :   Optional[Any]=None      ,      lowerCAmelCase  :   str=0.02      ,      **lowerCAmelCase  :   int      ,      )	-> Dict:
        super().__init__(**lowerCAmelCase   )
        a__     		=     vision_config if vision_config is not None else {}
        a__     		=     prompt_encoder_config if prompt_encoder_config is not None else {}
        a__     		=     mask_decoder_config if mask_decoder_config is not None else {}
  
        if isinstance(lowerCAmelCase      ,      lowerCAmelCase   ):
          a__     		=     vision_config.to_dict()
        if isinstance(lowerCAmelCase      ,      lowerCAmelCase   ):
          a__     		=     prompt_encoder_config.to_dict()
        if isinstance(lowerCAmelCase      ,      lowerCAmelCase   ):
          a__     		=     mask_decoder_config.to_dict()
  
        a__     		=     SamVisionConfig(**lowerCAmelCase   )
        a__     		=     SamPromptEncoderConfig(**lowerCAmelCase   )
        a__     		=     SamMaskDecoderConfig(**lowerCAmelCase   )
        a__     		=     initializer_range
  
  
  
  
      def 						__lowerCamelCase (					self  :   Optional[Any]   )	-> Union[str, Any]:
        a__     		=     copy.deepcopy(self.__dict__   )
        a__     		=     self.vision_config.to_dict()
        a__     		=     self.prompt_encoder_config.to_dict()
        a__     		=     self.mask_decoder_config.to_dict()
        a__     		=     self.__class__.model_type
        return output
  
  