-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPW_AL.py
1338 lines (1132 loc) · 44.1 KB
/
PW_AL.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from skimage.measure import regionprops
from skimage.segmentation import slic
#from matplotlib import pyplot as plt
import tensorflow as tf
import numpy as np
import linecache
import shutil
import pickle
import scipy
import time
import nrrd
import yaml
import copy
import pdb
import os
import PW_analyze_results
import patch_utils
import NNAL_tools
import PW_NNAL
import PW_NN
import NNAL
import NN
# avioding TF warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
class Experiment(object):
"""class of an active learning experiments with
voxel-wise querying
"""
def __init__(self,
root_dir,
pars={}):
"""Constructor
It needs the root directory of the experiment,
which will contain all runs' folders, and the
path to the data that will be used for all
different active learning experiments. This
data will be partitioned randomly in each run
of the experiments into test, training and
unlabeled pool samples.
If the experiment does not exist, variables
`img_path_list` and `labels` have to be given.
If a set of parameters are given, they will
be saved in the root. Otherwise, we just leave
it there.
"""
self.root_dir = root_dir
self.nclass=2
if not(os.path.exists(root_dir)):
os.mkdir(root_dir)
# if there are parameter given, write them
# into a text file to be used later
if len(pars)>0:
if os.path.exists(os.path.join(
root_dir,
'parameters.txt')):
print("Some parameters already exist")
else:
self.save_parameters(pars)
def modify_parameters(self, mod_dict):
"""Modifying parameters of a given
experiment according to a given
dictionary which has a subset of keys
and the corresponding modified values
CAUTIOUS: only use this method for testing.
Never change parameters of an experiment
whose runs are completed
"""
if not(hasattr(self, 'pars')):
self.load_parameters()
for var, value in mod_dict.items():
self.pars[var] = value
# saving the modified parameters
self.save_parameters(self.pars)
def save_parameters(self, pars):
"""Saving a given dictionary of parameters
into a text file in the root folder of the
experiment
"""
with open(os.path.join(
self.root_dir,
'parameters.txt'),'w') as f:
self.pars = copy.deepcopy(pars)
yaml.dump(pars, f)
def load_parameters(self):
"""Loading the parameters that are saved
into the text file into the local variables
"""
with open(os.path.join(
self.root_dir,
'parameters.txt'),'r') as f:
self.pars = yaml.load(f)
def get_runs(self):
"""List all the runs that this experiment has
so far
"""
# assuming that the root directory has only
# folders of the runs
return [
d for d in os.listdir(self.root_dir)
if os.path.isdir(
os.path.join(self.root_dir,d))
]
def remove_run(self, run):
"""Removing a given run by deleting the folder,
and renaming all the folders
"""
shutil.rmtre(os.path.join(self.root_dir, run))
self.organize_runs()
def organize_runs(self):
"""Organizing run folders such that they have
names from 1 to n
Having such organized folder for the runs, makes
it much easier to add a new run.
"""
run_dirs = self.get_runs()
for i,name in enumerate(run_dirs):
if not(i==int(name)):
os.rename(os.path.join(self.root_dir, name),
os.path.join(self.root_dir, str(i)))
def prep_data(self, flag=''):
"""Adding a run to this experiment
Each run will have its pool and test
image indices, which will be sampled
to get the pool and test data sets
"""
if not(hasattr(self, 'pars')):
self.load_parameters()
# preparing the indices
# -----------------------
prep_AL_data(self, flag)
# get the test indices for initial
# performance evaluation
test_inds = read_ints(os.path.join(
self.root_dir,'test_inds.txt'))
test_labels = np.array(read_ints(os.path.join(
self.root_dir,'test_labels.txt')))
# evaluating the initial performance
# -------------------------
# create the NN model
tf.reset_default_graph()
m = len(self.pars['img_paths'])
patch_shape = self.pars['patch_shape'][:2] + \
(m*self.pars['patch_shape'][2],)
model = NN.create_model(
self.pars['model_name'],
self.pars['dropout_rate'],
self.nclass,
self.pars['learning_rate'],
self.pars['grad_layers'],
self.pars['train_layers'],
self.pars['optimizer_name'],
patch_shape)
# computing pool statistics
#self.pars['stats'] = [mu, sigma] #[65., 54.5]
#self.save_parameters(self.pars)
# start a session to do the training
with tf.Session() as sess:
# training from initial training data
model.initialize_graph(sess)
if 'init_weights_path' in self.pars:
model.load_weights(
self.pars['init_weights_path'],
sess)
# get a prediction of the test samples
test_preds = PW_NN.batch_eval(
model,
sess,
self.pars['img_paths'],
test_inds,
self.pars['patch_shape'],
self.pars['ntb'],
self.pars['stats'],
'prediction')[0]
# save the predictions
np.savetxt(os.path.join(self.root_dir,
'init_predicts.txt'),
np.expand_dims(test_preds,axis=0),
fmt='%d')
# initial, performance evaluation
Fmeas = PW_analyze_results.get_Fmeasure(
test_preds, test_labels)
print("Initial F-measure: %f"% Fmeas)
perf_eval_path = os.path.join(
self.root_dir, 'init_perf_eval.txt')
with open(perf_eval_path, 'w') as f:
f.write('%f\n'% Fmeas)
def add_method(self, method_name):
"""Adding a method to a given run of the experiment
"""
# check if the method already exists in this run
if os.path.exists(os.path.join(self.root_dir,
method_name)):
print("This method already exists")
print("Nothing else to do..")
return
# create a directory for the method
os.mkdir(os.path.join(self.root_dir,
method_name))
# create a directory for the queries
os.mkdir(os.path.join(self.root_dir,
method_name,
'queries'))
# copying the following files:
# pool_inds --> curr_pool
# init_predicts --> predicts
# `init_weights_path` --> curr_weights.h5
# init_perf_eval --> perf_evals
method_path = os.path.join(self.root_dir,
method_name)
shutil.copy(
os.path.join(self.root_dir,'init_pool_inds.txt'),
os.path.join(method_path,'pool_inds.txt')
)
shutil.copy(
os.path.join(self.root_dir,'init_pool_labels.txt'),
os.path.join(method_path,'pool_labels.txt')
)
shutil.copy(
os.path.join(self.root_dir,'init_predicts.txt'),
os.path.join(method_path,'predicts.txt')
)
shutil.copy(
self.pars['init_weights_path'],
os.path.join(method_path,'curr_weights.h5')
)
shutil.copy(
os.path.join(self.root_dir,'init_perf_eval.txt'),
os.path.join(method_path,'perf_evals.txt')
)
def run_method(self, method_name, max_queries):
"""Running a querying method in a run until a
given number of queries are drawn
"""
# read all the modalities and pad them
rads = np.zeros(3,dtype=int)
for i in range(3):
rads[i] = int(
(self.pars['patch_shape'][i]-1)/2.)
padded_imgs = []
for path in self.pars['img_paths']:
img,_ = nrrd.read(path)
padded_img = np.pad(
img,
((rads[0],rads[0]),
(rads[1],rads[1]),
(rads[2],rads[2])),
'constant')
padded_imgs += [padded_img]
mask,_ = nrrd.read(self.pars['mask_path'])
# set up the paths
method_path = os.path.join(self.root_dir,
method_name)
labels_path = os.path.join(self.root_dir,
'labels.txt')
# count how many queries have been
# selected before
n_oldqueries = 0
iter_cnt = 0
Q_path = os.path.join(method_path,'queries')
Q_files = os.listdir(Q_path)
for f in Q_files:
Qs = np.loadtxt(os.path.join(
Q_path, f))
n_oldqueries += len(Qs)
iter_cnt += 1
# preparing the indices
test_inds = read_ints(os.path.join(
self.root_dir,'test_inds.txt'))
test_labels = np.array(read_ints(os.path.join(
self.root_dir,'test_labels.txt')))
pool_inds = np.array(read_ints(os.path.join(
method_path, 'pool_inds.txt')))
pool_labels = read_ints(os.path.join(
method_path, 'pool_labels.txt'))
# for training
train_path = os.path.join(
method_path, 'train_inds.txt')
if os.path.exists(train_path):
train_inds = np.int32(
np.loadtxt(train_path))
else:
train_inds = []
print('Test-size: %d'% (len(test_inds)))
print('Pool-size: %d'% (len(pool_inds)))
print('Train-size: %d'% (len(train_inds)))
if not(hasattr(self, 'pars')):
self.load_parameters()
""" Loading the model """
print("Loading the current model..")
tf.reset_default_graph()
# create a model-holder
m = len(self.pars['img_paths'])
patch_shape = self.pars['patch_shape'][:2] + \
(m*self.pars['patch_shape'][2],)
model = NN.create_model(
self.pars['model_name'],
self.pars['dropout_rate'],
self.nclass,
self.pars['learning_rate'],
self.pars['grad_layers'],
self.pars['train_layers'],
self.pars['optimizer_name'],
patch_shape)
# printing the accuracies so far:
curr_fmeas = np.loadtxt(os.path.join(
method_path, 'perf_evals.txt'))
if curr_fmeas.size==1:
curr_fmeas = [curr_fmeas]
print("Current F-measures: ", end='')
print(*curr_fmeas, sep=', ')
with tf.Session() as sess:
# loading the stored weights
model.initialize_graph(sess)
model.load_weights(os.path.join(
method_path,'curr_weights.h5'), sess)
sess.graph.finalize()
# starting the iterations
print("Starting iterations of %s"%
method_name)
nqueries = 0
while nqueries < max_queries:
print("Iter. %d: "% iter_cnt,
end='\n\t')
""" querying """
# decide the number of queries
# for this iteration (only to
# be used fro non-fi algorithms)
if 'iter_k' in self.pars:
self.pars['k'] = self.pars[
'iter_k'][iter_cnt]
Q_inds = PW_NNAL.CNN_query(
self,
model,
sess,
padded_imgs,
pool_inds,
train_inds,
method_name)
if self.pars['k']==1:
Q = [pool_inds[Q_inds]]
else:
Q = pool_inds[Q_inds]
# save the queries
np.savetxt(os.path.join(
method_path,
'queries',
'%d.txt'% (
iter_cnt)
), Q, fmt='%d')
# update the indices
if len(train_inds)==0:
train_inds = Q
else:
train_inds = np.append(
train_inds, Q)
pool_inds = np.delete(
pool_inds, Q_inds)
""" updating the model """
for i in range(self.pars['epochs']):
finetune(model,
sess,
self,
padded_imgs,
mask,
train_inds)
print('%d'% i, end=',')
""" evluating the updated model """
test_preds = PW_NN.batch_eval(
model,
sess,
padded_imgs,
test_inds,
self.pars['patch_shape'],
self.pars['ntb'],
self.pars['stats'],
'prediction')[0]
# saving the predictions
curr_predicts = np.loadtxt(
os.path.join(method_path,
'predicts.txt'))
# loading the previous predictions,
# appending the new ones to them,
# and save them back
if curr_predicts.ndim<2:
curr_predicts = np.expand_dims(
curr_predicts, axis=0)
new_predicts = np.append(
curr_predicts,
np.expand_dims(test_preds, axis=0),
axis=0)
np.savetxt(os.path.join(
method_path, 'predicts.txt'),
new_predicts, fmt='%d')
# performance evaluation
Fmeas = PW_analyze_results.get_Fmeasure(
test_preds, test_labels)
with open(os.path.join(
method_path,
'perf_evals.txt'), 'a') as f:
f.write('%f\n'% Fmeas)
# update the loop variables
nqueries += len(Q_inds)
iter_cnt += 1
print('\n\t', end='')
print("Total queries: %d"%
(len(train_inds)),
end='\n\t')
print("F-measure: %.4f"% Fmeas)
# when querying is done..
# save the current training and pool
np.savetxt(os.path.join(
method_path, 'pool_inds.txt'),
pool_inds,
fmt='%d')
np.savetxt(train_path,
train_inds,
fmt='%d')
# save the current weights
model.save_weights(
os.path.join(
method_path,
'curr_weights.h5'))
def finetune_wpool(self, run,
save_names=[],
train_lines_path=[],
full=False,
tb_files=[]):
"""Finetuning the initial model of an
experiment with all the pool samples of
a given run
"""
if len(train_lines_path)>0:
train_lines = np.int32(np.loadtxt(
train_lines_path))
else:
train_lines = np.int32(np.loadtxt(
os.path.join(self.root_dir,str(run),
'init_pool_lines.txt')))
test_lines = np.int32(np.loadtxt(
os.path.join(self.root_dir,str(run),
'test_lines.txt')))
pool_Fmeas, model = finetune_winds(
self, run,
train_lines,
test_lines,
tb_files)
print('Final F-measure: %f'% pool_Fmeas)
if save_names:
save_path = os.path.join(
self.root_dir, str(run),
'%s.txt'% (save_names[0]))
with open(save_path, 'w') as f:
f.write('%f\n'% pool_Fmeas)
save_path = os.path.join(
self.root_dir, str(run),
'%s.txt'% (save_names[1]))
model.save_weights(save_path)
def load_results(self, run):
"""Loading performance evaluations
for all the methods in a given
run of the experiment
"""
methods = os.listdir(os.path.join(
self.root_dir, str(run)))
methods = [
f for f in methods
if os.path.isdir(os.path.join(
self.root_dir,
str(run),f))]
# load performance evaluations
# together with number of queries
# in each method
Q_lens = []
perf_evals = []
for method in methods:
method_path = os.path.join(
self.root_dir, str(run),
method)
# performance evaluation
F = np.loadtxt(os.path.join(
method_path, 'perf_evals.txt'))
perf_evals += [F]
# length of the queries
Q_path = os.path.join(
method_path,'queries')
Q_files = os.listdir(Q_path)
L = [0]
for f in Q_files:
Qs = np.loadtxt(os.path.join(
Q_path, f))
L += [len(Qs)]
Q_lens += [L]
return perf_evals, Q_lens, methods
class Experiment_MultiImg(Experiment):
"""Active learning experiments that use multiple
images as for training/pool and test data set
(to be used for Universal active learning)
Note that for applications such as hippocampus
segmentation where a mask not only contain
labels, but also indicates those voxels we
should ignore, we also have NaN label. We
will discrad grid samples (in testing or
training) that are NaN in the mask
"""
def __init__(self, root_dir,
pars={},
train_paths={},
test_paths={}):
Experiment.__init__(self,
root_dir,
pars)
if not(hasattr(self, 'pars')):
self.load_parameters()
# saving paths to train and test data
tr_file = os.path.join(self.root_dir,
'train_paths.txt')
if not(os.path.exists(tr_file)):
with open(tr_file, 'w') as f:
yaml.dump(train_paths, f)
self.train_paths = train_paths
else:
with open(tr_file, 'r') as f:
self.train_paths = yaml.load(f)
# take care of the statistics
if os.path.exists(os.path.join(
self.root_dir, 'train_stats.txt')):
self.train_stats = np.loadtxt(os.path.join(
self.root_dir, 'train_stats.txt'))
# if only one subject, NumPy squeezes the
# array when saving it (hence dropping the
# dimension) --> bring back that dim.
if self.train_stats.ndim==1:
self.train_stats = np.expand_dims(
self.train_stats, axis=0)
else:
self.train_stats = get_stats(self.train_paths)
np.savetxt(os.path.join(
self.root_dir,
'train_stats.txt'),self.train_stats)
def test_eval(self, model, sess,
test_inds=[], test_labels=[]):
m = len(self.test_paths[0])-1
if len(test_inds)==0:
test_inds, test_labels = gen_multimg_inds(
self.test_paths, self.pars['grid_spacing'])
tP,tTP,tFP = 0,0,0
for i in range(len(test_inds)):
stats = []
for j in range(m):
stats += [[self.test_stats[i,2*j],
self.test_stats[i,2*j+1]]]
test_preds = PW_NN.batch_eval(
model,
sess,
self.test_paths[i][:-1],
test_inds[i],
self.pars['patch_shape'],
self.pars['ntb'],
stats,
'prediction')[0]
(P,N,TP,
FP,TN,FN) = PW_analyze_results.get_preds_stats(
test_preds, np.array(test_labels[i]))
tP += P
tTP += TP
tFP += FP
# compute total Pr/Rc and F1
Pr = tTP / (tTP + tFP)
Rc = tTP / tP
if Pr>0 and Rc>0:
F1 = 2. / (1/Pr + 1/Rc)
else:
F1 = 0
return F1, test_preds
def add_method(self, method_name):
method_path = os.path.join(self.root_dir,
method_name)
if not(os.path.exists(method_path)):
os.mkdir(method_path)
os.mkdir(os.path.join(method_path,
'queries'))
os.mkdir(os.path.join(method_path,
'AL_running_times'))
def run_method(self, method_name, max_queries):
method_path = os.path.join(self.root_dir,
method_name)
""" Pool/Training Indices """
# first load up all the indices for
# pool images
if 'pool_paths' in self.pars:
pool_inds = [[] for i in
range(len(self.train_paths))]
for i in self.pars['pool_paths']:
pinds,_ = gen_multimg_inds(
[self.train_paths[i]], self.pars['grid_spacing'])
pool_inds[i] = pinds[0]
else:
pool_inds,_ = gen_multimg_inds(
self.train_paths, self.pars['grid_spacing'])
""" Training Indices """
# initial indices, if any
init_training_inds = [[] for i in
range(len(self.train_paths))]
init_train_path = os.path.join(
self.root_dir,'init_train_inds.txt')
if os.path.exists(init_train_path):
init_inds = np.int32(
np.loadtxt(init_train_path))
for ind in np.unique(init_inds[:,1]):
I = init_inds[init_inds[:,1]==ind,0]
init_training_inds[ind] += I.tolist()
# already labeled queries
training_inds = [[] for i in
range(len(self.train_paths))]
Q_path = os.path.join(method_path,
'queries')
Q_files = os.listdir(Q_path)
iters = len(Q_files)
for f in Q_files:
Qs = np.int32(np.loadtxt(os.path.join(
Q_path, f)))
for ind in np.unique(Qs[:,1]):
I = Qs[Qs[:,1]==ind,0]
training_inds[ind] += I.tolist()
[pool_inds[ind].remove(i) for i in I]
""" Load and Pad Training Images """
rads = np.zeros(3, dtype=int)
for i in range(3):
rads[i] = int(
(self.pars['patch_shape'][i]-1)/2.)
all_padded_imgs = []
m = len(self.train_paths[0])-1
for sub_paths in self.train_paths:
padded_imgs = []
for i,path in enumerate(sub_paths):
img,_ = nrrd.read(path)
# if mask, don't pad it
if i==m:
padded_imgs += [img]
continue
padded_img = np.pad(
img,
((rads[0],rads[0]),
(rads[1],rads[1]),
(rads[2],rads[2])),
'constant')
padded_imgs += [padded_img]
all_padded_imgs += [padded_imgs]
""" Loading the Model """
tf.reset_default_graph()
# create a model-holder
m = len(self.train_paths[0])-1
patch_shape = self.pars['patch_shape'][:2] + \
(m*self.pars['patch_shape'][2],)
model = NN.create_model(
self.pars['model_name'],
self.pars['dropout_rate'],
self.nclass,
self.pars['learning_rate'],
self.pars['grad_layers'],
self.pars['train_layers'],
self.pars['optimizer_name'],
patch_shape)
model.add_assign_ops()
if method_name=='ensemble':
self.model_holder = NN.create_model(
self.pars['model_name'],
self.pars['dropout_rate'],
self.nclass,
self.pars['learning_rate'],
self.pars['grad_layers'],
self.pars['train_layers'],
self.pars['optimizer_name'],
patch_shape)
self.model_holder.add_assign_ops()
with tf.Session() as sess:
# doing a first global initialization
sess.run(tf.global_variables_initializer())
sess.graph.finalize()
model.perform_assign_ops(
self.pars['init_weights_path'], sess)
""" Start AL iterations """
nqueries = 0
while nqueries < max_queries:
print("Iter. %d: "% iters,end='\n\t')
""" Preparing AL-specific Variables """
# preparing the already labeled data
n_labels = np.sum([len(training_inds[i]) for
i in range(len(training_inds))])
if method_name=='core-set' and n_labels==0:
T1_addrs,T2_addrs,mask_addrs,_ = patch_utils.\
extract_Hakims_data_path()
self.labeled_paths = [
[T1_addrs[i], T2_addrs[i], mask_addrs[i]]
for i in range(10)]
labeled_inds,_ = gen_multimg_inds(
self.labeled_paths,50)
self.labeled_stats = get_stats(self.labeled_paths)
else:
self.labeled_paths = self.train_paths
labeled_inds = training_inds
self.labeled_stats = self.train_stats
if method_name=='ensemble' and n_labels==0:
# paths to ensemble of pre-trained models
base_path = '/fileserver/external/rawabd/'+\
'Jamshid/PWNNAL_results/'+\
'bimodal_10/multiple_pretraining/'
self.pretrained_paths = [
self.pars['init_weights_path']] + \
[os.path.join(base_path,'%s/model_pars.h5'%i)
for i in range(1,7)]
elif method_name=='ensemble' and n_labels>0:
# load the model_holder by the weights
# of the previous model
# current iter: t
# current pre-update model: M(t-1)
# previous iter's model: M(t-2)
weights_path = os.path.join(
self.root_dir, method_name,
'curr_weights_%d.h5'% (iters))
self.model_holder.perform_assign_ops(
weights_path, sess)
""" Querying """
t1 = time.time()
Q_inds = PW_NNAL.query_multimg(
self, model, sess,
all_padded_imgs,
pool_inds,labeled_inds,
method_name)
t2 = time.time()
dt = t2 - t1
# moving Qs from pool --> training
nQ = np.sum([len(qind) for qind in Q_inds])
nqueries += nQ
# Q_mat: 1st column=voxel indices
# 2nd column=training image index
Q_mat = np.zeros((nQ,2))
q_file = os.path.join(self.root_dir,
method_name,
'queries/%d'% iters)
t_file = os.path.join(self.root_dir,
method_name,
'AL_running_times/dt_%d'% iters)
cnt = 0
for ind in range(len(Q_inds)):
if len(Q_inds[ind]>0):
Q_mat[cnt:cnt+len(Q_inds[ind]),
0] = np.array(pool_inds[ind])[Q_inds[ind]]
Q_mat[cnt:cnt+len(Q_inds[ind]),
1] = ind
cnt += len(Q_inds[ind])
# adding to the training
training_inds[ind] += list(np.array(
pool_inds[ind])[Q_inds[ind]])
# remove from the pool
sorted_inds = -np.sort(-Q_inds[ind])
[pool_inds[ind].pop(i) for i in sorted_inds]
np.savetxt(q_file, Q_mat, fmt='%d')
np.savetxt(t_file, [dt])
iters += 1
""" Finetuning the Model """
print('\tFinetuning..')
finetune_multimg(self,
model, sess,
all_padded_imgs,
training_inds)
# save the current weights
model.save_weights(
os.path.join(self.root_dir, method_name,
'curr_weights_%d.h5'% iters))
def get_stats(paths):
"""Computing statistics (mean/STD) of a set of
images given by their paths (their paths include
all modalities plus their mask)
"""
m = len(paths[0])-1
n = len(paths)
stats=np.zeros((n, 2*m))
for i, dat_paths in enumerate(paths):
mask,_ = nrrd.read(dat_paths[-1])
for j in range(m):
img,_ = nrrd.read(dat_paths[j])
stats[i,j*m] = np.mean(img[~np.isnan(mask)])
stats[i,j*m+1] = np.std(img[~np.isnan(mask)])
return stats
def gen_multimg_inds(dat_paths, grid_spacing):
"""Gegenerating inidices from a set of
images
The input contains a list of image paths,
where each path is another list of at
least two elements: raw image path(s),
and the corresponding mask path as the
last element.
NOTE: We discard those voxels that are
NaN within the mask.
"""
# number of test subjects
n = len(dat_paths)
all_inds = []
all_labels = []
for i in range(n):
mask,_ = nrrd.read(dat_paths[i][-1])
# forming the 2D grid
s = mask.shape
Y, X = np.meshgrid(np.arange(s[1]),
np.arange(s[0]))
X = np.ravel(X)
Y = np.ravel(Y)
grid_locs = np.logical_and(
X%grid_spacing==0,
Y%grid_spacing==0)
grid_X = np.array(X[grid_locs])
grid_Y = np.array(Y[grid_locs])
# forming the 3D grid
inds = []
labels = []
for i in range(s[2]):
grid_Z = np.ones(
len(grid_X),dtype=int)*i
grid_3D = np.ravel_multi_index(
(grid_X, grid_Y, grid_Z), s)
inds += list(grid_3D)
slice_labels = mask[
grid_X, grid_Y, grid_Z]
labels += list(slice_labels)
# discard NaN voxels
inds_to_keep = ~np.isnan(labels)
inds = np.array(inds)[inds_to_keep]
labels = np.array(labels)[inds_to_keep]
all_inds += [list(inds)]
all_labels += [list(labels)]
return all_inds, all_labels
def prep_AL_data(expr, flag=''):
"""Preparing the target data set, including
unlabeled pool and test samples for running
an active learning experiment, based on a
single subject
The pool and test will be tried to be selected
from similar slices. Hence, one strategy is to
draw samples from the even slices for the pool,
and from the odd slices for the test data set.
"""
# assuming that all image modalities have the
# same shape, we use the first modality to
# prepare grid indices
img_addr = expr.pars['img_paths'][0]
mask_addr = expr.pars['mask_path']
"""Sampling from the slices"""
# grid sampling
inds, labels = gen_multimg_inds(
[[expr.pars['img_paths']]+[expr.pars['mask_path']]],
expr.pars['grid_spacing'])