Base Model¶
sahi.models.base
¶
Base class for all detection models in SAHI.
Provides a unified interface for loading, inference, and prediction conversion across different detection frameworks.
Classes¶
DetectionModel
¶
Base class for all detection models in SAHI.
Subclasses must implement load_model, perform_inference, and
_create_object_prediction_list_from_original_predictions to integrate
a new detection framework. The base class handles device management,
dependency checking, category remapping, and the public prediction API.
Source code in sahi/models/base.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 78 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
Attributes¶
object_prediction_list
property
¶
Returns the object predictions for the first image.
This is a convenience accessor for single-image inference. For batch
inference results, use object_prediction_list_per_image instead.
object_prediction_list_per_image
property
¶
Returns object predictions grouped by image.
Each element is a list of ObjectPrediction instances for the
corresponding image in the batch.
original_predictions
property
¶
Returns the raw predictions from the underlying model.
The format is model-specific and is set by perform_inference or
perform_batch_inference.
Functions¶
__init__(model_path=None, model=None, config_path=None, device=None, mask_threshold=0.5, confidence_threshold=0.3, category_mapping=None, category_remapping=None, load_at_init=True, image_size=None)
¶
Init object detection/instance segmentation model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
¶ |
str | None
|
str Path for the instance segmentation model weight |
None
|
model
¶ |
Any | None
|
Any A pre-loaded detection model instance. |
None
|
config_path
¶ |
str | None
|
str Path for the mmdetection instance segmentation model config file |
None
|
device
¶ |
str | None
|
Torch device, "cpu", "mps", "cuda", "cuda:0", "cuda:1", etc. |
None
|
mask_threshold
¶ |
float
|
float Value to threshold mask pixels, should be between 0 and 1 |
0.5
|
confidence_threshold
¶ |
float
|
float All predictions with score < confidence_threshold will be discarded |
0.3
|
category_mapping
¶ |
dict | None
|
dict: str to str Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"} |
None
|
category_remapping
¶ |
dict | None
|
dict: str to int Remap category ids based on category names, after performing inference e.g. {"car": 3} |
None
|
load_at_init
¶ |
bool
|
bool If True, automatically loads the model at initialization |
True
|
image_size
¶ |
int | None
|
int Inference input size. |
None
|
Source code in sahi/models/base.py
check_dependencies(packages=None)
¶
Ensures required dependencies are installed.
If 'packages' is None, uses self.required_packages. Subclasses may still call with a custom list for dynamic needs.
Source code in sahi/models/base.py
convert_original_predictions(shift_amount=[[0, 0]], full_shape=None)
¶
Convert raw predictions to ObjectPrediction lists.
Should be called after perform_inference or perform_batch_inference.
When the default (sequential) perform_batch_inference was used,
this method runs inference + conversion one image at a time so that
each model's internal _original_predictions format is preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shift_amount
¶ |
list[list[int | float]] | None
|
Per-image shift amounts |
[[0, 0]]
|
full_shape
¶ |
list[list[int | float]] | None
|
Per-image full image sizes |
None
|
Source code in sahi/models/base.py
load_model()
¶
Load the detection model from disk and assign it to self.model.
Subclasses must override this method. The implementation should use
self.model_path, self.config_path, and self.device to
construct the underlying model object and store it in self.model.
Source code in sahi/models/base.py
perform_batch_inference(images)
¶
Performs inference on a batch of images.
Subclasses can override this for native batch support (e.g.
UltralyticsDetectionModel passes the full list to YOLO for
true GPU batching, HuggingfaceDetectionModel feeds all images
to the processor in one call).
The default does not run inference here. It stores images so
that convert_original_predictions can call perform_inference
per image, preserving each model's _original_predictions format.
Subclasses with native batch support override this to run inference
immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
¶ |
list[ndarray]
|
list[np.ndarray] List of numpy arrays (H, W, C) to run inference on. |
required |
Source code in sahi/models/base.py
perform_inference(image)
¶
Run inference on a single image and store raw predictions.
Subclasses must override this method. The implementation should run
the model on image and assign the raw results to
self._original_predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
np.ndarray A numpy array (H, W, C) containing the image to run inference on. |
required |
Source code in sahi/models/base.py
set_device(device=None)
¶
Sets the device pytorch should use for the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
¶ |
str | None
|
Torch device, "cpu", "mps", "cuda", "cuda:0", "cuda:1", etc. |
None
|
set_model(model, **kwargs)
¶
Set an already-instantiated model as the underlying detection model.
Subclasses must override this method to assign model to
self.model and perform any additional setup (e.g. category mapping).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
¶ |
Any
|
Any A pre-loaded detection model instance. |
required |
**kwargs
¶ |
Any
|
Any Additional keyword arguments for subclass-specific setup. |
{}
|