tracking
sleap_nn.inference.tracking
¶
Tracking integration for the new Predictor flow.
Wraps :class:sleap_nn.tracking.tracker.Tracker (and its post-processing
helpers cull_instances / connect_single_breaks) as a value-typed
config + a labels-in / labels-out function. TrackerConfig.tracker selects the
tracker: "classic" (the default, the tracker above) or one of the modern MOT
trackers of :mod:sleap_nn.tracking.mot (SORT, DeepSORT, StrongSORT, ByteTrack,
BoT-SORT, OC-SORT, Deep OC-SORT), which share the same labels-in / labels-out
contract and the same per-video track naming.
Why a config: keeps :class:~sleap_nn.inference.predictor.Predictor
picklable and lets the CLI / factory layer build the tracking
configuration once and forward it as plain data, the same shape as
FilterConfig from PR 8.
Why labels-in / labels-out: the tracker is stateful across frames and
operates on sio.PredictedInstance objects, so the natural seam is
after :meth:Predictor.to_labels converts Outputs to
LabeledFrames. apply_tracking builds a fresh Tracker per
video (no shared state across predict() invocations, or across the
videos of one Labels), runs it in (video, frame_idx) order, and
tracks the given Labels in place, so every field it does not track
(the other carrier, ROIs, suggestions, ...) passes through untouched.
What this does NOT cover:
--stream-to-file+--tracking— still a UsageError. End-of- stream post-processing (cull_instances/connect_single_breaks) needs the full LabeledFrame list, which defeats streaming.- Pre-tracking filter knobs (
filter_max_overlap_*/filter_min_node_count/filter_min_*_score) — these run as a separate stage in legacyrun_inferenceand aren't routed through the new flow yet. The CLI predicate keeps falling through to legacy when those flags are set.
Classes:
| Name | Description |
|---|---|
CarrierCounts |
How many detections each carrier holds, and how many of them qualify. |
TrackerConfig |
Frozen value type capturing every knob |
Functions:
| Name | Description |
|---|---|
appearance_blend_way_out |
How to make an |
apply_tracking |
Track predicted instances on every frame and run post-cleanup, IN PLACE. |
check_embedding_mask_tracking_options |
Refuse the pose-only options for appearance-only tracking of masks. |
check_tracking_plan |
Raise what :func: |
classic_linking_params |
The MOT parameters the classic tracker's identity linking reads. |
count_carriers |
Count each carrier's detections, and those satisfying |
embedding_carriers |
Where the appearance (re-ID) vectors are, per carrier. |
resolve_mask_tracking_options |
|
resolve_mot_params |
The :class: |
resolve_tracked_carrier |
The carrier :func: |
tracker_config_params |
|
validate_mot_config |
Refuse options the selected tracker family would silently ignore. |
CarrierCounts
¶
How many detections each carrier holds, and how many of them qualify.
A .slp can hold both carriers at once -- top-down segmentation and SAM output
are poses with linked masks -- so "which carrier?" questions (where are the
appearance vectors? which carrier was tracked?) are answered by COUNTING, never
by "any detection anywhere": a single stray mask or vector must not flip a whole
file. Built by :func:count_carriers with the qualifying predicate of the
question being asked (:func:embedding_carriers for appearance vectors).
Methods:
| Name | Description |
|---|---|
n_with |
Qualifying detections on |
total |
Detections on |
Attributes:
| Name | Type | Description |
|---|---|---|
dominant |
Optional[str]
|
The carrier holding MORE qualifying detections; |
Source code in sleap_nn/inference/tracking.py
TrackerConfig
¶
Frozen value type capturing every knob run_tracker exposes.
Mirrors :func:sleap_nn.tracking.tracker.run_tracker's signature.
Picklable so it can sit on :class:Predictor without compromising
the picklability contract from PR 2.
tracker selects the tracker family. "classic" (the default) reads the
candidate-window options; a modern MOT tracker (:data:MOT_TRACKER_NAMES)
reads the MOT options (None = the tracker's preset, see
:meth:~sleap_nn.tracking.mot.params.MOTParams.for_tracker) plus the shared
max_tracks (the known animal count: closed-set identity linking),
oks_stddev (None = the MOT default), scoring_method (when
explicit: oks / iou / euclidean_dist -> centroid similarity)
and features (keypoints / masks: the carrier). Every option of the
other family must stay at its default (see :func:validate_mot_config).
link_tracklets runs identity linking after either family.
Attributes:
| Name | Type | Description |
|---|---|---|
candidates_method_explicit |
bool
|
Whether |
features_explicit |
bool
|
Whether |
follows_vector_carrier |
bool
|
Whether the tracked carrier is the one holding the appearance vectors. |
is_mot |
bool
|
Whether a modern MOT tracker (not the classic tracker) runs. |
requires_vectors |
bool
|
Whether a run without any appearance vector is refused. |
runs_linking |
bool
|
Whether identity linking runs at the end of each video. |
scoring_method_explicit |
bool
|
Whether |
uses_appearance |
bool
|
Whether the run reads the detections' appearance (re-ID) vectors. |
Source code in sleap_nn/inference/tracking.py
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 | |
candidates_method_explicit
property
¶
Whether candidates_method was chosen explicitly (else re-resolved).
features_explicit
property
¶
Whether features was chosen explicitly (else it is re-resolved).
follows_vector_carrier
property
¶
Whether the tracked carrier is the one holding the appearance vectors.
Classic: features="embeddings". MOT: an appearance tracker whose
features were not chosen explicitly. Otherwise the carrier follows
the geometry (:func:resolve_tracked_carrier).
is_mot
property
¶
Whether a modern MOT tracker (not the classic tracker) runs.
requires_vectors
property
¶
Whether a run without any appearance vector is refused.
Classic: features="embeddings" or appearance_weight > 0. MOT: the
appearance trackers (deepsort, strongsort, botsort, deepocsort); for
deepsort / strongsort the TRACKED carrier must hold them, botsort /
deepocsort only warn when the vectors ride on the other carrier.
runs_linking
property
¶
Whether identity linking runs at the end of each video.
link_tracklets (either tracker family), or for a MOT tracker a known
max_tracks (closed-set linking) -- given as an option or in
mot_params, mirroring :attr:MOTParams.link
<sleap_nn.tracking.mot.params.MOTParams.link> without resolving the
parameters (which can raise).
scoring_method_explicit
property
¶
Whether scoring_method was chosen explicitly (else it is re-resolved).
uses_appearance
property
¶
Whether the run reads the detections' appearance (re-ID) vectors.
Classic: features="embeddings" or an appearance_weight blend. MOT:
an appearance tracker, or identity linking (:attr:runs_linking: also
when requested through mot_params), which uses vectors where they are
present.
appearance_blend_way_out(vector_carrier)
¶
How to make an appearance_weight blend read vectors on vector_carrier.
For a blend that tracks the other carrier, whose detections carry no vector.
Source code in sleap_nn/inference/tracking.py
apply_tracking(labels, config, progress_callback=None)
¶
Track predicted instances on every frame and run post-cleanup, IN PLACE.
Mirrors :func:sleap_nn.tracking.tracker.run_tracker but accepts a
sio.Labels directly (instead of a list of LabeledFrames) and runs a fresh
Tracker per VIDEO: tracks never span two videos, and their names continue
across videos (video 2's first new track follows video 1's last) so no two
videos share a track name.
The input is modified and returned -- it is NOT copied. Tracks and tracking
scores are written onto the input's detections, each frame's tracked carrier
(lf.instances or lf.masks) is replaced by the tracker's output, frames
are reordered to (video, frame_idx) order, and labels.tracks is rebuilt
to the tracks the frames reference. Everything else passes through untouched:
the other carrier, lf.rois / lf.centroids / lf.bboxes, suggestions,
sessions, identities, provenance -- except an earlier run's
provenance["tracking"] record, which no longer describes the tracks and is
replaced (MOT / identity linking) or removed (snapshot labels.provenance
first to keep it as lineage). To keep the input (e.g. an in-memory sweep over
tracker settings), pass labels.copy(). A lazy Labels is materialized
first, so for one the return value is a new object.
config.tracker selects the tracker: the classic tracker (default), or a
modern MOT tracker (:mod:sleap_nn.tracking.mot), which tracks every
detection it is given without dropping any (unconfirmed ones come out with
track=None), runs identity linking at the end of each video when
link_tracklets / max_tracks ask for it, and records what each video
resolved (parameters, calibration, linking) in
labels.provenance["tracking"]. link_tracklets with the classic tracker
links each video's tracks after tracking it (recorded the same way).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
Untracked predictions. Each |
required |
config
|
TrackerConfig
|
Tracking configuration. |
required |
progress_callback
|
Optional[Callable[[int, int], None]]
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
Labels
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
|
Source code in sleap_nn/inference/tracking.py
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 | |
check_embedding_mask_tracking_options(config)
¶
Refuse the pose-only options for appearance-only tracking of masks.
Raises:
| Type | Description |
|---|---|
ValueError
|
|
Source code in sleap_nn/inference/tracking.py
check_tracking_plan(config, *, has_masks, has_predicted_instances, vector_carrier, vectors_from='', vectors_kept=False)
¶
Raise what :func:apply_tracking will raise, before the labels are tracked.
For the routes that run a detection stack and/or an embedding pass before they
track: the carriers the labels will hold and the carrier the vectors will be on
are known (or predictable) beforehand, so the carrier rules of
:func:apply_tracking can fail first instead of after minutes of inference.
Checks, in :func:apply_tracking's order: an explicit features naming a
carrier the labels will not hold, the options that tracking the mask carrier
cannot honor, then an appearance_weight blend whose tracked carrier will
hold no vector (a silent no-op: it reads only the tracked detections' own
vectors), and identity linking as the only reader of vectors that will ride
on the other carrier (the embedding pass would be wasted).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TrackerConfig
|
The tracker configuration. |
required |
has_masks
|
bool
|
Whether the labels will hold masks. |
required |
has_predicted_instances
|
bool
|
Whether they will hold predicted poses. |
required |
vector_carrier
|
Optional[str]
|
The carrier the appearance vectors will be on, or |
required |
vectors_from
|
str
|
Why the vectors land on |
''
|
vectors_kept
|
bool
|
Whether the vectors persist in the output
( |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
The plan cannot be tracked. |
Source code in sleap_nn/inference/tracking.py
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 | |
classic_linking_params(config)
¶
The MOT parameters the classic tracker's identity linking reads.
{key: value} for every :data:CLASSIC_LINKING_MOT_PARAMS key: the
mot_params override when given, else the :class:MOTParams default
(time_base="auto", body_size=None: measured per video).
Raises:
| Type | Description |
|---|---|
ValueError
|
An invalid value (e.g. an unknown |
Source code in sleap_nn/inference/tracking.py
count_carriers(labels, predicate)
¶
Count each carrier's detections, and those satisfying predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
Labels to scan (every frame's |
required |
predicate
|
Callable[[object], bool]
|
|
required |
Source code in sleap_nn/inference/tracking.py
embedding_carriers(labels)
¶
Where the appearance (re-ID) vectors are, per carrier.
Both carriers -- pose Instance / PredictedInstance and
PredictedSegmentationMask -- hold a vector in the single
identity_embedding slot (sleap-io #535). :func:apply_tracking routes
features="embeddings" to :attr:CarrierCounts.dominant, and checks that the
carrier an appearance_weight blend tracks actually holds vectors.
Source code in sleap_nn/inference/tracking.py
resolve_mask_tracking_options(config)
¶
(features, scoring_method) for tracking the mask carrier by geometry.
Unset (non-explicit) options resolve to masks / mask_iou.
Raises:
| Type | Description |
|---|---|
ValueError
|
An explicit option mask tracking cannot honor: another
|
Source code in sleap_nn/inference/tracking.py
resolve_mot_params(config)
¶
The :class:~sleap_nn.tracking.mot.params.MOTParams a MOT config runs with.
Starts from the tracker's preset (mot_params["preset"], default
"animal") and applies, on top, every MOT option that is set (None =
preset), the shared options (max_tracks; oks_stddev when set; an
explicit scoring_method as the geometric similarity: oks -> oks,
iou -> iou, euclidean_dist -> centroid) and the
mot_params overrides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TrackerConfig
|
A configuration with |
required |
Returns:
| Type | Description |
|---|---|
MOTParams
|
The resolved, validated parameters. |
Raises:
| Type | Description |
|---|---|
ValueError
|
The classic tracker, an option given both as a flag and in
|
Source code in sleap_nn/inference/tracking.py
resolve_tracked_carrier(features, has_masks, has_predicted_instances)
¶
The carrier :func:apply_tracking tracks by geometry.
The masks (lf.masks) when the labels hold masks and either no predicted pose
or an explicit features="masks"; the poses otherwise. It is also the carrier
an appearance_weight blend reads its vectors from. (features="embeddings"
instead follows the vectors to the carrier holding more of them.)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Optional[str]
|
The tracker's |
required |
has_masks
|
bool
|
Whether any frame holds a mask. |
required |
has_predicted_instances
|
bool
|
Whether any frame holds a predicted pose. |
required |
Source code in sleap_nn/inference/tracking.py
tracker_config_params(config)
¶
config as a JSON-safe dict for the tracking_config provenance record.
The *_explicit flags are recorded under their public names, as resolved.
For a classic run the MOT options are left out (all unset by
:func:validate_mot_config), so a classic run's provenance is what it was
before the MOT trackers existed; link_tracklets is kept when it ran, and
so are the mot_params its identity linking read
(:data:CLASSIC_LINKING_MOT_PARAMS). For a
MOT run the classic-only options are left out (they are at their defaults and
did not apply), and so are scoring_method / features when they were not
chosen explicitly (the run used its preset similarity / carrier, recorded
under provenance["tracking"]).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TrackerConfig
|
The tracker configuration. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
|
Source code in sleap_nn/inference/tracking.py
validate_mot_config(config)
¶
Refuse options the selected tracker family would silently ignore.
For the classic tracker: any MOT-only option (track_buffer, ...,
mot_params; link_tracklets is shared, and with it mot_params may
hold the :data:CLASSIC_LINKING_MOT_PARAMS keys its identity linking reads).
For a MOT tracker: every
classic-only option away from its default (the candidate window, the scoring
knobs, motion models, appearance_weight, the pre-cull / clean / connect
cleanups -- identity linking covers what those do), features other than
keypoints / masks, an explicit candidates_method, and the MOT
parameters themselves (resolved with :func:resolve_mot_params).
Also refuses oks_stddev (an option or a mot_params key) when the
resolved similarity is not oks: no other similarity reads it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TrackerConfig
|
The tracker configuration. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
Naming every offending option and the way out. |
Source code in sleap_nn/inference/tracking.py
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 | |