layers
sleap_nn.inference.layers
¶
Inference layers — model-type-aware wrappers around a runtime backend.
Layers are model-type-aware (peak finding, NMS, multi-class identity grouping). Backends are runtime-aware (PyTorch, ONNX, TensorRT). Crossing the two gives 6 × 3 = 18 conceptual variants — but with this protocol-based split we only ship 6 + 3 = 9 classes total, with zero duplication.
Modules:
| Name | Description |
|---|---|
backends |
Runtime backends for inference layers. |
base |
|
bottomup |
|
bottomup_multiclass |
|
centered_instance |
|
centroid |
|
configs |
|
embedding |
Embedding (crop -> appearance vector, re-ID) inference layers. |
exported |
Export-adapter layers — thin translators around exported ONNX/TRT models. |
segmentation |
|
single_instance |
|
tiled |
Sliding-window tiled inference wrappers. |
topdown |
|
topdown_multiclass |
|
topdown_segmentation |
Top-down (crop-centered) instance-segmentation inference layers (#622). |
Classes:
| Name | Description |
|---|---|
InferenceLayer |
Abstract base for model-type-specific inference layers. |
PostprocessConfig |
Knobs that govern how raw model outputs become keypoints. |
PreprocessConfig |
Preprocessing knobs applied before the model forward pass. |
SingleInstanceLayer |
Single-pose-per-frame inference layer. |
InferenceLayer
¶
Bases: ABC
Abstract base for model-type-specific inference layers.
Subclasses implement preprocess (image → tensor + PreprocInfo),
postprocess (raw backend output + PreprocInfo → Outputs),
and may override predict for composed layers (top-down). The
default predict is preprocess → backend → postprocess.
Attributes:
| Name | Type | Description |
|---|---|---|
backend |
The runtime backend ( |
|
preprocess_config |
Knobs governing input transformation. |
|
postprocess_config |
Knobs governing peak decoding and what intermediate tensors to keep. |
|
output_stride |
Confmap → input-pixel stride. Read from the model's head config at construction. |
Methods:
| Name | Description |
|---|---|
__call__ |
Alias for :meth: |
__init__ |
Validate the backend protocol and stash configs. |
postprocess |
Turn the backend's raw dict into a structured |
predict |
Run the full preprocess → backend → postprocess pipeline. |
preprocess |
Run the full preprocessing chain on a raw frame. |
warmup |
Prime the backend by running |
Source code in sleap_nn/inference/layers/base.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 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 330 331 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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | |
warmup_input_shape
property
¶
Warmup shape -- only used when sample_shape is passed.
The default warmup() path ignores this and synthesizes a real
raw frame instead.
__call__(image)
¶
__init__(backend, preprocess_config, postprocess_config, output_stride, max_stride=1)
¶
Validate the backend protocol and stash configs.
Source code in sleap_nn/inference/layers/base.py
postprocess(raw_out, info)
abstractmethod
¶
predict(image)
¶
Run the full preprocess → backend → postprocess pipeline.
preprocess(image)
¶
Run the full preprocessing chain on a raw frame.
Delegates to :meth:_apply_full_preprocess:
ensure_rgb/grayscale -> per-sample sizematcher (records eff_scale) ->
input_scale -> pad_to_stride -> n_samples wrap.
Subclasses that need non-standard behaviour (e.g. a different
output_stride attribute or extra logic) can override this.
Source code in sleap_nn/inference/layers/base.py
warmup(sample_shape=None)
¶
Prime the backend by running predict() on a synthesized frame.
The synthesized frame goes through the layer's full preprocess
chain (sizematcher → input_scale → ensure_rgb/grayscale → pad →
n_samples wrap) so the model receives an input with the same
rank / channel-count / device contract as real inference, and
cuDNN's algorithm cache is primed for the right shape.
When sample_shape is None (the default), a tiny raw frame
is synthesized and routed through the layer's full preprocess
chain so cuDNN's algorithm cache is primed for the correct input
shape. This avoids shape-mismatch crashes that can occur when a
bare backend.warmup bypasses preprocess and cuDNN caches
an algorithm for a degenerate dummy shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_shape
|
Tuple[int, ...] | None
|
Escape hatch. When provided, dispatches straight
to |
None
|
Source code in sleap_nn/inference/layers/base.py
PostprocessConfig
¶
Knobs that govern how raw model outputs become keypoints.
Distinct from the post-inference FilterConfig: this struct governs
the decoding step (peak finding, integral refinement, NMS), while
FilterConfig filters the keypoints that come out the other side.
peak_threshold here decides which confmap pixels become peaks;
min_peak_value in FilterConfig filters peaks the decoder
already returned.
Attributes:
| Name | Type | Description |
|---|---|---|
peak_threshold |
float
|
Minimum confmap activation to consider a peak. |
refinement |
Literal['integral', 'none']
|
|
integral_patch_size |
int
|
Side length of the refinement patch. |
max_instances |
Optional[int]
|
Cap on instances per frame (centroid layer only). |
return_confmaps |
bool
|
Keep |
return_pafs |
bool
|
Keep |
return_paf_graph |
bool
|
Keep the bottom-up PAF graph tuple (opt-in). |
return_class_maps |
bool
|
Keep |
return_class_vectors |
bool
|
Keep |
Source code in sleap_nn/inference/layers/configs.py
effective_refinement
property
¶
Return the refinement string or None when "none".
Every postprocess site needs refinement=None (not the string
"none") to disable refinement. This property centralises that
coercion.
PreprocessConfig
¶
Preprocessing knobs applied before the model forward pass.
Defaults are the no-op identity for every field — calling the layer on an already-correctly-shaped batch produces zero extra work.
Attributes:
| Name | Type | Description |
|---|---|---|
ensure_rgb |
Optional[bool]
|
|
ensure_grayscale |
Optional[bool]
|
Inverse of |
max_height |
Optional[int]
|
Resize so height ≤ this (preserves aspect ratio). |
max_width |
Optional[int]
|
Same for width. |
scale |
float
|
Multiplicative input-scale factor applied (after size matching)
via :func: |
crop_size |
Optional[Tuple[int, int]]
|
Top-down stage 2 only — square crop side length. |
Methods:
| Name | Description |
|---|---|
__attrs_post_init__ |
Reject the contradictory |
Source code in sleap_nn/inference/layers/configs.py
__attrs_post_init__()
¶
Reject the contradictory ensure_rgb=True + ensure_grayscale=True.
Source code in sleap_nn/inference/layers/configs.py
SingleInstanceLayer
¶
Bases: InferenceLayer
Single-pose-per-frame inference layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
ModelBackend
|
Runtime backend (e.g. |
required |
preprocess_config
|
Optional[PreprocessConfig]
|
Pre-forward transformation knobs. |
None
|
postprocess_config
|
Optional[PostprocessConfig]
|
Peak decoding + intermediate-return knobs. |
None
|
output_stride
|
int
|
Stride between confmap and input pixels (read from the head config at construction). |
required |
max_stride
|
int
|
Backbone-network stride; inputs are padded bottom-right
to a multiple of this in |
1
|
Methods:
| Name | Description |
|---|---|
__init__ |
Compose the layer with default empty configs when omitted. |
postprocess |
Decode confmaps → keypoints, reverse coord ladder, build |
Source code in sleap_nn/inference/layers/single_instance.py
__init__(backend, output_stride, max_stride=1, preprocess_config=None, postprocess_config=None)
¶
Compose the layer with default empty configs when omitted.
Source code in sleap_nn/inference/layers/single_instance.py
postprocess(raw_out, info)
¶
Decode confmaps → keypoints, reverse coord ladder, build Outputs.
This layer always runs the torch decode path: it is only ever built
with a TorchBackend (does_baked_postproc=False). The exported
ONNX/TRT path uses the separate :class:ExportedSingleInstanceLayer
adapter, which returns already-final peaks without the coord ladder —
so this method must NOT special-case a baked backend (doing so would
double-apply the ladder; #584).