appearance
sleap.qc.features.appearance
¶
Appearance-outlier detection: points placed on occluders / the wrong object.
This module implements detector (e): "points placed on occluders / wrong object" -- e.g. a keypoint dragged onto white cotton bedding sitting over a dark mouse, or onto a cage bar. Unlike the geometry-only detectors, the geometry of such an error can look perfectly plausible (the point is in a sensible place for the pose); what gives it away is the image content under the node. We therefore build a small per-node appearance model from the labeled data and flag nodes whose local image patch does not look like that node usually looks.
How it works¶
At fit time, for every visible node of every (clean) labeled instance we cut a
patch_size x patch_size window centred on the node's (x, y) in its frame
and reduce it to a compact appearance descriptor: per-channel mean and
standard deviation of the patch intensities (a length-2 vector for grayscale
(H, W, 1) frames, length-6 for RGB). Mean captures brightness ("white cotton
bedding" vs "dark fur"), std captures local texture/contrast. We accumulate
these descriptors per node and fit a robust Gaussian (median + regularized
covariance), giving a per-node squared Mahalanobis distance. Nodes with
fewer than min_samples descriptors are left unmodeled (no opinion).
At score time, for each visible node we cut the same descriptor and compute its
Mahalanobis distance to that node's learned model, then squash it to [0, 1]
against a per-node distance scale learned at fit time (so "normal" appearance
sits near 0 and clear outliers approach 1). The per-instance score is the
max over nodes -- one badly-misplaced node is enough to warrant review.
This is a NON-GMM channel detector (default-OFF / experimental): it produces a
per-instance appearance_outlier_score in [0, 1] that the integration
layer stores in :attr:QCResults.channel_scores under the "appearance"
channel, exactly like the missing-node detector.
Honest scope / limitations¶
- The descriptor is intentionally tiny (mean + std per channel). It catches gross "this node is on visually-different stuff" errors (bright bedding vs dark fur, a node on the cage floor vs on the animal). It will NOT distinguish two regions that have the same brightness/texture but are semantically different (e.g. two similarly-grey body parts).
- It learns the project's own appearance statistics. If a node is systematically mislabeled onto the same wrong surface across the whole dataset, that wrong surface becomes "normal" and is not flagged.
- Patches near the image border are clipped to the valid region, so an edge node is described by fewer pixels but is still modeled/scored.
Functions:
| Name | Description |
|---|---|
extract_patch_descriptor |
Cut a patch around |
fit_appearance |
Learn a per-node appearance model from labeled instances. |
score_appearance |
Score an instance for appearance outliers against a fitted model. |
extract_patch_descriptor(frame, x, y, patch_size=7)
¶
Cut a patch around (x, y) and reduce it to an appearance descriptor.
The descriptor is the per-channel mean followed by the per-channel
standard deviation of the patch intensities. For a grayscale frame
(H, W, 1) (or (H, W)) this is a length-2 vector [mean, std]; for
an RGB frame (H, W, 3) it is length-6 [mean_r, mean_g, mean_b, std_r,
std_g, std_b]. Patches that fall partly outside the image are clipped to
the valid region (an edge node is described by fewer pixels).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
ndarray
|
Decoded image as |
required |
x
|
float
|
Node x-coordinate (column) in pixels. May be fractional; rounded to the nearest pixel for the patch centre. |
required |
y
|
float
|
Node y-coordinate (row) in pixels. |
required |
patch_size
|
int
|
Side length (pixels) of the square window. Values < 1 are treated as 1. Defaults to 7. |
7
|
Returns:
| Type | Description |
|---|---|
Optional[ndarray]
|
A 1-D |
Source code in sleap/qc/features/appearance.py
fit_appearance(frames_and_instances, n_nodes, patch_size=7, min_samples=DEFAULT_MIN_SAMPLES)
¶
Learn a per-node appearance model from labeled instances.
For each (frame, points) pair and each visible node, cut a
patch_size x patch_size patch at the node's (x, y) and reduce it to
an appearance descriptor (per-channel mean + std). Descriptors are pooled
per node and a robust Gaussian is fit per node that has at least
min_samples samples; nodes below that are left unmodeled.
Decoding is the caller's responsibility: pass already-decoded frame arrays
((H, W), (H, W, 1) or (H, W, C)) so the heavy video I/O stays
out of unit tests. Pairs whose frame is None (e.g. undecodable) are
skipped; NaN / out-of-frame nodes are skipped per node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frames_and_instances
|
Iterable[tuple]
|
Iterable of |
required |
n_nodes
|
int
|
Number of skeleton nodes (length of each |
required |
patch_size
|
int
|
Side length of the square appearance patch. Defaults to 7. |
7
|
min_samples
|
int
|
Minimum patch samples for a node to be modeled. Defaults to
:data: |
DEFAULT_MIN_SAMPLES
|
Returns:
| Type | Description |
|---|---|
dict
|
A model dict with:
|
Source code in sleap/qc/features/appearance.py
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 | |
score_appearance(frame, points, model, patch_size=None)
¶
Score an instance for appearance outliers against a fitted model.
For each visible node that the model has an opinion on, cut its patch,
compute the Mahalanobis distance of its descriptor to the node's learned
model, and squash it to [0, 1]. The per-instance score is the max over
scored nodes (one clearly-misplaced node is enough to flag the instance).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
ndarray
|
Decoded image ( |
required |
points
|
ndarray
|
|
required |
model
|
dict
|
Model dict from :func: |
required |
patch_size
|
Optional[int]
|
Patch side length. If |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with:
|
Source code in sleap/qc/features/appearance.py
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 | |