| 1004 | |
| 1005 | |
| 1006 | def calc_map(ap_data): |
| 1007 | print('Calculating mAP...') |
| 1008 | aps = [{'box': [], 'mask': []} for _ in iou_thresholds] |
| 1009 | |
| 1010 | for _class in range(len(cfg.dataset.class_names)): |
| 1011 | for iou_idx in range(len(iou_thresholds)): |
| 1012 | for iou_type in ('box', 'mask'): |
| 1013 | ap_obj = ap_data[iou_type][iou_idx][_class] |
| 1014 | |
| 1015 | if not ap_obj.is_empty(): |
| 1016 | aps[iou_idx][iou_type].append(ap_obj.get_ap()) |
| 1017 | |
| 1018 | all_maps = {'box': OrderedDict(), 'mask': OrderedDict()} |
| 1019 | |
| 1020 | # Looking back at it, this code is really hard to read :/ |
| 1021 | for iou_type in ('box', 'mask'): |
| 1022 | all_maps[iou_type]['all'] = 0 # Make this first in the ordereddict |
| 1023 | for i, threshold in enumerate(iou_thresholds): |
| 1024 | mAP = sum(aps[i][iou_type]) / len(aps[i][iou_type]) * 100 if len(aps[i][iou_type]) > 0 else 0 |
| 1025 | all_maps[iou_type][int(threshold*100)] = mAP |
| 1026 | all_maps[iou_type]['all'] = (sum(all_maps[iou_type].values()) / (len(all_maps[iou_type].values())-1)) |
| 1027 | |
| 1028 | print_maps(all_maps) |
| 1029 | |
| 1030 | # Put in a prettier format so we can serialize it to json during training |
| 1031 | all_maps = {k: {j: round(u, 2) for j, u in v.items()} for k, v in all_maps.items()} |
| 1032 | return all_maps |
| 1033 | |
| 1034 | def print_maps(all_maps): |
| 1035 | # Warning: hacky |