






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 ConvNextImageProcessor



class 							A					( unittest.TestCase							):
					'''simple docstring'''






					def __init__(self					:		List[str]   ,      _UpperCAmelCase					:		Tuple   ,      _UpperCAmelCase					:		int=7   ,      _UpperCAmelCase					:		List[Any]=3   ,      _UpperCAmelCase					:		Tuple=18   ,      _UpperCAmelCase					:		str=30   ,      _UpperCAmelCase					:		Optional[Any]=400   ,      _UpperCAmelCase					:		List[Any]=True   ,      _UpperCAmelCase					:		Optional[Any]=None   ,      _UpperCAmelCase					:		List[str]=0.875   ,      _UpperCAmelCase					:		List[Any]=True   ,      _UpperCAmelCase					:		Dict=[0.5, 0.5, 0.5]   ,      _UpperCAmelCase					:		Any=[0.5, 0.5, 0.5]   ,      )		->     List[Any]:




							"""simple docstring"""


							lowercase__      =	size if size is not None else {"""shortest_edge""": 20}
							lowercase__      =	parent
							lowercase__      =	batch_size
							lowercase__      =	num_channels
							lowercase__      =	image_size
							lowercase__      =	min_resolution
							lowercase__      =	max_resolution
							lowercase__      =	do_resize
							lowercase__      =	size
							lowercase__      =	crop_pct
							lowercase__      =	do_normalize
							lowercase__      =	image_mean
							lowercase__      =	image_std


					def 						lowerCamelCase__  (self					:		Any	)		->     List[str]:




							"""simple docstring"""


							return {
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							    "do_normalize": self.do_normalize,
							    "do_resize": self.do_resize,
							    "size": self.size,
							    "crop_pct": self.crop_pct,
							}






@require_torch
@require_vision
class 							A					( UpperCAmelCase__				,						unittest.TestCase							):
					'''simple docstring'''






					A__    =				ConvNextImageProcessor if is_vision_available() else None

					def 						lowerCamelCase__  (self					:		Union[str, Any]	)		->     Any:




							"""simple docstring"""


							lowercase__      =	ConvNextImageProcessingTester(self	)

					@property
					def 						lowerCamelCase__  (self					:		str	)		->     str:




							"""simple docstring"""


							return self.image_processor_tester.prepare_image_processor_dict()

					def 						lowerCamelCase__  (self					:		List[Any]	)		->     Dict:




							"""simple docstring"""


							lowercase__      =	self.image_processing_class(**self.image_processor_dict	)
							self.assertTrue(hasattr(_UpperCAmelCase   ,      """do_resize"""	)	)
							self.assertTrue(hasattr(_UpperCAmelCase   ,      """size"""	)	)
							self.assertTrue(hasattr(_UpperCAmelCase   ,      """crop_pct"""	)	)
							self.assertTrue(hasattr(_UpperCAmelCase   ,      """do_normalize"""	)	)
							self.assertTrue(hasattr(_UpperCAmelCase   ,      """image_mean"""	)	)
							self.assertTrue(hasattr(_UpperCAmelCase   ,      """image_std"""	)	)

					def 						lowerCamelCase__  (self					:		Dict	)		->     Union[str, Any]:




							"""simple docstring"""


							lowercase__      =	self.image_processing_class.from_dict(self.image_processor_dict	)
							self.assertEqual(image_processor.size   ,      {"""shortest_edge""": 20}	)

							lowercase__      =	self.image_processing_class.from_dict(self.image_processor_dict   ,      size=42	)
							self.assertEqual(image_processor.size   ,      {"""shortest_edge""": 42}	)

					def 						lowerCamelCase__  (self					:		int	)		->     Dict:




							"""simple docstring"""


							pass

					def 						lowerCamelCase__  (self					:		Union[str, Any]	)		->     List[str]:




							"""simple docstring"""


							lowercase__      =	self.image_processing_class(**self.image_processor_dict	)
							# create random PIL images
							lowercase__      =	prepare_image_inputs(self.image_processor_tester   ,      equal_resolution=_UpperCAmelCase	)
							for image in image_inputs:
									self.assertIsInstance(_UpperCAmelCase   ,      Image.Image	)

							# Test not batched input
							lowercase__      =	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.size["""shortest_edge"""],
							        self.image_processor_tester.size["""shortest_edge"""],
							    )   ,      )

							# Test batched
							lowercase__      =	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.size["""shortest_edge"""],
							        self.image_processor_tester.size["""shortest_edge"""],
							    )   ,      )

					def 						lowerCamelCase__  (self					:		Dict	)		->     Optional[Any]:




							"""simple docstring"""


							lowercase__      =	self.image_processing_class(**self.image_processor_dict	)
							# create random numpy tensors
							lowercase__      =	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
							lowercase__      =	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.size["""shortest_edge"""],
							        self.image_processor_tester.size["""shortest_edge"""],
							    )   ,      )

							# Test batched
							lowercase__      =	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.size["""shortest_edge"""],
							        self.image_processor_tester.size["""shortest_edge"""],
							    )   ,      )


					def 						lowerCamelCase__  (self					:		str	)		->     Dict:




							"""simple docstring"""


							lowercase__      =	self.image_processing_class(**self.image_processor_dict	)
							# create random PyTorch tensors
							lowercase__      =	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
							lowercase__      =	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.size["""shortest_edge"""],
							        self.image_processor_tester.size["""shortest_edge"""],
							    )   ,      )

							# Test batched
							lowercase__      =	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.size["""shortest_edge"""],
							        self.image_processor_tester.size["""shortest_edge"""],
							    )   ,      )





