-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheval_utils.py
439 lines (373 loc) · 15.1 KB
/
eval_utils.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import numpy as np
import itertools
import shutil
import nrrd
import copy
import pdb
import os
from sklearn.metrics import f1_score
import NN_extended
from post_processing import connected_component_analysis_3d, fill_holes
from datasets.utils import gen_batch_inds
from patch_utils import extract_Hakims_data_path
def eval_metrics(model, sess,
dat_gen,
iters=50,
update=True,
alt_attr=None):
""" The alternative attribute will be used if `alt_attr`
is given; otherwise `model.valid_metrics` will be used
"""
# metrics
if alt_attr is not None:
assert hasattr(model,alt_attr), 'The alternative attribute'+\
' does not exist.'
valid_metrics = getattr(model, alt_attr)
else:
valid_metrics = model.valid_metrics
eval_metrics = list(valid_metrics.keys())
op_dict = {}
eval_dict = {}
model_inclusion = False
MT_model_inclusion = False
if 'av_acc' in eval_metrics:
op_dict.update({'accs': model.posteriors})
eval_dict.update({'accs': []})
model_inclusion = True
if 'F1' in eval_metrics:
op_dict.update({'F1s': model.posteriors})
eval_dict.update({'F1s': []})
model_inclusion = True
# binary or multiple F1 score
F1_score = (lambda x,y: binary_F1_score(x,y)) if model.class_num==2 \
else lambda x,y: multi_F1_score(x,y,model.class_num)[1]
if 'av_loss' in eval_metrics:
op_dict.update({'av_loss': model.loss})
eval_dict.update({'av_loss': 0.})
model_inclusion = True
all_preds = []
all_masks = []
vol = 0
for _ in range(iters):
batch_X, batch_mask = dat_gen()
b = batch_X.shape[0]
feed_dict = {}
if model_inclusion:
feed_dict.update({model.x:batch_X,
model.y_:batch_mask,
model.keep_prob:1.,
model.is_training:False})
if hasattr(model, 'teacher'):
feed_dict.update({model.teacher.keep_prob:1.,
model.teacher.is_training:False})
results = sess.run(op_dict, feed_dict=feed_dict)
for key, val in results.items():
if 'loss' in key:
# eval_dict[key] : total av. loss computed so far
# val==results[key] : the newest av. loss computed
eval_dict[key] = (vol*eval_dict[key]+val*b) / (vol+b)
if ('F1s' in key) or ('accs' in key):
# val in this case is actually posterior
preds = np.argmax(val, axis=-1)
all_preds += [preds]
nohot_batch_mask = np.argmax(batch_mask, axis=-1)
all_masks += [nohot_batch_mask]
vol += b
if update:
for metric in eval_metrics:
if metric=='acc':
preds = np.concatenate(all_preds, axis=0)
masks = np.concatenate(all_masks, axis=0)
valid_metrics[metric] += [np.sum(preds==masks)/np.prod(preds.shape)]
elif metric=='F1':
preds = np.concatenate(all_preds, axis=0)
masks = np.concatenate(all_masks, axis=0)
valid_metrics[metric] += [F1_score(preds, masks)]
elif 'loss' in metric:
valid_metrics[metric] += [eval_dict[metric]]
else:
return eval_dict
def full_slice_segment(model,sess,img_paths_or_mats, data_reader, op='prediction'):
# size of batch
b = 4
if isinstance(img_paths_or_mats, list):
m = len(img_paths_or_mats)
if isinstance(img_paths_or_mats[0], np.ndarray):
h,w,z = img_paths_or_mats[0].shape
paths_or_mats = 'mats'
else:
h,w,z = data_reader(img_paths_or_mats[0]).shape
paths_or_mats = 'paths'
else:
m = 1
if isinstance(img_paths_or_mats, np.ndarray):
h,w,z = img_paths_or_mats.shape
paths_or_mats = 'mats'
else:
h,w,z = data_reader(img_paths_or_mats).shape
paths_or_mats = 'paths'
hx,wx = [model.x.shape[1].value, model.x.shape[2].value]
assert h==hx and w==wx, 'Shape of data and model.x should match.'
# loading images
# m: number of input channels
if paths_or_mats=='mats':
img_list = img_paths_or_mats
else:
img_list = []
for i in range(m):
if m==1:
img_list = [data_reader(img_paths_or_mats)]
else:
img_list += [data_reader(img_paths_or_mats[i])]
# performing the op for all slices in batches
if op=='prediction':
out_tensor = np.zeros((h,w,z))
elif op=='loss':
out_tensor = 0.
cnt = 0
elif op=='output' and (model.AU_4U or model.AU_4L):
c = model.output.shape[-1].value
out_tensor = np.zeros((c,h,w,z))
elif op=='AU_vals' and model.AU_4U:
out_tensor = np.zeros((h,w,z))
else:
c = model.y_.shape[-1].value # = model.class_num in new version
out_tensor = np.zeros((c,h,w,z))
batches = gen_batch_inds(z, b)
for batch in batches:
batch_inds = np.sort(batch)
batch_X = np.zeros((len(batch_inds),h,w,m))
for j in range(m):
batch_X[:,:,:,j] = np.rollaxis(img_list[j][:,:,batch_inds],
axis=-1)
feed_dict = {model.x:batch_X, model.keep_prob:1., model.is_training:False}
if op=='prediction':
P = sess.run(model.posteriors, feed_dict=feed_dict)
batch_preds = np.argmax(P, axis=-1)
out_tensor[:,:,batch_inds] = np.rollaxis(batch_preds,axis=0,start=3)
elif op=='AU_vals':
P = sess.run(model.AU_vals, feed_dict=feed_dict)
out_tensor[:,:,batch_inds] = np.swapaxes(np.swapaxes(P,1,2),0,2)
elif op=='output':
P = sess.run(model.output, feed_dict=feed_dict)
out_tensor[:,:,:,batch_inds] = np.swapaxes(P,0,3)
elif op=='posterior':
P = sess.run(model.posteriors, feed_dict=feed_dict)
out_tensor[:,:,:,batch_inds] = np.swapaxes(P,0,3)
elif op=='MC-posterior':
feed_dict[model.keep_prob] = 1-model.dropout_rate
T = 10
av_P = sess.run(model.posteriors, feed_dict=feed_dict)
for i in range(1,T):
av_P = (i*av_P + sess.run(model.posteriors, feed_dict=feed_dict))/(i+1)
out_tensor[:,:,:,batch_inds] = np.swapaxes(av_P,0,3)
elif op=='loss':
loss = sess.run(model.loss, feed_dict=feed_dict)
out_tensor = (len(batch)*loss + cnt*out_tensor) / (cnt+len(batch))
cnt += len(batch)
elif op=='sigma':
out = sess.run(model.output, feed_dict=feed_dict)
out_tensor[:,:,:,batch_inds] = np.swapaxes(out[:,:,:,c:],0,3)
elif op=='MC-sigma':
feed_dict[model.keep_prob] = 1-model.dropout_rate
T = 10
out = sess.run(model.output, feed_dict=feed_dict)
av_sigma = out[:,:,:,c:]
for i in range(1,T):
out = sess.run(model.output, feed_dict=feed_dict)
av_sigma = (i*av_sigma + out[:,:,:,c:])/(i+1)
out_tensor[:,:,:,batch_inds] = np.swapaxes(av_sigma,0,3)
return out_tensor
def get_full_segs(models_dict,
sess,
dat,
post_process=False,
save_path=None,
dat_writer=None):
n = len(dat.img_addrs[dat.mods[0]])
#F1_score = (lambda x,y:binary_F1_score(x,y)) if dat.C==2 \
# else lambda x,y:multi_F1_score(x,y,dat.C)[1]
segs = []
for i in range(n):
mask = dat.mask_reader(dat.mask_addrs[i])
shape = mask.shape[:2]
img_paths = [dat.img_addrs[mod][i] for mod in dat.mods]
model_key = '{}'.format(shape)
model = models_dict[model_key]
seg = full_slice_segment(model,sess,img_paths, dat.reader)
if post_process:
seg = connected_component_analysis_3d(seg)
seg = fill_holes(seg)
segs += [seg]
if save_path is not None:
if not(os.path.exists(save_path)):
print('The specified path for saving data does not exist.')
return segs
if dat_writer is None:
dat_writer = lambda path,dat: nrrd.write(path,dat)
for i,seg in enumerate(segs):
dat_writer(os.path.join(save_path,'seg_{}.nrrd'.format(i)),seg)
return segs
def eval_full_segs_explicit_partitions(seg_paths_or_mats,
mask_paths_or_mats,
slice_partitions,
**kwargs):
"""
"""
kwargs.setdefault('dat_reader', lambda x:nrrd.read(x)[0])
kwargs.setdefault('mask_reader', lambda x:nrrd.read(x)[0])
dat_reader = kwargs['dat_reader']
mask_reader = kwargs['mask_reader']
# loading the data if only some paths are given
if isinstance(seg_paths_or_mats[0], str):
segs = []
for path in seg_paths_or_mats:
segs += [dat_reader(path)]
else:
segs = seg_paths_or_mats
if isinstance(mask_paths_or_mats[0], str):
masks = []
for path in mask_paths_or_mats:
masks += [mask_reader(path)]
else:
masks = mask_paths_or_mats
# get the partitions
if isinstance(slice_partitions, list):
slice_partitions = np.repeat(np.array(slice_partitions),
len(segs), axis=0)
# computing the scores
M = slice_partitions.shape[1]+1
part_Fscores = np.zeros((len(segs), M))
overall_Fscores = np.zeros(len(segs))
for i in range(len(segs)):
overall_Fscores[i] = binary_F1_score(segs[i], masks[i])
# first partition
seg = segs[i]
mask = masks[i]
seg_part = seg[:,:,:slice_partitions[i,0]]
mask_part = mask[:,:,:slice_partitions[i,0]]
part_Fscores[i,0] = binary_F1_score(seg_part, mask_part)
# middle partitions (if any)
for j in range(slice_partitions.shape[1]-1):
seg_part = seg[:,:,slice_partitions[i,j]:slice_partitions[i,j+1]]
mask_part = mask[:,:,slice_partitions[i,j]:slice_partitions[i,j+1]]
part_Fscores[i,j+1] = binary_F1_score(seg_part, mask_part)
# last partition
seg_part = seg[:,:,slice_partitions[i,-1]:]
mask_part = mask[:,:,slice_partitions[i,-1]:]
part_Fscores[i,-1] = binary_F1_score(seg_part, mask_part)
return overall_Fscores, part_Fscores
def eval_full_segs_label_percentage(seg_paths_or_mats,
mask_paths_or_mats,
label, percentage,
**kwargs):
""" This is for a 3-fold partitioning of top and bottom
slices that with voxels of a particular label less than
a certain threshold (percentage in terms of the number of
voxels in axial slices).
"""
kwargs.setdefault('dat_reader', lambda x:nrrd.read(x)[0])
kwargs.setdefault('mask_reader', lambda x:nrrd.read(x)[0])
dat_reader = kwargs['dat_reader']
mask_reader = kwargs['mask_reader']
# loading the data if only some paths are given
if isinstance(seg_paths_or_mats[0], str):
segs = []
for path in seg_paths_or_mats:
segs += [dat_reader(path)]
else:
segs = seg_paths_or_mats
if isinstance(mask_paths_or_mats[0], str):
masks = []
for path in mask_paths_or_mats:
masks += [mask_reader(path)]
else:
masks = mask_paths_or_mats
# computing the scores
M = 3
part_Fscores = np.zeros((len(segs), M))
overall_Fscores = np.zeros(len(segs))
for i in range(len(segs)):
overall_Fscores[i] = binary_F1_score(segs[i], masks[i])
# get the partition for this volume
label_num = np.sum(masks[i]==label, axis=(0,1))
thr_slices = np.where(label_num/np.prod(masks[i].shape[:2])
<percentage)[0]
gap_loc = np.where((thr_slices[1:] - thr_slices[:-1]) > 1)[0]
if len(gap_loc)>1:
print('There are more than one gap for slice-wise label volume' + \
' of image {}'.format(i))
continue
edge_1 = thr_slices[gap_loc[0]]
edge_2 = thr_slices[gap_loc[0]+1]
# top partition
seg = segs[i]
mask = masks[i]
seg_part = seg[:,:,:edge_1]
mask_part = mask[:,:,:edge_1]
part_Fscores[i,0] = binary_F1_score(seg_part, mask_part)
# middle partitions
seg_part = seg[:,:,edge_1:edge_2]
mask_part = mask[:,:,edge_1:edge_2]
part_Fscores[i,1] = binary_F1_score(seg_part, mask_part)
# last partition
seg_part = seg[:,:,edge_2:]
mask_part = mask[:,:,edge_2:]
part_Fscores[i,2] = binary_F1_score(seg_part, mask_part)
return overall_Fscores, part_Fscores
def binary_F1_score(preds, labels):
TP = np.sum(preds*labels)
P = np.sum(labels)
TPFP = np.sum(preds)
return 2*TP/(P+TPFP) if P+TPFP!=0. else 0.
def multi_F1_score(preds,labels,C=None):
"""F1 score for multi-class classification
using `f1_score` function of scikit-learn package
"""
if C is None:
C = len(np.unique(labels))
indiv_scores = f1_score(np.ravel(labels), np.ravel(preds),
labels=np.arange(1,C),average=None)
# in computing the weighted average, do not consider
# background as a separate class
av_score = f1_score(np.ravel(labels), np.ravel(preds),
labels=np.arange(1,C),average='weighted')
return indiv_scores, av_score
def simple_eval_model(model,sess,dat_gen):
preds = []
grounds = []
if model.dropout_rate is None:
feed_dict = {}
else:
feed_dict = {model.keep_prob: 1.}
for Xb, Yb,_ in dat_gen:
feed_dict.update({model.x:Xb})
preds += [sess.run(model.prediction,
feed_dict=feed_dict)]
grounds += [np.argmax(Yb, axis=0)]
preds = np.concatenate(preds)
grounds = np.concatenate(grounds)
acc = np.sum(preds==grounds) / len(preds)
return acc, preds
def models_dict_for_different_sizes(model_builder,
dat):
"""Form a dictionary of FCN models, which has a model
for each image size exists in the given data set
Only one field of data class `dat` will be used, and that
is `img_addrs` that contains image paths of all modalities.
Model builder should be a function that only takes an
input size and a model name, and returns a model object
that accepts inputs of the given size.
"""
shapes = []
for i in range(len(dat.img_addrs[dat.mods[0]])):
img = dat.reader(dat.img_addrs[dat.mods[0]][i])
shapes += [img.shape[:2]]
shapes = np.unique(set(shapes))[0]
models_dict = {}
for shape in shapes:
key = str(shape)
model_name = '{}x{}'.format(shape[0],shape[1])
models_dict[key] = model_builder(list(shape), model_name)
models_dict[key].add_assign_ops()
return models_dict