




from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
				import PIL


lowercase_			:       Dict						       =				logging.get_logger(__name__)






class 					__lowerCAmelCase				( UpperCAmelCase__				):
			snake_case_   :				Optional[Any]				  =  ["pixel_values"]







			def __init__( self					:    Union[str, Any]					,					snake_case__					:    bool = True					,					snake_case__					:    Dict[str, int] = None					,					snake_case__					:    bool = True					,					snake_case__					:    Dict[str, int] = None					,					snake_case__					:    PILImageResampling = PILImageResampling.BICUBIC					,					snake_case__					:    bool = True					,					snake_case__					:    Union[int, float] = 1 / 255					,					snake_case__					:    bool = True					,					snake_case__					:    Optional[Union[float, List[float]]] = None					,					snake_case__					:    Optional[Union[float, List[float]]] = None					,					**snake_case__					:    Tuple					,					):




									"""simple docstring"""


									super().__init__(**snake_case__   )
									_UpperCAmelCase									=  crop_size if crop_size is not None else {"height": 256, "width": 256}
									_UpperCAmelCase									=  get_size_dict(snake_case__					,					param_name="crop_size"   )
									_UpperCAmelCase									=  size if size is not None else {"height": 224, "width": 224}
									_UpperCAmelCase									=  get_size_dict(snake_case__   )

									_UpperCAmelCase									=  do_center_crop
									_UpperCAmelCase									=  crop_size
									_UpperCAmelCase									=  do_resize
									_UpperCAmelCase									=  size
									_UpperCAmelCase									=  resample
									_UpperCAmelCase									=  do_rescale
									_UpperCAmelCase									=  rescale_factor
									_UpperCAmelCase									=  do_normalize
									_UpperCAmelCase									=  image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
									_UpperCAmelCase									=  image_std if image_std is not None else IMAGENET_DEFAULT_STD







			def UpperCamelCase    ( self					:    List[Any]					,					snake_case__					:    np.ndarray					,					snake_case__					:    Dict[str, int]					,					snake_case__					:    Optional[int] = None					,					snake_case__					:    Optional[Union[str, ChannelDimension]] = None					,					**snake_case__					:    str					,					):




									"""simple docstring"""


									_UpperCAmelCase									=  self.size if size is None else size
									_UpperCAmelCase									=  get_size_dict(snake_case__   )
									_UpperCAmelCase									=  get_size_dict(snake_case__					,					param_name="crop_size"   )

									_UpperCAmelCase							,				_UpperCAmelCase									=  get_image_size(snake_case__   )
									_UpperCAmelCase									=  min(snake_case__					,					snake_case__   )
									_UpperCAmelCase									=  (size["height"] / crop_size["height"]) * min_dim
									_UpperCAmelCase									=  (size["width"] / crop_size["width"]) * min_dim
									return center_crop(snake_case__					,					size=(cropped_height, cropped_width)					,					data_format=snake_case__					,					**snake_case__   )







			def UpperCamelCase    ( self					:    Dict					,					snake_case__					:    np.ndarray					,					snake_case__					:    Dict[str, int]					,					snake_case__					:    PILImageResampling = PIL.Image.BICUBIC					,					snake_case__					:    Optional[Union[str, ChannelDimension]] = None					,					**snake_case__					:    Optional[int]					,					):




									"""simple docstring"""


									_UpperCAmelCase									=  get_size_dict(snake_case__   )
									if "height" not in size or "width" not in size:
															raise ValueError(F"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}"""   )
									return resize(
									    snake_case__					,					size=(size["height"], size["width"])					,					resample=snake_case__					,					data_format=snake_case__					,					**snake_case__   )







			def UpperCamelCase    ( self					:    List[Any]					,					snake_case__					:    np.ndarray					,					snake_case__					:    Union[int, float]					,					snake_case__					:    Optional[Union[str, ChannelDimension]] = None					,					**snake_case__					:    List[str]					,					):




									"""simple docstring"""


									return rescale(snake_case__					,					scale=snake_case__					,					data_format=snake_case__					,					**snake_case__   )







			def UpperCamelCase    ( self					:    Optional[int]					,					snake_case__					:    np.ndarray					,					snake_case__					:    Union[float, List[float]]					,					snake_case__					:    Union[float, List[float]]					,					snake_case__					:    Optional[Union[str, ChannelDimension]] = None					,					**snake_case__					:    int					,					):




									"""simple docstring"""


									return normalize(snake_case__					,					mean=snake_case__					,					std=snake_case__					,					data_format=snake_case__					,					**snake_case__   )







			def UpperCamelCase    ( self					:    Optional[int]					,					snake_case__					:    ImageInput					,					snake_case__					:    Optional[bool] = None					,					snake_case__					:    Optional[Dict[str, int]] = None					,					snake_case__					:    Optional[bool] = None					,					snake_case__					:    Optional[Dict[str, int]] = None					,					snake_case__					:    PILImageResampling = None					,					snake_case__					:    Optional[bool] = None					,					snake_case__					:    Optional[float] = None					,					snake_case__					:    Optional[bool] = None					,					snake_case__					:    Optional[Union[float, List[float]]] = None					,					snake_case__					:    Optional[Union[float, List[float]]] = None					,					snake_case__					:    Optional[Union[str, TensorType]] = None					,					snake_case__					:    ChannelDimension = ChannelDimension.FIRST					,					**snake_case__					:    str					,					):




									"""simple docstring"""


									_UpperCAmelCase									=  do_center_crop if do_center_crop is not None else self.do_center_crop
									_UpperCAmelCase									=  crop_size if crop_size is not None else self.crop_size
									_UpperCAmelCase									=  get_size_dict(snake_case__					,					param_name="crop_size"   )
									_UpperCAmelCase									=  do_resize if do_resize is not None else self.do_resize
									_UpperCAmelCase									=  size if size is not None else self.size
									_UpperCAmelCase									=  get_size_dict(snake_case__   )
									_UpperCAmelCase									=  resample if resample is not None else self.resample
									_UpperCAmelCase									=  do_rescale if do_rescale is not None else self.do_rescale
									_UpperCAmelCase									=  rescale_factor if rescale_factor is not None else self.rescale_factor
									_UpperCAmelCase									=  do_normalize if do_normalize is not None else self.do_normalize
									_UpperCAmelCase									=  image_mean if image_mean is not None else self.image_mean
									_UpperCAmelCase									=  image_std if image_std is not None else self.image_std

									_UpperCAmelCase									=  make_list_of_images(snake_case__   )

									if not valid_images(snake_case__   ):
															raise ValueError(
															    "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
															    "torch.Tensor, tf.Tensor or jax.ndarray."   )

									if do_center_crop and crop_size is None:
															raise ValueError("If `do_center_crop` is set to `True`, `crop_size` must be provided."   )

									if do_resize and size is None:
															raise ValueError("Size must be specified if do_resize is True."   )

									if do_rescale and rescale_factor is None:
															raise ValueError("Rescale factor must be specified if do_rescale is True."   )

									if do_normalize and (image_mean is None or image_std is None):
															raise ValueError("Image mean and image standard deviation must be specified if do_normalize is True."   )

									# All transformations expect numpy arrays.
									_UpperCAmelCase									=  [to_numpy_array(snake_case__   ) for image in images]

									if do_center_crop:
															_UpperCAmelCase									=  [self.center_crop(snake_case__					,					snake_case__					,					size=snake_case__   ) for image in images]

									if do_resize:
															_UpperCAmelCase									=  [self.resize(image=snake_case__					,					size=snake_case__					,					resample=snake_case__   ) for image in images]

									if do_rescale:
															_UpperCAmelCase									=  [self.rescale(image=snake_case__					,					scale=snake_case__   ) for image in images]

									if do_normalize:
															_UpperCAmelCase									=  [self.normalize(image=snake_case__					,					mean=snake_case__					,					std=snake_case__   ) for image in images]

									_UpperCAmelCase									=  [to_channel_dimension_format(snake_case__					,					snake_case__   ) for image in images]

									_UpperCAmelCase									=  {"pixel_values": images}
									return BatchFeature(data=snake_case__					,					tensor_type=snake_case__   )










