bytetrack
sleap_nn.tracking.mot.bytetrack
¶
ByteTrack (Zhang et al., 2022) and BoT-SORT (Aharon et al., 2022).
ByteTrack associates every detection box: high-score detections first, then the
remaining tracked tracks against LOW-score detections (occluded animals), then
unconfirmed tracks. BoT-SORT adds a [cx, cy, w, h] Kalman filter and fuses
re-ID appearance into the first stages -- here only inside the geometric gate and
only where appearance discriminates. Original implementations from the published
algorithms.
Algorithm (per frame, after every live track is predicted to this step; sim is
the geometric similarity of a track's prediction to a detection, s the
detection score):
- Split the detections with the references' strict comparisons:
D_hi = {s > track_high_threshold}andD_lo = {track_low_threshold < s < track_high_threshold}. Everything else (includingsexactly at the high threshold) is ignored. OnlyD_hidetections scoring at leastnew_track_thresholdmay start a track. - Stage 1: confirmed and lost tracks vs
D_hi. Cost1 - sim * s(score fusion,fuse_score) or1 - sim; cost cap1 - min_match_similarity. A lost track that matches is re-activated with its id. - Stage 2: the tracks still unmatched that were matched at the previous step
(
CONFIRMED, notLOST) vsD_lo: cost1 - sim(never fused), cap1 - low_stage_min_similarity. This is what keeps an occluded animal -- whose detection score drops -- on its track. Tracks unmatched after this stage becomeLOST. - Stage 3: tentative tracks vs the
D_hidetections left after stage 1, stage-1 cost, cap1 - unconfirmed_min_similarity. A tentative track left unmatched dies (tentative_dies_on_miss). - Duplicate removal (reference
remove_duplicate_stracks): every pair of a track matched this frame and a lost track whose predicted box overlaps that detection's box with IoU aboveduplicate_similarityis a duplicate; the one with the shorter history (last_frame - start_frame; ties remove the matched one) is removed. A removed matched track's detection stays untracked and does not start a track. Detections that would start a track count as matched tracks with an empty history, so a would-be track duplicating a lost track is not started.
BoT-SORT changes stages 1 and 3 only (stage 2 stays geometric)::
d_geo = 1 - sim * s (or 1 - sim without fuse_score)
far = sim < proximity_threshold
cos = cosine(track appearance EMA, detection vector)
in_gate = cos >= gate and not far
use appearance where: in_gate, and the cell is the top in-gate cosine of its
row AND of its column, each by a top-1 minus top-2 margin > 0 and
>= margin (``paper`` preset: every in-gate cell, as published)
C = min(d_geo, (1 - cos) / 2) + 1e-3 * d_geo where appearance is used
= d_geo elsewhere
The gate is appearance_threshold (calibrated EMA gate under "auto",
disabled when the video's calibration says appearance does not discriminate; see
:meth:BaseMOTTracker._appearance_gate); the margin is the calibrated
m_assoc. Appearance therefore never creates a match outside the geometric
gate, never lowers the cost of a pair it ranks below another candidate, and
non-discriminative vectors leave the tracker identical to its geometric
(ByteTrack-style) self.
Adaptations to sleap-nn (documented deviations from the references):
- The geometric similarity is the tracker's resolved kind (IoU of padded boxes, OKS of the predicted pose, or centroid proximity).
- Pose IoU compares the same physical extent on both sides. A keypoint box only
spans the VISIBLE nodes, so occlusion -- exactly when a detection's score drops
into
D_lo-- shrinks it, while the track's Kalman box keeps the full body (the references' detectors predict full-extent boxes even under occlusion). The similarity of a pose pair is therefore the larger of the Kalman-box IoU and the IoU of the padded boxes of the nodes visible in BOTH the track's predicted pose (last pose shifted by the Kalman motion) and the detection (:func:shared_node_iou; at least 2 shared nodes). The published thresholds (stage-2 0.5, fused stage-1 0.2) keep their IoU meaning. - Duplicate removal uses the IoU of the lost track's predicted box and the
matched detection's box (the reference test; the detection stands in for the
updated tracked box) for the
iouandcentroidsimilarities, and the OKS of the predicted pose foroks(a left/right-flipped pose overlaps its original box). The centroid kernel must not be used here: at 0.85 it admits ~3.5x the displacement an IoU of 0.85 does, which removed the tracks of nearby animals after a single missed detection. - Tracks are labeled from
min_hitsmatches on (retroactively), never on the first frame alone; BoT-SORT's reference also emits unconfirmed tracks, these stay unlabeled until confirmed. - The geometric tie-break
1e-3 * d_geoonly applies where the appearance cost is in use, so BoT-SORT without usable appearance reproduces ByteTrack's costs exactly. - BoT-SORT with no usable appearance vector (
FrameDetections.has_embedding) associates on geometry alone, with a warning; refusing vector-less input isapply_tracking's job.
Classes:
| Name | Description |
|---|---|
BoTSORTTracker |
BoT-SORT tracker (see :mod: |
ByteTracker |
ByteTrack tracker (see :mod: |
Functions:
| Name | Description |
|---|---|
shared_node_iou |
IoU of keypoint boxes built from the nodes visible on BOTH sides of a pair. |
BoTSORTTracker
¶
Bases: ByteTracker
BoT-SORT tracker (see :mod:sleap_nn.tracking.mot.bytetrack).
Methods:
| Name | Description |
|---|---|
__init__ |
Create a tracker for one video. |
Source code in sleap_nn/tracking/mot/bytetrack.py
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 | |
__init__(params, carrier='pose', n_nodes=None)
¶
Create a tracker for one video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
MOTParams
|
Resolved :class: |
required |
carrier
|
str
|
|
'pose'
|
n_nodes
|
Optional[int]
|
Skeleton size for pose detections ( |
None
|
Source code in sleap_nn/tracking/mot/bytetrack.py
ByteTracker
¶
Bases: BaseMOTTracker
ByteTrack tracker (see :mod:sleap_nn.tracking.mot.bytetrack).
Class attributes
size_velocity: Kalman state indices zeroed before predicting a track that
was not matched at the previous step, per filter (ByteTrack: the
height velocity; the area velocity for xysr). Selects
lost_zero_velocity for the filter in use.
Methods:
| Name | Description |
|---|---|
__init__ |
Create a tracker for one video. |
Source code in sleap_nn/tracking/mot/bytetrack.py
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 | |
__init__(params, carrier='pose', n_nodes=None)
¶
Create a tracker for one video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
MOTParams
|
Resolved :class: |
required |
carrier
|
str
|
|
'pose'
|
n_nodes
|
Optional[int]
|
Skeleton size for pose detections ( |
None
|
Source code in sleap_nn/tracking/mot/bytetrack.py
shared_node_iou(track_keypoints, det_keypoints, *, box_padding=0.1, min_box_size=None)
¶
IoU of keypoint boxes built from the nodes visible on BOTH sides of a pair.
For each (track, detection) pair, both boxes span only the nodes visible in
the track's pose AND in the detection, and are padded and floored with the
detection rules (box_padding * max(w, h) per side, then at least
min_box_size per axis). A partial (occluded) detection is thus compared
with the same part of the track's pose instead of its full-body box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track_keypoints
|
ndarray
|
|
required |
det_keypoints
|
ndarray
|
|
required |
box_padding
|
float
|
Padding per side, as a fraction of |
0.1
|
min_box_size
|
Optional[float]
|
Minimum padded side (px) per axis; |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
|