legacy_predict
sleap_nn.legacy_predict
¶
Entry point for running inference.
Functions:
| Name | Description |
|---|---|
frame_list |
Converts 'n-m' string to list of ints. |
run_inference |
Entry point to run inference on trained SLEAP-NN models. |
frame_list(frame_str)
¶
Converts 'n-m' string to list of ints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_str
|
str
|
string representing range |
required |
Returns:
| Type | Description |
|---|---|
Optional[List[int]]
|
List of ints, or None if string does not represent valid range. |
Source code in sleap_nn/legacy_predict.py
run_inference(data_path=None, input_labels=None, input_video=None, model_paths=None, backbone_ckpt_path=None, head_ckpt_path=None, max_instances=None, max_width=None, max_height=None, ensure_rgb=None, input_scale=None, ensure_grayscale=None, anchor_part=None, only_labeled_frames=False, only_suggested_frames=False, exclude_user_labeled=False, only_predicted_frames=False, no_empty_frames=False, batch_size=4, queue_maxsize=32, video_index=None, video_dataset=None, video_input_format='channels_last', frames=None, crop_size=None, peak_threshold=0.2, filter_overlapping=False, filter_overlapping_method='iou', filter_overlapping_threshold=0.8, filter_min_visible_nodes=0, filter_min_visible_node_fraction=0.0, filter_min_mean_node_score=0.0, filter_min_instance_score=0.0, integral_refinement='integral', integral_patch_size=5, return_confmaps=False, return_pafs=False, return_paf_graph=False, max_edge_length_ratio=0.25, dist_penalty_weight=1.0, n_points=10, min_instance_peaks=0, min_line_scores=0.25, return_class_maps=False, return_class_vectors=False, make_labels=True, output_path=None, device='auto', tracking=False, tracking_window_size=5, min_new_track_points=0, candidates_method='fixed_window', min_match_points=0, features='keypoints', scoring_method='oks', scoring_reduction='mean', robust_best_instance=1.0, oks_stddev=None, track_matching_method='hungarian', max_tracks=None, use_flow=False, of_img_scale=1.0, of_window_size=21, of_max_levels=3, use_kalman=False, kf_track_features='centroid', kf_init_frame_count=10, kf_node_indices=None, kf_reset_gap_size=5, post_connect_single_breaks=False, tracking_target_instance_count=None, tracking_pre_cull_to_target=0, tracking_pre_cull_iou_threshold=0, tracking_clean_instance_count=0, tracking_clean_iou_threshold=0, gui=False)
¶
Entry point to run inference on trained SLEAP-NN models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_path
|
Optional[str]
|
(str) Path to |
None
|
input_labels
|
Optional[Labels]
|
(sio.Labels) Labels object to run inference on. This is an alternative to specifying the data_path. |
None
|
input_video
|
Optional[Video]
|
(sio.Video) Video to run inference on. This is an alternative to specifying the data_path. If both input_labels and input_video are provided, input_labels are used. |
None
|
model_paths
|
Optional[List[str]]
|
(List[str]) List of paths to the directory where the best.ckpt and training_config.yaml are saved. |
None
|
backbone_ckpt_path
|
Optional[str]
|
(str) To run inference on any |
None
|
head_ckpt_path
|
Optional[str]
|
(str) Path to |
None
|
max_instances
|
Optional[int]
|
(int) Max number of instances to consider from the predictions. |
None
|
max_width
|
Optional[int]
|
(int) Maximum width the image should be padded to. If not provided, the values from the training config are used. Default: None. |
None
|
max_height
|
Optional[int]
|
(int) Maximum height the image should be padded to. If not provided, the values from the training config are used. Default: None. |
None
|
input_scale
|
Optional[float]
|
(float) Scale factor to apply to the input image. If not provided, the values from the training config are used. Default: None. |
None
|
ensure_rgb
|
Optional[bool]
|
(bool) True if the input image should have 3 channels (RGB image). If input has only one
channel when this is set to |
None
|
ensure_grayscale
|
Optional[bool]
|
(bool) True if the input image should only have a single channel. If input has three channels (RGB) and this
is set to True, then we convert the image to grayscale (single-channel)
image. If the source image has only one channel and this is set to False, then we retain the single channel input. If not provided, the
values from the training config are used. Default: |
None
|
anchor_part
|
Optional[str]
|
(str) The node name to use as the anchor for the centroid. If not
provided, the anchor part in the |
None
|
only_labeled_frames
|
bool
|
(bool) |
False
|
only_suggested_frames
|
bool
|
(bool) |
False
|
exclude_user_labeled
|
bool
|
(bool) |
False
|
only_predicted_frames
|
bool
|
(bool) |
False
|
no_empty_frames
|
bool
|
(bool) |
False
|
batch_size
|
int
|
(int) Number of samples per batch. Default: 4. |
4
|
queue_maxsize
|
int
|
(int) Maximum size of the frame buffer queue. Default: 32. |
32
|
video_index
|
Optional[int]
|
(int) Integer index of video in .slp file to predict on. To be used with an .slp path as an alternative to specifying the video path. |
None
|
video_dataset
|
Optional[str]
|
(str) The dataset for HDF5 videos. |
None
|
video_input_format
|
str
|
(str) The input_format for HDF5 videos. |
'channels_last'
|
frames
|
Optional[list]
|
(list) List of frames indices. If |
None
|
crop_size
|
Optional[int]
|
(int) Crop size. If not provided, the crop size from training_config.yaml is used.
If |
None
|
peak_threshold
|
Union[float, List[float]]
|
(float) Minimum confidence threshold. Peaks with values below
this will be ignored. Default: 0.2. This can also be |
0.2
|
filter_overlapping
|
bool
|
(bool) If True, removes overlapping instances after inference using greedy NMS. Applied independently of tracking. Default: False. |
False
|
filter_overlapping_method
|
str
|
(str) Similarity metric for filtering overlapping instances. One of "iou" (bounding box) or "oks" (keypoint similarity). Default: "iou". |
'iou'
|
filter_overlapping_threshold
|
float
|
(float) Similarity threshold for filtering. Instances with similarity > threshold are removed (keeping higher-scoring). Typical values: 0.3 (aggressive) to 0.8 (permissive). Default: 0.8. |
0.8
|
filter_min_visible_nodes
|
int
|
(int) Minimum number of visible (non-NaN) keypoints required. Instances with fewer visible nodes are removed. Default: 0 (no filtering by absolute count). |
0
|
filter_min_visible_node_fraction
|
float
|
(float) Minimum fraction of skeleton nodes that must be visible. Value should be in [0, 1]. For example, 0.5 requires at least half of the skeleton's nodes to be detected. Default: 0.0 (no filtering by fraction). |
0.0
|
filter_min_mean_node_score
|
float
|
(float) Minimum mean confidence score across visible nodes. Instances with lower mean node scores are removed. Default: 0.0 (no filtering by mean node score). |
0.0
|
filter_min_instance_score
|
float
|
(float) Minimum overall instance confidence score. Instances with lower scores are removed. Default: 0.0 (no filtering by instance score). |
0.0
|
integral_refinement
|
Optional[str]
|
(str) If |
'integral'
|
integral_patch_size
|
int
|
(int) Size of patches to crop around each rough peak as an integer scalar. Default: 5. |
5
|
return_confmaps
|
bool
|
(bool) If |
False
|
return_pafs
|
bool
|
(bool) If |
False
|
return_class_vectors
|
bool
|
If |
False
|
return_paf_graph
|
bool
|
(bool) If |
False
|
max_edge_length_ratio
|
float
|
(float) The maximum expected length of a connected pair of points as a fraction of the image size. Candidate connections longer than this length will be penalized during matching. Default: 0.25. |
0.25
|
dist_penalty_weight
|
float
|
(float) A coefficient to scale weight of the distance penalty as a scalar float. Set to values greater than 1.0 to enforce the distance penalty more strictly.Default: 1.0. |
1.0
|
n_points
|
int
|
(int) Number of points to sample along the line integral. Default: 10. |
10
|
min_instance_peaks
|
Union[int, float]
|
Union[int, float] Minimum number of peaks the instance should have to be considered a real instance. Instances with fewer peaks than this will be discarded (useful for filtering spurious detections). Default: 0. |
0
|
min_line_scores
|
float
|
(float) Minimum line score (between -1 and 1) required to form a match between candidate point pairs. Useful for rejecting spurious detections when there are no better ones. Default: 0.25. |
0.25
|
return_class_maps
|
bool
|
If |
False
|
make_labels
|
bool
|
(bool) If |
True
|
output_path
|
Optional[str]
|
(str) Path to save the labels file if |
None
|
device
|
str
|
(str) Device on which torch.Tensor will be allocated. One of the
('cpu', 'cuda', 'mps', 'auto').
Default: "auto" (based on available backend either cuda, mps or cpu is chosen). If |
'auto'
|
tracking
|
bool
|
(bool) If True, runs tracking on the predicted instances. |
False
|
tracking_window_size
|
int
|
Number of frames to look for in the candidate instances to match with the current detections. Default: 5. |
5
|
min_new_track_points
|
int
|
We won't spawn a new track for an instance with fewer than this many points. Default: 0. |
0
|
candidates_method
|
str
|
Either of |
'fixed_window'
|
min_match_points
|
int
|
Minimum non-NaN points for match candidates. Default: 0. |
0
|
features
|
str
|
Feature representation for the candidates to update current detections.
One of [ |
'keypoints'
|
scoring_method
|
str
|
Method to compute association score between features from the
current frame and the previous tracks. One of [ |
'oks'
|
scoring_reduction
|
str
|
Method to aggregate and reduce multiple scores if there are
several detections associated with the same track. One of [ |
'mean'
|
robust_best_instance
|
float
|
If the value is between 0 and 1 (excluded), use a robust quantile similarity score for the track. If the value is 1, use the max similarity (non-robust). For selecting a robust score, 0.95 is a good value. |
1.0
|
track_matching_method
|
str
|
Track matching algorithm. One of |
'hungarian'
|
max_tracks
|
Optional[int]
|
Meaximum number of new tracks to be created to avoid redundant tracks. (only for local queues candidate) Default: None. |
None
|
use_flow
|
bool
|
If True, |
False
|
optical flow shifts. Default
|
|
required | |
of_img_scale
|
float
|
Factor to scale the images by when computing optical flow. Decrease
this to increase performance at the cost of finer accuracy. Sometimes
decreasing the image scale can improve performance with fast movements.
Default: 1.0. (only if |
1.0
|
of_window_size
|
int
|
Optical flow window size to consider at each pyramid scale
level. Default: 21. (only if |
21
|
of_max_levels
|
int
|
Number of pyramid scale levels to consider. This is different
from the scale parameter, which determines the initial image scaling.
Default: 3. (only if |
3
|
oks_stddev
|
Optional[float]
|
Keypoint-spread normalization constant for |
None
|
use_kalman
|
bool
|
If True, |
False
|
kf_track_features
|
str
|
What the Kalman motion model tracks: |
'centroid'
|
kf_init_frame_count
|
int
|
Number of warm-up frames tracked with the base path before
the Kalman filters are fit via EM. Default: 10. (only if |
10
|
kf_node_indices
|
Optional[List[int]]
|
Skeleton node (row) indices to track with the motion model.
|
None
|
kf_reset_gap_size
|
int
|
Number of consecutive missed frames after which a stale
track's filter is reset. Default: 5. (only if |
5
|
post_connect_single_breaks
|
bool
|
If True and |
False
|
tracking_target_instance_count
|
Optional[int]
|
Target number of instances to track per frame. (default: None) |
None
|
tracking_pre_cull_to_target
|
int
|
If non-zero and target_instance_count is also non-zero, then cull instances over target count per frame before tracking. (default: 0) |
0
|
tracking_pre_cull_iou_threshold
|
float
|
If non-zero and pre_cull_to_target also set, then use IOU threshold to remove overlapping instances over count before tracking. (default: 0) |
0
|
tracking_clean_instance_count
|
int
|
Target number of instances to clean after tracking. (default: 0) |
0
|
tracking_clean_iou_threshold
|
float
|
IOU to use when culling instances after tracking. (default: 0) |
0
|
gui
|
bool
|
(bool) If True, outputs JSON progress lines for GUI integration instead of Rich progress bars. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
|
Returns |
.. deprecated:: This function is deprecated and slated for removal in a future release. Prefer the new flow:
* **Inference on a checkpoint**::
from sleap_nn.inference import Predictor
predictor = Predictor.from_model_paths([model_dir])
labels = predictor.predict(provider, make_labels=True, ...)
* **Streaming to disk**: ``predictor.predict_to_file(...)``
* **Pure-tracking retrack**: ``Predictor.retrack(labels, tracker_config)``
``sleap-nn predict`` / ``sleap-nn track`` already route through the
new flow internally; this function is the only remaining
Python-level legacy entry point.
Source code in sleap_nn/legacy_predict.py
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 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 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 | |