evaluation
sleap_nn.evaluation
¶
This module is to compute evaluation metrics for trained models.
Classes:
Name | Description |
---|---|
Evaluator |
Compute the standard evaluation metrics with the predicted and the ground-truth Labels. |
MatchInstance |
Class to have a new structure for sio.Instance object. |
Functions:
Name | Description |
---|---|
compute_dists |
Compute Euclidean distances between matched pairs of instances. |
compute_instance_area |
Compute the area of the bounding box of a set of keypoints. |
compute_oks |
Compute the object keypoints similarity between sets of points. |
find_frame_pairs |
Find corresponding frames across two sets of labels. |
get_instances |
Get a list of instances of type MatchInstance from the Labeled Frame. |
load_metrics |
Load the metrics for a given model and split. |
match_frame_pairs |
Match all ground truth and predicted instances within each pair of frames. |
match_instances |
Match pairs of instances between ground truth and predictions in a frame. |
run_evaluation |
Evaluate SLEAP-NN model predictions against ground truth labels. |
Evaluator
¶
Compute the standard evaluation metrics with the predicted and the ground-truth Labels.
This class is used to calculate the common metrics for pose estimation models which includes voc metrics (with oks and pck), mOKS, distance metrics, pck metrics and visibility metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground_truth_instances
|
Labels
|
The |
required |
predicted_instances
|
Labels
|
The |
required |
oks_stddev
|
float
|
The standard deviation to use for calculating object
keypoint similarity; see |
0.025
|
oks_scale
|
Optional[float]
|
The scale to use for calculating object
keypoint similarity; see |
None
|
match_threshold
|
float
|
The threshold to use on oks scores when determining which instances match between ground truth and predicted frames. |
0
|
user_labels_only
|
bool
|
If False, predicted instances in the ground truth frame may be considered for matching. |
True
|
Methods:
Name | Description |
---|---|
__init__ |
Initialize the Evaluator class with ground-truth and predicted labels. |
distance_metrics |
Compute the Euclidean distance error at different percentiles using the pairwise distances. |
evaluate |
Return the evaluation metrics. |
mOKS |
Return the meanOKS value. |
pck_metrics |
Compute PCK across a range of thresholds using the pair-wise distances. |
visibility_metrics |
Compute node visibility metrics for the matched pair of instances. |
voc_metrics |
Compute VOC metrics for a matched pairs of instances positive pairs and false negatives. |
Source code in sleap_nn/evaluation.py
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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 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 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 |
|
__init__(ground_truth_instances, predicted_instances, oks_stddev=0.025, oks_scale=None, match_threshold=0, user_labels_only=True)
¶
Initialize the Evaluator class with ground-truth and predicted labels.
Source code in sleap_nn/evaluation.py
distance_metrics()
¶
Compute the Euclidean distance error at different percentiles using the pairwise distances.
Returns:
Type | Description |
---|---|
A dictionary of distance metrics. |
Source code in sleap_nn/evaluation.py
evaluate()
¶
Return the evaluation metrics.
Source code in sleap_nn/evaluation.py
mOKS()
¶
pck_metrics(thresholds=np.linspace(1, 10, 10))
¶
Compute PCK across a range of thresholds using the pair-wise distances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thresholds
|
ndarray
|
A list of distance thresholds in pixels. |
linspace(1, 10, 10)
|
Returns:
Type | Description |
---|---|
A dictionary of PCK metrics evaluated at each threshold. |
Source code in sleap_nn/evaluation.py
visibility_metrics()
¶
Compute node visibility metrics for the matched pair of instances.
Returns:
Type | Description |
---|---|
A dictionary of visibility metrics, including the confusion matrix. |
Source code in sleap_nn/evaluation.py
voc_metrics(match_score_by='oks', match_score_thresholds=np.linspace(0.5, 0.95, 10), recall_thresholds=np.linspace(0, 1, 101))
¶
Compute VOC metrics for a matched pairs of instances positive pairs and false negatives.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
match_score_by
|
The score to be used for computing the metrics. "ock" or "pck" |
'oks'
|
|
match_score_thresholds
|
ndarray
|
Score thresholds at which to consider matches as a true positive match. |
linspace(0.5, 0.95, 10)
|
recall_thresholds
|
ndarray
|
Recall thresholds at which to evaluate Average Precision. |
linspace(0, 1, 101)
|
Returns:
Type | Description |
---|---|
A dictionary of VOC metrics. |
Source code in sleap_nn/evaluation.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 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 |
|
MatchInstance
¶
compute_dists(positive_pairs)
¶
Compute Euclidean distances between matched pairs of instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
positive_pairs
|
List[Tuple[Instance, PredictedInstance, Any]]
|
A list of tuples of the form |
required |
Returns:
Type | Description |
---|---|
Dict[str, Union[ndarray, List[int], List[str]]]
|
A dictionary with the following keys:
dists: An array of pairwise distances of shape |
Source code in sleap_nn/evaluation.py
compute_instance_area(points)
¶
Compute the area of the bounding box of a set of keypoints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points
|
ndarray
|
A numpy array of coordinates. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The area of the bounding box of the points. |
Source code in sleap_nn/evaluation.py
compute_oks(points_gt, points_pr, scale=None, stddev=0.025, use_cocoeval=True)
¶
Compute the object keypoints similarity between sets of points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points_gt
|
ndarray
|
Ground truth instances of shape (n_gt, n_nodes, n_ed), where n_nodes is the number of body parts/keypoint types, and n_ed is the number of Euclidean dimensions (typically 2 or 3). Keypoints that are missing/not visible should be represented as NaNs. |
required |
points_pr
|
ndarray
|
Predicted instance of shape (n_pr, n_nodes, n_ed). |
required |
use_cocoeval
|
bool
|
Indicates whether the OKS score is calculated like cocoeval method or not. True indicating the score is calculated using the cocoeval method (widely used and the code can be found here at https://github.com/cocodataset/cocoapi/blob/8c9bcc3cf640524c4c20a9c40e89cb6a2f2fa0e9/PythonAPI/pycocotools/cocoeval.py#L192C5-L233C20) and False indicating the score is calculated using the method exactly as given in the paper referenced in the Notes below. |
True
|
scale
|
Optional[float]
|
Size scaling factor to use when weighing the scores, typically the area of the bounding box of the instance (in pixels). This should be of the length n_gt. If a scalar is provided, the same number is used for all ground truth instances. If set to None, the bounding box area of the ground truth instances will be calculated. |
None
|
stddev
|
float
|
The standard deviation associated with the spread in the localization accuracy of each node/keypoint type. This should be of the length n_nodes. "Easier" keypoint types will have lower values to reflect the smaller spread expected in localizing it. |
0.025
|
Returns:
Type | Description |
---|---|
ndarray
|
The object keypoints similarity between every pair of ground truth and predicted instance, a numpy array of of shape (n_gt, n_pr) in the range of [0, 1.0], with 1.0 denoting a perfect match. |
Notes
It's important to set the stddev appropriately when accounting for the difficulty of each keypoint type. For reference, the median value for all keypoint types in COCO is 0.072. The "easiest" keypoint is the left eye, with stddev of 0.025, since it is easy to precisely locate the eyes when labeling. The "hardest" keypoint is the left hip, with stddev of 0.107, since it's hard to locate the left hip bone without external anatomical features and since it is often occluded by clothing.
The implementation here is based off of the descriptions in: Ronch & Perona. "Benchmarking and Error Diagnosis in Multi-Instance Pose Estimation." ICCV (2017).
Source code in sleap_nn/evaluation.py
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 |
|
find_frame_pairs(labels_gt, labels_pr, user_labels_only=True)
¶
Find corresponding frames across two sets of labels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels_gt
|
Labels
|
A |
required |
labels_pr
|
Labels
|
A |
required |
user_labels_only
|
bool
|
If False, frames with predicted instances in |
True
|
Returns:
Type | Description |
---|---|
List[Tuple[LabeledFrame, LabeledFrame]]
|
A list of pairs of |
Source code in sleap_nn/evaluation.py
get_instances(labeled_frame)
¶
Get a list of instances of type MatchInstance from the Labeled Frame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labeled_frame
|
LabeledFrame
|
Input Labeled frame of type sio.LabeledFrame. |
required |
Returns:
Type | Description |
---|---|
List[MatchInstance]
|
List of MatchInstance objects for the given labeled frame. |
Source code in sleap_nn/evaluation.py
load_metrics(model_path, split='val')
¶
Load the metrics for a given model and split.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_path
|
str
|
Path to a model folder or metrics file (.npz). |
required |
split
|
Name of the split to load the metrics for. Must be |
'val'
|
Source code in sleap_nn/evaluation.py
match_frame_pairs(frame_pairs, stddev=0.025, scale=None, threshold=0)
¶
Match all ground truth and predicted instances within each pair of frames.
This is a wrapper for match_instances()
but operates on lists of frames.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
frame_pairs
|
List[Tuple[LabeledFrame, LabeledFrame]]
|
A list of pairs of |
required |
stddev
|
float
|
The expected spread of coordinates for OKS computation. |
0.025
|
scale
|
Optional[float]
|
The scale for normalizing the OKS. If not set, the bounding box area will be used. |
None
|
threshold
|
float
|
The minimum OKS between a candidate pair of instances to be considered a match. |
0
|
Returns:
Type | Description |
---|---|
Tuple[List[Tuple[Instance, PredictedInstance, float]], List[Instance]]
|
A tuple of (
|
Source code in sleap_nn/evaluation.py
match_instances(frame_gt, frame_pr, stddev=0.025, scale=None, threshold=0)
¶
Match pairs of instances between ground truth and predictions in a frame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
frame_gt
|
LabeledFrame
|
A |
required |
frame_pr
|
LabeledFrame
|
A |
required |
stddev
|
float
|
The expected spread of coordinates for OKS computation. |
0.025
|
scale
|
Optional[float]
|
The scale for normalizing the OKS. If not set, the bounding box area will be used. |
None
|
threshold
|
float
|
The minimum OKS between a candidate pair of instances to be considered a match. |
0
|
Returns:
Type | Description |
---|---|
Tuple[List[Tuple[Instance, PredictedInstance, float]], List[Instance]]
|
A tuple of (
|
Notes
This function uses the approach from the PASCAL VOC scoring procedure. Briefly, predictions are sorted descending by their instance-level prediction scores and greedily matched to ground truth instances which are then removed from the pool of available instances.
Ground truth instances that remain unmatched are considered false negatives.
Source code in sleap_nn/evaluation.py
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 |
|
run_evaluation(ground_truth_path, predicted_path, oks_stddev=0.025, oks_scale=None, match_threshold=0, user_labels_only=True, save_metrics=None)
¶
Evaluate SLEAP-NN model predictions against ground truth labels.