Reference for ultralytics/models/fastsam/predict.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/fastsam/predict.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.fastsam.predict.FastSAMPredictor
Bases: SegmentationPredictor
FastSAMPredictor is specialized for fast SAM (Segment Anything Model) segmentation prediction tasks.
This class extends the SegmentationPredictor, customizing the prediction pipeline specifically for fast SAM. It adjusts post-processing steps to incorporate mask prediction and non-maximum suppression while optimizing for single-class segmentation.
Attributes:
Name | Type | Description |
---|---|---|
prompts |
dict
|
Dictionary containing prompt information for segmentation (bboxes, points, labels, texts). |
device |
device
|
Device on which model and tensors are processed. |
clip_model |
Any
|
CLIP model for text-based prompting, loaded on demand. |
clip_preprocess |
Any
|
CLIP preprocessing function for images, loaded on demand. |
Methods:
Name | Description |
---|---|
postprocess |
Applies box postprocessing for FastSAM predictions. |
prompt |
Performs image segmentation inference based on various prompt types. |
_clip_inference |
Performs CLIP inference to calculate similarity between images and text prompts. |
set_prompts |
Sets prompts to be used during inference. |
This initializes a predictor specialized for Fast SAM (Segment Anything Model) segmentation tasks. The predictor extends SegmentationPredictor with custom post-processing for mask prediction and non-maximum suppression optimized for single-class segmentation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
dict
|
Configuration for the predictor. Defaults to Ultralytics DEFAULT_CFG. |
DEFAULT_CFG
|
overrides
|
dict
|
Configuration overrides. |
None
|
_callbacks
|
list
|
List of callback functions. |
None
|
Source code in ultralytics/models/fastsam/predict.py
postprocess
Apply postprocessing to FastSAM predictions and handle prompts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
List[Tensor]
|
Raw predictions from the model. |
required |
img
|
Tensor
|
Input image tensor that was fed to the model. |
required |
orig_imgs
|
List[ndarray]
|
Original images before preprocessing. |
required |
Returns:
Type | Description |
---|---|
List[Results]
|
Processed results with prompts applied. |
Source code in ultralytics/models/fastsam/predict.py
prompt
Perform image segmentation inference based on cues like bounding boxes, points, and text prompts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results
|
Results | List[Results]
|
Original inference results from FastSAM models without any prompts. |
required |
bboxes
|
ndarray | List
|
Bounding boxes with shape (N, 4), in XYXY format. |
None
|
points
|
ndarray | List
|
Points indicating object locations with shape (N, 2), in pixels. |
None
|
labels
|
ndarray | List
|
Labels for point prompts, shape (N, ). 1 = foreground, 0 = background. |
None
|
texts
|
str | List[str]
|
Textual prompts, a list containing string objects. |
None
|
Returns:
Type | Description |
---|---|
List[Results]
|
Output results filtered and determined by the provided prompts. |