Calculate the pose accuracy of PCK for each individual keypoint and the averaged accuracy across all keypoints from heatmaps. Note: PCK metric measures accuracy of the localization of the body joints. The distances between predicted positions and the ground-truth ones
(output, target, mask, thr=0.05, normalize=None)
| 439 | |
| 440 | |
| 441 | def pose_pck_accuracy(output, target, mask, thr=0.05, normalize=None): |
| 442 | """Calculate the pose accuracy of PCK for each individual keypoint and the |
| 443 | averaged accuracy across all keypoints from heatmaps. |
| 444 | |
| 445 | Note: |
| 446 | PCK metric measures accuracy of the localization of the body joints. |
| 447 | The distances between predicted positions and the ground-truth ones |
| 448 | are typically normalized by the bounding box size. |
| 449 | The threshold (thr) of the normalized distance is commonly set |
| 450 | as 0.05, 0.1 or 0.2 etc. |
| 451 | |
| 452 | batch_size: N |
| 453 | num_keypoints: K |
| 454 | heatmap height: H |
| 455 | heatmap width: W |
| 456 | |
| 457 | Args: |
| 458 | output (np.ndarray[N, K, H, W]): Model output heatmaps. |
| 459 | target (np.ndarray[N, K, H, W]): Groundtruth heatmaps. |
| 460 | mask (np.ndarray[N, K]): Visibility of the target. False for invisible |
| 461 | joints, and True for visible. Invisible joints will be ignored for |
| 462 | accuracy calculation. |
| 463 | thr (float): Threshold of PCK calculation. Default 0.05. |
| 464 | normalize (np.ndarray[N, 2]): Normalization factor for H&W. |
| 465 | |
| 466 | Returns: |
| 467 | tuple: A tuple containing keypoint accuracy. |
| 468 | |
| 469 | - np.ndarray[K]: Accuracy of each keypoint. |
| 470 | - float: Averaged accuracy across all keypoints. |
| 471 | - int: Number of valid keypoints. |
| 472 | """ |
| 473 | N, K, H, W = output.shape |
| 474 | if K == 0: |
| 475 | return None, 0, 0 |
| 476 | if normalize is None: |
| 477 | normalize = np.tile(np.array([[H, W]]), (N, 1)) |
| 478 | |
| 479 | pred, _ = _get_max_preds(output) |
| 480 | gt, _ = _get_max_preds(target) |
| 481 | return keypoint_pck_accuracy(pred, gt, mask, thr, normalize) |
| 482 | |
| 483 | |
| 484 | def keypoint_pck_accuracy(pred, gt, mask, thr, normalize): |
no test coverage detected