import copy

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


lowercase_			:       Tuple						       =				logging.get_logger(__name__)

lowercase_			:       List[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 					__lowerCAmelCase				( UpperCAmelCase__				):







			def __init__( self					:    Optional[int]					,					snake_case__					:    int=256					,					snake_case__					:    Dict=1_024					,					snake_case__					:    Dict=16					,					snake_case__					:    Union[str, Any]=16					,					snake_case__					:    Any=4					,					snake_case__					:    Tuple="gelu"					,					snake_case__					:    Tuple=1e-6					,					**snake_case__					:    str					,					):




									"""simple docstring"""


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






class 					__lowerCAmelCase				( UpperCAmelCase__				):







			def __init__( self					:    str					,					snake_case__					:    List[str]=256					,					snake_case__					:    Tuple="relu"					,					snake_case__					:    int=2_048					,					snake_case__					:    Optional[int]=2					,					snake_case__					:    Dict=8					,					snake_case__					:    List[Any]=2					,					snake_case__					:    List[Any]=3					,					snake_case__					:    List[Any]=3					,					snake_case__					:    Optional[int]=256					,					snake_case__					:    Tuple=1e-6					,					**snake_case__					:    Any					,					):




									"""simple docstring"""


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






class 					__lowerCAmelCase				( UpperCAmelCase__				):







			def __init__( self					:    List[str]					,					snake_case__					:    str=768					,					snake_case__					:    Dict=256					,					snake_case__					:    Any=12					,					snake_case__					:    Union[str, Any]=12					,					snake_case__					:    Any=3					,					snake_case__					:    List[str]=1_024					,					snake_case__					:    Union[str, Any]=16					,					snake_case__					:    Any="gelu"					,					snake_case__					:    List[str]=1e-06					,					snake_case__					:    Union[str, Any]=0.0					,					snake_case__					:    Optional[int]=1e-10					,					snake_case__					:    int=True					,					snake_case__					:    Dict=4.0					,					snake_case__					:    Optional[Any]=True					,					snake_case__					:    Tuple=True					,					snake_case__					:    List[Any]=14					,					snake_case__					:    List[str]=[2, 5, 8, 11]					,					snake_case__					:    List[Any]=128					,					snake_case__					:    List[Any]=None					,					**snake_case__					:    Any					,					):




									"""simple docstring"""


									super().__init__(**snake_case__   )

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






class 					__lowerCAmelCase				( UpperCAmelCase__				):
			snake_case_   :				Tuple				  =  "sam"
			snake_case_   :				Tuple				  =  True







			def __init__( self					:    Optional[int]					,					snake_case__					:    Optional[Any]=None					,					snake_case__					:    Any=None					,					snake_case__					:    int=None					,					snake_case__					:    List[Any]=0.02					,					**snake_case__					:    str					,					):




									"""simple docstring"""


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

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

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







			def UpperCamelCase    ( self					:    Any   ):




									"""simple docstring"""


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




