-
Notifications
You must be signed in to change notification settings - Fork 0
/
measures.py
290 lines (231 loc) · 8.2 KB
/
measures.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
import numpy as np
from abc import ABC, abstractmethod
import tqdm
'''reference: FAU deep learning exercise'''
'''
Measures for multi-class segmentation
Input: labels and predictions must be of the same shape
labels: numpy.ndarray, shape=[batch_size, ...]
predictions: numpy.ndarray, shape=[batch_size, ...]
Available measures:
Accuracy,
Precision,
Recall,
MeanIoU,
FrequencyWeightedIoU,
'''
# TODO: classwise evaluation
class MeasureBase(ABC):
def __init__(self, class_names, **kwargs):
self._class_name = class_names
self._eval_mat = None
self.kwargs = kwargs
@property
@abstractmethod
def name(self):
pass
@abstractmethod
def _new_batch(self, eval_mat):
# add the new input batch for evaluation
pass
def add_batch(self, eval_mat):
# labels and predictions must be of the same shape
self._eval_mat = eval_mat
self._new_batch(self._eval_mat)
@abstractmethod
def value(self):
# calculate the measured value
pass
def labels(self):
return self._class_name
# evaluation matrix
@property
def eval_mat(self):
return self._eval_mat
@eval_mat.setter
def eval_mat(self, x):
self._eval_mat = x
# def calculate_eval_matrix(num_cls, labels, predictions):
# # labels & predictions: 1D reshaped vector
# # return:
# # eval_mat[n_batch, i, j]: num of pixels of class i, predicted as class j
# #print(labels.shape, predictions.shape)
# assert labels.shape == predictions.shape
#
# # convert to same data type
# n_batch = labels.shape[0]
#
# labels = labels.astype(np.uint8).flatten()
# predictions = predictions.astype(np.uint8).flatten()
#
# eval_mat = np.zeros([num_cls, num_cls])
# for i in range(num_cls):
# for j in range(num_cls):
# #eval_mat[b, i, j] = np.sum(labels==i & predictions==j)
# eval_mat[i, j] = np.sum(np.logical_and(labels==i, predictions==j))
# return eval_mat
def calculate_eval_matrix(num_cls, gt, eval_vol,batch_size=100):
# flatten the inputs
confusion = np.zeros([num_cls, num_cls])
start = 0
end = batch_size
pbar = tqdm.tqdm(total=int(eval_vol.shape[0]) + 1)
while start < eval_vol.shape[0]:
if end > eval_vol.shape[0]: end = eval_vol.shape[0]
gt_batch = gt[..., start: end]
eval_vol_batch = eval_vol[..., start: end]
gt_batch = np.array(gt_batch).flatten()
eval_vol_batch = np.array(eval_vol_batch).flatten()
# confusion[i, j]: num of pixels of class i, predicted as class j
for i in range(num_cls):
for j in range(num_cls):
confusion[i, j] += np.sum(np.logical_and(gt_batch == i, eval_vol_batch == j))
start = end
end = start + batch_size
pbar.update(1)
pbar.close()
return confusion.astype(np.int32)
def calculate_union(eval_mat):
# calculate the union for IoU evaluation
# eval_mat.shape: [n_batch, n_cls, n_cls]
# return: union.shape = [n_batch, n_cls]
n_batch = eval_mat.shape[0]
n_cls = eval_mat.shape[1]
union = np.sum(eval_mat, axis=1) + np.sum(eval_mat, axis=0) - np.diagonal(eval_mat, axis1=0, axis2=1)
# assert union.shape[0] == n_cls
# assert union.shape[1] == n_cls
return union
def calculate_intersection(eval_mat):
return np.diagonal(eval_mat, axis1=0, axis2=1)
def calculate_IoU(eval_mat):
return np.around(calculate_intersection(eval_mat)/calculate_union(eval_mat),decimals=3)
def calculate_dice(eval_mat):
TP=calculate_intersection(eval_mat)
return np.around(TP*2/(calculate_union(eval_mat)+TP),decimals=3)
class Accuracy(MeasureBase):
def __init__(self, class_names, **kwargs):
super().__init__(class_names, **kwargs)
self.count = None
self.count_correct = None
# exclude bg for better measurement
self.no_background = kwargs.get('no_background', False)
@property
def name(self):
return "Accuracy"
def _new_batch(self, eval_mat):
# count the correctly predicted pixels
# eval_mat: [batch, num_cls, num_cls]
if self.no_background is False:
pass
else:
eval_mat[:, 0, :] = 0
self.count = np.sum(eval_mat[0])
count_correct = np.diagonal(eval_mat, axis1=1, axis2=2)
self.count_correct = np.sum(count_correct, axis=-1)
def value(self):
if self.count > 0:
return np.mean(self.count_correct)/self.count
else:
return 0
# class ClasswiseAccuracy(MeasureBase):
# def __init__(self, class_names):
# super().__init__(class_names)
# self._class_names = class_names
# self._count = None
# self._count_correct = None
#
# @property
# def name(self):
# return "ClasswiseAccuracy"
#
# def _new_batch(self, eval_mat):
#
# self._count = np.sum(eval_mat, axis=-1)
# self._count_correct = np.diagonal(eval_mat, axis1=1, axis2=2)
#
# def value(self):
# res = np.zeros(len(self._class_names))
# for i in range(len(res)):
# res_tmp = np.divide(self._count_correct[:, i], self._count[:, i],
# out = np.zeros_like(self._count[:, i]), where=self._count[:, i]!=0)
# res[i] = np.mean(res_tmp)
#
# return res
class Precision(MeasureBase):
def __init__(self, class_names, **kwargs):
super().__init__(class_names, **kwargs)
self._count = None
self._sum = None
self.no_background = kwargs.get('no_background', False)
@property
def name(self):
return 'Precision'
def _new_batch(self, eval_mat):
if self.no_background is False:
pass
else:
eval_mat = eval_mat[:, 1:, 1:]
self._count = np.diagonal(eval_mat, axis1=1, axis2=2)
self._sum = np.sum(eval_mat, axis=1)
def value(self):
res = np.mean(self._count/self._sum, axis=1)
return np.mean(res)
class Recall(MeasureBase):
def __init__(self, class_names, **kwargs):
super().__init__(class_names, **kwargs)
self._count = None
self._sum = None
self.no_background = kwargs.get('no_background', False)
@property
def name(self):
return 'Recall'
def _new_batch(self, eval_mat):
if self.no_background is False:
pass
else:
eval_mat = eval_mat[:, 1:, 1:]
self._count = np.diagonal(eval_mat, axis1=1, axis2=2)
self._sum = np.sum(eval_mat, axis=2)
def value(self):
res = np.mean(self._count/self._sum)
return res
class MeanIoU(MeasureBase):
def __init__(self, class_names, **kwargs):
super().__init__(class_names, **kwargs)
self._count_I = None
self._count_U = None
self.no_background = kwargs.get('no_background', False)
self.smooth = kwargs.get('smooth', 1.0)
@property
def name(self):
return 'MeanIoU'
def _new_batch(self, eval_mat):
self._count_I = np.diagonal(eval_mat, axis1=1, axis2=2)
self._count_U = calculate_union(eval_mat) + self.smooth
def value(self):
IoU = self._count_I / self._count_U
if self.no_background is True:
IoU = IoU[:, 1:]
res = np.mean(IoU, axis=1)
return np.mean(res)
class FrequencyWeightedIoU(MeasureBase):
def __init__(self, class_names, **kwargs):
super(FrequencyWeightedIoU, self).__init__(class_names, **kwargs)
self._count_I = None
self._count_U = None
self._count_cls = None
self.no_background = kwargs.get('no_background', False)
@property
def name(self):
return 'FrequencyWeightedIoU'
def _new_batch(self, eval_mat):
self._count_I = np.diagonal(eval_mat, axis1=1, axis2=2)
self._count_U = calculate_union(eval_mat)
self._count_cls = np.sum(eval_mat, axis=2)
def value(self):
IoU = self._count_I / self._count_U
if self.no_background is True:
IoU = IoU[:, 1:]
self._count_cls = self._count_cls[:, 1:]
res = np.sum(IoU * self._count_cls, axis=1) / np.sum(self._count_cls, axis=1)
return np.mean(res)