outputs
sleap_nn.inference.outputs
¶
Outputs — the structured container produced by every InferenceLayer.
Single source of truth for what an inference call yields, how to manipulate its tensors (device, dtype, autograd), and how to reduce it to a slimmer form for cross-process transport.
Design constraints baked into the class:
slots=Truehalves per-instance memory; long videos can produce millions of these.eq=Falseskips__eq__machinery; we never compare twoOutputsfor equality and skipping it speeds up construction.- Custom
__repr__prints field shapes, not tensor contents — a fatOutputswould otherwise dump megabytes into stack traces. slim()is a hard contract: the returned object MUST be pickleable. This guarantees multi-process post-processing and the streaming writer can shipOutputsbetween processes without surprises. Enforced by tests.- No live references: every field is a value (tensor, ndarray, ints, the
PreprocInfostruct). NoInferenceLayer/Backend/LightningModule/ file handle / generator. Enforced by tests.
Classes:
| Name | Description |
|---|---|
Outputs |
Structured container for inference outputs. |
Outputs
¶
Structured container for inference outputs.
Shape convention
B = batch size, I = max instances, N = nodes,
C = classes, H/W = spatial dims, E = number of edges.
NaN indicates missing/invalid predictions in keypoint fields.
Methods:
| Name | Description |
|---|---|
__repr__ |
Compact |
cpu |
Return a new |
detach |
Return a new |
numpy |
Return non- |
slim |
Drop heavy intermediates and force CPU + detach for transport. |
to |
Return a new |
to_centroids |
Convert one batch slot's centroids into |
to_instances |
Convert one batch slot into a list of |
to_labels |
Convert this |
to_masks |
Convert one batch slot's masks into |
to_rois |
Convert one batch slot's masks into simplified |
Attributes:
| Name | Type | Description |
|---|---|---|
batch_size |
int
|
Batch dimension B, or 0 if no batch-bearing field is set. |
n_instances |
int
|
Per-frame instance dimension I, or 0 if no instance field is set. |
n_nodes |
int
|
Skeleton-node dimension N, or 0 if not derivable. |
Source code in sleap_nn/inference/outputs.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 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 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 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 | |
batch_size
property
¶
Batch dimension B, or 0 if no batch-bearing field is set.
n_instances
property
¶
Per-frame instance dimension I, or 0 if no instance field is set.
n_nodes
property
¶
Skeleton-node dimension N, or 0 if not derivable.
__repr__()
¶
Compact Outputs(...) summary listing only populated fields.
Source code in sleap_nn/inference/outputs.py
cpu()
¶
detach()
¶
numpy()
¶
Return non-None fields as numpy.
Tensors become np.ndarray (CPU + detached automatically).
Tuples-of-tensors become tuples-of-ndarrays. None fields are
omitted. Non-tensor fields (preprocess_info, integers) pass
through untouched.
Source code in sleap_nn/inference/outputs.py
slim()
¶
Drop heavy intermediates and force CPU + detach for transport.
Hard contract: the returned Outputs is guaranteed pickle-safe.
Use this before sending across a queue / process boundary
(multiprocessing.Queue, concurrent.futures).
Drops: original_image, processed_image, crops,
pred_confmaps, pred_pafs, pred_class_maps,
pred_paf_graph. These are opt-in heavies; if you needed them
downstream, call this after the consumer is done with them.
Source code in sleap_nn/inference/outputs.py
to(device)
¶
to_centroids(batch_index=0, *, source='center_of_mass', tracks=None)
¶
Convert one batch slot's centroids into sio.PredictedCentroids.
Centroid-only packaging alternative to :meth:to_instances: each
non-NaN centroid becomes a sio.PredictedCentroid (stored in
LabeledFrame.centroids) carrying the centroid value as its
instance-level score and a source method tag (#586-consistent).
Returns [] when there are no centroids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_index
|
int
|
Which sample in the batch to convert. |
0
|
source
|
str
|
|
'center_of_mass'
|
tracks
|
Optional[list['sio.Track']]
|
Optional per-slot |
None
|
Source code in sleap_nn/inference/outputs.py
to_instances(skeleton, batch_index=0, anchor_ind=None, tracks=None, *, identities=None, collapse_skeleton=None)
¶
Convert one batch slot into a list of sio.PredictedInstance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skeleton
|
'sio.Skeleton'
|
|
required |
batch_index
|
int
|
Which sample in the batch to convert. Defaults to 0 (the common single-frame call site). |
0
|
anchor_ind
|
Optional[int]
|
Centroid-only packaging — when |
None
|
tracks
|
Optional[list['sio.Track']]
|
Multi-class identity packaging — a list of |
None
|
identities
|
Optional[list['sio.Identity']]
|
Multi-class identity packaging — a list of
|
None
|
collapse_skeleton
|
Optional['sio.Skeleton']
|
Centroid-only collapse — when supplied (a 1-node
'centroid' skeleton), standalone-centroid output is packaged on
it with the centroid at node 0, instead of NaN-padding the
multi-node |
None
|
Returns:
| Type | Description |
|---|---|
list['sio.PredictedInstance']
|
One |
Notes
Coordinates are taken verbatim from pred_keypoints —
assumed to already be in original-image space. Per-keypoint
scores come from pred_peak_values; per-instance scores
from instance_scores if present, else the SUM of node scores
(np.nansum), matching legacy SingleInstancePredictor.
Centroid-only mode (pred_keypoints is None and
pred_centroids is not None): packages each predicted
centroid into a PredictedInstance with the centroid
coordinate at anchor_ind (or node 0 if unset) and NaN at
every other node. Per-instance score = centroid value.
Source code in sleap_nn/inference/outputs.py
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 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 | |
to_labels(skeleton, videos=None, anchor_ind=None, tracks=None, *, identities=None, collapse_skeleton=None, emit_centroid='instance', source='center_of_mass', mask_output='mask', polygon_epsilon=0.01, keep_empty_frames=False)
¶
Convert this Outputs to a sleap_io.Labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skeleton
|
'sio.Skeleton'
|
|
required |
videos
|
Optional[list['sio.Video']]
|
List of |
None
|
anchor_ind
|
Optional[int]
|
Forwarded to :meth: |
None
|
tracks
|
Optional[list['sio.Track']]
|
Multi-class identity |
None
|
identities
|
Optional[list['sio.Identity']]
|
Multi-class canonical |
None
|
collapse_skeleton
|
Optional['sio.Skeleton']
|
When set (a 1-node 'centroid' skeleton), a
standalone centroid model's output is packaged on it instead of
the original multi-node |
None
|
emit_centroid
|
str
|
Output representation for centroid-only packaging:
|
'instance'
|
source
|
str
|
|
'center_of_mass'
|
mask_output
|
str
|
Segmentation-mask output representation: |
'mask'
|
polygon_epsilon
|
float
|
Douglas-Peucker tolerance (fraction of perimeter) for the polygon/both ROIs. |
0.01
|
keep_empty_frames
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
'sio.Labels'
|
A |
Notes
For full multi-video / per-frame metadata handling, use
:meth:Predictor.predict which aggregates per-batch
Outputs into a single sio.Labels.
Source code in sleap_nn/inference/outputs.py
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 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 | |
to_masks(batch_index=0)
¶
Convert one batch slot's masks into sio.PredictedSegmentationMasks.
Each entry of pred_masks[batch_index] is a dict with a boolean
"mask", a float "score", and "scale"/"offset" mapping
the mask back to image pixels (image_coord = mask_coord / scale +
offset). By default mask is at output-stride resolution; with
full_res_masks it is at original-image resolution with identity
scale/offset. scale/offset are read with identity
defaults for back-compat callers that build this dict directly. An entry
may also carry optional "instance"/"track"/"tracking_score"
provenance (set by the SAM mask layer per PLAN L8 when the mask was
produced from a paired pose/centroid/track); these default to absent so
the model-driven seg layers are unchanged. Each entry becomes a
sio.PredictedSegmentationMask (stored in LabeledFrame.masks).
Returns [] when there are no masks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_index
|
int
|
Which sample in the batch to convert. |
0
|
Source code in sleap_nn/inference/outputs.py
to_rois(batch_index=0, epsilon=0.01)
¶
Convert one batch slot's masks into simplified sio.PredictedROIs.
Each predicted mask's exterior silhouette is extracted via sio
to_polygon() (honoring the mask's scale/offset, so coordinates are
image-space) and Douglas-Peucker-simplified with tolerance epsilon
times the silhouette perimeter. Used for mask_output polygon/both;
the masks themselves are left exact. Returns [] when there are no
masks or none has a polygonal silhouette.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_index
|
int
|
Which sample in the batch to convert. |
0
|
epsilon
|
float
|
Simplification tolerance as a fraction of the perimeter. |
0.01
|