import copy

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


A :	Optional[Any]												=     logging.get_logger(__name__)

A :	Union[str, Any]												=     {
    '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 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self					:		Dict   ,      _UpperCAmelCase					:		List[str]=256   ,      _UpperCAmelCase					:		Tuple=1024   ,      _UpperCAmelCase					:		List[str]=16   ,      _UpperCAmelCase					:		Optional[Any]=16   ,      _UpperCAmelCase					:		Union[str, Any]=4   ,      _UpperCAmelCase					:		str="gelu"   ,      _UpperCAmelCase					:		List[str]=1E-6   ,      **_UpperCAmelCase					:		List[Any]   ,      )		->     Tuple:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase	)
							lowercase__      =	hidden_size
							lowercase__      =	image_size
							lowercase__      =	patch_size
							lowercase__      =	image_size // patch_size
							lowercase__      =	mask_input_channels
							lowercase__      =	num_point_embeddings
							lowercase__      =	hidden_act
							lowercase__      =	layer_norm_eps






class 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self					:		List[str]   ,      _UpperCAmelCase					:		Optional[int]=256   ,      _UpperCAmelCase					:		int="relu"   ,      _UpperCAmelCase					:		Union[str, Any]=2048   ,      _UpperCAmelCase					:		int=2   ,      _UpperCAmelCase					:		Optional[Any]=8   ,      _UpperCAmelCase					:		Any=2   ,      _UpperCAmelCase					:		Tuple=3   ,      _UpperCAmelCase					:		List[str]=3   ,      _UpperCAmelCase					:		List[str]=256   ,      _UpperCAmelCase					:		Tuple=1E-6   ,      **_UpperCAmelCase					:		int   ,      )		->     Tuple:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase	)
							lowercase__      =	hidden_size
							lowercase__      =	hidden_act
							lowercase__      =	mlp_dim
							lowercase__      =	num_hidden_layers
							lowercase__      =	num_attention_heads
							lowercase__      =	attention_downsample_rate
							lowercase__      =	num_multimask_outputs
							lowercase__      =	iou_head_depth
							lowercase__      =	iou_head_hidden_dim
							lowercase__      =	layer_norm_eps






