reconciliation
sleap_nn.inference.sam.reconciliation
¶
ID reconciliation for matching SAM3 masks to poses or input masks.
Lifted from talmolab/sam-track (BSD-3-Clause) at commit
7b2531d92b5035f5f83016b12350f7c394b92522:
https://github.com/talmolab/sam-track/blob/7b2531d92b5035f5f83016b12350f7c394b92522/src/sam_track/reconciliation.py
This attribution header was added, and the implementation was subsequently modified in sleap-nn. The changes relative to the upstream source are:
IDReconciler.compute_cost_matrixwas vectorized across masks (numerically identical to the original triple-loop).- Keypoint visibility now requires BOTH x and y to be finite (both-axes NaN
check), replacing the inherited x-only test, in both
compute_cost_matrixandmatch_frame. - The implicit default
match_predicateis nowrequire_min_keypoints_inside(3)instead of the weakerdefault_match_predicate(>= 1 keypoint inside). match_framenow validates per-frame lengths (masks vs. object_ids vs. scores) and raises a descriptiveValueErrornaming the frame.- The previously-undocumented
ignore_gt_tracksattribute is documented in theIDReconcilerclass docstring (doc-only; no behavior change).
BSD 3-Clause License
Copyright © 2025, Talmo Lab
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
-
Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This module provides tools for: - Matching SAM3 segmentation masks to pose instances using Hungarian algorithm - Matching SAM3 segmentation masks to input masks using IoU - Detecting identity swaps across frames - Building frame-to-ID mappings for output reconciliation
The key insight from experimentation is that SAM3 mid-propagation re-prompting works for adding NEW objects, but NOT for correcting existing tracks. Therefore, identity correction must be done via post-processing with mask-to-pose matching or mask-to-mask matching (IoU-based).
Classes:
| Name | Description |
|---|---|
IDReconciler |
Matches SAM3 masks to poses and reconciles track IDs. |
MaskAssignment |
A single mask-to-mask assignment at a frame. |
MaskReconciler |
Matches SAM3 masks to input masks using IoU and reconciles track IDs. |
MatchContext |
Context for match predicate evaluation. |
SwapEvent |
Detected identity swap. |
TrackAssignment |
A single track assignment at a frame. |
TrackNameResolver |
Resolves SAM3 obj_ids to GT track names via nearest-anchor flood fill. |
Functions:
| Name | Description |
|---|---|
default_match_predicate |
Default match predicate: require at least 1 keypoint inside mask. |
require_centroid_proximity |
Create predicate requiring pose centroid near mask centroid. |
require_min_fraction_inside |
Create predicate requiring minimum fraction of keypoints inside mask. |
require_min_keypoints_inside |
Create predicate requiring minimum keypoints inside mask. |
require_reasonable_mask_area |
Create predicate requiring mask area within bounds. |
IDReconciler
dataclass
¶
Matches SAM3 masks to poses and reconciles track IDs.
This class implements Hungarian algorithm matching between pose instances and SAM3 segmentation masks, using keypoints-inside-mask as the cost metric.
Attributes:
| Name | Type | Description |
|---|---|---|
skeleton |
Skeleton
|
The SLEAP skeleton for node name lookups. |
exclude_nodes |
set[str]
|
Set of node names to exclude from matching. |
match_predicates |
list[MatchPredicate]
|
List of predicates that must all pass for a valid match. |
ignore_gt_tracks |
bool
|
If True, do not propagate GT track names onto assignments (track_name is set to None). |
Example
reconciler = IDReconciler( ... skeleton=handler.skeleton, ... exclude_nodes={"tail0", "tail1"}, ... ) for frame_idx in gt_frame_indices: ... assignments = reconciler.match_frame( ... frame_idx=frame_idx, ... poses=lf.instances, ... masks=result.masks, ... object_ids=result.object_ids, ... ) swaps = reconciler.detect_swaps() id_map = reconciler.build_id_map()
Methods:
| Name | Description |
|---|---|
__post_init__ |
Add default predicate if none provided. |
build_id_map |
Build frame -> {sam3_id -> track_name} mapping. |
clear |
Clear accumulated assignments. |
compute_cost_matrix |
Compute cost matrix for Hungarian matching. |
detect_swaps |
Detect identity swaps from accumulated assignments. |
get_assignments |
Get all accumulated assignments. |
match_frame |
Match poses to masks for a single frame. |
Source code in sleap_nn/inference/sam/reconciliation.py
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 | |
__post_init__()
¶
Add default predicate if none provided.
The implicit default requires at least 3 keypoints inside the mask. The
weaker default_match_predicate (>= 1) is kept defined for direct use
but is no longer the implicit default. require_min_keypoints_inside
is defined later in this module; it resolves at call time, so referencing
it here is fine.
Source code in sleap_nn/inference/sam/reconciliation.py
build_id_map()
¶
Build frame -> {sam3_id -> track_name} mapping.
This can be used to remap SAM3 object IDs to consistent track names in output files.
Returns:
| Type | Description |
|---|---|
dict[int, dict[int, str]]
|
Dictionary mapping frame_idx to {sam3_obj_id: track_name}. |
Source code in sleap_nn/inference/sam/reconciliation.py
clear()
¶
compute_cost_matrix(poses, masks)
¶
Compute cost matrix for Hungarian matching.
The cost is the negative number of visible keypoints inside each mask. Lower cost = better match (more keypoints inside).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poses
|
list[Instance]
|
List of pose instances to match. |
required |
masks
|
ndarray
|
Array of masks with shape (N, H, W). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Cost matrix with shape (n_poses, n_masks). |
Source code in sleap_nn/inference/sam/reconciliation.py
detect_swaps()
¶
Detect identity swaps from accumulated assignments.
A swap occurs when a track name is matched to different SAM3 object IDs across frames.
Returns:
| Type | Description |
|---|---|
list[SwapEvent]
|
List of SwapEvent objects describing detected swaps. |
Source code in sleap_nn/inference/sam/reconciliation.py
get_assignments()
¶
Get all accumulated assignments.
Returns:
| Type | Description |
|---|---|
list[TrackAssignment]
|
List of all TrackAssignment objects from match_frame() calls. |
match_frame(frame_idx, poses, masks, object_ids, scores=None)
¶
Match poses to masks for a single frame.
Uses Hungarian algorithm for optimal assignment, then filters matches through predicates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_idx
|
int
|
Frame index for this match. |
required |
poses
|
list[Instance]
|
List of pose instances to match. |
required |
masks
|
ndarray
|
Array of masks with shape (N, H, W) or (N, 1, H, W). |
required |
object_ids
|
ndarray
|
Array of SAM3 object IDs corresponding to masks. |
required |
scores
|
ndarray | None
|
Optional SAM3 mask detection confidence scores, shape (N,). |
None
|
Returns:
| Type | Description |
|---|---|
list[TrackAssignment]
|
List of valid TrackAssignment objects. |
Source code in sleap_nn/inference/sam/reconciliation.py
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 | |
MaskAssignment
dataclass
¶
A single mask-to-mask assignment at a frame.
Used for matching input (anchor) masks to SAM3 output masks.
Attributes:
| Name | Type | Description |
|---|---|---|
frame_idx |
int
|
Frame index where assignment was made. |
input_track_id |
int
|
Track ID from the input/anchor mask. |
input_track_name |
str | None
|
Track name from the input/anchor mask. |
sam3_obj_id |
int
|
SAM3 object ID that was matched. |
iou |
float
|
Intersection over Union score for the match. |
sam3_score |
float
|
SAM3 mask detection confidence score. |
Source code in sleap_nn/inference/sam/reconciliation.py
MaskReconciler
dataclass
¶
Matches SAM3 masks to input masks using IoU and reconciles track IDs.
This class implements Hungarian algorithm matching between input/anchor masks and SAM3 segmentation masks, using IoU (Intersection over Union) as the cost metric. This enables post-hoc identity correction using sparse ground truth mask annotations.
Unlike IDReconciler (which uses keypoints-in-mask for pose matching), this reconciler works purely with mask overlap, making it suitable for workflows where users have corrected masks at specific frames that should be used as identity anchors.
Attributes:
| Name | Type | Description |
|---|---|---|
min_iou |
float
|
Minimum IoU threshold for a valid match. Matches below this threshold are rejected. |
track_names |
dict[int, str]
|
Optional mapping of input track_id -> name for naming. |
Example
reconciler = MaskReconciler(min_iou=0.3) for frame_idx in anchor_frames: ... assignments = reconciler.match_frame( ... frame_idx=frame_idx, ... input_masks=reader.get_masks(frame_idx), ... input_track_ids=reader.get_track_ids(frame_idx), ... sam3_masks=result.masks, ... sam3_obj_ids=result.object_ids, ... ) swaps = reconciler.detect_swaps() id_map = reconciler.build_id_map()
Methods:
| Name | Description |
|---|---|
build_id_map |
Build frame -> {sam3_id -> track_name} mapping. |
clear |
Clear accumulated assignments. |
compute_cost_matrix |
Compute cost matrix for Hungarian matching. |
compute_iou |
Compute Intersection over Union between two binary masks. |
detect_swaps |
Detect identity swaps from accumulated assignments. |
get_assignments |
Get all accumulated assignments. |
get_iou_stats |
Get IoU statistics from accumulated assignments. |
match_frame |
Match input masks to SAM3 masks for a single frame. |
Source code in sleap_nn/inference/sam/reconciliation.py
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 | |
build_id_map()
¶
Build frame -> {sam3_id -> track_name} mapping.
This can be used to remap SAM3 object IDs to consistent track names in output files.
Returns:
| Type | Description |
|---|---|
dict[int, dict[int, str]]
|
Dictionary mapping frame_idx to {sam3_obj_id: track_name}. |
Source code in sleap_nn/inference/sam/reconciliation.py
clear()
¶
compute_cost_matrix(input_masks, sam3_masks)
¶
Compute cost matrix for Hungarian matching.
The cost is the negative IoU (because Hungarian minimizes cost). Lower cost = better match (higher IoU).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_masks
|
ndarray
|
Input/anchor masks with shape (N, H, W). |
required |
sam3_masks
|
ndarray
|
SAM3 output masks with shape (M, H, W) or (M, 1, H, W). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Cost matrix with shape (n_input, n_sam3). |
Source code in sleap_nn/inference/sam/reconciliation.py
compute_iou(mask1, mask2)
staticmethod
¶
Compute Intersection over Union between two binary masks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask1
|
ndarray
|
First binary mask as (H, W) array. |
required |
mask2
|
ndarray
|
Second binary mask as (H, W) array. |
required |
Returns:
| Type | Description |
|---|---|
float
|
IoU score between 0 and 1. |
Source code in sleap_nn/inference/sam/reconciliation.py
detect_swaps()
¶
Detect identity swaps from accumulated assignments.
A swap occurs when an input track is matched to different SAM3 object IDs across frames.
Returns:
| Type | Description |
|---|---|
list[SwapEvent]
|
List of SwapEvent objects describing detected swaps. |
Source code in sleap_nn/inference/sam/reconciliation.py
get_assignments()
¶
Get all accumulated assignments.
Returns:
| Type | Description |
|---|---|
list[MaskAssignment]
|
List of all MaskAssignment objects from match_frame() calls. |
get_iou_stats()
¶
Get IoU statistics from accumulated assignments.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary with 'min', 'max', 'mean', 'median' IoU values. |
Source code in sleap_nn/inference/sam/reconciliation.py
match_frame(frame_idx, input_masks, input_track_ids, sam3_masks, sam3_obj_ids, scores=None)
¶
Match input masks to SAM3 masks for a single frame.
Uses Hungarian algorithm for optimal assignment, then filters matches by IoU threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_idx
|
int
|
Frame index for this match. |
required |
input_masks
|
ndarray
|
Input/anchor masks with shape (N, H, W). |
required |
input_track_ids
|
ndarray
|
Track IDs corresponding to input masks. |
required |
sam3_masks
|
ndarray
|
SAM3 output masks with shape (M, H, W) or (M, 1, H, W). |
required |
sam3_obj_ids
|
ndarray
|
SAM3 object IDs corresponding to SAM3 masks. |
required |
scores
|
ndarray | None
|
Optional SAM3 mask detection confidence scores, shape (M,). |
None
|
Returns:
| Type | Description |
|---|---|
list[MaskAssignment]
|
List of valid MaskAssignment objects. |
Source code in sleap_nn/inference/sam/reconciliation.py
MatchContext
dataclass
¶
Context for match predicate evaluation.
Attributes:
| Name | Type | Description |
|---|---|---|
frame_idx |
int
|
Frame index where the match was made. |
sam3_obj_id |
int
|
SAM3 object ID of the matched mask. |
cost |
float
|
Raw cost from the cost matrix (negative keypoints inside). |
keypoints_inside |
int
|
Number of visible keypoints inside the mask. |
keypoints_visible |
int
|
Total number of visible keypoints in the pose. |
mask_area |
int
|
Area of the mask in pixels. |
mask_centroid |
tuple[float, float]
|
Centroid of the mask as (x, y). |
Source code in sleap_nn/inference/sam/reconciliation.py
SwapEvent
dataclass
¶
Detected identity swap.
Attributes:
| Name | Type | Description |
|---|---|---|
frame_idx |
int
|
Frame where the swap was detected. |
track_name |
str
|
Name of the track that swapped. |
old_sam3_id |
int
|
Previous SAM3 object ID. |
new_sam3_id |
int
|
New SAM3 object ID after swap. |
Source code in sleap_nn/inference/sam/reconciliation.py
TrackAssignment
dataclass
¶
A single track assignment at a frame.
Attributes:
| Name | Type | Description |
|---|---|---|
frame_idx |
int
|
Frame index where assignment was made. |
pose_track_name |
str | None
|
Name of the pose's track (None if untracked). |
pose_idx |
int
|
Index of the pose in the frame's instance list. |
sam3_obj_id |
int
|
SAM3 object ID that was matched. |
confidence |
float
|
Match quality score (0-1, higher is better). |
sam3_score |
float
|
SAM3 mask detection confidence score. |
Source code in sleap_nn/inference/sam/reconciliation.py
TrackNameResolver
dataclass
¶
Resolves SAM3 obj_ids to GT track names via nearest-anchor flood fill.
This class takes the sparse ID mappings from GT anchor frames and propagates them to all frames using a nearest-anchor approach. Each frame uses the mapping from its closest GT anchor frame.
Attributes:
| Name | Type | Description |
|---|---|---|
gt_anchors |
dict[int, dict[int, str]]
|
Mapping of frame_idx -> {sam3_obj_id: track_name} at GT frames. |
fallback_names |
dict[int, str]
|
Optional mapping of sam3_obj_id -> name for objects without GT matches (e.g., from initial prompt). |
Example
resolver = TrackNameResolver.from_reconciler(reconciler)
Get track name for a specific frame and object¶
name = resolver.get_track_name(frame_idx=150, sam3_obj_id=1)
Get all mappings for batch processing¶
all_mappings = resolver.resolve_all_frames(total_frames=1000)
Methods:
| Name | Description |
|---|---|
__post_init__ |
Cache sorted anchor frames for efficient lookup. |
from_id_map |
Create resolver from an existing ID map. |
from_reconciler |
Create resolver from an IDReconciler with accumulated assignments. |
get_all_sam3_obj_ids |
Get all unique SAM3 object IDs from GT anchors. |
get_all_track_names |
Get all unique track names from GT anchors. |
get_anchor_frames |
Get sorted list of GT anchor frame indices. |
get_anchor_source |
Get the anchor frame and propagation direction for a frame. |
get_canonical_mapping |
Get a canonical sam3_obj_id -> track_name mapping. |
get_mapping_at_frame |
Get the sam3_obj_id -> track_name mapping for a frame. |
get_track_name |
Get track name for a SAM3 obj_id at a given frame. |
resolve_all_frames |
Get resolved mappings for all frames. |
Source code in sleap_nn/inference/sam/reconciliation.py
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 | |
__post_init__()
¶
from_id_map(id_map, fallback_names=None)
classmethod
¶
Create resolver from an existing ID map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_map
|
dict[int, dict[int, str]]
|
Mapping of frame_idx -> {sam3_obj_id: track_name}. |
required |
fallback_names
|
dict[int, str] | None
|
Optional mapping for objects without GT matches. |
None
|
Returns:
| Type | Description |
|---|---|
TrackNameResolver
|
TrackNameResolver initialized with the ID map. |
Source code in sleap_nn/inference/sam/reconciliation.py
from_reconciler(reconciler, fallback_names=None)
classmethod
¶
Create resolver from an IDReconciler with accumulated assignments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reconciler
|
IDReconciler
|
IDReconciler that has processed GT frames. |
required |
fallback_names
|
dict[int, str] | None
|
Optional mapping for objects without GT matches. |
None
|
Returns:
| Type | Description |
|---|---|
TrackNameResolver
|
TrackNameResolver initialized with the reconciler's ID map. |
Source code in sleap_nn/inference/sam/reconciliation.py
get_all_sam3_obj_ids()
¶
Get all unique SAM3 object IDs from GT anchors.
Returns:
| Type | Description |
|---|---|
set[int]
|
Set of all SAM3 object IDs found in GT mappings. |
Source code in sleap_nn/inference/sam/reconciliation.py
get_all_track_names()
¶
Get all unique track names from GT anchors.
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of all track names found in GT mappings. |
Source code in sleap_nn/inference/sam/reconciliation.py
get_anchor_frames()
¶
Get sorted list of GT anchor frame indices.
Returns:
| Type | Description |
|---|---|
list[int]
|
List of frame indices where GT anchors exist. |
get_anchor_source(frame_idx)
¶
Get the anchor frame and propagation direction for a frame.
Useful for debugging and visualization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_idx
|
int
|
The frame index to check. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int | None, str]
|
Tuple of (anchor_frame_idx, direction) where direction is one of: - "anchor": frame_idx is a GT anchor - "forward": propagated forward from an earlier anchor - "backward": propagated backward from a later anchor - "none": no anchors exist |
Source code in sleap_nn/inference/sam/reconciliation.py
get_canonical_mapping()
¶
Get a canonical sam3_obj_id -> track_name mapping.
Returns a single global mapping from SAM3 object IDs to track names. For objects that appear in multiple anchors, uses the name from the first anchor frame.
This is useful for writers that need a single consistent mapping (like BBoxWriter and SegmentationWriter) rather than per-frame mappings.
Returns:
| Type | Description |
|---|---|
dict[int, str]
|
Dictionary mapping sam3_obj_id to track_name. |
Source code in sleap_nn/inference/sam/reconciliation.py
get_mapping_at_frame(frame_idx)
¶
Get the sam3_obj_id -> track_name mapping for a frame.
Uses the mapping from the nearest GT anchor frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_idx
|
int
|
The frame index to get mapping for. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, str]
|
Dictionary mapping sam3_obj_id to track_name. Returns empty dict if no GT anchors exist. |
Source code in sleap_nn/inference/sam/reconciliation.py
get_track_name(frame_idx, sam3_obj_id, default=None)
¶
Get track name for a SAM3 obj_id at a given frame.
Uses the mapping from the nearest GT anchor frame. Falls back to fallback_names, then to a generated name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_idx
|
int
|
The frame index. |
required |
sam3_obj_id
|
int
|
The SAM3 object ID. |
required |
default
|
str | None
|
Optional default name if not found. If None, generates a name like "track_{sam3_obj_id}". |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The resolved track name. |
Source code in sleap_nn/inference/sam/reconciliation.py
resolve_all_frames(total_frames)
¶
Get resolved mappings for all frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_frames
|
int
|
Total number of frames in the video. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, dict[int, str]]
|
Dictionary mapping frame_idx -> {sam3_obj_id: track_name}. Empty frames (no mapping) are not included in the result. |
Source code in sleap_nn/inference/sam/reconciliation.py
default_match_predicate(pose, mask, ctx)
¶
Default match predicate: require at least 1 keypoint inside mask.
require_centroid_proximity(max_dist=100.0)
¶
Create predicate requiring pose centroid near mask centroid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_dist
|
float
|
Maximum allowed distance between centroids in pixels. |
100.0
|
Returns:
| Type | Description |
|---|---|
MatchPredicate
|
A MatchPredicate function. |
Source code in sleap_nn/inference/sam/reconciliation.py
require_min_fraction_inside(min_frac=0.5)
¶
Create predicate requiring minimum fraction of keypoints inside mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_frac
|
float
|
Minimum fraction (0-1) of visible keypoints inside mask. |
0.5
|
Returns:
| Type | Description |
|---|---|
MatchPredicate
|
A MatchPredicate function. |
Source code in sleap_nn/inference/sam/reconciliation.py
require_min_keypoints_inside(min_count=3)
¶
Create predicate requiring minimum keypoints inside mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_count
|
int
|
Minimum number of keypoints required inside mask. |
3
|
Returns:
| Type | Description |
|---|---|
MatchPredicate
|
A MatchPredicate function. |
Source code in sleap_nn/inference/sam/reconciliation.py
require_reasonable_mask_area(min_area=1000, max_area=500000)
¶
Create predicate requiring mask area within bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_area
|
int
|
Minimum mask area in pixels. |
1000
|
max_area
|
int
|
Maximum mask area in pixels. |
500000
|
Returns:
| Type | Description |
|---|---|
MatchPredicate
|
A MatchPredicate function. |