Tracking API¶
DeepHMSort is the built-in multi-object tracker. It takes an sv.Detections and
returns the subset that matched a track, with tracker_id filled in — the same
contract as sv.ByteTrack.update_with_detections, so anything already built around
supervision keeps working.
See the tracking guide for what the algorithm does, which knobs matter, and what the defaults assume about your footage.
Tracker¶
modern_yolonas.tracking.DeepHMSort
¶
Online multi-object tracker: detections in, stable ids out.
Usage mirrors sv.ByteTrack, so it is a drop-in for anything already built
around supervision::
from modern_yolonas import Task, YoloNASDetector
from modern_yolonas.tracking import DeepHMSort
detector = YoloNASDetector("yolo_nas_s", conf_threshold=0.4)
tracker = DeepHMSort()
for _, frame, _ in detector.detect_video("match.mp4"):
result = detector.predict(frame, Task.DETECT | Task.EMBED_OBJECTS)
tracked = tracker.update_with_detections(result.detections)
tracked.tracker_id # (N,) stable ids
Or let the detector drive the whole loop, which is the same thing with the
thresholds already lined up — see
:meth:~modern_yolonas.inference.detect.YoloNASDetector.track_video.
The appearance vectors come from detections.data["embedding"]. Anything
(N, D) works, so a purpose-trained re-identification model can be dropped
in where the YOLO-NAS features are; without the key the tracker degrades
cleanly to motion-only association, which is HM-SORT without the "Deep".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track_high_threshold
|
float
|
Detections at or above this score go into the first association round. The paper's 0.6. |
0.6
|
track_low_threshold
|
float
|
Detections below this are dropped entirely; between
this and |
0.4
|
new_track_threshold
|
float
|
Lowest score an unmatched detection may have and
still become a new track. The paper gives 0.5 as "the threshold below
which we discard tracks"; that phrasing also fits
|
0.5
|
match_threshold
|
float
|
Association costs above this are refused in the first round. The paper's 0.8. |
0.8
|
proximity_threshold
|
float
|
IoU-distance above which the appearance cost is not
trusted. See :func: |
0.5
|
appearance_threshold
|
float
|
Cosine-distance above which the appearance cost is discarded. The paper's 0.3. |
0.3
|
expansion
|
float
|
Box growth for the first association round; see
:func: |
0.3
|
expansion_step
|
float
|
Added to |
0.3
|
expansion_rounds
|
int
|
How many scale-up rounds to run. Deep-EIoU's 2. |
2
|
low_score_expansion
|
float
|
Box growth for the low-score round, and for the unconfirmed round. Deep-EIoU hardcodes 0.5 in both places. |
0.5
|
low_score_match_threshold
|
float
|
Cost ceiling for the low-score round. |
0.5
|
unconfirmed_match_threshold
|
float
|
Cost ceiling for the round that gives first-frame tracks a second chance. |
0.7
|
feature_momentum
|
float
|
|
0.9
|
fusion
|
str
|
|
'harmonic'
|
max_lost_seconds
|
float | None
|
How long a track may stay unmatched before it is
discarded, in seconds of video — converted to frames through
The paper keeps every tracklet for the whole sequence, and 2 s is Deep-EIoU's own default ( |
2.0
|
frame_rate
|
float
|
Frames per second of the video being tracked, used only to turn
|
30.0
|
class_aware
|
bool
|
Refuse to associate a detection with a track of a different class. The paper is single-class (athletes) and says nothing about this; off is the faithful reading, and a class-agnostic track simply carries whatever class its last match had. |
False
|
Source code in src/modern_yolonas/tracking/deep_hm_sort.py
38 39 40 41 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 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 | |
max_lost_frames
property
¶
:attr:max_lost_seconds in frames at the current :attr:frame_rate.
Never less than one frame: a positive budget that rounds to zero would discard every track the instant it is missed, which is not what any number the caller wrote could have meant.
tracks
property
¶
Every live track, matched this frame or not, for inspection.
reset()
¶
Forget every track and restart ids from 1.
Call between videos — ids are unique within a tracker instance, not globally, so two videos tracked by the same instance share a namespace.
Source code in src/modern_yolonas/tracking/deep_hm_sort.py
update_with_detections(detections)
¶
Associate one frame's detections with the live tracks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
detections
|
Detections
|
This frame's detections, in source-image pixels. Per-object
appearance vectors are read from |
required |
Returns:
| Type | Description |
|---|---|
Detections
|
The subset of |
Detections
|
|
Detections
|
|
Detections
|
untouched. Rows come back in track-id order. |
Source code in src/modern_yolonas/tracking/deep_hm_sort.py
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 | |
Track state¶
modern_yolonas.tracking.Track
¶
One tracked object.
There is no Kalman filter here, and that is deliberate rather than missing.
Deep-EIoU — the tracker Deep HM-SORT extends — abandons it: a constant-velocity
Gaussian model is a poor description of an athlete changing direction, and the
prediction it contributes is worth less than the expanded box it would be
compared against. So :attr:xyxy is the last box this track actually matched,
not a prediction of where it will be.
Attributes:
| Name | Type | Description |
|---|---|---|
track_id |
Stable id, unique within the tracker instance that made it. |
|
xyxy |
|
|
score |
Confidence of the detection that last matched. |
|
class_id |
Class of the detection that last matched. Tracks are not pinned
to a class unless the tracker was built |
|
feature |
ndarray | None
|
|
state |
See :class: |
|
start_frame |
Frame index the track was created on. |
|
frame_id |
Frame index it was last matched on. |
|
hits |
How many frames it has matched in total. |
|
source_index |
int | None
|
Row of the current frame's detections it matched, or |
Source code in src/modern_yolonas/tracking/track.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 133 134 135 136 137 138 139 140 141 142 143 | |
update(xyxy, score, class_id, feature, frame_id, source_index, momentum)
¶
Attach a detection to this track.
The appearance vector is blended rather than replaced::
v_k = momentum * v_{k-1} + (1 - momentum) * f_k
and then re-normalized — without that last step the running vector drifts off the unit sphere and a dot product stops being a cosine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xyxy
|
ndarray
|
|
required |
score
|
float
|
Its confidence. |
required |
class_id
|
int
|
Its class. |
required |
feature
|
ndarray | None
|
|
required |
frame_id
|
int
|
Current frame index. |
required |
source_index
|
int
|
Row of the current frame's detections. |
required |
momentum
|
float
|
The |
required |
Source code in src/modern_yolonas/tracking/track.py
modern_yolonas.tracking.TrackState
¶
Bases: Enum
Where a track stands this frame.
Members
TRACKED: Matched in the current frame. LOST: Alive but unmatched — still in the association pool, and still eligible to be found again. REMOVED: Out of the pool for good.
Source code in src/modern_yolonas/tracking/track.py
Video helpers¶
modern_yolonas.inference.detect.YoloNASDetector.track_video(source, tracker=None, conf_threshold=None, iou_threshold=None, appearance=True, skip_frames=0)
¶
Detect and track each frame of a video, in one forward pass per frame.
The appearance vectors Deep HM-SORT associates on are the same per-object
embeddings :meth:predict produces, read off the features the detection
pass already computed — so tracking with appearance costs one pass per
frame, not the two a bolted-on re-identification model would need.
The detector runs at the tracker's track_low_threshold unless told
otherwise. That is deliberate: Deep HM-SORT's second association round
exists to hold a track through a frame where the detector wavers, and it
can only do that if those weak detections reach it. Raising
conf_threshold to the tracker's track_high_threshold turns that
round off.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | Path | int
|
Video file path or camera index (0 for webcam). |
required |
tracker
|
DeepHMSort | None
|
A configured :class: |
None
|
conf_threshold
|
float | None
|
Override the tracker-derived detection threshold. |
None
|
iou_threshold
|
float | None
|
Override the instance default. |
None
|
appearance
|
bool
|
Compute per-object embeddings and associate on them.
|
True
|
skip_frames
|
int
|
Process every N-th frame (0 = every frame). Note the tracker sees only the processed frames, so objects move further between them and the expansion has more work to do. |
0
|
Yields:
| Type | Description |
|---|---|
int
|
|
ndarray
|
|
Detections
|
that matched a track. |
Source code in src/modern_yolonas/inference/detect.py
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 | |
modern_yolonas.inference.detect.YoloNASDetector.track_video_to_file(source, output, tracker=None, conf_threshold=None, iou_threshold=None, appearance=True, codec='mp4v', show_fps=False)
¶
Track a video and write it out with ids drawn on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | Path
|
Input video path. |
required |
output
|
str | Path
|
Output video path. |
required |
tracker
|
DeepHMSort | None
|
See :meth: |
None
|
conf_threshold
|
float | None
|
See :meth: |
None
|
iou_threshold
|
float | None
|
Override the instance default. |
None
|
appearance
|
bool
|
See :meth: |
True
|
codec
|
str
|
FourCC codec string. |
'mp4v'
|
show_fps
|
bool
|
Burn the per-frame time and frame rate into the output. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, int | float]
|
Dict with |
dict[str, int | float]
|
the last being how many distinct objects the tracker believes it saw, |
dict[str, int | float]
|
which is the number to watch when tuning: far above the truth means |
dict[str, int | float]
|
ids are fragmenting. |
Source code in src/modern_yolonas/inference/detect.py
modern_yolonas.inference.detect.YoloNASDetector.annotate_tracks(image, detections, show_fps=False)
¶
Draw tracked boxes, coloured and labelled by tracker_id.
Colouring by id rather than by class is what makes an ID-swap visible: the box changes colour the instant the tracker changes its mind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
ndarray
|
BGR frame to draw on. |
required |
detections
|
Detections
|
Output of :meth: |
required |
show_fps
|
bool
|
Overlay the last frame's time and frame rate. |
False
|
Source code in src/modern_yolonas/inference/detect.py
Matching primitives¶
The cost functions are plain numpy and are usable on their own — to build a different tracker, or to check what the tracker is seeing on a frame you care about.
modern_yolonas.tracking.matching.expand_boxes(boxes, expansion)
¶
Grow each box outwards by expansion times its own width and height.
Each side moves out by expansion * w (or * h), so the expanded box is
(1 + 2 * expansion) times as wide and as tall, centred on the original.
expansion=0 returns the box unchanged.
This is ExpansionIoU's remedy for fast motion: two boxes of the same object in consecutive frames can have no overlap at all, which a plain IoU cost reads as "different objects". Expanding both before intersecting restores a usable signal without expanding the detector's own boxes.
.. note::
The reference Deep-EIoU implementation's expand() adds half the
expanded width to each side rather than half the increase, so for the
same e its boxes end up 2 * (1 + e) times as wide — larger than the
(1 + 2e) the name implies. This uses the plain reading. It matters when
porting a tuned expansion value across the two.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boxes
|
ndarray
|
|
required |
expansion
|
float
|
Non-negative growth factor. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in src/modern_yolonas/tracking/matching.py
modern_yolonas.tracking.matching.expansion_iou_distance(a, b, expansion)
¶
1 - IoU between the two box sets after both are expanded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ndarray
|
|
required |
b
|
ndarray
|
|
required |
expansion
|
float
|
Passed to :func: |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in src/modern_yolonas/tracking/matching.py
modern_yolonas.tracking.matching.cosine_distance(a, b)
¶
Pairwise cosine distance, rescaled to [0, 1].
Cosine distance 1 - cos lives in [0, 2] for vectors that may point
opposite ways. Halving it puts it on the same scale as the IoU distance it is
about to be fused with, which is what makes a single threshold — and a
harmonic mean — meaningful across both.
Rows are L2-normalized here, so a caller that skipped normalization, or an EMA that drifted off the unit sphere, still gets a true cosine. A zero row gives distance 0.5 against everything (no information), not a NaN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ndarray
|
|
required |
b
|
ndarray
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in src/modern_yolonas/tracking/matching.py
modern_yolonas.tracking.matching.harmonic_mean(d1, d2)
¶
Element-wise harmonic mean of two cost matrices.
2 * d1 * d2 / (d1 + d2), the form Deep HM-SORT puts in place of
Deep-EIoU's min(d1, d2).
The harmonic mean leans towards the smaller of the two, so a strong cue still
carries the pair — but unlike min it does not ignore the other one. Two
teammates in the same kit have near-identical embeddings; min will happily
match the wrong one on appearance alone, while the harmonic mean lets the
motion cost veto it. That is the paper's whole argument for the swap, and its
ablation is where the ID-switch count drops.
Both inputs are expected in [0, 1]. A zero anywhere makes the result zero,
including 0 against 0, which the plain quotient would make a NaN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d1
|
ndarray
|
|
required |
d2
|
ndarray
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in src/modern_yolonas/tracking/matching.py
modern_yolonas.tracking.matching.fuse_costs(iou_distance, embedding_distance, *, proximity_threshold, appearance_threshold, fusion='harmonic')
¶
Combine a motion cost and an appearance cost into the association cost.
Two gates run before the fusion, both from BoT-SORT by way of Deep-EIoU:
- an appearance cost above
appearance_thresholdis not evidence of anything — same-team players sit well inside it — so it is discarded; - an appearance cost is not trusted for a pair that is nowhere near each
other geometrically, so an IoU distance above
proximity_thresholddiscards it too. Without this, a track can be re-attached across the frame by appearance alone.
A pair whose appearance cost is discarded falls back to the motion cost
alone, rather than being fused with a sentinel. That is a decision the
paper does not make for us, and it is not cosmetic: harmonic_mean(d, 1) is
2d / (d + 1), which is larger than d for every d < 1. Fusing
against a placeholder would therefore charge a pair for evidence that was
never available — and since the gate fires on exactly the distant pairs the
expansion scale-up exists to reach, it would quietly cancel the scale-up
(match_threshold 0.8 would start refusing at an IoU distance of 0.67).
Deep-EIoU's min has no such effect, so falling back also keeps the two
fusions comparable where only one cue exists, which is what makes an ablation
between them mean anything.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iou_distance
|
ndarray
|
|
required |
embedding_distance
|
ndarray | None
|
|
required |
proximity_threshold
|
float
|
IoU-distance gate on the appearance cost. |
required |
appearance_threshold
|
float
|
Cosine-distance gate on the appearance cost. |
required |
fusion
|
str
|
|
'harmonic'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in src/modern_yolonas/tracking/matching.py
modern_yolonas.tracking.matching.linear_assignment(cost, threshold)
¶
Hungarian assignment, with pairs costlier than threshold refused.
Uses scipy.optimize.linear_sum_assignment — an optimal solver over the
whole matrix — and then drops the pairs that came back above threshold.
Gated entries are raised above every candidate first so the solver routes
around them where it can; a gated pair that survives that is still dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost
|
ndarray
|
|
required |
threshold
|
float
|
Highest cost still considered a match. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[int, int]]
|
|
list[int]
|
of |