run
sleap_nn.inference.run
¶
Top-level predict — one-call inference from model paths to Labels.
This is the "I just want predictions" entry point. It builds a
:class:Predictor, runs inference, and returns sio.Labels. For
more control (streaming, raw Outputs, custom filtering), use
:class:Predictor directly.
Usage::
from sleap_nn.inference import predict
# Simplest call — returns sio.Labels
labels = predict("video.mp4", model_paths=["/path/to/model"])
# With prediction-time overrides
labels = predict(
"video.mp4",
model_paths=["/path/to/centroid", "/path/to/centered_instance"],
peak_threshold=0.3,
centroid_threshold=0.5,
keypoint_threshold=0.1,
)
# Save to disk
labels = predict("video.mp4", model_paths=[...], output_path="preds.slp")
Functions:
| Name | Description |
|---|---|
predict |
Build a predictor, run inference, return Labels. |
save_analysis_h5_files |
Write SLEAP Analysis HDF5 file(s) from a predicted |
save_predictions |
Save predicted |
predict(source, *, model_paths=None, export_dir=None, device='auto', batch_size=4, runtime='auto', backbone_ckpt_path=None, head_ckpt_path=None, preprocess_config=None, anchor_part=None, paf_workers=0, centroid_only=False, emit_centroid='instance', max_edge_length_ratio=0.25, dist_penalty_weight=1.0, n_points=10, min_instance_peaks=0, min_line_scores=0.25, fg_threshold=0.5, min_mask_area=0, center_nms_kernel=3, mask_cleanup=False, mask_cleanup_radius=0, distance_gate_alpha=None, merge_fragments=False, merge_method='greedy', merge_thresholds=(0.85, 0.6, 0.4), merge_w_valley=1.0, merge_w_offset=0.25, merge_dilate=1, full_res_masks=False, mask_output='mask', polygon_epsilon=0.01, mask_backend=None, sam_checkpoint=None, sam_model_type='vit_h', sam3_model_id='facebook/sam3', sam_prompt_mode='pose', sam_anchor_ind=None, sam_disjointify_masks=False, overlay_path=None, frames=None, peak_threshold=None, centroid_threshold=None, keypoint_threshold=None, max_instances=None, integral_refinement=None, integral_patch_size=None, return_confmaps=False, return_crops=False, return_pafs=False, return_paf_graph=False, return_class_maps=False, return_class_vectors=False, filter_config=None, tracker_config=None, output_path=None, output_format='slp', embed='false', restore_source_videos=False, save_embedding_vectors=None, clean_empty_frames=False, progress_callback=None, tracking_progress_callback=None)
¶
Build a predictor, run inference, return Labels.
Exactly one of model_paths or export_dir must be provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Any
|
Video path, |
required |
model_paths
|
Optional[List[str]]
|
Trained model directories — or a path to a model's
|
None
|
export_dir
|
Optional[str]
|
Path to an exported ONNX/TRT directory (alternative
to |
None
|
device
|
str
|
|
'auto'
|
batch_size
|
int
|
Frames per batch. |
4
|
runtime
|
str
|
Runtime for an exported model: |
'auto'
|
backbone_ckpt_path
|
Optional[str]
|
Optional backbone weight override. |
None
|
head_ckpt_path
|
Optional[str]
|
Optional head weight override. |
None
|
preprocess_config
|
Optional[Any]
|
Optional OmegaConf preprocessing overrides. |
None
|
anchor_part
|
Optional[str]
|
Override centroid anchor node name. |
None
|
paf_workers
|
int
|
CPU worker processes for bottom-up PAF grouping. |
0
|
centroid_only
|
bool
|
Force centroid-only output even when a
centered-instance model is among |
False
|
emit_centroid
|
str
|
Centroid-only output representation: |
'instance'
|
max_edge_length_ratio
|
float
|
Bottom-up PAF max edge length ratio. |
0.25
|
dist_penalty_weight
|
float
|
Bottom-up PAF distance penalty weight. |
1.0
|
n_points
|
int
|
Bottom-up PAF line integration sample count. |
10
|
min_instance_peaks
|
float
|
Bottom-up min peaks for a valid instance. |
0
|
min_line_scores
|
float
|
Bottom-up per-edge match threshold. (These five apply only to plain bottom-up models.) |
0.25
|
fg_threshold
|
float
|
Foreground probability threshold for binarizing the segmentation map (bottom-up segmentation only). |
0.5
|
min_mask_area
|
int
|
Minimum predicted-mask area in original-image pixels;
smaller masks are dropped to suppress over-segmentation. |
0
|
center_nms_kernel
|
int
|
Odd window size for center-peak NMS; larger merges nearby duplicate centers (bottom-up segmentation only). |
3
|
mask_cleanup
|
bool
|
Keep-largest-CC + hole-fill per mask (bottom-up segmentation only). |
False
|
mask_cleanup_radius
|
int
|
Morphological open->close radius (output-stride
pixels) applied during |
0
|
distance_gate_alpha
|
Optional[float]
|
Adaptive distance-gate strength; |
None
|
merge_fragments
|
bool
|
Enable the RAG fragment-merge that re-fuses
over-segmented animal halves while keeping touching distinct animals
apart; |
False
|
merge_method
|
str
|
|
'greedy'
|
merge_thresholds
|
tuple
|
Greedy-merge decreasing affinity thresholds (default
|
(0.85, 0.6, 0.4)
|
merge_w_valley
|
float
|
Center-valley merge-term weight (default |
1.0
|
merge_w_offset
|
float
|
Offset-agreement merge-term weight (default |
0.25
|
merge_dilate
|
int
|
Merge contact-test dilation iterations (default |
1
|
full_res_masks
|
bool
|
Encode masks at full original resolution instead of the
output-stride grid (default |
False
|
mask_output
|
str
|
Mask output representation — |
'mask'
|
polygon_epsilon
|
float
|
Douglas-Peucker tolerance (fraction of perimeter) for
|
0.01
|
mask_backend
|
Optional[str]
|
Explicit SAM mask backend (PLAN L2): |
None
|
sam_checkpoint
|
Optional[str]
|
SAM1 checkpoint path (required for |
None
|
sam_model_type
|
str
|
SAM1 model registry key. |
'vit_h'
|
sam3_model_id
|
str
|
Hugging Face model id for the gated SAM3 path
( |
'facebook/sam3'
|
sam_prompt_mode
|
str
|
|
'pose'
|
sam_anchor_ind
|
Optional[int]
|
Centroid anchor node index for |
None
|
sam_disjointify_masks
|
bool
|
Make per-frame masks disjoint when >=2 instances. |
False
|
overlay_path
|
Optional[str]
|
Optional review-overlay PNG path (PLAN L4; SAM path only). |
None
|
frames
|
Optional[List[int]]
|
Frame indices to predict. |
None
|
peak_threshold
|
Optional[float]
|
Override peak threshold for all stages. |
None
|
centroid_threshold
|
Optional[float]
|
Override centroid-stage threshold (top-down). |
None
|
keypoint_threshold
|
Optional[float]
|
Override centered-instance threshold (top-down). |
None
|
max_instances
|
Optional[int]
|
Cap on instances per frame. |
None
|
integral_refinement
|
Optional[str]
|
|
None
|
integral_patch_size
|
Optional[int]
|
Refinement patch size. |
None
|
return_confmaps
|
bool
|
Keep confidence maps on Outputs. |
False
|
return_crops
|
bool
|
Keep per-instance crops on Outputs (top-down). |
False
|
return_pafs
|
bool
|
Keep part-affinity fields on Outputs (bottom-up). |
False
|
return_paf_graph
|
bool
|
Keep the PAF graph on Outputs (bottom-up). |
False
|
return_class_maps
|
bool
|
Keep class maps on Outputs (multi-class bottom-up). |
False
|
return_class_vectors
|
bool
|
Keep class vectors on Outputs (multi-class top-down). |
False
|
filter_config
|
Optional['FilterConfig']
|
Post-inference :class: |
None
|
tracker_config
|
Optional['TrackerConfig']
|
:class: |
None
|
output_path
|
Optional[str]
|
If set, save the Labels to this path. |
None
|
output_format
|
Union[str, Sequence[str]]
|
Format to save the Labels in when |
'slp'
|
embed
|
Union[str, bool]
|
Image-embedding policy for a |
'false'
|
restore_source_videos
|
bool
|
On a non-embedding |
False
|
save_embedding_vectors
|
Optional[bool]
|
Whether to persist appearance (re-ID) vectors on
the saved detections. |
None
|
clean_empty_frames
|
bool
|
Drop frames with no instances. |
False
|
progress_callback
|
Optional[Callable[[int, int], None]]
|
|
None
|
tracking_progress_callback
|
Optional[Callable[[int, int], None]]
|
|
None
|
Returns:
| Type | Description |
|---|---|
Labels
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
Source code in sleap_nn/inference/run.py
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 | |
save_analysis_h5_files(labels, slp_output_path, video_index=None)
¶
Write SLEAP Analysis HDF5 file(s) from a predicted Labels object.
Analysis HDF5 files store a single video each, so one file is written per
video. The video name is embedded in the filename when more than one video
is exported (mirroring the multi-video .slp naming). Videos with no
predicted frames are skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
Predicted |
required |
slp_output_path
|
Union[str, Path]
|
Path to the canonical |
required |
video_index
|
Optional[int]
|
If not |
None
|
Returns:
| Type | Description |
|---|---|
List[Path]
|
List of |
Source code in sleap_nn/inference/run.py
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 | |
save_predictions(labels, output_path, output_format='slp', video_index=None, embed='false', restore_source_videos=False, save_embedding_vectors=None)
¶
Save predicted Labels to disk in the requested format(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
Predicted |
required |
output_path
|
Union[str, Path]
|
Canonical |
required |
output_format
|
Union[str, Sequence[str]]
|
One format or several — |
'slp'
|
video_index
|
Optional[int]
|
Restrict the analysis HDF5 export to a single video index;
|
None
|
embed
|
Union[str, bool]
|
Image-embedding policy for the |
'false'
|
restore_source_videos
|
bool
|
On a non-embedding |
False
|
save_embedding_vectors
|
Optional[bool]
|
Whether to persist appearance (re-ID) vectors
attached to the detections. |
None
|
Returns:
| Type | Description |
|---|---|
List[Path]
|
The list of analysis HDF5 paths written (empty unless |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any requested format is not |