Skip to content

Commit 0e4b87e

Browse files
committed
feat: 🚀 add perform_batch_inference to DetectionModel base class
Add a default sequential fallback for batch inference that all model subclasses inherit automatically. Subclasses can override for native batch support. Signed-off-by: Onuralp SEZER <thunderbirdtr@gmail.com>
1 parent 6f84664 commit 0e4b87e

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

sahi/models/base.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,28 @@ def perform_inference(self, image: np.ndarray):
122122
"""
123123
raise NotImplementedError()
124124

125+
def perform_batch_inference(self, images: list[np.ndarray]):
126+
"""Performs inference on a batch of images.
127+
128+
Subclasses can override this for native batch support. The default
129+
implementation falls back to sequential single-image inference.
130+
131+
After this call, ``self._original_predictions`` contains one entry per
132+
image and ``self._original_shapes`` stores the shape of each input image.
133+
134+
Args:
135+
images: list[np.ndarray]
136+
List of numpy arrays (H, W, C) to run inference on.
137+
"""
138+
all_predictions: list = []
139+
original_shapes: list = []
140+
for image in images:
141+
self.perform_inference(image)
142+
all_predictions.extend(self._original_predictions)
143+
original_shapes.append(image.shape)
144+
self._original_predictions = all_predictions
145+
self._original_shapes = original_shapes
146+
125147
def _create_object_prediction_list_from_original_predictions(
126148
self,
127149
shift_amount_list: list[list[int]] | None = [[0, 0]],

0 commit comments

Comments
 (0)