deeplabcut.pose_estimation_pytorch.apis.evaluation
Functions:
| Name | Description |
|---|---|
evaluate |
Args: |
evaluate_network |
Evaluates a snapshot. |
evaluate_snapshot |
Evaluates a snapshot. The evaluation results are stored in the .h5 and .csv file |
image_to_dlc_df_index |
Args: |
plot_gt_and_predictions |
Plot ground truth and predictions on an image. |
predict |
Predicts poses on data contained in a loader. |
save_evaluation_results |
Saves the evaluation results to a CSV file. Adds the evaluation results for the |
save_rmse_per_bodypart |
Saves the evaluation results per bodypart to a CSV file. |
visualize_predictions |
Visualize model predictions alongside ground truth keypoints. |
evaluate
evaluate(
pose_runner: InferenceRunner,
loader: Loader,
mode: str,
detector_runner: InferenceRunner | None = None,
parameters: PoseDatasetParameters | None = None,
comparison_bodyparts: str | list[str] | None = None,
per_keypoint_evaluation: bool = False,
pcutoff: float | list[float] = 0.6,
force_multi_animal: bool = False,
) -> tuple[dict[str, float], dict[str, dict[str, np.ndarray]]]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
InferenceRunner
|
The runner for pose estimation |
required |
|
Loader
|
The loader containing the data to evaluate |
required |
|
str
|
Either 'train' or 'test' |
required |
|
InferenceRunner | None
|
If the loader's |
None
|
|
PoseDatasetParameters | None
|
PoseDatasetParameters to use. If None, the parameters will be obtained from the given Loader. This can be used to change the names of bodyparts, e.g. when a model is trained with memory replay. |
None
|
|
str | list[str] | None
|
A subset of the bodyparts for which to compute the evaluation metrics. Passing "all" or None evaluates on all bodyparts. |
None
|
|
bool
|
Compute the train and test RMSE for each keypoint, and save the results to a {model_name}-keypoint-results.csv in the evaluation-results-pytorch folder. |
False
|
|
float | list[float]
|
Confidence threshold for RMSE computation. If a list is provided, there should be one value for each bodypart and one value for each unique bodypart (if there are any). |
0.6
|
|
bool
|
If False - the scenario (single- or multi-animal) is inferred from the loader. If True - the multi-animal is used during evaluation, even if the loader contains only a single animal. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[dict[str, float], dict[str, dict[str, ndarray]]]
|
A dict containing the evaluation results A dict mapping the paths of images for which predictions were computed to the different predictions made by each model head |
Source code in deeplabcut/pose_estimation_pytorch/apis/evaluation.py
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 | |
evaluate_network
evaluate_network(
config: str | Path,
shuffles: Iterable[int] = (1,),
trainingsetindex: int | str = 0,
snapshotindex: int | str | None = None,
device: str | None = None,
plotting: bool | str = False,
show_errors: bool = True,
transform: Compose = None,
snapshots_to_evaluate: list[str] | None = None,
comparison_bodyparts: str | list[str] | None = None,
per_keypoint_evaluation: bool = False,
modelprefix: str = "",
detector_snapshot_index: int | None = None,
pcutoff: float | list[float] | dict[str, float] | None = None,
) -> None
Evaluates a snapshot.
The evaluation results are stored in the .h5 and .csv file under the subdirectory 'evaluation_results'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | Path
|
path to the project's config file |
required |
|
Iterable[int]
|
Iterable of integers specifying the shuffle indices to evaluate. |
(1,)
|
|
int | str
|
Integer specifying which training set fraction to use. Evaluates all fractions if set to "all" |
0
|
|
int | str | None
|
index (starting at 0) of the snapshot we want to load. To evaluate the last one, use -1. To evaluate all snapshots, use "all". For example if we have 3 models saved - snapshot-0.pt - snapshot-50.pt - snapshot-100.pt and we want to evaluate snapshot-50.pt, snapshotindex should be 1. If None, the snapshotindex is loaded from the project configuration. |
None
|
|
str | None
|
the device to run evaluation on |
None
|
|
bool | str
|
Plots the predictions on the train and test images. If provided it must
be either |
False
|
|
bool
|
display train and test errors. |
True
|
|
Compose
|
transformation pipeline for evaluation ** Should normalise the data the same way it was normalised during training ** |
None
|
|
list[str] | None
|
List of snapshot names to evaluate (e.g. ["snapshot-50",
"snapshot-75"]). If defined, |
None
|
|
str | list[str] | None
|
A subset of the bodyparts for which to compute the evaluation metrics. |
None
|
|
bool
|
Compute the train and test RMSE for each keypoint, and save the results to a {model_name}-keypoint-results.csv in the evaluation-results-pytorch folder. |
False
|
|
str
|
directory containing the deeplabcut models to use when evaluating the network. By default, they are assumed to exist in the project folder. |
''
|
|
int | None
|
Only for TD models. If defined, uses the detector with the given index for pose estimation. |
None
|
|
float | list[float] | dict[str, float] | None
|
The cutoff to use for computing evaluation metrics. When |
None
|
Examples:
If you want to evaluate on shuffle 1 without plotting predictions.
>>> import deeplabcut
>>> deeplabcut.evaluate_network(
>>> '/analysis/project/reaching-task/config.yaml', shuffles=[1],
>>> )
If you want to evaluate shuffles 0 and 1 and plot the predictions.
>>> deeplabcut.evaluate_network(
>>> '/analysis/project/reaching-task/config.yaml',
>>> shuffles=[0, 1],
>>> plotting=True,
>>> )
If you want to plot assemblies for a maDLC project
>>> deeplabcut.evaluate_network(
>>> '/analysis/project/reaching-task/config.yaml',
>>> shuffles=[1],
>>> plotting="individual",
>>> )
Source code in deeplabcut/pose_estimation_pytorch/apis/evaluation.py
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 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 | |
evaluate_snapshot
evaluate_snapshot(
cfg: dict,
loader: DLCLoader,
snapshot: Snapshot,
scorer: str,
transform: Compose | None = None,
plotting: bool | str = False,
show_errors: bool = True,
comparison_bodyparts: str | list[str] | None = None,
per_keypoint_evaluation: bool = False,
detector_snapshot: Snapshot | None = None,
pcutoff: float | list[float] | dict[str, float] | None = None,
) -> pd.DataFrame
Evaluates a snapshot. The evaluation results are stored in the .h5 and .csv file under the subdirectory 'evaluation_results'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
the content of the project's config file |
required |
|
DLCLoader
|
the loader for the shuffle to evaluate |
required |
|
Snapshot
|
the snapshot to evaluate |
required |
|
str
|
the scorer name to use for the snapshot |
required |
|
Compose | None
|
transformation pipeline for evaluation ** Should normalise the data the same way it was normalised during training ** |
None
|
|
bool | str
|
Plots the predictions on the train and test images. If provided it must
be either |
False
|
|
bool
|
whether to compare predictions and ground truth |
True
|
|
str | list[str] | None
|
A subset of the bodyparts for which to compute the evaluation metrics. |
None
|
|
bool
|
Compute the train and test RMSE for each keypoint, and save the results to a {model_name}-keypoint-results.csv in the evaluation-results-pytorch folder. |
False
|
|
Snapshot | None
|
Only for TD models. If defined, evaluation metrics are computed using the detections made by this snapshot |
None
|
|
float | list[float] | dict[str, float] | None
|
The cutoff to use for computing evaluation metrics. When |
None
|
Source code in deeplabcut/pose_estimation_pytorch/apis/evaluation.py
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 | |
image_to_dlc_df_index
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
the path of the image to map to a DLC index |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
the image index to create a multi-animal DLC dataframe: ("labeled-data", video_name, image_name) |
Source code in deeplabcut/pose_estimation_pytorch/apis/evaluation.py
plot_gt_and_predictions
plot_gt_and_predictions(
image_path: str | Path,
output_dir: str | Path,
gt_bodyparts: ndarray,
pred_bodyparts: ndarray,
gt_unique_bodyparts: ndarray | None = None,
pred_unique_bodyparts: ndarray | None = None,
mode: str = "bodypart",
colormap: str = "rainbow",
dot_size: int = 12,
alpha_value: float = 0.7,
p_cutoff: float | list[float] = 0.6,
bounding_boxes: tuple[ndarray, ndarray] | None = None,
bboxes_pcutoff: float = 0.6,
bounding_boxes_color: str = "auto",
)
Plot ground truth and predictions on an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | Path
|
Path to the image |
required |
|
ndarray
|
Ground truth keypoints array (num_animals, num_keypoints, 3) |
required |
|
ndarray
|
Predicted keypoints array (num_animals, num_keypoints, 3) |
required |
|
str | Path
|
Directory where labeled images will be saved |
required |
|
ndarray | None
|
Ground truth unique bodyparts if any |
None
|
|
ndarray | None
|
Predicted unique bodyparts if any |
None
|
|
str
|
How to color the points ("bodypart" or "individual") |
'bodypart'
|
|
str
|
Matplotlib colormap name |
'rainbow'
|
|
int
|
Size of the plotted points |
12
|
|
float
|
Transparency of the points |
0.7
|
|
float | list[float]
|
Confidence threshold for showing predictions. If a list is provided, there should be one value for each bodypart and one value for each unique bodypart (if there are any). |
0.6
|
|
tuple[ndarray, ndarray] | None
|
bounding boxes (top-left corner, size) and their respective confidence levels, |
None
|
|
float
|
bounding boxes confidence cutoff threshold. |
0.6
|
|
str
|
If plotting bounding boxes, this is the color that will be used for bounding boxes. If set to "auto" (default value): - if mode is "bodypart", the bbox color will be a default color - if mode is "individual", each individual's color will be used for its bounding box |
'auto'
|
Source code in deeplabcut/pose_estimation_pytorch/apis/evaluation.py
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 | |
predict
predict(
pose_runner: InferenceRunner, loader: Loader, mode: str, detector_runner: InferenceRunner | None = None
) -> dict[str, dict[str, np.ndarray]]
Predicts poses on data contained in a loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
InferenceRunner
|
The runner to use for pose estimation |
required |
|
Loader
|
The loader containing the data to predict poses on |
required |
|
str
|
{"train", "test"} The mode to predict on |
required |
|
InferenceRunner | None
|
If the loader's |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, ndarray]]
|
The paths of images for which predictions were computed mapping to the different predictions made by each model head |
Source code in deeplabcut/pose_estimation_pytorch/apis/evaluation.py
save_evaluation_results
save_evaluation_results(df_scores: DataFrame, scores_path: Path, print_results: bool, pcutoff: float) -> None
Saves the evaluation results to a CSV file. Adds the evaluation results for the model to the combined results file, or creates it if it does not yet exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
DataFrame
|
the scores dataframe for a snapshot |
required |
|
Path
|
the path where the model scores CSV should be saved |
required |
|
bool
|
whether to print evaluation results to the console |
required |
|
float
|
the pcutoff used to get the evaluation results |
required |
Source code in deeplabcut/pose_estimation_pytorch/apis/evaluation.py
save_rmse_per_bodypart
save_rmse_per_bodypart(rmse_per_bodypart: dict[str, dict[str, float]], output_path: Path, print_results: bool) -> None
Saves the evaluation results per bodypart to a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, dict[str, float]]
|
The scores dataframe for a snapshot |
required |
|
Path
|
The path of the file where |
required |
|
bool
|
Whether to print results to the console |
required |
Source code in deeplabcut/pose_estimation_pytorch/apis/evaluation.py
visualize_predictions
visualize_predictions(
predictions: dict,
ground_truth: dict,
output_dir: str | Path | None = None,
num_samples: int | None = None,
random_select: bool = False,
show_ground_truth: bool = True,
plot_bboxes: bool = True,
) -> None
Visualize model predictions alongside ground truth keypoints.
This function processes keypoint predictions and ground truth data, applies visibility masks, and generates visualization plots. It supports random or sequential sampling of images for visualization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
Dictionary mapping image paths to prediction data. Each prediction contains: - bodyparts: array of shape [N, num_keypoints, 3] where 3 represents (x, y, confidence) - bboxes: array of shape [N, 4] for bounding boxes (optional) - bbox_scores: array of shape [N,] for bbox confidences (optional) |
required |
|
dict
|
Dictionary mapping image paths to ground truth keypoints. Each value has shape [N, num_keypoints, 3] where 3 represents (x, y, visibility) |
required |
|
str | Path | None
|
Path to save visualization outputs. Defaults to "predictions_visualizations" |
None
|
|
int | None
|
Number of images to visualize. If None, processes all images |
None
|
|
bool
|
If True, randomly samples images; if False, uses first N images |
False
|
|
bool
|
If True, displays ground truth poses alongside predictions. If False, only shows predictions but uses GT visibility mask |
True
|
|
bool
|
If True and the model is a top-down model, predicted bboxes will be shown in the images as well |
True
|
Source code in deeplabcut/pose_estimation_pytorch/apis/evaluation.py
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 | |