Generic functions to compute precision/recall for object detection for a single class. Args: pred (dict): Predictions mapping from image id to bounding boxes and scores. gt (dict): Ground truths mapping from image id to bounding boxes. iou_thr (list[float
(pred, gt, iou_thr=None)
| 54 | |
| 55 | |
| 56 | def eval_det_cls(pred, gt, iou_thr=None): |
| 57 | """Generic functions to compute precision/recall for object detection for a |
| 58 | single class. |
| 59 | |
| 60 | Args: |
| 61 | pred (dict): Predictions mapping from image id to bounding boxes |
| 62 | and scores. |
| 63 | gt (dict): Ground truths mapping from image id to bounding boxes. |
| 64 | iou_thr (list[float]): A list of iou thresholds. |
| 65 | |
| 66 | Return: |
| 67 | tuple (np.ndarray, np.ndarray, float): Recalls, precisions and |
| 68 | average precision. |
| 69 | """ |
| 70 | |
| 71 | # {img_id: {'bbox': box structure, 'det': matched list}} |
| 72 | class_recs = {} |
| 73 | npos = 0 |
| 74 | # figure out the bbox code size first |
| 75 | gt_bbox_code_size = 9 |
| 76 | pred_bbox_code_size = 9 |
| 77 | for img_id in gt.keys(): |
| 78 | if len(gt[img_id]) != 0: |
| 79 | gt_bbox_code_size = gt[img_id][0].tensor.shape[1] |
| 80 | break |
| 81 | for img_id in pred.keys(): |
| 82 | if len(pred[img_id][0]) != 0: |
| 83 | pred_bbox_code_size = pred[img_id][0][0].tensor.shape[1] |
| 84 | break |
| 85 | assert gt_bbox_code_size == pred_bbox_code_size |
| 86 | for img_id in gt.keys(): |
| 87 | cur_gt_num = len(gt[img_id]) |
| 88 | if cur_gt_num != 0: |
| 89 | gt_cur = torch.zeros([cur_gt_num, gt_bbox_code_size], |
| 90 | dtype=torch.float32) |
| 91 | for i in range(cur_gt_num): |
| 92 | gt_cur[i] = gt[img_id][i].tensor |
| 93 | bbox = gt[img_id][0].new_box(gt_cur) |
| 94 | else: |
| 95 | bbox = gt[img_id] |
| 96 | det = [[False] * len(bbox) for i in iou_thr] |
| 97 | npos += len(bbox) |
| 98 | class_recs[img_id] = {'bbox': bbox, 'det': det} |
| 99 | |
| 100 | # construct dets |
| 101 | image_ids = [] |
| 102 | confidence = [] |
| 103 | ious = [] |
| 104 | for img_id in pred.keys(): |
| 105 | cur_num = len(pred[img_id]) |
| 106 | if cur_num == 0: |
| 107 | continue |
| 108 | pred_cur = torch.zeros((cur_num, pred_bbox_code_size), |
| 109 | dtype=torch.float32) |
| 110 | box_idx = 0 |
| 111 | for box, score in pred[img_id]: |
| 112 | image_ids.append(img_id) |
| 113 | confidence.append(score) |
no test coverage detected