
import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__snake_case					:							str           =							logging.get_logger(__name__)

__snake_case					:							int           =							{
    '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__       (  UpperCAmelCase__):

						'''simple docstring'''



						def __init__(  self  , A=2_56  , A=10_24  , A=16  , A=16  , A=4  , A="gelu"  , A=1e-6  , **A  , )  ->Optional[int]:
									super().__init__(**A	)
									UpperCAmelCase__		:List[str] 		=    hidden_size
									UpperCAmelCase__		:List[str] 		=    image_size
									UpperCAmelCase__		:Optional[int] 		=    patch_size
									UpperCAmelCase__		:Any 		=    image_size // patch_size
									UpperCAmelCase__		:Dict 		=    mask_input_channels
									UpperCAmelCase__		:List[str] 		=    num_point_embeddings
									UpperCAmelCase__		:str 		=    hidden_act
									UpperCAmelCase__		:Dict 		=    layer_norm_eps


class 	UpperCamelCase__       (  UpperCAmelCase__):

						'''simple docstring'''



						def __init__(  self  , A=2_56  , A="relu"  , A=20_48  , A=2  , A=8  , A=2  , A=3  , A=3  , A=2_56  , A=1e-6  , **A  , )  ->str:
									super().__init__(**A	)
									UpperCAmelCase__		:Optional[Any] 		=    hidden_size
									UpperCAmelCase__		:Any 		=    hidden_act
									UpperCAmelCase__		:Union[str, Any] 		=    mlp_dim
									UpperCAmelCase__		:Any 		=    num_hidden_layers
									UpperCAmelCase__		:Optional[int] 		=    num_attention_heads
									UpperCAmelCase__		:Optional[int] 		=    attention_downsample_rate
									UpperCAmelCase__		:List[Any] 		=    num_multimask_outputs
									UpperCAmelCase__		:Union[str, Any] 		=    iou_head_depth
									UpperCAmelCase__		:List[Any] 		=    iou_head_hidden_dim
									UpperCAmelCase__		:Optional[int] 		=    layer_norm_eps


class 	UpperCamelCase__       (  UpperCAmelCase__):

						'''simple docstring'''



						def __init__(  self  , A=7_68  , A=2_56  , A=12  , A=12  , A=3  , A=10_24  , A=16  , A="gelu"  , A=1e-06  , A=0.0  , A=1e-10  , A=True  , A=4.0  , A=True  , A=True  , A=14  , A=[2, 5, 8, 11]  , A=1_28  , A=None  , **A  , )  ->Optional[int]:
									super().__init__(**A	)

									UpperCAmelCase__		:Optional[Any] 		=    hidden_size
									UpperCAmelCase__		:Tuple 		=    output_channels
									UpperCAmelCase__		:Union[str, Any] 		=    num_hidden_layers
									UpperCAmelCase__		:str 		=    num_attention_heads
									UpperCAmelCase__		:Any 		=    num_channels
									UpperCAmelCase__		:Optional[int] 		=    image_size
									UpperCAmelCase__		:List[str] 		=    patch_size
									UpperCAmelCase__		:Any 		=    hidden_act
									UpperCAmelCase__		:Tuple 		=    layer_norm_eps
									UpperCAmelCase__		:Optional[Any] 		=    attention_dropout
									UpperCAmelCase__		:Optional[int] 		=    initializer_range
									UpperCAmelCase__		:Optional[Any] 		=    qkv_bias
									UpperCAmelCase__		:List[str] 		=    mlp_ratio
									UpperCAmelCase__		:Any 		=    use_abs_pos
									UpperCAmelCase__		:Optional[int] 		=    use_rel_pos
									UpperCAmelCase__		:Optional[int] 		=    window_size
									UpperCAmelCase__		:Optional[Any] 		=    global_attn_indexes
									UpperCAmelCase__		:List[Any] 		=    num_pos_feats
									UpperCAmelCase__		:Optional[int] 		=    int(hidden_size * mlp_ratio	) if mlp_dim is None else mlp_dim


class 	UpperCamelCase__       (  UpperCAmelCase__):

						'''simple docstring'''



						__a     :    List[Any]												=			"""sam"""
						__a     :    int												=			True


						def __init__(  self  , A=None  , A=None  , A=None  , A=0.02  , **A  , )  ->str:
									super().__init__(**A	)
									UpperCAmelCase__		:Optional[int] 		=    vision_config if vision_config is not None else {}
									UpperCAmelCase__		:str 		=    prompt_encoder_config if prompt_encoder_config is not None else {}
									UpperCAmelCase__		:Any 		=    mask_decoder_config if mask_decoder_config is not None else {}

									if isinstance(A  , A	):
												UpperCAmelCase__		:List[Any] 		=    vision_config.to_dict()
									if isinstance(A  , A	):
												UpperCAmelCase__		:int 		=    prompt_encoder_config.to_dict()
									if isinstance(A  , A	):
												UpperCAmelCase__		:List[Any] 		=    mask_decoder_config.to_dict()

									UpperCAmelCase__		:int 		=    SamVisionConfig(**A	)
									UpperCAmelCase__		:Union[str, Any] 		=    SamPromptEncoderConfig(**A	)
									UpperCAmelCase__		:Dict 		=    SamMaskDecoderConfig(**A	)
									UpperCAmelCase__		:Any 		=    initializer_range


						def    A__       (  self	)  ->Any:
									UpperCAmelCase__		:int 		=    copy.deepcopy(self.__dict__	)
									UpperCAmelCase__		:Optional[Any] 		=    self.vision_config.to_dict()
									UpperCAmelCase__		:Dict 		=    self.prompt_encoder_config.to_dict()
									UpperCAmelCase__		:List[str] 		=    self.mask_decoder_config.to_dict()
									UpperCAmelCase__		:Tuple 		=    self.__class__.model_type
									return output







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 DeiTImageProcessor


class 	UpperCamelCase__       (  unittest.TestCase):

						'''simple docstring'''



						def __init__(  self  , A  , A=7  , A=3  , A=18  , A=30  , A=4_00  , A=True  , A=None  , A=True  , A=None  , A=True  , A=[0.5, 0.5, 0.5]  , A=[0.5, 0.5, 0.5]  , )  ->Any:
									UpperCAmelCase__		:Optional[int] 		=    size if size is not None else {'height': 20, 'width': 20}
									UpperCAmelCase__		:str 		=    crop_size if crop_size is not None else {'height': 18, 'width': 18}

									UpperCAmelCase__		:Dict 		=    parent
									UpperCAmelCase__		:Optional[Any] 		=    batch_size
									UpperCAmelCase__		:Optional[Any] 		=    num_channels
									UpperCAmelCase__		:str 		=    image_size
									UpperCAmelCase__		:Optional[Any] 		=    min_resolution
									UpperCAmelCase__		:Optional[int] 		=    max_resolution
									UpperCAmelCase__		:Dict 		=    do_resize
									UpperCAmelCase__		:Any 		=    size
									UpperCAmelCase__		:Any 		=    do_center_crop
									UpperCAmelCase__		:Optional[int] 		=    crop_size
									UpperCAmelCase__		:Dict 		=    do_normalize
									UpperCAmelCase__		:List[Any] 		=    image_mean
									UpperCAmelCase__		:Tuple 		=    image_std


						def    A__       (  self	)  ->str:
									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,
									}


