(det_boxes, gt_boxes, iou_threshold=0.5, method='area')
| 34 | |
| 35 | |
| 36 | def compute_map(det_boxes, gt_boxes, iou_threshold=0.5, method='area'): |
| 37 | # det_boxes = [ |
| 38 | # { |
| 39 | # 'person' : [[x1, y1, x2, y2, score], ...], |
| 40 | # 'car' : [[x1, y1, x2, y2, score], ...] |
| 41 | # } |
| 42 | # {det_boxes_img_2}, |
| 43 | # ... |
| 44 | # {det_boxes_img_N}, |
| 45 | # ] |
| 46 | # |
| 47 | # gt_boxes = [ |
| 48 | # { |
| 49 | # 'person' : [[x1, y1, x2, y2], ...], |
| 50 | # 'car' : [[x1, y1, x2, y2], ...] |
| 51 | # }, |
| 52 | # {gt_boxes_img_2}, |
| 53 | # ... |
| 54 | # {gt_boxes_img_N}, |
| 55 | # ] |
| 56 | |
| 57 | gt_labels = {cls_key for im_gt in gt_boxes for cls_key in im_gt.keys()} |
| 58 | gt_labels = sorted(gt_labels) |
| 59 | all_aps = {} |
| 60 | # average precisions for ALL classes |
| 61 | aps = [] |
| 62 | for idx, label in enumerate(gt_labels): |
| 63 | # Get detection predictions of this class |
| 64 | cls_dets = [ |
| 65 | [im_idx, im_dets_label] for im_idx, im_dets in enumerate(det_boxes) |
| 66 | if label in im_dets for im_dets_label in im_dets[label] |
| 67 | ] |
| 68 | |
| 69 | # cls_dets = [ |
| 70 | # (0, [x1_0, y1_0, x2_0, y2_0, score_0]), |
| 71 | # ... |
| 72 | # (0, [x1_M, y1_M, x2_M, y2_M, score_M]), |
| 73 | # (1, [x1_0, y1_0, x2_0, y2_0, score_0]), |
| 74 | # ... |
| 75 | # (1, [x1_N, y1_N, x2_N, y2_N, score_N]), |
| 76 | # ... |
| 77 | # ] |
| 78 | |
| 79 | # Sort them by confidence score |
| 80 | cls_dets = sorted(cls_dets, key=lambda k: -k[1][-1]) |
| 81 | |
| 82 | # For tracking which gt boxes of this class have already been matched |
| 83 | gt_matched = [[False for _ in im_gts[label]] for im_gts in gt_boxes] |
| 84 | # Number of gt boxes for this class for recall calculation |
| 85 | num_gts = sum([len(im_gts[label]) for im_gts in gt_boxes]) |
| 86 | tp = [0] * len(cls_dets) |
| 87 | fp = [0] * len(cls_dets) |
| 88 | |
| 89 | # For each prediction |
| 90 | for det_idx, (im_idx, det_pred) in enumerate(cls_dets): |
| 91 | # Get gt boxes for this image and this label |
| 92 | im_gts = gt_boxes[im_idx][label] |
| 93 | max_iou_found = -1 |
no test coverage detected