-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
303 lines (251 loc) · 11 KB
/
metrics.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
"""
Metrics used for validation during training and evaluation:
Dice Score, Normalised Dice score, Lesion F1 score and nDSC R-AAC.
"""
import numpy as np
from functools import partial
from scipy import ndimage
from collections import Counter
from joblib import Parallel, delayed
from sklearn import metrics
from scipy.stats import pearsonr
def dice_metric_numpy(ground_truth, predictions):
return dice_metric(ground_truth, predictions)
def dice_metric(ground_truth, predictions):
"""
Compute Dice coefficient for a single example.
Args:
ground_truth: `numpy.ndarray`, binary ground truth segmentation target,
with shape [W, H, D].
predictions: `numpy.ndarray`, binary segmentation predictions,
with shape [W, H, D].
Returns:
Dice coefficient overlap (`float` in [0.0, 1.0])
between `ground_truth` and `predictions`.
"""
# Calculate intersection and union of y_true and y_predict
intersection = np.sum(predictions * ground_truth)
union = np.sum(predictions) + np.sum(ground_truth)
# Calcualte dice metric
if intersection == 0.0 and union == 0.0:
dice = 1.0
else:
dice = (2. * intersection) / union
return dice
def dice_norm_metric(ground_truth, predictions):
"""
Compute Normalised Dice Coefficient (nDSC),
False positive rate (FPR),
False negative rate (FNR) for a single example.
Args:
ground_truth: `numpy.ndarray`, binary ground truth segmentation target,
with shape [H, W, D].
predictions: `numpy.ndarray`, binary segmentation predictions,
with shape [H, W, D].
Returns:
Normalised dice coefficient (`float` in [0.0, 1.0]),
False positive rate (`float` in [0.0, 1.0]),
False negative rate (`float` in [0.0, 1.0]),
between `ground_truth` and `predictions`.
"""
# Reference for normalized DSC
r = 0.001
# Cast to float32 type
gt = ground_truth.astype("float32")
seg = predictions.astype("float32")
im_sum = np.sum(seg) + np.sum(gt)
if im_sum == 0:
return 1.0
else:
if np.sum(gt) == 0:
k = 1.0
else:
k = (1 - r) * np.sum(gt) / (r * (len(gt.flatten()) - np.sum(gt)))
tp = np.sum(seg[gt == 1])
fp = np.sum(seg[gt == 0])
fn = np.sum(gt[seg == 0])
fp_scaled = k * fp
dsc_norm = 2. * tp / (fp_scaled + 2. * tp + fn)
return dsc_norm
def ndsc_aac_metric(ground_truth, predictions, uncertainties, parallel_backend=None):
"""
Compute area above Normalised Dice Coefficient (nDSC) retention curve for
one subject. `ground_truth`, `predictions`, `uncertainties` - are flattened
arrays of correponding 3D maps within the foreground mask only.
Args:
ground_truth: `numpy.ndarray`, binary ground truth segmentation target,
with shape [H * W * D].
predictions: `numpy.ndarray`, binary segmentation predictions,
with shape [H * W * D].
uncertainties: `numpy.ndarray`, voxel-wise uncertainties,
with shape [H * W * D].
parallel_backend: `joblib.Parallel`, for parallel computation
for different retention fractions.
Returns:
nDSC R-AAC (`float` in [0.0, 1.0]).
"""
def compute_dice_norm(frac_, preds_, gts_, N_):
pos = int(N_ * frac_)
curr_preds = preds if pos == N_ else np.concatenate(
(preds_[:pos], gts_[pos:]))
return dice_norm_metric(gts_, curr_preds)
if parallel_backend is None:
parallel_backend = Parallel(n_jobs=1)
ordering = uncertainties.argsort()
gts = ground_truth[ordering].copy()
preds = predictions[ordering].copy()
N = len(gts)
# # Significant class imbalance means it is important to use logspacing between values
# # so that it is more granular for the higher retention fractions
fracs_retained = np.log(np.arange(200 + 1)[1:])
fracs_retained /= np.amax(fracs_retained)
process = partial(compute_dice_norm, preds_=preds, gts_=gts, N_=N)
dsc_norm_scores = np.asarray(
parallel_backend(delayed(process)(frac)
for frac in fracs_retained)
)
return 1. - metrics.auc(fracs_retained, dsc_norm_scores)
def ndsc_retention_curve(ground_truth, predictions, uncertainties, fracs_retained, parallel_backend=None):
"""
Compute Normalised Dice Coefficient (nDSC) retention curve.
Args:
ground_truth: `numpy.ndarray`, binary ground truth segmentation target,
with shape [H * W * D].
predictions: `numpy.ndarray`, binary segmentation predictions,
with shape [H * W * D].
uncertainties: `numpy.ndarray`, voxel-wise uncertainties,
with shape [H * W * D].
fracs_retained: `numpy.ndarray`, array of increasing valies of retained
fractions of most certain voxels, with shape [N].
parallel_backend: `joblib.Parallel`, for parallel computation
for different retention fractions.
Returns:
(y-axis) nDSC at each point of the retention curve (`numpy.ndarray` with shape [N]).
"""
def compute_dice_norm(frac_, preds_, gts_, N_):
pos = int(N_ * frac_)
curr_preds = preds if pos == N_ else np.concatenate(
(preds_[:pos], gts_[pos:]))
return dice_norm_metric(gts_, curr_preds)
if parallel_backend is None:
parallel_backend = Parallel(n_jobs=1)
ordering = uncertainties.argsort()
gts = ground_truth[ordering].copy()
preds = predictions[ordering].copy()
N = len(gts)
process = partial(compute_dice_norm, preds_=preds, gts_=gts, N_=N)
dsc_norm_scores = np.asarray(
parallel_backend(delayed(process)(frac)
for frac in fracs_retained)
)
return dsc_norm_scores
def intersection_over_union(mask1, mask2):
"""
Compute IoU for 2 binary masks.
Args:
mask1: `numpy.ndarray`, binary mask.
mask2: `numpy.ndarray`, binary mask of the same shape as `mask1`.
Returns:
Intersection over union between `mask1` and `mask2` (`float` in [0.0, 1.0]).
"""
return np.sum(mask1 * mask2) / np.sum(mask1 + mask2 - mask1 * mask2)
def lesion_f1_score(ground_truth, predictions, IoU_threshold=0.25, parallel_backend=None):
"""
Compute lesion-scale F1 score.
Args:
ground_truth: `numpy.ndarray`, binary ground truth segmentation target,
with shape [H, W, D].
predictions: `numpy.ndarray`, binary segmentation predictions,
with shape [H, W, D].
IoU_threshold: `float` in [0.0, 1.0], IoU threshold for max IoU between
predicted and ground truth lesions to classify them as
TP, FP or FN.
parallel_backend: `joblib.Parallel`, for parallel computation
for different retention fractions.
Returns:
Intersection over union between `mask1` and `mask2` (`float` in [0.0, 1.0]).
"""
def get_tp_fp(label_pred, mask_multi_pred, mask_multi_gt):
mask_label_pred = (mask_multi_pred == label_pred).astype(int)
all_iou = [0.0]
# iterate only intersections
for int_label_gt in np.unique(mask_multi_gt * mask_label_pred):
if int_label_gt != 0.0:
mask_label_gt = (mask_multi_gt == int_label_gt).astype(int)
all_iou.append(intersection_over_union(
mask_label_pred, mask_label_gt))
max_iou = max(all_iou)
if max_iou >= IoU_threshold:
return 'tp'
else:
return 'fp'
def get_fn(label_gt, mask_multi_pred, mask_multi_gt):
mask_label_gt = (mask_multi_gt == label_gt).astype(int)
all_iou = [0]
for int_label_pred in np.unique(mask_multi_pred * mask_label_gt):
if int_label_pred != 0.0:
mask_label_pred = (mask_multi_pred ==
int_label_pred).astype(int)
all_iou.append(intersection_over_union(
mask_label_pred, mask_label_gt))
max_iou = max(all_iou)
if max_iou < IoU_threshold:
return 1
else:
return 0
mask_multi_pred_, n_les_pred = ndimage.label(predictions)
mask_multi_gt_, n_les_gt = ndimage.label(ground_truth)
if parallel_backend is None:
parallel_backend = Parallel(n_jobs=1)
process_fp_tp = partial(get_tp_fp, mask_multi_pred=mask_multi_pred_,
mask_multi_gt=mask_multi_gt_)
tp_fp = parallel_backend(delayed(process_fp_tp)(label_pred)
for label_pred in np.unique(mask_multi_pred_) if label_pred != 0)
counter = Counter(tp_fp)
tp = float(counter['tp'])
fp = float(counter['fp'])
process_fn = partial(get_fn, mask_multi_pred=mask_multi_pred_,
mask_multi_gt=mask_multi_gt_)
fn = parallel_backend(delayed(process_fn)(label_gt)
for label_gt in np.unique(mask_multi_gt_) if label_gt != 0)
fn = float(np.sum(fn))
f1 = 1.0 if tp + 0.5 * (fp + fn) == 0.0 else tp / (tp + 0.5 * (fp + fn))
return f1
def dsc_aac_metric(ground_truth, predictions, uncertainties, parallel_backend=None):
"""
Compute area above Dice Coefficient (nDSC) retention curve for
one subject. `ground_truth`, `predictions`, `uncertainties` - are flattened
arrays of correponding 3D maps within the foreground mask only.
Args:
ground_truth: `numpy.ndarray`, binary ground truth segmentation target,
with shape [H * W * D].
predictions: `numpy.ndarray`, binary segmentation predictions,
with shape [H * W * D].
uncertainties: `numpy.ndarray`, voxel-wise uncertainties,
with shape [H * W * D].
parallel_backend: `joblib.Parallel`, for parallel computation
for different retention fractions.
Returns:
nDSC R-AAC (`float` in [0.0, 1.0]).
"""
def compute_dice(frac_, preds_, gts_, N_):
pos = int(N_ * frac_)
curr_preds = preds if pos == N_ else np.concatenate(
(preds_[:pos], gts_[pos:]))
return dice_metric(gts_, curr_preds)
if parallel_backend is None:
parallel_backend = Parallel(n_jobs=1)
ordering = uncertainties.argsort()
gts = ground_truth[ordering].copy()
preds = predictions[ordering].copy()
N = len(gts)
# # Significant class imbalance means it is important to use logspacing between values
# # so that it is more granular for the higher retention fractions
fracs_retained = np.log(np.arange(200 + 1)[1:])
fracs_retained /= np.amax(fracs_retained)
process = partial(compute_dice, preds_=preds, gts_=gts, N_=N)
dsc_scores = np.asarray(
parallel_backend(delayed(process)(frac)
for frac in fracs_retained)
)
return 1. - metrics.auc(fracs_retained, dsc_scores)