runtime
Runtime Module for ONNX-based Models
This module provides the necessary functionality for loading, preprocessing, running inference, and benchmarking ONNX-based models using different execution providers such as CUDA, TensorRT, OpenVINO, and CPU. It includes utility functions for image preprocessing, postprocessing, and interfacing with the ONNXRuntime library.
Functions:
Name | Description |
---|---|
det_postprocess |
Postprocesses detection model outputs into sv.Detections. |
semseg_postprocess |
Postprocesses semantic segmentation model outputs into sv.Detections. |
get_runtime |
Returns an ONNXRuntime instance configured for the given runtime type. |
Classes:
Name | Description |
---|---|
ONNXRuntime |
A class that interfaces with ONNX Runtime for model inference. |
ONNXRuntime
#
A class that interfaces with ONNX Runtime for model inference using different execution providers (CUDA, TensorRT, OpenVINO, CoreML, etc.). It manages preprocessing, inference, and postprocessing of data, as well as benchmarking the performance of the model.
Attributes:
Name | Type | Description |
---|---|---|
logger |
Logger
|
Logger for the ONNXRuntime instance. |
name |
str
|
The name of the model (derived from its path). |
opts |
OnnxEngineOpts
|
Options used for configuring the ONNX Runtime. |
model_metadata |
ModelMetadata
|
Metadata related to the model. |
postprocess_fn |
Callable
|
The function used to postprocess the model's output. |
ort_sess |
InferenceSession
|
The ONNXRuntime inference session. |
dtype |
dtype
|
The data type for the model input. |
binding |
Optional[str]
|
The binding type for the runtime (e.g., CUDA, CPU). |
Source code in focoos/runtime.py
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
__call__(im, conf_threshold)
#
Runs inference on the provided input image and returns the model's detections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im |
ndarray
|
The preprocessed input image. |
required |
conf_threshold |
float
|
The confidence threshold for filtering results. |
required |
Returns:
Type | Description |
---|---|
Detections
|
sv.Detections: A sv.Detections object containing the model's output detections. |
Source code in focoos/runtime.py
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 |
|
__init__(model_path, opts, model_metadata)
#
Initializes the ONNXRuntime instance with the specified model and configuration options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_path |
str
|
Path to the ONNX model file. |
required |
opts |
OnnxEngineOpts
|
The configuration options for ONNX Runtime. |
required |
model_metadata |
ModelMetadata
|
Metadata for the model (e.g., task type). |
required |
Source code in focoos/runtime.py
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 |
|
benchmark(iterations=20, size=640)
#
Benchmarks the model by running multiple inference iterations and measuring the latency.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterations |
int
|
Number of iterations to run for benchmarking. Defaults to 20. |
20
|
size |
int
|
The input image size for benchmarking. Defaults to 640. |
640
|
Returns:
Name | Type | Description |
---|---|---|
LatencyMetrics |
LatencyMetrics
|
The latency metrics (e.g., FPS, mean, min, max, and standard deviation). |
Source code in focoos/runtime.py
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
det_postprocess(out, im0_shape, conf_threshold)
#
Postprocesses the output of an object detection model and filters detections based on a confidence threshold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out |
List[ndarray]
|
The output of the detection model. |
required |
im0_shape |
Tuple[int, int]
|
The original shape of the input image (height, width). |
required |
conf_threshold |
float
|
The confidence threshold for filtering detections. |
required |
Returns:
Type | Description |
---|---|
Detections
|
sv.Detections: A sv.Detections object containing the filtered bounding boxes, class ids, and confidences. |
Source code in focoos/runtime.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
get_runtime(runtime_type, model_path, model_metadata, warmup_iter=0)
#
Creates and returns an ONNXRuntime instance based on the specified runtime type and model path, with options for various execution providers (CUDA, TensorRT, CPU, etc.).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
runtime_type |
RuntimeTypes
|
The type of runtime to use (e.g., ONNX_CUDA32, ONNX_TRT32). |
required |
model_path |
str
|
The path to the ONNX model. |
required |
model_metadata |
ModelMetadata
|
Metadata describing the model. |
required |
warmup_iter |
int
|
Number of warmup iterations before benchmarking. Defaults to 0. |
0
|
Returns:
Name | Type | Description |
---|---|---|
ONNXRuntime |
ONNXRuntime
|
A fully configured ONNXRuntime instance. |
Source code in focoos/runtime.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
|
semseg_postprocess(out, im0_shape, conf_threshold)
#
Postprocesses the output of a semantic segmentation model and filters based on a confidence threshold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out |
List[ndarray]
|
The output of the semantic segmentation model. |
required |
im0_shape |
Tuple[int, int]
|
The original shape of the input image (height, width). |
required |
conf_threshold |
float
|
The confidence threshold for filtering detections. |
required |
Returns:
Type | Description |
---|---|
Detections
|
sv.Detections: A sv.Detections object containing the masks, class ids, and confidences. |
Source code in focoos/runtime.py
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 |
|