appearance
sleap_nn.tracking.mot.appearance
¶
Per-track appearance memory for the MOT trackers.
Each appearance-using track keeps an :class:AppearanceMemory: an exponential
moving average (EMA) of its L2-normalized re-ID vectors plus a bounded gallery of
its most recent CONFIDENT vectors. Updates are quality-gated
(:func:passes_quality_gate): a vector from an ambiguous frame (a crossing, a
merged or partial crop, a low-confidence detection) changes neither the EMA nor
the gallery, so one contaminated crop cannot pull the memory toward the wrong
animal (the self-reinforcing swap that made ungated appearance tracking lose on
dense video).
Classes:
| Name | Description |
|---|---|
AppearanceMemory |
EMA + gallery of one track's appearance vectors. |
Functions:
| Name | Description |
|---|---|
passes_quality_gate |
Decide whether a matched detection's vector may update its track's memory. |
unit_rows |
Row-normalize |
AppearanceMemory
¶
EMA + gallery of one track's appearance vectors.
Every stored vector is L2-normalized. Vectors that are missing, non-finite, zero-norm or of a different dimension than the memory's are ignored, so a detection without a usable vector never changes the memory.
Attributes:
| Name | Type | Description |
|---|---|---|
ema_alpha |
Weight on the old EMA (momentum). |
|
gallery_size |
Maximum gallery length (most recent kept). |
|
adaptive |
Deep OC-SORT score-adaptive EMA: |
|
score_floor |
|
|
ema |
Optional[ndarray]
|
Current EMA vector (L2-normalized) or |
gallery |
deque
|
|
n_updates |
Number of vectors absorbed (seed included). |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize an empty memory. |
ema_cos |
Cosine of |
ema_cos_unit |
:meth: |
gallery_max_cos |
Max cosine of each query over the gallery (EMA if the gallery is empty). |
gallery_max_cos_unit |
:meth: |
gallery_topk_mean |
Mean of the top- |
seed |
Initialize from a track's first vector (always accepted). |
update |
Absorb a matched detection's vector. |
vectors |
Gallery as a read-only |
Source code in sleap_nn/tracking/mot/appearance.py
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 | |
is_empty
property
¶
Whether the memory holds no vector.
__init__(ema_alpha=0.8, gallery_size=30, adaptive=False, score_floor=0.5)
¶
Initialize an empty memory.
Source code in sleap_nn/tracking/mot/appearance.py
ema_cos(queries)
¶
Cosine of (n, D) normalized queries to the EMA; NaN if empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queries
|
ndarray
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in sleap_nn/tracking/mot/appearance.py
ema_cos_unit(rows, ok)
¶
:meth:ema_cos of queries already normalized by :func:unit_rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
ndarray
|
|
required |
ok
|
ndarray
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in sleap_nn/tracking/mot/appearance.py
gallery_max_cos(queries)
¶
Max cosine of each query over the gallery (EMA if the gallery is empty).
This is DeepSORT's nearest-neighbor appearance statistic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queries
|
ndarray
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in sleap_nn/tracking/mot/appearance.py
gallery_max_cos_unit(rows, ok)
¶
:meth:gallery_max_cos of queries already normalized by :func:unit_rows.
Lets a tracker normalize a frame's queries once for all its tracks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
ndarray
|
|
required |
ok
|
ndarray
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in sleap_nn/tracking/mot/appearance.py
gallery_topk_mean(queries, k=5)
¶
Mean of the top-k gallery cosines per query; NaN if empty.
With fewer than k gallery vectors, all of them are averaged; with an
empty gallery (e.g. gallery_size=0) the EMA cosine is used. This is
the identity linker's statistic (the oracle its calibration is tested
against).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queries
|
ndarray
|
|
required |
k
|
int
|
Number of best gallery matches averaged per query. |
5
|
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in sleap_nn/tracking/mot/appearance.py
seed(vec)
¶
Initialize from a track's first vector (always accepted).
The spawn vector seeds both the EMA and the gallery whatever its quality
(a track needs an appearance to be compared at all). Any previous content
is discarded. An unusable vector (None, zero-norm, non-finite) leaves
the memory unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec
|
ndarray
|
The |
required |
Source code in sleap_nn/tracking/mot/appearance.py
update(vec, score=1.0, gated=True)
¶
Absorb a matched detection's vector.
gated=False (the quality gate failed) leaves both the EMA and the
gallery unchanged. A memory with no EMA yet is seeded instead (whatever
gated: like the spawn vector, the first usable vector is always
accepted). Otherwise the EMA becomes renorm(a * ema + (1 - a) * vec)
with a = ema_alpha (or the score-adaptive momentum when adaptive)
and the vector is appended to the gallery.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec
|
ndarray
|
The |
required |
score
|
float
|
Detection confidence (used by the adaptive EMA only). |
1.0
|
gated
|
bool
|
Whether the vector passed the quality gate. |
True
|
Source code in sleap_nn/tracking/mot/appearance.py
vectors()
¶
Gallery as a read-only (G, D) array ((0, 0) when empty).
The stacked array is cached until the gallery changes (every statistic reads it, once per track and frame).
Source code in sleap_nn/tracking/mot/appearance.py
passes_quality_gate(det_score, n_visible, n_nodes, max_iou_other, margin, *, high_threshold, min_margin, gallery_len, min_visible_fraction=0.5, max_overlap_iou=0.1)
¶
Decide whether a matched detection's vector may update its track's memory.
All must hold: det_score >= high_threshold; n_visible / n_nodes >=
min_visible_fraction; max_iou_other < max_overlap_iou (padded-box IoU to
every other detection in the frame and every other live track's predicted
box); and, when min_margin is not None and gallery_len >= 3, the
appearance margin (cosine to the matched track minus the best cosine to any
other in-gate track; inf when there is no competitor) is >= min_margin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
det_score
|
float
|
Detection confidence. |
required |
n_visible
|
int
|
Visible nodes of the detection (masks: 1). |
required |
n_nodes
|
int
|
Nodes per detection (masks: 1). |
required |
max_iou_other
|
float
|
Largest padded-box IoU to any other box (pass 0.0 when there is none; NaN counts as no overlap). |
required |
margin
|
float
|
Appearance margin of the match (NaN fails the margin test). |
required |
high_threshold
|
float
|
Minimum detection confidence. |
required |
min_margin
|
Optional[float]
|
Minimum appearance margin, or |
required |
gallery_len
|
int
|
Current length of the track's gallery; the margin test is skipped while it is shorter than 3 (a young memory has no reliable margin yet). |
required |
min_visible_fraction
|
float
|
Minimum fraction of visible nodes. |
0.5
|
max_overlap_iou
|
float
|
Overlap (IoU) at or above which the crop may be merged with a neighbor's. |
0.1
|
Returns:
| Type | Description |
|---|---|
bool
|
Whether the vector may update the EMA and the gallery. |
Source code in sleap_nn/tracking/mot/appearance.py
unit_rows(queries)
¶
Row-normalize (n, D) (or a single (D,)) queries.
Every :class:AppearanceMemory statistic normalizes its queries with this;
a caller scoring the same queries against many memories normalizes them
once and uses the *_unit methods (e.g.
:meth:AppearanceMemory.gallery_max_cos_unit).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queries
|
|
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray]
|
|