-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevaluation.py
410 lines (322 loc) · 15 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# -*- coding: utf-8 -*-
"""
evaluation module
"""
import torch
import numpy as np
from sklearn.metrics import confusion_matrix as cm_sklearn
class EvaluationReport:
""" Evaluation report for a multiclass pixel-level segmentation task """
averages = np.array(["binary", "macro", "weighted"])
metrics = {"accuracy", "sensitivity", "specificity", "dice_coeff",
"jaccard_sim", "precision", "recall", "f1_score"}
def __init__(self, confusion_matrix, labels, weights=None):
self._cm = confusion_matrix
self._decimal_places = 4
self._labels = np.array(labels)
self._n_classes = len(labels)
self._weights = weights
if self._weights is None:
total = 0
self._weights = np.zeros(len(labels))
for label in self._labels:
c = np.where(self._labels == label)[0][0]
nc = sum(self._cm[c,:])
total += nc
self._weights[c] = nc
self._weights = self._weights / total
@classmethod
def from_predictions(cls, ground_truths, predictions, labels, weights=None):
""" Create a MetricsReport object given the ground-truth labels and the
predicted labels.
Args:
ground_truths (array-like of shape) - Ground-truth (correct) label values
predictions (array-like of shape) - Predicted label values
labels (array-like of shape) - Possible pixel labels
weights (array-like of shape, optional) - Weights of the different classes
Returs:
(EvaluationReport) - A evaluation report
"""
cm = cm_sklearn(ground_truths, predictions, labels=labels)
return cls(cm, labels, weights)
@classmethod
def from_model(cls, dataloader, model, labels, weights=None):
""" Create a MetricsReport object given a dataloader and the model to
make the predictions.
Args:
dataloader (torch.utils.data.Dataloader) - Dataloader
model (torch.nn.Module) - Model to make the predictions
labels (array-like of shape) - Possible pixel labels
weights (array-like of shape, optional) - Weights of the different
classes
Returs:
(EvaluationReport) - A evaluation report
"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
true_list = []
pred_list = []
model = model.to(device)
model.eval()
with torch.no_grad():
for inputs, ground_truths in dataloader:
inputs = inputs.to(device)
outputs = model(inputs)
pred_list.append(torch.argmax(outputs.detach().cpu(), dim = 1))
true_list.append(ground_truths)
true_list = torch.flatten(torch.cat(true_list))
pred_list = torch.flatten(torch.cat(pred_list))
cm = cm_sklearn(true_list.detach(), pred_list.detach(), labels=labels)
return cls(cm, labels, weights)
@property
def weights(self):
return self._weights
@property
def decimal_places(self):
return self._decimal_places
@property
def labels(self):
return self._labels
@weights.setter
def weights(self, weights):
assert len(weights) == len(self._labels), "invalid number of weights"
self._weights = weights
@decimal_places.setter
def decimal_places(self, decimal_places):
assert decimal_places >= 1, "the minimimum number of decimal places is 1"
self._decimal_places = decimal_places
@staticmethod
def _TN(TN, FP, FN, TP):
return TN
@staticmethod
def _FP(TN, FP, FN, TP):
return FP
@staticmethod
def _FN(TN, FP, FN, TP):
return FN
@staticmethod
def _TP(TN, FP, FN, TP):
return TP
@staticmethod
def _accuracy(TN, FP, FN, TP):
return (TP + TN) / (TP + TN + FP + FN + 1e-7)
@staticmethod
def _precision(TN, FP, FN, TP):
return (TP / (TP + FP + 1e-7))
@staticmethod
def _recall(TN, FP, FN, TP):
return (TP / (TP + FN + 1e-7))
@staticmethod
def _specificity(TN, FP, FN, TP):
return (TN / (TN + FP + 1e-7))
@staticmethod
def _jaccard_similarity(TN, FP, FN, TP):
return (TP / (TP + FP + FN + 1e-7))
@staticmethod
def _f1_score(TN, FP, FN, TP):
return (2*TP / (2*TP + FP + FN + 1e-7))
def _weighted_sum(self, values, weights, normalize=False):
""" Compute a weighted sum """
value = np.average(values, weights=weights)
return round(value, self._decimal_places)
def _confusion_matrix(self, c):
""" Obtain the confusion matrix for class c """
TP = self._cm[c, c]
FN = sum(self._cm[c,:]) - self._cm[c,c]
FP = sum(self._cm[:,c]) - self._cm[c,c]
TN = sum(sum(self._cm)) - TP - FN - FP
return np.array([[TN, FP], [FN, TP]])
def _general_metric(self, metric, pos_label=1, average="binary"):
""" Obtain the necessary confusion matrix and weights to compute any metric.
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(array-like, array-like) - Indices of the confusion matrices and weights
"""
assert any(np.isin(EvaluationReport.averages, average)), "unknown 'average' method"
assert any(np.isin(self._labels, pos_label)), "unknown target class '{}'".format(pos_label)
indices = []
weights = []
scores = []
if average == "binary":
index_label = np.where(self._labels == pos_label)[0][0]
indices.append(index_label)
weights.append(1)
else:
for label in self._labels:
index_label = np.where(self._labels == label)[0][0]
indices.append(index_label)
if average == "macro":
weights = np.ones(len(self._labels))
else:
weights = self._weights
indices = np.array(indices)
for i in indices:
TN, FP, FN, TP = self._confusion_matrix(i).ravel()
score_i = metric(TN, FP, FN, TP)
scores.append(score_i)
return self._weighted_sum(scores, weights)
def confusion_matrix(self, pos_label=1):
""" Return the confusion matrix of a certain label.
Args:
pos_label(int) -
Returns:
(numpy.array) - Confusion matrix
"""
assert any(np.isin(self._labels, pos_label)), "unknown target class '{}'".format(pos_label)
c = np.where(self._labels == pos_label)[0][0]
return self._confusion_matrix(c)
def get_metrics(self, metrics="all", pos_label=1, average="binary"):
""" Compute a set of metrics.
Args:
metrics (any subset of {"all", "accuracy", "sensitivity", "specificity",
"dice_coeff", "jaccard_sim", "precision", "recall", "f1_score"'}, default="all") -
Metrics to be computed.
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’, ’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - All metrics
"""
assert metrics == "all" or set(metrics).issubset(EvaluationReport.metrics), "invalid list of metrics"
report = dict()
include_all = (metrics == "all")
metrics = np.array(metrics)
if include_all or any(np.isin(metrics, "accuracy")):
report["accuracy"] = self.accuracy(pos_label=pos_label, average=average)
if include_all or any(np.isin(metrics, "sensitivity")):
report["sensitivity"] = self.sensitivity(pos_label=pos_label, average=average)
if include_all or any(np.isin(metrics, "specificity")):
report["specificity"] = self.specificity(pos_label=pos_label, average=average)
if include_all or any(np.isin(metrics, "dice_coeff")):
report["dice_coeff"] = self.dice_coeff(pos_label=pos_label, average=average)
if include_all or any(np.isin(metrics, "jaccard_sim")):
report["jaccard_sim"] = self.jaccard_similarity(pos_label=pos_label, average=average)
if include_all or any(np.isin(metrics, "precision")):
report["precision"] = self.precision(pos_label=pos_label, average=average)
if include_all or any(np.isin(metrics, "recall")):
report["recall"] = self.recall(pos_label=pos_label, average=average)
if include_all or any(np.isin(metrics, "f1_score")):
report["f1_score"] = self.f1_score(pos_label=pos_label, average=average)
return report
def TN(self, pos_label=1, average="binary"):
""" Compute the True Negatives.
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - True Negatives
"""
return self._general_metric(EvaluationReport._TN, pos_label, average)
def FP(self, pos_label=1, average="binary"):
""" Compute the False Positives.
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - False Positives
"""
return self._general_metric(EvaluationReport._FP, pos_label, average)
def FN(self, pos_label=1, average="binary"):
""" Compute the False Negatives.
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - False Negatives
"""
return self._general_metric(EvaluationReport._FN, pos_label, average)
def TP(self, pos_label=1, average="binary"):
""" Compute the True Positives.
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - True Positives
"""
return self._general_metric(EvaluationReport._TP, pos_label, average)
def accuracy(self, pos_label=1, average="binary"):
""" Compute the accuracy in a per-class basis.
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - Accuracy
"""
return self._general_metric(EvaluationReport._accuracy, pos_label, average)
def precision(self, pos_label=1, average="binary"):
""" Compute the precision.
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - precision
"""
return self._general_metric(EvaluationReport._precision, pos_label, average)
def recall(self, pos_label=1, average="binary"):
""" Compute the Recall.
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - Recall
"""
return self._general_metric(EvaluationReport._recall, pos_label, average)
def sensitivity(self, pos_label=1, average="binary"):
""" Compute the sensitivity.
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - Sensitivity
"""
return self._general_metric(EvaluationReport._recall, pos_label, average)
def specificity(self, pos_label=1, average="binary"):
""" Compute the specifity.
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - Specitifity
"""
return self._general_metric(EvaluationReport._specificity, pos_label, average)
def dice_coeff(self, pos_label=1, average="binary"):
""" Compute Dice's coefficient.
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - Dice's coefficient
"""
return self._general_metric(EvaluationReport._f1_score, pos_label, average)
def jaccard_similarity(self, pos_label=1, average="binary"):
""" Compute Jaccard similarity
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - Jaccard similarity
"""
return self._general_metric(EvaluationReport._jaccard_similarity, pos_label, average)
def f1_score(self, pos_label=1, average="binary"):
""" Compute Jaccard similarity
Args:
pos_label (str or int, default=1) - The class to report if average='binary'
and the data is binary.
average ({‘macro’,’weighted’, ‘binary’}, default='binary') -
Returns:
(float) - Jaccard similarity
"""
return self._general_metric(EvaluationReport._f1_score, pos_label, average)