-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluation_precison.py
175 lines (142 loc) · 6.32 KB
/
evaluation_precison.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import os.path as osp
import pandas as pd
import numpy as np
from PIL import Image
import multiprocessing
import argparse
import pdb
################################################################################
# Evaluate the performance by computing mIoU.
# It assumes that every CAM or CRF dict file is already infered and saved.
# For CAM, threshold will be searched in range [0.01, 0.80].
#
# If you want to evaluate CAM performance...
# python evaluation.py --name [exp_name] --task cam --dict_dir dict
#
# Or if you want to evaluate CRF performance of certain alpha (let, a1)...
# python evaluation.py --name [exp_name] --task crf --dict_dir crf/a1
#
# For AFF evaluation, go to evaluation_aff.py
################################################################################
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, task, threshold, 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,task,threshold):
for idx in range(start,len(name_list),step):
name = name_list[idx]
if task=='cam':
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((21,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 # Reject object boundary
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,task,threshold))
p.start()
p_list.append(p)
for p in p_list:
p.join()
precision = []
recall = []
# F = []
IoU = []
for i in range(num_cls):
precision.append(TP[i].value/(P[i].value+1e-10))
recall.append(TP[i].value/(T[i].value+1e-10))
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]] = precision[i] * 100
# for i in range(num_cls):
# F.append(2*precision[i]*recall[i]/(precision[i]+recall[i]))
miou = np.mean(np.array(IoU))
mp = np.mean(np.array(precision))
mr = np.mean(np.array(recall))
# mf = np.mean(np.array(F))
loglist['mIoU'] = miou * 100
loglist['mP'] = mp
loglist['mR'] = mr
# loglist['mF'] = mf
# if printlog:
# for i in range(num_cls):
# if i%2 != 1:
# print('%11s:%7.3f%%'%(categories[i],precision[i]*100),end='\t')
# else:
# print('%11s:%7.3f%%'%(categories[i],precision[i]*100))
# print('\n======================================================')
# print('%11s:%7.3f%%'%('mIoU',miou*100))
return loglist
def eval_in_script(logger=None, eval_list='train', task='cam', pred_dir=None, gt_dir='./data/VOC2012/SegmentationClass'):
eval_list = './data/VOC2012/ImageSets/Segmentation/' + eval_list + '.txt'
df = pd.read_csv(eval_list, names=['filename'])
name_list = df['filename'].values
max_miou = 0
max_th = 0
for i in range(20):
t = i/100.0+0.15
loglist = do_python_eval(pred_dir, gt_dir, name_list, 21, task, t, printlog=False)
miou_temp = loglist['mIoU']
if miou_temp>max_miou:
max_miou = miou_temp
max_th = t
precision = loglist['mP']
recall = loglist['mR']
ret_dict = {}
ret_dict['th'] = max_th
ret_dict['miou'] = max_miou
ret_dict['mp'] = precision
ret_dict['mr'] = recall
return ret_dict
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--list", default="train", type=str)
parser.add_argument("--task", required=True, type=str)
parser.add_argument("--name", required=True, type=str)
parser.add_argument("--dict_dir", required=True, type=str)
parser.add_argument("--gt_dir", default='./data/VOC2012/SegmentationClass', type=str)
args = parser.parse_args()
eval_list = './data/VOC2012/ImageSets/Segmentation/' + args.list + '.txt'
df = pd.read_csv(eval_list, names=['filename'])
name_list = df['filename'].values
pred_dir = "./experiments/"+osp.join(args.name,args.dict_dir)
print('Evaluate ' + pred_dir + ' with ' + eval_list)
if args.task=='cam':
for i in range(10):
t = i/100.+0.40
loglist = do_python_eval(pred_dir, args.gt_dir, name_list, 21, args.task, t, printlog=False)
print(loglist)
# print('%d/60 threshold: %.3f\tmIoU: %.3f \tmP: %.3f \tmR: %.3f \tmF: %.3f%%'%(i, t, loglist['mIoU'], loglist['mP'], loglist['mR'], loglist['mF']))
elif args.task=='crf':
loglist = do_python_eval(pred_dir, args.gt_dir, name_list, 21, args.task, 0, printlog=True)
elif args.task=='dl'or args.task=='png':
loglist = do_python_eval(pred_dir, args.gt_dir, name_list, 21, args.task, 0, printlog=True)