insample_prediction
sleap.qc.insample_prediction
¶
In-sample model prediction: labelable-but-unlabeled points (Tier-2).
This module implements the model-based (Tier-2) variant of detector (f), "labelable points left unlabeled" -- the maintainer's active-learning idea.
The geometry/visibility-only sibling
(:mod:sleap.qc.features.missing_node) can only catch outlier drops: an
instance missing a node its co-visible peers usually keep. It is blind to
dataset-wide systematic under-labeling (e.g. nobody ever labels the tail tip),
because the project's own visibility statistics never expect a node that is
rarely labeled.
This detector instead runs a trained model on the ALREADY-labeled frames (in-sample inference). For each node a human left UNLABELED/invisible, it reads the model's predicted confidence at the matched predicted instance's node and flags the cases where the model confidently localizes a part the human left blank. That distinguishes:
* "truly occluded" -- the model is also unsure (low confidence), and
* "labelable but skipped" -- the model is confident the part is there.
Design / scope:
* Import-safe. Importing this module never imports torch or
sleap_nn; those heavy imports happen lazily inside
:func:run_insample_prediction.
* Graceful. If sleap_nn/the model is unavailable, or the model's
skeleton node-names do not match labels.skeletons[0], the function
logs a reason and returns an empty/zero result instead of crashing.
* Non-GMM channel. The per-instance prediction_disagreement_score is
surfaced via :attr:QCResults.channel_scores (like the missing-node
channel), not as a GMM feature. Default-OFF / experimental.
The matching + scoring logic (:func:match_predictions_to_users and
:func:score_instance_disagreement) is a pure, model-free core so it can be
unit-tested with canned predicted instances and without any torch dependency.
Functions:
| Name | Description |
|---|---|
match_predictions_to_users |
Match each USER instance to the nearest PREDICTED instance in a frame. |
run_insample_prediction |
Flag labelable-but-unlabeled points via in-sample model prediction. |
score_instance_disagreement |
Score how strongly a model disagrees with a user's unlabeled nodes. |
match_predictions_to_users(user_points, pred_points)
¶
Match each USER instance to the nearest PREDICTED instance in a frame.
Bottom-up (and top-down) models emit predicted instances with no guaranteed correspondence to the user instances in the same frame, so we associate them by spatial proximity: each user instance is paired with the predicted instance whose visible-node centroid is closest (greedy nearest, one predicted instance per user instance, mutually exclusive).
The matching is symmetric in spirit to "match each predicted instance to the nearest user instance" -- a greedy global nearest-centroid assignment -- but is indexed by user instance so the caller can directly look up the prediction for a given user instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_points
|
list[ndarray]
|
List of |
required |
pred_points
|
list[ndarray]
|
List of |
required |
Returns:
| Type | Description |
|---|---|
list[Optional[int]]
|
A list parallel to |
Source code in sleap/qc/insample_prediction.py
run_insample_prediction(labels, model_path, peak_threshold=0.2, min_confidence=0.5, device='auto', progress_callback=None)
¶
Flag labelable-but-unlabeled points via in-sample model prediction.
Runs a trained sleap_nn model on the ALREADY-labeled frames of
labels (in-sample), matches each predicted instance to the nearest user
instance, and -- for every node the user left invisible -- reads the matched
prediction's confidence there. A confident prediction at a blank node is a
"disagreement" (the model expects a labeled part the human skipped).
This is the model-based (Tier-2) variant of detector (f). It is import-safe
(no top-level torch/sleap_nn import) and graceful: any failure to load
or run the model, or a skeleton node-name mismatch, returns a zero result
with ran=False and a logged reason rather than raising.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
'sio.Labels'
|
Labels with user-annotated instances to evaluate (in-sample). |
required |
model_path
|
str
|
Path to a trained |
required |
peak_threshold
|
float
|
Minimum peak confidence for the model's peak finding.
Lower values let the model report weaker peaks (more candidate
disagreements). Passed through to inference. Defaults to |
0.2
|
min_confidence
|
float
|
Confidence at/above which a model prediction at an
unlabeled node counts as a disagreement (gates the per-instance
score). Defaults to |
0.5
|
device
|
str
|
Torch device for inference ( |
'auto'
|
progress_callback
|
Optional callable |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with:
|
Source code in sleap/qc/insample_prediction.py
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 | |
score_instance_disagreement(user_points, pred_scores, min_confidence=0.5)
¶
Score how strongly a model disagrees with a user's unlabeled nodes.
For each node the user left invisible/unlabeled (NaN), look up the matched predicted instance's confidence at that node. A node is a disagreement when the model is confident the part is present::
disagreement at node k <=> user node k is invisible
AND pred_scores[k] >= min_confidence
The per-instance prediction_disagreement_score is the maximum predicted
confidence over the instance's unlabeled nodes, but only counting nodes whose
confidence clears min_confidence (so it is gated -- a model that is mildly
unsure about every blank node yields 0.0). The result is therefore in
[0, 1] and 0.0 when the model never confidently fills a human-left blank.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_points
|
ndarray
|
|
required |
pred_scores
|
Optional[ndarray]
|
|
required |
min_confidence
|
float
|
Confidence at/above which a model prediction at an
unlabeled node counts as a disagreement. Defaults to |
0.5
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with:
|
Source code in sleap/qc/insample_prediction.py
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 | |