(preds, gt)
| 56 | |
| 57 | |
| 58 | def calc_prediction_errors(preds, gt): |
| 59 | kpts_gt = gt["metadata"]["keypoints"] |
| 60 | kpts_pred = preds["metadata"]["keypoints"] |
| 61 | map_ = {kpts_gt.index(kpt): i for i, kpt in enumerate(kpts_pred)} |
| 62 | annot = gt["annotations"] |
| 63 | |
| 64 | map_images = _map(list(preds["predictions"]), list(annot)) |
| 65 | |
| 66 | errors = np.full( |
| 67 | ( |
| 68 | len(preds["predictions"]), |
| 69 | len(gt["metadata"]["animals"]), |
| 70 | len(kpts_gt), |
| 71 | 2, # Hold distance to GT and confidence |
| 72 | ), |
| 73 | np.nan, |
| 74 | ) |
| 75 | for n, (path, preds_) in enumerate(preds["predictions"].items()): |
| 76 | if not preds_: |
| 77 | continue |
| 78 | xy_gt = annot[map_images[path]].swapaxes(0, 1) |
| 79 | xy_pred = preds_["coordinates"][0] |
| 80 | conf_pred = preds_["confidence"] |
| 81 | for i, xy_gt_ in enumerate(xy_gt): |
| 82 | visible = np.flatnonzero(np.all(~np.isnan(xy_gt_), axis=1)) |
| 83 | xy_pred_ = xy_pred[map_[i]] |
| 84 | if visible.size and xy_pred_.size: |
| 85 | # Pick the predictions closest to ground truth, |
| 86 | # rather than the ones the model has most confident in. |
| 87 | neighbors = crossvalutils.find_closest_neighbors(xy_gt_[visible], xy_pred_, k=3) |
| 88 | found = neighbors != -1 |
| 89 | if ~np.any(found): |
| 90 | continue |
| 91 | min_dists = np.linalg.norm( |
| 92 | xy_gt_[visible][found] - xy_pred_[neighbors[found]], |
| 93 | axis=1, |
| 94 | ) |
| 95 | conf_pred_ = conf_pred[map_[i]] |
| 96 | errors[n, visible[found], i, 0] = min_dists |
| 97 | errors[n, visible[found], i, 1] = conf_pred_[neighbors[found], 0] |
| 98 | return errors |
| 99 | |
| 100 | |
| 101 | def _map(strings, substrings): |
no test coverage detected