@require_torch
@require_vision
class 	UpperCamelCase__       (  UpperCAmelCase__						,			unittest.TestCase):

						'''simple docstring'''



						__a     :    Tuple												=			DeiTImageProcessor if is_vision_available() else None
						__a     :    Tuple												=			True


						def    A__       (  self	)  ->Optional[int]:
									UpperCAmelCase__		:int 		=    DeiTImageProcessingTester(self	)


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


						def    A__       (  self	)  ->Optional[Any]:
									UpperCAmelCase__		:Tuple 		=    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  , 'center_crop'	)	)
									self.assertTrue(hasattr(A  , 'do_normalize'	)	)
									self.assertTrue(hasattr(A  , 'image_mean'	)	)
									self.assertTrue(hasattr(A  , 'image_std'	)	)


						def    A__       (  self	)  ->Tuple:
									UpperCAmelCase__		:Union[str, Any] 		=    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}	)

									UpperCAmelCase__		:List[Any] 		=    self.image_processing_class.from_dict(self.image_processor_dict  , size=42  , crop_size=84	)
									self.assertEqual(image_processor.size  , {'height': 42, 'width': 42}	)
									self.assertEqual(image_processor.crop_size  , {'height': 84, 'width': 84}	)


						def    A__       (  self	)  ->List[str]:
									pass


						def    A__       (  self	)  ->List[str]:
									# Initialize image_processing
									UpperCAmelCase__		:int 		=    self.image_processing_class(**self.image_processor_dict	)
									# create random PIL images
									UpperCAmelCase__		:Tuple 		=    prepare_image_inputs(self.image_processor_tester  , equal_resolution=A	)
									for image in image_inputs:
												self.assertIsInstance(A  , Image.Image	)

									# Test not batched input
									UpperCAmelCase__		:int 		=    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
									UpperCAmelCase__		: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    A__       (  self	)  ->str:
									# Initialize image_processing
									UpperCAmelCase__		:Tuple 		=    self.image_processing_class(**self.image_processor_dict	)
									# create random numpy tensors
									UpperCAmelCase__		:Dict 		=    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
									UpperCAmelCase__		:List[str] 		=    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
									UpperCAmelCase__		:Optional[Any] 		=    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    A__       (  self	)  ->Tuple:
									# Initialize image_processing
									UpperCAmelCase__		:List[Any] 		=    self.image_processing_class(**self.image_processor_dict	)
									# create random PyTorch tensors
									UpperCAmelCase__		: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
									UpperCAmelCase__		:List[str] 		=    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
									UpperCAmelCase__		:Optional[int] 		=    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'],
									    )  , )





