


from typing import Any, Dict, List, Union

import numpy as np

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
   from PIL import Image

   from ..image_utils import load_image

if is_torch_available():
   from ..models.auto.modeling_auto import (
       MODEL_FOR_IMAGE_SEGMENTATION_MAPPING,
       MODEL_FOR_INSTANCE_SEGMENTATION_MAPPING,
       MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING,
       MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING,
   )


UpperCamelCase__				       =			logging.get_logger(__name__)


UpperCamelCase__				       =			Dict[str, Any]
UpperCamelCase__				       =			List[Prediction]

@add_end_docstrings(A__		)
class 						UpperCAmelCase__   (      A__		):



     '''simple docstring'''
     def __init__( self       :       List[str]	,      *UpperCamelCase       :       List[str]	,      **UpperCamelCase       :       Tuple		):





            """simple docstring"""



            super().__init__(*_lowerCAmelCase	,      **_lowerCAmelCase		)

            if self.framework == "tf":
                   raise ValueError(F'The {self.__class__} is only available in PyTorch.'		)

            requires_backends(self	,      '''vision'''		)
            self.check_model_type(
                dict(
                    MODEL_FOR_IMAGE_SEGMENTATION_MAPPING.items()
                    + MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING.items()
                    + MODEL_FOR_INSTANCE_SEGMENTATION_MAPPING.items()
                    + MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING.items()		)		)
     def   lowerCAmelCase_   ( self       :       str	,      **UpperCamelCase       :       List[str]		):





            """simple docstring"""



            _lowercase					:				int           =			{}
            _lowercase					:				List[str]           =			{}
            if "subtask" in kwargs:
                   _lowercase					:				str           =			kwargs['''subtask''']
                   _lowercase					:				List[Any]           =			kwargs['''subtask''']
            if "threshold" in kwargs:
                   _lowercase					:				List[str]           =			kwargs['''threshold''']
            if "mask_threshold" in kwargs:
                   _lowercase					:				Tuple           =			kwargs['''mask_threshold''']
            if "overlap_mask_area_threshold" in kwargs:
                   _lowercase					:				List[str]           =			kwargs['''overlap_mask_area_threshold''']

            return preprocess_kwargs, {}, postprocess_kwargs
     def __call__( self       :       str	,      UpperCamelCase       :       Dict	,      **UpperCamelCase       :       Tuple		):





            """simple docstring"""



            return super().__call__(_lowerCAmelCase	,      **_lowerCAmelCase		)
     def   lowerCAmelCase_   ( self       :       Union[str, Any]	,      UpperCamelCase       :       str	,      UpperCamelCase       :       Dict=None		):





            """simple docstring"""



            _lowercase					:				Dict           =			load_image(_lowerCAmelCase		)
            _lowercase					:				Union[str, Any]           =			[(image.height, image.width)]
            if self.model.config.__class__.__name__ == "OneFormerConfig":
                   if subtask is None:
                          _lowercase					:				List[Any]           =			{}
                   else:
                          _lowercase					:				Optional[int]           =			{'''task_inputs''': [subtask]}
                   _lowercase					:				Dict           =			self.image_processor(images=[image]	,      return_tensors='''pt'''	,      **_lowerCAmelCase		)
                   _lowercase					:				Optional[int]           =			self.tokenizer(
                       inputs['''task_inputs''']	,      padding='''max_length'''	,      max_length=self.model.config.task_seq_len	,      return_tensors=self.framework	,      )['''input_ids''']
            else:
                   _lowercase					:				List[Any]           =			self.image_processor(images=[image]	,      return_tensors='''pt'''		)
            _lowercase					:				Tuple           =			target_size
            return inputs
     def   lowerCAmelCase_   ( self       :       int	,      UpperCamelCase       :       str		):





            """simple docstring"""



            _lowercase					:				Any           =			model_inputs.pop('''target_size'''		)
            _lowercase					:				Dict           =			self.model(**_lowerCAmelCase		)
            _lowercase					:				List[str]           =			target_size
            return model_outputs


     def   lowerCAmelCase_   ( self       :       str	,      UpperCamelCase       :       Any	,      UpperCamelCase       :       Optional[Any]=None	,      UpperCamelCase       :       List[Any]=0.9	,      UpperCamelCase       :       Union[str, Any]=0.5	,      UpperCamelCase       :       List[str]=0.5		):





            """simple docstring"""



            _lowercase					:				List[str]           =			None
            if subtask in {"panoptic", None} and hasattr(self.image_processor	,      '''post_process_panoptic_segmentation'''		):
                   _lowercase					:				Dict           =			self.image_processor.post_process_panoptic_segmentation
            elif subtask in {"instance", None} and hasattr(self.image_processor	,      '''post_process_instance_segmentation'''		):
                   _lowercase					:				Any           =			self.image_processor.post_process_instance_segmentation

            if fn is not None:
                   _lowercase					:				Any           =			fn(
                       _lowerCAmelCase	,      threshold=_lowerCAmelCase	,      mask_threshold=_lowerCAmelCase	,      overlap_mask_area_threshold=_lowerCAmelCase	,      target_sizes=model_outputs['''target_size''']	,      )[0]

                   _lowercase					:				Any           =			[]
                   _lowercase					:				int           =			outputs['''segmentation''']

                   for segment in outputs["segments_info"]:
                          _lowercase					:				Any           =			(segmentation == segment['''id''']) * 2_55
                          _lowercase					:				Any           =			Image.fromarray(mask.numpy().astype(np.uinta		)	,      mode='''L'''		)
                          _lowercase					:				Optional[Any]           =			self.model.config.idalabel[segment['''label_id''']]
                          _lowercase					:				Dict           =			segment['''score''']
                          annotation.append({'''score''': score, '''label''': label, '''mask''': mask}		)

            elif subtask in {"semantic", None} and hasattr(self.image_processor	,      '''post_process_semantic_segmentation'''		):
                   _lowercase					:				Tuple           =			self.image_processor.post_process_semantic_segmentation(
                       _lowerCAmelCase	,      target_sizes=model_outputs['''target_size''']		)[0]

                   _lowercase					:				Optional[Any]           =			[]
                   _lowercase					:				Union[str, Any]           =			outputs.numpy()
                   _lowercase					:				List[Any]           =			np.unique(_lowerCAmelCase		)

                   for label in labels:
                          _lowercase					:				Optional[int]           =			(segmentation == label) * 2_55
                          _lowercase					:				List[str]           =			Image.fromarray(mask.astype(np.uinta		)	,      mode='''L'''		)
                          _lowercase					:				str           =			self.model.config.idalabel[label]
                          annotation.append({'''score''': None, '''label''': label, '''mask''': mask}		)
            else:
                   raise ValueError(F'Subtask {subtask} is not supported for model {type(self.model		)}'		)
            return annotation



