-
Notifications
You must be signed in to change notification settings - Fork 16
/
evaluation.py
151 lines (138 loc) · 5.95 KB
/
evaluation.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import pandas as pd
import numpy as np
from PIL import Image
import multiprocessing
import argparse
categories = ['background','aeroplane','bicycle','bird','boat','bottle','bus','car','cat','chair','cow',
'diningtable','dog','horse','motorbike','person','pottedplant','sheep','sofa','train','tvmonitor']
def do_python_eval(predict_folder, gt_folder, name_list, num_cls=21, input_type='png', threshold=1.0, printlog=False):
TP = []
P = []
T = []
for i in range(num_cls):
TP.append(multiprocessing.Value('i', 0, lock=True))
P.append(multiprocessing.Value('i', 0, lock=True))
T.append(multiprocessing.Value('i', 0, lock=True))
def compare(start,step,TP,P,T,input_type,threshold):
for idx in range(start,len(name_list),step):
name = name_list[idx]
if input_type == 'png':
predict_file = os.path.join(predict_folder,'%s.png'%name)
predict = np.array(Image.open(predict_file)) #cv2.imread(predict_file)
if num_cls == 81:
predict = predict - 91
elif input_type == 'npy':
predict_file = os.path.join(predict_folder,'%s.npy'%name)
predict_dict = np.load(predict_file, allow_pickle=True).item()
h, w = list(predict_dict.values())[0].shape
tensor = np.zeros((num_cls,h,w),np.float32)
for key in predict_dict.keys():
tensor[key+1] = predict_dict[key]
tensor[0,:,:] = threshold
predict = np.argmax(tensor, axis=0).astype(np.uint8)
gt_file = os.path.join(gt_folder,'%s.png'%name)
gt = np.array(Image.open(gt_file))
cal = gt<255
mask = (predict==gt) * cal
for i in range(num_cls):
P[i].acquire()
P[i].value += np.sum((predict==i)*cal)
P[i].release()
T[i].acquire()
T[i].value += np.sum((gt==i)*cal)
T[i].release()
TP[i].acquire()
TP[i].value += np.sum((gt==i)*mask)
TP[i].release()
p_list = []
for i in range(8):
p = multiprocessing.Process(target=compare, args=(i,8,TP,P,T,input_type,threshold))
p.start()
p_list.append(p)
for p in p_list:
p.join()
IoU = []
T_TP = []
P_TP = []
FP_ALL = []
FN_ALL = []
for i in range(num_cls):
IoU.append(TP[i].value/(T[i].value+P[i].value-TP[i].value+1e-10))
T_TP.append(T[i].value/(TP[i].value+1e-10))
P_TP.append(P[i].value/(TP[i].value+1e-10))
FP_ALL.append((P[i].value-TP[i].value)/(T[i].value + P[i].value - TP[i].value + 1e-10))
FN_ALL.append((T[i].value-TP[i].value)/(T[i].value + P[i].value - TP[i].value + 1e-10))
loglist = {}
for i in range(num_cls):
loglist[categories[i]] = IoU[i] * 100
miou = np.mean(np.array(IoU))
loglist['mIoU'] = miou * 100
fp = np.mean(np.array(FP_ALL))
loglist['FP'] = fp * 100
fn = np.mean(np.array(FN_ALL))
loglist['FN'] = fn * 100
if printlog:
for i in range(num_cls):
if i%2 != 1:
print('%11s:%7.3f%%'%(categories[i],IoU[i]*100),end='\t')
else:
print('%11s:%7.3f%%'%(categories[i],IoU[i]*100))
print('\n======================================================')
print('%11s:%7.3f%%'%('mIoU',miou*100))
print('\n')
print(f'FP = {fp*100}, FN = {fn*100}')
return loglist
def writedict(file, dictionary):
s = ''
for key in dictionary.keys():
sub = '%s:%s '%(key, dictionary[key])
s += sub
s += '\n'
file.write(s)
def writelog(filepath, metric, comment):
filepath = filepath
logfile = open(filepath,'a')
import time
logfile.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
logfile.write('\t%s\n'%comment)
writedict(logfile, metric)
logfile.write('=====================================\n')
logfile.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--list", default='./VOC2012/ImageSets/Segmentation/train.txt', type=str)
parser.add_argument("--predict_dir", default='./out_rw', type=str)
parser.add_argument("--gt_dir", default='./VOC2012/SegmentationClass', type=str)
parser.add_argument('--logfile', default='./evallog.txt',type=str)
parser.add_argument('--comment', required=True, type=str)
parser.add_argument('--type', default='png', choices=['npy', 'png'], type=str)
parser.add_argument('--t', default=None, type=float)
parser.add_argument('--curve', default=False, type=bool)
parser.add_argument('--num_classes', default=21, type=int)
parser.add_argument('--start', default=0, type=int)
parser.add_argument('--end', default=60, type=int)
args = parser.parse_args()
if args.type == 'npy':
assert args.t is not None or args.curve
df = pd.read_csv(args.list, names=['filename'])
name_list = df['filename'].values
if not args.curve:
loglist = do_python_eval(args.predict_dir, args.gt_dir, name_list, args.num_classes, args.type, args.t, printlog=True)
writelog(args.logfile, loglist, args.comment)
else:
l = []
max_mIoU = 0.0
best_thr = 0.0
for i in range(args.start, args.end):
t = i/100.0
loglist = do_python_eval(args.predict_dir, args.gt_dir, name_list, args.num_classes, args.type, t)
l.append(loglist['mIoU'])
print('%d/60 background score: %.3f\tmIoU: %.3f%%'%(i, t, loglist['mIoU']))
if loglist['mIoU'] > max_mIoU:
max_mIoU = loglist['mIoU']
best_thr = t
else:
break
print('Best background score: %.3f\tmIoU: %.3f%%' % (best_thr, max_mIoU))
writelog(args.logfile, {'mIoU':l, 'Best mIoU': max_mIoU, 'Best threshold': best_thr}, args.comment)