providers
sleap_nn.inference.providers
¶
Provider protocol + concrete data sources for Predictor.
A Provider yields batches of raw images plus per-batch metadata
(frame indices, video indices, optionally GT instances). The
Predictor consumes these batches and routes them through an
InferenceLayer.
Three concrete implementations:
- :class:
NumpyProvider— emits an in-memory tensor batch as a single iterate. Right for testing, real-time loops, or when the caller has already loaded frames. - :class:
VideoProvider— wraps a video path; yields frames in batches. Built onsleap_io.Videovia the existing data-layer reader so we don't duplicate decoding. - :class:
LabelsProvider— wraps a.slpfile; yields the labeled frames + their GT instances (needed for theuse_gt_centroids/use_gt_peakslayer paths).
Classes:
| Name | Description |
|---|---|
Batch |
One per-batch payload produced by a :class: |
LabelsProvider |
Yield batches from a |
MultiVideoProvider |
Concatenate several providers, OFFSETTING per-source video indices. |
NumpyProvider |
Emit a pre-loaded tensor as one or more batches. |
Provider |
Iterator-of-batches contract that |
VideoProvider |
Yield batches from a video file via |
Batch
¶
One per-batch payload produced by a :class:Provider.
Attributes:
| Name | Type | Description |
|---|---|---|
images |
ndarray | Tensor
|
|
frame_indices |
Optional[ndarray]
|
Optional |
video_indices |
Optional[ndarray]
|
Optional |
instances |
Optional[ndarray]
|
Optional |
Source code in sleap_nn/inference/providers.py
LabelsProvider
¶
Yield batches from a .slp file with GT instances attached.
Used by the GT-fallback layer paths (CentroidLayer.use_gt_centroids
and CenteredInstanceLayer.use_gt_peaks). Each yielded Batch
carries both the source images and the GT instance keypoints
from the .slp so the layer can match centroids → GT keypoints
or build crops from GT centroids without a centroid model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Path to a |
required | |
batch_size
|
Frames per yielded |
required | |
only_labeled_frames
|
Yield only frames that have at least one
user-supplied instance (default |
required | |
only_suggested_frames
|
Yield only frames listed in
|
required | |
exclude_user_labeled
|
Skip any frame that has a user instance.
Mutually exclusive with |
required | |
only_predicted_frames
|
Yield only frames that already have at least one predicted instance. |
required | |
remote_kwargs
|
Optional mapping of remote-loading options forwarded to
|
required | |
prefetch
|
If |
required | |
queue_maxsize
|
Bound on how many decoded batches may sit in the prefetch queue ahead of the consumer. |
required | |
frames
|
Optional 0-indexed positions to keep from the (possibly
already |
required |
Methods:
| Name | Description |
|---|---|
__attrs_post_init__ |
Resolve the labels source and pre-filter the labeled frames. |
__iter__ |
Yield batches, prefetching frame decode on a background thread. |
__len__ |
Number of batches over the (filtered) labeled-frame list. |
num_frames |
Total number of frames over the (filtered) labeled-frame list. |
Attributes:
| Name | Type | Description |
|---|---|---|
videos |
'list[sio.Video]'
|
Source videos of the underlying |
Source code in sleap_nn/inference/providers.py
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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 | |
videos
property
¶
Source videos of the underlying Labels, for output packaging.
Frame filtering (only_suggested_frames etc.) narrows which frames
are yielded, not the video list — predicted frames still reference these
real videos. Used by Predictor._make_provider so a pre-built
provider source doesn't yield a None-video Labels that crashes
sio.Labels.save (#699).
__attrs_post_init__()
¶
Resolve the labels source and pre-filter the labeled frames.
Source code in sleap_nn/inference/providers.py
__iter__()
¶
Yield batches, prefetching frame decode on a background thread.
See :meth:VideoProvider.__iter__ for the rationale and shutdown
semantics (lazy thread start, bounded queue, exception propagation,
stop-on-early-exit).
Source code in sleap_nn/inference/providers.py
__len__()
¶
MultiVideoProvider
¶
Concatenate several providers, OFFSETTING per-source video indices.
Wraps an ordered list of already-built providers (one per input source)
and yields their batches in order, shifting each batch's
video_indices by that source's starting global video index. Each
sub-provider emits its own local video indices (VideoProvider always
0; a LabelsProvider over a multi-video .slp emits per-frame
0..N-1), so adding the per-source offset attributes every frame to the
correct video in the merged multi-video .slp — and supports both
single-video and multi-video sources in the list (#582).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
providers
|
Ordered list of per-source :class: |
required | |
video_offsets
|
Parallel list giving each source's starting index
into the merged |
required |
Methods:
| Name | Description |
|---|---|
__iter__ |
Yield each sub-provider's batches with its video offset applied. |
__len__ |
Total batches across all sub-providers (or |
num_frames |
Total frames across all sub-providers (or |
Attributes:
| Name | Type | Description |
|---|---|---|
videos |
list
|
Merged source videos across sub-providers, in offset order (#699). |
Source code in sleap_nn/inference/providers.py
videos
property
¶
Merged source videos across sub-providers, in offset order (#699).
__iter__()
¶
Yield each sub-provider's batches with its video offset applied.
Source code in sleap_nn/inference/providers.py
__len__()
¶
Total batches across all sub-providers (or -1 if any unknown).
num_frames()
¶
Total frames across all sub-providers (or -1 if any unknown).
NumpyProvider
¶
Emit a pre-loaded tensor as one or more batches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
|
required | |
batch_size
|
Number of frames per yielded |
required | |
frame_indices
|
Optional explicit frame indices; defaults to
|
required | |
video_indices
|
Optional explicit video indices; defaults to all zeros (single-video assumption). |
required |
Notes
Right for: real-time loops, notebook calls where frames are
already loaded, integration tests. For video files use
:class:VideoProvider and for .slp use :class:LabelsProvider.
Methods:
| Name | Description |
|---|---|
__attrs_post_init__ |
Default per-frame metadata if the caller didn't provide any. |
__iter__ |
Yield |
__len__ |
Number of batches; |
num_frames |
Total number of frames in the pre-loaded tensor. |
Source code in sleap_nn/inference/providers.py
__attrs_post_init__()
¶
Default per-frame metadata if the caller didn't provide any.
Source code in sleap_nn/inference/providers.py
__iter__()
¶
Yield Batches of batch_size frames at a time.
Source code in sleap_nn/inference/providers.py
__len__()
¶
Provider
¶
Bases: Protocol
Iterator-of-batches contract that Predictor consumes.
Methods:
| Name | Description |
|---|---|
__iter__ |
Yield |
__len__ |
Return the total number of batches the provider will yield. |
num_frames |
Return the total number of frames the provider will yield. |
Source code in sleap_nn/inference/providers.py
__iter__()
¶
__len__()
¶
Return the total number of batches the provider will yield.
Used by the Predictor for progress reporting. Providers over
unbounded sources (live cameras) may return -1 to signal
unknown length.
Source code in sleap_nn/inference/providers.py
num_frames()
¶
Return the total number of frames the provider will yield.
Used by the Predictor for frame-based progress reporting, which
is batch-size-invariant (unlike __len__, which counts batches).
Providers over unbounded sources may return -1 to signal unknown
length.
Source code in sleap_nn/inference/providers.py
VideoProvider
¶
Yield batches from a video file via sleap_io.Video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
Path to a video file ( |
required | |
batch_size
|
Number of frames per yielded |
required | |
frames
|
Optional list of frame indices to read (e.g., |
required | |
dataset
|
For HDF5-backed videos, the dataset name (forwarded to
|
required | |
input_format
|
For HDF5-backed videos, |
required | |
remote_kwargs
|
Optional mapping of remote-loading options forwarded to
|
required | |
prefetch
|
If |
required | |
queue_maxsize
|
Bound on how many decoded batches may sit in the
prefetch queue ahead of the consumer. Kept small and explicit
(like |
required |
Notes
Yields raw (B, H, W, C) frames as np.uint8. The
layer's preprocess does the canonicalization
(e.g., ensure_grayscale).
Methods:
| Name | Description |
|---|---|
__attrs_post_init__ |
Resolve the video path → |
__iter__ |
Read frames in batches of |
__len__ |
Number of batches; |
num_frames |
Total number of frames this provider will yield. |
Attributes:
| Name | Type | Description |
|---|---|---|
videos |
'list[sio.Video]'
|
The source |
Source code in sleap_nn/inference/providers.py
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 | |
videos
property
¶
The source sio.Video(s), for packaging the output Labels.
Lets Predictor._make_provider attach the real video to predicted
frames when a pre-built provider is passed as the source, instead of a
None placeholder that later crashes sio.Labels.save (#699).
__attrs_post_init__()
¶
Resolve the video path → sio.Video and stash frame indices.
Source code in sleap_nn/inference/providers.py
__iter__()
¶
Read frames in batches of batch_size and yield them.
When prefetch=True (default), decoding happens on a background
thread reading from a private video copy, overlapping CPU decode of
the next batch with GPU inference on the current one. The thread is
started here (not at construction) so multi-source callers like
MultiVideoProvider only ever have one video being decoded ahead
of time, matching today's sequential, one-source-at-a-time behavior.