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
FastSAMPredictor(cfg=DEFAULT_CFG, overrides=None, _callbacks=None)
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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
postprocess
postprocess(preds, img, orig_imgs)
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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
prompt
prompt(results, bboxes=None, points=None, labels=None, texts=None)
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. |
Source code in ultralytics/models/fastsam/predict.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
set_prompts
set_prompts(prompts)
Set prompts to be used during inference.
Source code in ultralytics/models/fastsam/predict.py
179 180 181 |
|