-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_map.py
51 lines (47 loc) · 1.96 KB
/
gen_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import sys
from voc_eval import voc_eval
import os
import argparse
import logging as log
def generate_aps(results_root="res", epoch=0):
results_dir = results_root+ "/{}.txt"
output_path = results_root+ "/perf_" + str(epoch) + ".csv"
anno_path = "./data/VOC2007/Annotations/{}.xml"
# imageset_path = "VOCdevkit/VOC2007/ImageSets/Main/test.txt"
class_names = ["aeroplane", "bicycle", "boat", "bottle", "bus", "car", "chair", "diningtable", "motorbike", "sofa", "train", "tvmonitor"]
recs = []
precs = []
aps = []
tested_class = []
for class_name in class_names:
imageset_path = "./data/pascal3d+/Imagesets/{}.txt".format(class_name)
cachedir = "./data"
# cache_file = os.path.join(cachedir, "annots.pkl")
# if os.path.exists(cache_file):
# os.remove(cache_file)
if not os.path.exists(results_dir.format(class_name)):
log.warning("missing data: {}".format(class_name))
rec, prec, ap = 0., 0., 0.
else:
rec, prec, ap = voc_eval(results_dir, anno_path, imageset_path, class_name, cachedir, use_07_metric=True)
recs.append(rec)
precs.append(prec)
aps.append(ap)
tested_class.append(class_name)
mAP = np.array(aps).mean()
header = ",".join([*class_names, "mean"])+'\n'
data = ",".join(["{}".format(ap) for ap in aps]+ ["{}".format(mAP)])+'\n'
with open(output_path, "w") as f:
f.write(header)
f.write(data)
# print(header+data)
for i, test_c in enumerate(tested_class):
print("{} accuracy: {:.2f}%".format(test_c, aps[i] * 100))
print("mean accuracy: {:.2f}%".format(100 * np.array(aps).mean()))
print('--------------------------------------------------')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='mcnn worldexp.')
parser.add_argument('--root', type=str, default="res")
args = parser.parse_args()
generate_aps(args.root)