import copy

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


UpperCamelCase__				       =			logging.get_logger(__name__)

UpperCamelCase__				       =			{
    '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_		):



     '''simple docstring'''




     def __init__( self       :       List[Any]	,      UpperCamelCase       :       Optional[Any]=2_56	,      UpperCamelCase       :       str=10_24	,      UpperCamelCase       :       Any=16	,      UpperCamelCase       :       Optional[Any]=16	,      UpperCamelCase       :       Union[str, Any]=4	,      UpperCamelCase       :       str="gelu"	,      UpperCamelCase       :       Dict=1E-6	,      **UpperCamelCase       :       Tuple	,      ):





            """simple docstring"""



            super().__init__(**UpperCamelCase		)
            _lowercase					:				Union[str, Any]           =			hidden_size
            _lowercase					:				Optional[Any]           =			image_size
            _lowercase					:				Tuple           =			patch_size
            _lowercase					:				List[str]           =			image_size // patch_size
            _lowercase					:				Optional[Any]           =			mask_input_channels
            _lowercase					:				str           =			num_point_embeddings
            _lowercase					:				List[Any]           =			hidden_act
            _lowercase					:				Union[str, Any]           =			layer_norm_eps




class 						UpperCAmelCase__   (      A_		):



     '''simple docstring'''




     def __init__( self       :       Optional[int]	,      UpperCamelCase       :       str=2_56	,      UpperCamelCase       :       int="relu"	,      UpperCamelCase       :       Any=20_48	,      UpperCamelCase       :       List[Any]=2	,      UpperCamelCase       :       Union[str, Any]=8	,      UpperCamelCase       :       List[str]=2	,      UpperCamelCase       :       Any=3	,      UpperCamelCase       :       List[Any]=3	,      UpperCamelCase       :       int=2_56	,      UpperCamelCase       :       List[str]=1E-6	,      **UpperCamelCase       :       Tuple	,      ):





            """simple docstring"""



            super().__init__(**UpperCamelCase		)
            _lowercase					:				List[Any]           =			hidden_size
            _lowercase					:				Optional[int]           =			hidden_act
            _lowercase					:				Union[str, Any]           =			mlp_dim
            _lowercase					:				Optional[int]           =			num_hidden_layers
            _lowercase					:				List[str]           =			num_attention_heads
            _lowercase					:				Dict           =			attention_downsample_rate
            _lowercase					:				Optional[Any]           =			num_multimask_outputs
            _lowercase					:				Optional[Any]           =			iou_head_depth
            _lowercase					:				Dict           =			iou_head_hidden_dim
            _lowercase					:				List[str]           =			layer_norm_eps




class 						UpperCAmelCase__   (      A_		):



     '''simple docstring'''




     def __init__( self       :       List[str]	,      UpperCamelCase       :       Union[str, Any]=7_68	,      UpperCamelCase       :       Optional[Any]=2_56	,      UpperCamelCase       :       Optional[Any]=12	,      UpperCamelCase       :       Dict=12	,      UpperCamelCase       :       Dict=3	,      UpperCamelCase       :       Optional[int]=10_24	,      UpperCamelCase       :       Any=16	,      UpperCamelCase       :       Union[str, Any]="gelu"	,      UpperCamelCase       :       int=1E-06	,      UpperCamelCase       :       Tuple=0.0	,      UpperCamelCase       :       List[str]=1E-10	,      UpperCamelCase       :       List[Any]=True	,      UpperCamelCase       :       Any=4.0	,      UpperCamelCase       :       str=True	,      UpperCamelCase       :       Optional[int]=True	,      UpperCamelCase       :       Optional[int]=14	,      UpperCamelCase       :       Any=[2, 5, 8, 11]	,      UpperCamelCase       :       Tuple=1_28	,      UpperCamelCase       :       str=None	,      **UpperCamelCase       :       int	,      ):





            """simple docstring"""



            super().__init__(**UpperCamelCase		)

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




class 						UpperCAmelCase__   (      A_		):



     '''simple docstring'''
     UpperCAmelCase_         =  '''sam'''
     UpperCAmelCase_         =  True




     def __init__( self       :       Tuple	,      UpperCamelCase       :       Tuple=None	,      UpperCamelCase       :       Any=None	,      UpperCamelCase       :       Tuple=None	,      UpperCamelCase       :       Dict=0.02	,      **UpperCamelCase       :       Union[str, Any]	,      ):





            """simple docstring"""



            super().__init__(**UpperCamelCase		)
            _lowercase					:				Optional[int]           =			vision_config if vision_config is not None else {}
            _lowercase					:				Any           =			prompt_encoder_config if prompt_encoder_config is not None else {}
            _lowercase					:				Optional[Any]           =			mask_decoder_config if mask_decoder_config is not None else {}

            if isinstance(UpperCamelCase	,      UpperCamelCase		):
                   _lowercase					:				Dict           =			vision_config.to_dict()
            if isinstance(UpperCamelCase	,      UpperCamelCase		):
                   _lowercase					:				List[str]           =			prompt_encoder_config.to_dict()
            if isinstance(UpperCamelCase	,      UpperCamelCase		):
                   _lowercase					:				Optional[int]           =			mask_decoder_config.to_dict()

            _lowercase					:				Union[str, Any]           =			SamVisionConfig(**UpperCamelCase		)
            _lowercase					:				Optional[Any]           =			SamPromptEncoderConfig(**UpperCamelCase		)
            _lowercase					:				List[Any]           =			SamMaskDecoderConfig(**UpperCamelCase		)
            _lowercase					:				Union[str, Any]           =			initializer_range




     def   lowerCAmelCase_   ( self       :       Union[str, Any]		):





            """simple docstring"""



            _lowercase					:				List[Any]           =			copy.deepcopy(self.__dict__		)
            _lowercase					:				int           =			self.vision_config.to_dict()
            _lowercase					:				Optional[int]           =			self.prompt_encoder_config.to_dict()
            _lowercase					:				List[Any]           =			self.mask_decoder_config.to_dict()
            _lowercase					:				List[Any]           =			self.__class__.model_type
            return output