retrack
sleap_nn.inference.sam.retrack
¶
Mask-based re-tracking: refine existing pose/centroid track identities.
This module provides :func:retrack, a thin orchestration over the lifted
:mod:sleap_nn.inference.sam.reconciliation primitives. It implements the
"refine existing tracks" path of the SAM segmentation stack: given a set of
labeled frames whose instances already carry (possibly wrong) track labels,
plus identity-consistent per-frame masks (e.g. from a SAM3 video tracker), it
re-derives a clean, swap-free track assignment for every instance.
The recipe (re-derived from talmolab/sam-track's CLI glue and validated by
prototype P3) is:
- Match. At each frame, Hungarian-match poses to masks by keypoints-inside
(:class:
~sleap_nn.inference.sam.reconciliation.IDReconciler), gated by a match predicate (the default requires >=3 keypoints inside, i.e.require_min_keypoints_inside(3), the recommended strength for re-tracking). - Build anchors. Treat trusted frames (by default the frames whose
instances carry GT/user track labels, else all frames) as identity anchors,
yielding a sparse
frame -> {mask_obj_id: track_name}map. - Canonical remap. Name each
mask_obj_idby majority vote across anchor frames (strict majority wins); obj_ids with no clear majority (an exact tie) are omitted from the canonical map and resolved per-frame via the nearest anchor. - Relabel all frames. For every frame, reassign each matched instance's
trackto the resolved name (creating :class:sio.Trackobjects as needed). Instances with no mask match keep their original track.
This is the post-processing identity-correction path. Per the experimental finding baked into the reconciliation module's docstring, SAM3 mid-propagation re-prompting adds new objects but does not fix existing tracks, so identity correction must happen here, after tracking, via mask<->pose matching.
Classes:
| Name | Description |
|---|---|
RetrackResult |
Result of a :func: |
Functions:
| Name | Description |
|---|---|
retrack |
Refine instance track identities from per-frame masks. |
RetrackResult
dataclass
¶
Result of a :func:retrack run.
Attributes:
| Name | Type | Description |
|---|---|---|
labeled_frames |
list['sio.LabeledFrame']
|
The relabeled frames. Same objects as the input when
|
assignments |
list[TrackAssignment]
|
All :class: |
id_map |
dict[int, dict[int, str]]
|
Sparse anchor map |
canonical_map |
dict[int, str]
|
The global |
resolver |
TrackNameResolver | None
|
The :class: |
num_relabeled |
int
|
Number of instances whose |
num_matched |
int
|
Number of instances that received a mask match. |
anchor_frames |
list[int]
|
Sorted frame indices used as identity anchors. |
Source code in sleap_nn/inference/sam/retrack.py
retrack(labeled_frames, masks, object_ids, skeleton, *, scores=None, match_predicates=None, exclude_nodes=None, anchor_frame_indices=None, fallback_names=None, in_place=True)
¶
Refine instance track identities from per-frame masks.
Re-derives track assignments for labeled_frames by matching each frame's
pose instances to its masks (Hungarian on keypoints-inside), anchoring the
mask_obj_id -> track_name mapping on trusted frames, and relabeling every
instance to the canonical / nearest-anchor name implied by its matched mask.
The mask sequences are aligned to labeled_frames by position:
masks[i] / object_ids[i] describe the same frame as
labeled_frames[i]. Frames whose instances carry GT/user tracks act as
identity anchors by default (override via anchor_frame_indices).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labeled_frames
|
Sequence['sio.LabeledFrame']
|
Frames to re-track, in the same order as |
required |
masks
|
Sequence[ndarray]
|
Per-frame masks; |
required |
object_ids
|
Sequence[ndarray]
|
Per-frame object IDs; |
required |
skeleton
|
'sio.Skeleton'
|
Skeleton for node-name lookups during pose<->mask matching. |
required |
scores
|
Sequence[ndarray] | None
|
Optional per-frame mask confidence scores, |
None
|
match_predicates
|
list[MatchPredicate] | None
|
Predicates (all must pass) gating a valid match. When
|
None
|
exclude_nodes
|
set[str] | None
|
Node names to ignore when counting keypoints-inside (e.g. unreliable tail nodes). |
None
|
anchor_frame_indices
|
Sequence[int] | None
|
Explicit positions into |
None
|
fallback_names
|
dict[int, str] | None
|
Optional |
None
|
in_place
|
bool
|
When |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
RetrackResult
|
class: |
Example
from sleap_nn.inference.sam.reconciliation import ( ... require_min_keypoints_inside, ... ) result = retrack( ... labeled_frames=lfs, ... masks=[m for m in per_frame_masks], ... object_ids=[o for o in per_frame_obj_ids], ... skeleton=labels.skeleton, ... match_predicates=[require_min_keypoints_inside(3)], ... ) result.num_relabeled 12
Source code in sleap_nn/inference/sam/retrack.py
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 | |