onnx_backend
sleap_nn.inference.layers.backends.onnx_backend
¶
ONNXBackend — runs an exported ONNX model under the ModelBackend protocol.
The existing wrappers in sleap_nn/export/wrappers/ bake several
postprocessing steps into the ONNX graph itself (uint8 normalization,
input-scale resize, peak finding, optional PAF line scoring). When an
InferenceLayer runs against this backend, does_baked_postproc is
True so the layer skips its Python peak finding and just applies the
coord-transform ladder to whatever the session returned.
This backend is a thin shim around onnxruntime.InferenceSession: it
selects the right execution providers, runs the session, and converts
numpy outputs to torch tensors so downstream layer code is dtype/device
uniform with the TorchBackend path.
PR 11 (#519) deletes the legacy sleap_nn/export/predictors/onnx.py
runtime wrapper after this backend is wired through the predictor.
Classes:
| Name | Description |
|---|---|
ONNXBackend |
ONNX Runtime backend conforming to :class: |
ONNXBackend
¶
ONNX Runtime backend conforming to :class:ModelBackend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
Path to an exported |
required | |
device
|
|
required | |
providers
|
Explicit override for the execution-provider list. If
|
required |
Notes
does_baked_postproc=True — the ONNX wrappers in
sleap_nn/export/wrappers/ bake peak finding, normalization,
and (top-down) crop extraction into the graph. Layer postprocess
methods take the "peaks" / "peak_vals" keys directly from
the session output instead of running Python peak finding.
Methods:
| Name | Description |
|---|---|
__attrs_post_init__ |
Load the ONNX session and cache I/O metadata. |
__call__ |
Run the ONNX session. |
from_export_dir |
Load an ONNX backend from an export directory containing |
warmup |
Run a single dummy forward to prime the runtime / GPU caches. |
Attributes:
| Name | Type | Description |
|---|---|---|
does_baked_postproc |
bool
|
ONNX wrappers bake peak finding into the graph. |
Source code in sleap_nn/inference/layers/backends/onnx_backend.py
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 | |
does_baked_postproc
property
¶
ONNX wrappers bake peak finding into the graph.
__attrs_post_init__()
¶
Load the ONNX session and cache I/O metadata.
Source code in sleap_nn/inference/layers/backends/onnx_backend.py
__call__(x)
¶
Run the ONNX session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. Cast to the session's expected dtype before
handoff (most exported wrappers expect |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Tensor]
|
Dict mapping the session's output names to torch tensors.
Layer postprocess methods then look up |
Source code in sleap_nn/inference/layers/backends/onnx_backend.py
from_export_dir(export_dir, device='auto')
classmethod
¶
Load an ONNX backend from an export directory containing model.onnx.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
export_dir
|
Union[str, Path]
|
Directory written by |
required |
device
|
str
|
Device hint for execution-provider selection. |
'auto'
|
Returns:
| Type | Description |
|---|---|
'ONNXBackend'
|
A configured |
Source code in sleap_nn/inference/layers/backends/onnx_backend.py
warmup(input_shape)
¶
Run a single dummy forward to prime the runtime / GPU caches.