class 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self					:		Optional[int]   ,      _UpperCAmelCase					:		List[Any]=768   ,      _UpperCAmelCase					:		Union[str, Any]=256   ,      _UpperCAmelCase					:		Union[str, Any]=12   ,      _UpperCAmelCase					:		Optional[Any]=12   ,      _UpperCAmelCase					:		Tuple=3   ,      _UpperCAmelCase					:		Optional[int]=1024   ,      _UpperCAmelCase					:		str=16   ,      _UpperCAmelCase					:		Tuple="gelu"   ,      _UpperCAmelCase					:		Any=1E-0_6   ,      _UpperCAmelCase					:		Any=0.0   ,      _UpperCAmelCase					:		str=1E-1_0   ,      _UpperCAmelCase					:		Tuple=True   ,      _UpperCAmelCase					:		Tuple=4.0   ,      _UpperCAmelCase					:		Optional[Any]=True   ,      _UpperCAmelCase					:		Optional[int]=True   ,      _UpperCAmelCase					:		Optional[Any]=14   ,      _UpperCAmelCase					:		Optional[int]=[2, 5, 8, 11]   ,      _UpperCAmelCase					:		List[Any]=128   ,      _UpperCAmelCase					:		int=None   ,      **_UpperCAmelCase					:		int   ,      )		->     Dict:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase	)

							lowercase__      =	hidden_size
							lowercase__      =	output_channels
							lowercase__      =	num_hidden_layers
							lowercase__      =	num_attention_heads
							lowercase__      =	num_channels
							lowercase__      =	image_size
							lowercase__      =	patch_size
							lowercase__      =	hidden_act
							lowercase__      =	layer_norm_eps
							lowercase__      =	attention_dropout
							lowercase__      =	initializer_range
							lowercase__      =	qkv_bias
							lowercase__      =	mlp_ratio
							lowercase__      =	use_abs_pos
							lowercase__      =	use_rel_pos
							lowercase__      =	window_size
							lowercase__      =	global_attn_indexes
							lowercase__      =	num_pos_feats
							lowercase__      =	int(hidden_size * mlp_ratio	) if mlp_dim is None else mlp_dim






class 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					A__    =				'''sam'''
					A__    =				True

					def __init__(self					:		List[str]   ,      _UpperCAmelCase					:		Any=None   ,      _UpperCAmelCase					:		List[Any]=None   ,      _UpperCAmelCase					:		List[Any]=None   ,      _UpperCAmelCase					:		List[Any]=0.02   ,      **_UpperCAmelCase					:		Optional[Any]   ,      )		->     Dict:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase	)
							lowercase__      =	vision_config if vision_config is not None else {}
							lowercase__      =	prompt_encoder_config if prompt_encoder_config is not None else {}
							lowercase__      =	mask_decoder_config if mask_decoder_config is not None else {}

							if isinstance(_UpperCAmelCase   ,      _UpperCAmelCase	):
									lowercase__      =	vision_config.to_dict()
							if isinstance(_UpperCAmelCase   ,      _UpperCAmelCase	):
									lowercase__      =	prompt_encoder_config.to_dict()
							if isinstance(_UpperCAmelCase   ,      _UpperCAmelCase	):
									lowercase__      =	mask_decoder_config.to_dict()

							lowercase__      =	SamVisionConfig(**_UpperCAmelCase	)
							lowercase__      =	SamPromptEncoderConfig(**_UpperCAmelCase	)
							lowercase__      =	SamMaskDecoderConfig(**_UpperCAmelCase	)
							lowercase__      =	initializer_range


					def 						lowerCamelCase__  (self					:		Union[str, Any]	)		->     Tuple:




							"""simple docstring"""


							lowercase__      =	copy.deepcopy(self.__dict__	)
							lowercase__      =	self.vision_config.to_dict()
							lowercase__      =	self.prompt_encoder_config.to_dict()
							lowercase__      =	self.mask_decoder_config.to_dict()
							lowercase__      =	self.__class__.model_type
							return output

