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
¶
DetectionModel(
model_path: str | None = None,
model: Any | None = None,
config_path: str | None = None,
device: str | None = None,
mask_threshold: float = 0.5,
confidence_threshold: float = 0.3,
category_mapping: dict | None = None,
category_remapping: dict | None = None,
load_at_init: bool = True,
image_size: int | None = None,
)
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.
Init object detection/instance segmentation model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | None
|
str Path for the instance segmentation model weight |
None
|
|
Any | None
|
Any A pre-loaded detection model instance. |
None
|
|
str | None
|
str Path for the mmdetection instance segmentation model config file |
None
|
|
str | None
|
Torch device, "cpu", "mps", "cuda", "cuda:0", "cuda:1", etc. |
None
|
|
float
|
float Value to threshold mask pixels, should be between 0 and 1 |
0.5
|
|
float
|
float All predictions with score < confidence_threshold will be discarded |
0.3
|
|
dict | None
|
dict: str to str Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"} |
None
|
|
dict | None
|
dict: str to int Remap category ids based on category names, after performing inference e.g. {"car": 3} |
None
|
|
bool
|
bool If True, automatically loads the model at initialization |
True
|
|
int | None
|
int Inference input size. |
None
|
Source code in sahi/models/base.py
Attributes¶
object_prediction_list
property
¶
object_prediction_list: list[ObjectPrediction]
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
¶
object_prediction_list_per_image: list[
list[ObjectPrediction]
]
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.
Methods:¶
check_dependencies
¶
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
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
set_model
¶
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. |
{}
|
Source code in sahi/models/base.py
set_device
¶
set_device(device: str | None = None) -> 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
|
unload_model
¶
perform_inference
¶
perform_inference(image: ndarray) -> None
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
perform_batch_inference
¶
perform_batch_inference(images: list[ndarray]) -> None
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
convert_original_predictions
¶
convert_original_predictions(
shift_amount: list[list[int | float]] | None = [[0, 0]],
full_shape: list[list[int | float]] | None = None,
) -> 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
|