-
Notifications
You must be signed in to change notification settings - Fork 6
/
seg_iou.py
309 lines (217 loc) · 7.81 KB
/
seg_iou.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import numpy as np
import cv2
import copyreg
import types
def pixel_accuracy(eval_segm, gt_segm):
'''
sum_i(n_ii) / sum_i(t_i)
'''
check_size(eval_segm, gt_segm)
cl, n_cl = extract_classes(gt_segm)
eval_mask, gt_mask = extract_both_masks(eval_segm, gt_segm, cl, n_cl)
sum_n_ii = 0
sum_t_i = 0
for i, c in enumerate(cl):
curr_eval_mask = eval_mask[i, :, :]
curr_gt_mask = gt_mask[i, :, :]
sum_n_ii += np.sum(np.logical_and(curr_eval_mask, curr_gt_mask))
sum_t_i += np.sum(curr_gt_mask)
if (sum_t_i == 0):
pixel_accuracy_ = 0
else:
pixel_accuracy_ = sum_n_ii / sum_t_i
return pixel_accuracy_
def mean_accuracy(eval_segm, gt_segm):
'''
(1/n_cl) sum_i(n_ii/t_i)
'''
check_size(eval_segm, gt_segm)
cl, n_cl = extract_classes(gt_segm)
eval_mask, gt_mask = extract_both_masks(eval_segm, gt_segm, cl, n_cl)
accuracy = list([0]) * n_cl
for i, c in enumerate(cl):
curr_eval_mask = eval_mask[i, :, :]
curr_gt_mask = gt_mask[i, :, :]
n_ii = np.sum(np.logical_and(curr_eval_mask, curr_gt_mask))
t_i = np.sum(curr_gt_mask)
if (t_i != 0):
accuracy[i] = n_ii / t_i
mean_accuracy_ = np.mean(accuracy)
return mean_accuracy_
def Acc_Metric(Mask,GT):
GT_pos_sum = np.sum(GT == 1)
Mask_pos_sum = np.sum(Mask == 1)
True_pos_sum = np.sum((GT == 1) * (Mask == 1))
Precision = float(True_pos_sum) / (Mask_pos_sum + 1e-6)
Recall = float(True_pos_sum) / (GT_pos_sum + 1e-6)
IoU = float(True_pos_sum) / (GT_pos_sum + Mask_pos_sum - True_pos_sum + 1e-6)
# IoU = Precision * Recall / (Precision + Recall - Precision * Recall + 1e-6)
if GT_pos_sum==0 and Mask_pos_sum==0:
IoU =1
F1_score = 2 * Precision * Recall / (Precision + Recall + 1e-6)
return Recall,Precision,F1_score,IoU
def Modify_Lable(Mask,GT):
error = np.abs(GT-Mask)
error[error > 0.85] = 1
error[error <= 0.85] = 0
return np.abs(GT-error)
def r_iou(predict, label):
tp = np.sum(np.logical_and(predict == 1, label == 1))
fp = np.sum(predict==1)
fn = np.sum(label == 1)
if (fp+fn-tp)==0:
return 1
return tp/(fp+fn-tp)
def mean_IU(eval_segm, gt_segm):
'''
(1/n_cl) * sum_i(n_ii / (t_i + sum_j(n_ji) - n_ii))
'''
check_size(eval_segm, gt_segm)
cl, n_cl = union_classes(eval_segm, gt_segm)
_, n_cl_gt = extract_classes(gt_segm)
eval_mask, gt_mask = extract_both_masks(eval_segm, gt_segm, cl, n_cl)
IU = list([0]) * n_cl
for i, c in enumerate(cl):
curr_eval_mask = eval_mask[i, :, :]
curr_gt_mask = gt_mask[i, :, :]
if (np.sum(curr_eval_mask) == 0) or (np.sum(curr_gt_mask) == 0):
continue
n_ii = np.sum(np.logical_and(curr_eval_mask, curr_gt_mask))
t_i = np.sum(curr_gt_mask)
n_ij = np.sum(curr_eval_mask)
IU[i] = n_ii / (t_i + n_ij - n_ii)
return IU[-1]
# mean_IU_ = np.sum(IU) / n_cl_gt
# return mean_IU_
def frequency_weighted_IU(eval_segm, gt_segm):
'''
sum_k(t_k)^(-1) * sum_i((t_i*n_ii)/(t_i + sum_j(n_ji) - n_ii))
'''
check_size(eval_segm, gt_segm)
cl, n_cl = union_classes(eval_segm, gt_segm)
eval_mask, gt_mask = extract_both_masks(eval_segm, gt_segm, cl, n_cl)
frequency_weighted_IU_ = list([0]) * n_cl
for i, c in enumerate(cl):
curr_eval_mask = eval_mask[i, :, :]
curr_gt_mask = gt_mask[i, :, :]
if (np.sum(curr_eval_mask) == 0) or (np.sum(curr_gt_mask) == 0):
continue
n_ii = np.sum(np.logical_and(curr_eval_mask, curr_gt_mask))
t_i = np.sum(curr_gt_mask)
n_ij = np.sum(curr_eval_mask)
frequency_weighted_IU_[i] = (t_i * n_ii) / (t_i + n_ij - n_ii)
sum_k_t_k = get_pixel_area(eval_segm)
frequency_weighted_IU_ = np.sum(frequency_weighted_IU_) / sum_k_t_k
return frequency_weighted_IU_
'''
Auxiliary functions used during evaluation.
'''
def get_pixel_area(segm):
return segm.shape[0] * segm.shape[1]
def extract_both_masks(eval_segm, gt_segm, cl, n_cl):
eval_mask = extract_masks(eval_segm, cl, n_cl)
gt_mask = extract_masks(gt_segm, cl, n_cl)
return eval_mask, gt_mask
def extract_classes(segm):
cl = np.unique(segm)
n_cl = len(cl)
return cl, n_cl
def union_classes(eval_segm, gt_segm):
eval_cl, _ = extract_classes(eval_segm)
gt_cl, _ = extract_classes(gt_segm)
cl = np.union1d(eval_cl, gt_cl)
n_cl = len(cl)
return cl, n_cl
def extract_masks(segm, cl, n_cl):
h, w = segm_size(segm)
masks = np.zeros((n_cl, h, w))
for i, c in enumerate(cl):
masks[i, :, :] = segm == c
return masks
def segm_size(segm):
try:
height = segm.shape[0]
width = segm.shape[1]
except IndexError:
raise
return height, width
def check_size(eval_segm, gt_segm):
h_e, w_e = segm_size(eval_segm)
h_g, w_g = segm_size(gt_segm)
if (h_e != h_g) or (w_e != w_g):
print("DiffDim: Different dimensions of matrices!")
def _pickle_method(m):
if m.im_self is None:
return getattr, (m.im_class, m.im_func.func_name)
else:
return getattr, (m.im_self, m.im_func.func_name)
copyreg.pickle(types.MethodType, _pickle_method)
class ConfusionMatrix(object):
def __init__(self, nclass, classes=None, ignore_label=255):
self.nclass = nclass
self.classes = classes
self.M = np.zeros((nclass, nclass))
self.ignore_label = ignore_label
def add(self, gt, pred):
assert (np.max(pred) <= self.nclass)
assert (len(gt) == len(pred))
for i in range(len(gt)):
if not gt[i] == self.ignore_label:
self.M[gt[i], pred[i]] += 1.0
def addM(self, matrix):
assert (matrix.shape == self.M.shape)
self.M += matrix
def __str__(self):
pass
# Pii为预测正确的数量,Pij和Pji分别被解释为假正和假负,尽管两者都是假正与假负之和
def recall(self): # 预测为正确的像素中确认为正确像素的个数
recall = 0.0
for i in range(self.nclass):
recall += self.M[i, i] / np.sum(self.M[:, i])
return recall / self.nclass
def accuracy(self): # 分割正确的像素除以总像素
accuracy = 0.0
for i in range(self.nclass):
accuracy += self.M[i, i] / np.sum(self.M[i, :])
return accuracy / self.nclass
# 雅卡尔指数,又称为交并比(IOU)
def jaccard(self):
jaccard = 0.0
jaccard_perclass = []
for i in range(self.nclass):
if not self.M[i, i] == 0:
jaccard_perclass.append(self.M[i, i] / (np.sum(self.M[i, :]) + np.sum(self.M[:, i]) - self.M[i, i]))
return np.sum(jaccard_perclass) / len(jaccard_perclass), jaccard_perclass, self.M
def generateM(self, item):
gt, pred = item
m = np.zeros((self.nclass, self.nclass))
assert (len(gt) == len(pred))
for i in range(len(gt)):
if gt[i] < self.nclass: # and pred[i] < self.nclass:
m[gt[i], pred[i]] += 1.0
return m
def get_iou(data_list, class_num, save_path=None):
"""
Args:
data_list: a list, its elements [gt, output]
class_num: the number of label
"""
from multiprocessing import Pool
ConfM = ConfusionMatrix(class_num)
f = ConfM.generateM
pool = Pool()
m_list = pool.map(f, data_list)
pool.close()
pool.join()
for m in m_list:
ConfM.addM(m)
aveJ, j_list, M = ConfM.jaccard()
# print(j_list)
# print(M)
# print('meanIOU: ' + str(aveJ) + '\n')
if save_path:
with open(save_path, 'w') as f:
f.write('meanIOU: ' + str(aveJ) + '\n')
f.write(str(j_list) + '\n')
f.write(str(M) + '\n')
return aveJ, j_list