InferenceSlicer¶
InferenceSlicer performs slicing-based inference for small target detection. This method, often referred to as Slicing Adaptive Inference (SAHI), involves dividing a larger image into smaller slices, performing inference on each slice, and then merging the detections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
slice_wh |
Tuple[int, int]
|
Dimensions of each slice measured in pixels. The
tuple should be in the format |
(320, 320)
|
overlap_ratio_wh |
Optional[Tuple[float, float]]
|
A tuple representing the desired overlap ratio for width and height between consecutive slices. Each value should be in the range [0, 1), where 0 means no overlap and a value close to 1 means high overlap. |
(0.2, 0.2)
|
overlap_wh |
Optional[Tuple[int, int]]
|
A tuple representing the desired overlap for width and height between consecutive slices measured in pixels. Each value should be greater than or equal to 0. |
None
|
overlap_filter |
Union[OverlapFilter, str]
|
Strategy for filtering or merging overlapping detections in slices. |
NON_MAX_SUPPRESSION
|
iou_threshold |
float
|
Intersection over Union (IoU) threshold used when filtering by overlap. |
0.5
|
callback |
Callable
|
A function that performs inference on a given image slice and returns detections. |
required |
thread_workers |
int
|
Number of threads for parallel execution. |
1
|
Note
The class ensures that slices do not exceed the boundaries of the original image. As a result, the final slices in the row and column dimensions might be smaller than the specified slice dimensions if the image's width or height is not a multiple of the slice's width or height minus the overlap.
Source code in supervision/detection/tools/inference_slicer.py
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 292 293 294 |
|
Functions¶
__call__(image)
¶
Performs slicing-based inference on the provided image using the specified callback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
ndarray
|
The input image on which inference needs to be
performed. The image should be in the format
|
required |
Returns:
Name | Type | Description |
---|---|---|
Detections |
Detections
|
A collection of detections for the entire image after merging results from all slices and applying NMS. |
Example
import cv2
import supervision as sv
from ultralytics import YOLO
image = cv2.imread(SOURCE_IMAGE_PATH)
model = YOLO(...)
def callback(image_slice: np.ndarray) -> sv.Detections:
result = model(image_slice)[0]
return sv.Detections.from_ultralytics(result)
slicer = sv.InferenceSlicer(
callback=callback,
overlap_filter_strategy=sv.OverlapFilter.NON_MAX_SUPPRESSION,
)
detections = slicer(image)