-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPW_NNAL.py
1185 lines (995 loc) · 36.2 KB
/
PW_NNAL.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
import tensorflow as tf
import numpy as np
import warnings
#import nibabel
import time
import nrrd
import pdb
import os
import NN
import PW_NN
import PW_AL
import NNAL_tools
import patch_utils
def CNN_query(expr,
model,
sess,
padded_imgs,
pool_inds,
tr_inds,
method_name):
"""Querying strategies for active
learning of patch-wise model
Although the given image is padded,
the indices are given in terms of the
original dimensionalities
"""
if method_name=='random':
n = len(pool_inds)
q = np.random.permutation(n)[
:expr.pars['k']]
if method_name=="ps-random":
# pseudo-random: randomly
# selecting queries from regions
# with high local variance
thr = 2. # threhsold over the variance
valid_pool_inds = get_HV_inds(
padded_imgs[0], exp.pars['patch_shape'],
thr, pool_inds)
rand_inds = np.random.permutation(
len(valid_pool_inds))[:expr.pars['k']]
q = valid_pool_inds[rand_inds]
if method_name=='entropy':
# posteriors
posts = PW_NN.batch_eval(
model,
sess,
padded_imgs,
pool_inds,
expr.pars['patch_shape'],
expr.pars['ntb'],
expr.pars['stats'],
'posteriors')[0]
# k most uncertain (binary classes)
q = np.argsort(np.abs(posts-.5))[
:expr.pars['k']]
if method_name=='MC-entropy':
x_feed_dict = {model.keep_prob:
model.dropout_rate}
# iterative averaging over MC iterations
total_posts = 0
for i in range(expr.pars['MC_iters']):
posts = PW_NN.batch_eval(
model,
sess,
padded_imgs,
pool_inds,
expr.pars['patch_shape'],
expr.pars['ntb'],
expr.pars['stats'],
'posteriors',
x_feed_dict)[0]
total_posts = (posts+i*total_posts)/(i+1)
# k most uncertain (binary classes)
q = np.argsort(np.abs(total_posts-.5))[
:expr.pars['k']]
if method_name=='fi':
n = len(pool_inds)
m = len(expr.pars['img_paths'])
B = expr.pars['B']
lambda_ = expr.pars['lambda_']
# posteriors
posts = PW_NN.batch_eval(
model,
sess,
padded_imgs,
pool_inds,
expr.pars['patch_shape'],
expr.pars['ntb'],
expr.pars['stats'],
'posteriors')[0]
# vectories everything
# uncertainty filtering
if B < len(pool_inds):
sel_inds = np.argsort(
np.abs(posts-.5))[:B]
sel_posts = posts[sel_inds]
else:
B = posts.shape[1]
sel_posts = posts
sel_inds = np.arange(B)
# load the patches
# indices: sel_inds --> pool_inds
# CAUTIOUS: this will give an error if
# the selected indices in `sel_inds`
# contains only one index.
sel_patches = patch_utils.get_patches(
padded_imgs, pool_inds[sel_inds],
expr.pars['patch_shape'])
for j in range(m):
sel_patches[:,:,:,j] = (
sel_patches[:,:,:,j]-
expr.pars['stats'][j][0])/\
expr.pars['stats'][j][1]
# get the A-matrices (conditional FI's)
A = gen_A_matrices(expr,
model,
sess,
sel_patches,
sel_posts)
# prepare the feature vectors
F = PW_NN.batch_eval(model,
sess,
padded_imgs,
pool_inds[sel_inds],
expr.pars['patch_shape'],
expr.pars['ntb'],
expr.pars['stats'],
'feature_layer')[0]
ref_F = refine_feature_matrix(F, B)
# make the feature components zero-mean
ref_F -= np.repeat(np.expand_dims(
np.mean(ref_F, axis=1),
axis=1), F.shape[1], axis=1)
# SDP
# ----
soln = NNAL_tools.SDP_query_distribution(
A, lambda_, ref_F, expr.pars['k'])
print('status: %s'% (soln['status']), end='\n\t')
q_opt = np.array(soln['x'][:F.shape[1]])
# sampling from the optimal solution
Q_inds = NNAL_tools.sample_query_dstr(
q_opt, expr.pars['k'],
replacement=True)
q = sel_inds[Q_inds]
return q
def query_multimg(expr,
model,
sess,
all_padded_imgs,
pool_inds,
labeled_inds,
method_name):
"""Similar to (single image) query except
`all_padded_imgs` contains multiple images,
hence `pool_inds` and `tr_inds` include a
sequence of lists each of which contains
training indices associated with a training
subject
The output will be a list of N sets of indices
(N as the number of training subjects), where
each set (possibly empty) includes the indices
of the queries chosen from that subject
"""
k = expr.pars['k']
B = expr.pars['B']
img_ind_sizes = [len(pool_inds[i]) for
i in range(len(pool_inds))]
n = np.sum(img_ind_sizes)
m = len(all_padded_imgs[0]) - 1
if method_name=='random':
inds_num = [len(pool_inds[i]) for
i in range(len(pool_inds))]
npool = np.sum(inds_num)
inds = np.random.permutation(npool)[:k]
Q_inds = patch_utils.global2local_inds(
inds, inds_num)
if method_name=='ps-random':
thr = 2. # threhsold over the variance
valid_pool_inds = []
for i in range(len(all_padded_imgs)):
valid_pool_inds += [get_HV_inds(
all_padded_imgs[i][0], expr.pars['patch_shape'],
thr, pool_inds[i])]
valid_inds_sizes = [len(valid_pool_inds[i]) for
i in range(len(valid_pool_inds))]
nHV = np.sum(valid_inds_sizes)
rand_inds = np.random.permutation(nHV)[:k]
local_inds = patch_utils.global2local_inds(
rand_inds, valid_inds_sizes)
Q_inds = [valid_pool_inds[i][local_inds[i]] for
i in range(len(valid_pool_inds))]
if method_name=='entropy':
Q_inds = bin_uncertainty_filter_multimg(
expr, model, sess, all_padded_imgs,
pool_inds, k)[0]
if method_name=='MC-entropy':
x_feed_dict = {model.keep_prob:
model.dropout_rate}
av_posts = 0
for i in range(expr.pars['MC_iters']):
# the argument `k` won't be really used
# in this line
posts = bin_uncertainty_filter_multimg(
expr, model, sess, all_padded_imgs,
pool_inds, k, x_feed_dict)
av_posts = (posts+i*av_posts)/(i+1)
inds = np.argsort(np.abs(av_posts-.5))[:k]
Q_inds = patch_utils.global2local_inds(
inds, img_ind_sizes)
if method_name=='BALD':
x_feed_dict = {model.keep_prob:
model.dropout_rate}
av_posts = 0
av_ents = 0
for i in range(expr.pars['MC_iters']):
# the argument `k` won't be really used
# in this line
posts = bin_uncertainty_filter_multimg(
expr, model, sess, all_padded_imgs,
pool_inds, k, x_feed_dict)
av_posts = (posts+i*av_posts)/(i+1)
neg_posts = 1-posts
posts[posts==0] += 1e-6
neg_posts[neg_posts==0] += 1e-6
ents = -posts*np.log(posts) -\
neg_posts*np.log(neg_posts)
# average entropies
av_ents = (ents+i*av_ents)/(i+1)
# entropy of average posteriors
av_neg_posts = 1-av_posts
av_posts[av_posts==0] += 1e-6
av_neg_posts[av_neg_posts==0] += 1e-6
ent_av_posts = -av_posts*np.log(av_posts)-\
av_neg_posts*np.log(av_neg_posts)
scores = ent_av_posts - av_ents
inds = np.argsort(-scores)[:k]
Q_inds = patch_utils.global2local_inds(
inds, img_ind_sizes)
if method_name=='rep-entropy':
# extracting features
F = [[] for i in range(len(pool_inds))]
for i in range(len(pool_inds)):
stats = []
for j in range(m):
stats += [[expr.train_stats[i,2*j],
expr.train_stats[i,2*j+1]]]
F[i] = PW_NN.batch_eval(
model,sess,
all_padded_imgs[i][:-1],
pool_inds[i],
expr.pars['patch_shape'],
expr.pars['ntb'],
stats,
'feature_layer')[0]
# get the most uncertain samples
sel_inds,sel_posts = bin_uncertainty_filter_multimg(
expr, model, sess, all_padded_imgs,
pool_inds, B)
F_uncertain = [F[i][:,sel_inds[i]] for i in
range(len(sel_inds)) if
len(sel_inds[i])>0]
F_uncertain = np.concatenate(F_uncertain, axis=1)
for i in range(len(pool_inds)):
rem_inds = list(set(np.arange(len(pool_inds[i]))) -
set(sel_inds[i]))
F[i] = F[i][:, rem_inds]
F = np.concatenate(F, axis=1)
# norms
norms_rem = np.sqrt(np.sum(F**2, axis=0))
norms_uncertain = np.sqrt(np.sum(F_uncertain**2,
axis=0))
# compute cos-similarities between filtered images
# and the rest of the unlabeled samples
dots = np.dot(F.T, F_uncertain)
norms_outer = np.outer(norms_rem, norms_uncertain)
sims = dots / norms_outer
del dots, norms_rem, norms_uncertain, norms_outer
# start from empty set
Q_inds = []
nQ_inds = np.arange(B)
# add most representative samples one by one
for i in range(expr.pars['k']):
rep_scores = np.zeros(B-i)
for j in range(B-i):
cand_Q = Q_inds + [nQ_inds[j]]
rep_scores[j] = np.sum(
np.max(sims[:, cand_Q], axis=1))
iter_sel = nQ_inds[np.argmax(rep_scores)]
# update the iterating sets
Q_inds += [iter_sel]
nQ_inds = np.delete(
nQ_inds, np.argmax(rep_scores))
# transforming global Q_inds into local one
img_ind_sizes = [len(sel_inds[i]) for i
in range(len(sel_inds))]
local_inds = patch_utils.global2local_inds(
Q_inds, img_ind_sizes)
Q_inds = [np.array(sel_inds[i])[local_inds[i]]
for i in range(len(sel_inds))]
if method_name=='core-set':
# getting the feature matrices
# form full feature matrix of unlabeled pool
# because we need to have them in all iterations
F_u = [[] for i in range(len(pool_inds))]
for i in range(len(pool_inds)):
stats = []
for j in range(m):
stats += [[expr.train_stats[i,2*j],
expr.train_stats[i,2*j+1]]]
F_u[i] = PW_NN.batch_eval(
model,sess,
all_padded_imgs[i][:-1],
pool_inds[i],
expr.pars['patch_shape'],
expr.pars['ntb'],
stats,
'feature_layer')[0]
F_u = np.concatenate(F_u, axis=1)
n = F_u.shape[1]
norms_u = np.sqrt(np.sum(F_u**2, axis=0))
sims = -np.inf*np.ones(n)
nT = np.sum([len(labeled_inds[i]) for i
in range(len(labeled_inds))])
if True:
# for labeled data, do not need to keep
# the features in memory, since we only
# need to have the max-similarities
for i in range(len(labeled_inds)):
labeled_stats = []
for j in range(m):
labeled_stats += [
[expr.labeled_stats[i,2*j],
expr.labeled_stats[i,2*j+1]]]
# this extra-batching is because of
# memory issues;F_u and F_T are too
# large to keep in the memory at th
# same time
nT = len(labeled_inds[i])
batches = NN.gen_batch_inds(nT,1000)
for batch_inds in batches:
if expr.labeled_paths==expr.train_paths:
F_T = PW_NN.batch_eval(
model, sess,
all_padded_imgs[i][:-1],
np.array(labeled_inds[i])[batch_inds],
expr.pars['patch_shape'],
expr.pars['ntb'],
labeled_stats,
'feature_layer')[0]
else:
F_T = PW_NN.batch_eval(
model, sess,
expr.labeled_paths[i][:-1],
np.array(labeled_inds[i])[batch_inds],
expr.pars['patch_shape'],
expr.pars['ntb'],
labeled_stats,
'feature_layer')[0]
norms_T = np.sqrt(np.sum(F_T**2, axis=0))
# cosine similarities
dots = np.dot(F_T.T, F_u)
norms_outer = np.outer(norms_T, norms_u)
sims = np.max(np.concatenate((
dots / norms_outer,
np.expand_dims(sims, axis=0)), axis=0),
axis=0)
del dots, norms_T, norms_outer
if nT>2000:
np.savetxt(os.path.join(
expr.root_dir,'core-set/UT_sims.txt'), sims)
else:
sims = np.loadtxt(os.path.join(
expr.root_dir,'core-set/UT_sims.txt'))
Q_inds = []
for t in range(k):
q_ind = np.argmin(sims)
Q_inds += [q_ind]
# computing the similarities between the
# selected sample and rest of the pool
s_ind = np.dot(F_u[:,q_ind].T, F_u)/\
(norms_u*norms_u[q_ind])
sims = np.maximum(sims, s_ind)
# put inf in place of selected index as an
# indicator that it should be ignored
sims[q_ind] = np.inf
Q_inds = patch_utils.global2local_inds(
Q_inds, img_ind_sizes)
if method_name=='ensemble':
n_labels = np.sum([len(labeled_inds[i]) for
i in range(len(labeled_inds))])
av_posts = 0
x_feed_dict = {expr.model_holder.keep_prob: 1.}
for i in range(len(expr.pretrained_paths)):
if n_labels==0:
# if no labeled indices, go for ensemble
# of pre-trained models
expr.model_holder.perform_assign_ops(
expr.pretrained_paths[i], sess)
else:
# otherwise, create the ensemble by
# fine-tuning the previous model multiple
# times
expr.model_holder.perform_assign_ops(
expr.prev_weights_path, sess)
PW_AL.finetune_multimg(expr,
expr.model_holder,
sess,
all_padded_imgs,
labeled_inds)
# compute posteriors with the current model
# of the ensemble
posts = bin_uncertainty_filter_multimg(
expr, expr.model_holder, sess,
all_padded_imgs,
pool_inds, k, x_feed_dict)
av_posts = (posts+i*av_posts)/(i+1)
# sorting w.r.t uncertainty
inds = np.argsort(np.abs(av_posts-.5))[:k]
Q_inds = patch_utils.global2local_inds(
inds, img_ind_sizes)
if method_name=='QBC-JS':
n_labels = np.sum([len(labeled_inds[i]) for
i in range(len(labeled_inds))])
av_posts = 0
av_ents = 0
x_feed_dict = {expr.model_holder.keep_prob: 1.}
for i in range(len(expr.pretrained_paths)):
if n_labels==0:
# if no labeled indices, go for ensemble
# of pre-trained models
expr.model_holder.perform_assign_ops(
expr.pretrained_paths[i], sess)
else:
# otherwise, create the ensemble by
# fine-tuning the previous model multiple
# times
expr.model_holder.perform_assign_ops(
expr.prev_weights_path, sess)
PW_AL.finetune_multimg(expr,
expr.model_holder,
sess,
all_padded_imgs,
labeled_inds)
# compute posteriors with the current model
# of the ensemble
posts = bin_uncertainty_filter_multimg(
expr, expr.model_holder, sess,
all_padded_imgs,
pool_inds, k, x_feed_dict)
av_posts = (posts+i*av_posts)/(i+1)
neg_posts = 1-posts
posts[posts==0] += 1e-6
neg_posts[neg_posts==0] += 1e-6
ents = -posts*np.log(posts) -\
neg_posts*np.log(neg_posts)
# average entropies
av_ents = (ents+i*av_ents)/(i+1)
# entropy of average posteriors
av_neg_posts = 1-av_posts
av_posts[av_posts==0] += 1e-6
av_neg_posts[av_neg_posts==0] += 1e-6
ent_av_posts = -av_posts*np.log(av_posts)-\
av_neg_posts*np.log(av_neg_posts)
scores = ent_av_posts - av_ents
inds = np.argsort(-scores)[:k]
Q_inds = patch_utils.global2local_inds(
inds, img_ind_sizes)
if method_name=='fi':
# uncertainty-filtering
sel_inds,sel_posts = bin_uncertainty_filter_multimg(
expr, model, sess, all_padded_imgs,
pool_inds, B)
# loading patches
img_inds = [np.array(pool_inds[i])[sel_inds[i]]
for i in range(len(pool_inds))]
patches,_ = patch_utils.get_patches_multimg(
all_padded_imgs, img_inds,
expr.pars['patch_shape'],
expr.train_stats)
# form A-matrices and extract features
A = []
F = [[] for i in range(len(patches))]
for i in range(len(patches)):
if len(img_inds[i])==0:
continue
stats = []
for j in range(m):
stats += [[expr.train_stats[i,2*j],
expr.train_stats[i,2*j+1]]]
A += gen_A_matrices(expr,
model,
sess,
patches[i],
sel_posts[i],
1e-3)
#F[i] = PW_NN.batch_eval(
# model,sess,
# all_padded_imgs[i][:-1],
# img_inds[i],
# expr.pars['patch_shape'],
# expr.pars['ntb'],
# stats,
# 'feature_layer')[0]
#F = [F[i] for i in range(len(F)) if len(F[i])>0]
#F = np.concatenate(F, axis=1)
#ref_F = refine_feature_matrix(F, B)
# make the feature components zero-mean
#ref_F -= np.repeat(np.expand_dims(
# np.mean(ref_F, axis=1),
# axis=1), F.shape[1], axis=1)
ref_F = []
# SDP
# ----
lambda_ = expr.pars['lambda_']
if expr.pars['SDP_solver']=='CVXOPT':
soln = NNAL_tools.SDP_query_distribution(
A, lambda_, ref_F, k)
#print('status: %s'% (soln['status']), end='\n\t')
q_opt = np.array(soln['x'][:B])
#obj_val = soln['primal objective']
elif expr.pars['SDP_solver']=='MOSEK':
#print('Using MOSEK')
soln = NNAL_tools.solve_FIAL_SDP(A)
q_opt = soln[0]
obj_val = soln[1]
pdb.set_trace()
# sampling from the optimal solution
draws = NNAL_tools.sample_query_dstr(
q_opt, k, replacement=True)
img_ind_sizes = [len(sel_inds[i]) for i
in range(len(sel_inds))]
local_inds = patch_utils.global2local_inds(
draws, img_ind_sizes)
Q_inds = [np.array(sel_inds[i])[local_inds[i]]
for i in range(len(sel_inds))]
return Q_inds
def get_HV_inds(padded_img, patch_shape,
thr, pool_inds):
"""Getting the local indices of the pool samples
that have local variance higher than a threshold
(the output are indices of the samples in terms
their location in the `pool_inds` array)
"""
rads = np.int8((np.array(patch_shape)-1)/2)
# use T1 to compute the variances
(d1,d2,d3) = padded_img.shape
# un-padding
img_1 = padded_img[rads[0]:d1-rads[0],
rads[1]:d2-rads[1],
rads[2]:d3-rads[2]]
# compute 2D variance map
# (choosing first component of
# the patch shape as the radius of
# the loal variance computation)
var_map = np.zeros(img_1.shape)
for i in range(img_1.shape[2]):
slice_var = patch_utils.get_vars_2d(
img_1[:,:,i], rads[0])
var_map[:,:,i] = slice_var
# get variance scores of
# all given pool indices
pool_multinds = np.unravel_index(
pool_inds, img_1.shape)
inds_vscores = var_map[pool_multinds]
# filter-out the low-variance
# pool indices, and select randomly
valid_pool_inds = np.where(
inds_vscores>thr)[0]
return valid_pool_inds
def binary_uncertainty_filter(posts, B):
"""Uncertainty filtering for binary class
label distribution
Since there are only two classes, posterior
probability of only one of the classes
are given in form of 1D array.
"""
return np.argsort(np.abs(
np.array(posts)-0.5))[:B]
def bin_uncertainty_filter_multimg(expr,
model,
sess,
all_padded_imgs,
pool_inds,
B,
x_feed_dict={}):
# computing entropies for voxels of each
# image separately
s = len(pool_inds)
img_ind_sizes = [len(pool_inds[i]) for i in range(s)]
m = len(all_padded_imgs[0])-1
n = np.sum(img_ind_sizes)
H = [[] for i in range(s)]
for i in range(s):
if len(pool_inds[i])==0:
continue
# set the stats
stats = []
for j in range(m):
stats += [[expr.train_stats[i,2*j],
expr.train_stats[i,2*j+1]]]
posts = PW_NN.batch_eval(
model,
sess,
all_padded_imgs[i][:-1],
pool_inds[i],
expr.pars['patch_shape'],
expr.pars['ntb'],
stats,
'posteriors',
None,
x_feed_dict)[0]
H[i] = list(posts)
# spit out only the posteriors only if
# extra-feed-dict is given
tH = np.concatenate(H)
if len(x_feed_dict)>0:
return tH
# sort with respect to entropy values
tH = np.abs(tH - 0.5)
sorted_inds = np.argsort(tH)[:B]
sel_inds = patch_utils.global2local_inds(
sorted_inds, img_ind_sizes)
sel_posts = [np.array(H[i])[sel_inds[i]]
for i in range(s)]
return sel_inds, sel_posts
def gen_A_matrices(expr,
model,
sess,
sel_patches,
sel_posts,
diag_load=1e-5):
# forming A-matrices
# ------------------
# division by two in computing size of A is because
# in each layer we have gradients with respect to
# weights and bias terms --> number of layers that
# are considered is obtained after dividing by 2
A_size = int(len(model.grad_posts['1'])/2)
d3 = expr.pars['patch_shape'][-1]
c = expr.nclass
A = []
# len(sel_posts) == sel_patches.shape[0]
for i in range(len(sel_posts)):
# normalizing the patch
X_i = sel_patches[i,:,:,:]
feed_dict = {
model.x: np.expand_dims(X_i,axis=0),
model.keep_prob: 1.}
# preparing the poserior
# ASSUMOTION: binary classifications
x_post = sel_posts[i]
# Computing gradients and shrinkage
if x_post < 1e-6:
x_post = 0.
grads_0 = sess.run(
model.grad_posts['0'],
feed_dict=feed_dict)
grads_0 = NNAL_tools.\
shrink_gradient(
grads_0, 'sum')
grads_1 = 0.
elif x_post > 1-1e-6:
x_post = 1.
grads_0 = 0.
grads_1 = sess.run(
model.grad_posts['1'],
feed_dict=feed_dict)
grads_1 = NNAL_tools.\
shrink_gradient(
grads_1, 'sum')
else:
grads_0 = sess.run(
model.grad_posts['0'],
feed_dict=feed_dict)
grads_0 = NNAL_tools.\
shrink_gradient(
grads_0, 'sum')
grads_1 = sess.run(
model.grad_posts['1'],
feed_dict=feed_dict)
grads_1 = NNAL_tools.\
shrink_gradient(
grads_1, 'sum')
# the A-matrix
Ai = (1.-x_post) * np.outer(grads_0, grads_0) + \
x_post * np.outer(grads_1, grads_1)
# final diagonal-loading
A += [Ai+ np.eye(A_size)*diag_load]
return A
def refine_feature_matrix(F, B):
"""Refining a feature matrix to make it
full row-rank with a moderate condition number
"""
# selecting from those features that have the most
# non-zero values among the selected samples
nnz_feats = np.sum(F>0, axis=1)
feat_inds = np.argsort(-nnz_feats)[:int(B/2)]
ref_F = F[feat_inds,:]
# taking care of the rank
while np.linalg.matrix_rank(ref_F)<len(feat_inds):
# if the matrix is not full row-rank, discard
# the last selected index (worst among all)
feat_inds = feat_inds[:-1]
ref_F = F[feat_inds,:]
# taking care of the conditional number
while np.linalg.cond(ref_F) > 1e6:
feat_inds = feat_inds[:-1]
ref_F = F[feat_inds,:]
if len(feat_inds)==1:
print('Only one feature is selected.')
break
print('Cond. #: %f'% (np.linalg.cond(ref_F)),
end='\n\t')
print('# selected features: %d'%
(len(feat_inds)), end='\n\t')
return ref_F
def stoch_approx_IF(model,sess,
tr_patches,
pool_patches,
max_iter,
scale=50):
ntr = tr_patches.shape[0]
# gradient of the pool samples at
# their weak labels
feed_dict = {model.x:pool_patches,
model.keep_prob:1.}
weak_labels = sess.run(model.prediction,
feed_dict=feed_dict)
grads = NN.LLFC_grads(
model,sess,feed_dict,weak_labels)
# starting the iterations
V_t = grads
for t in range(max_iter):
# Hessian of a random training sample
rand_ind = [np.random.randint(ntr)]
feed_dict = {
model.x:tr_patches[rand_ind,:,:,:],
model.keep_prob:1.}
H = -NN.LLFC_hess(model,sess,feed_dict)
# iteration's step
V_t = grads + V_t - H@V_t/scale
return V_t, weak_labels
def SuPix_query(expr,
run,
model,
pool_lines,
train_inds,
overseg_img,
method_name,
sess):
"""Querying strategies for active
learning of patch-wise model
"""
k = expr.pars['k']
if method_name=='random':
n = len(pool_lines)
q = np.random.permutation(n)[:k]
if method_name=='entropy':
# posteriors
posts = PW_AL.batch_eval_wlines(
expr,
run,
model,
pool_lines,
'posteriors',
sess)
# explicit entropy scores
scores = np.abs(posts-.5)
# super-pixel scores
inds_path = os.path.join(
expr.root_dir, str(run),
'inds.txt')
inds_dict, locs_dict = PW_AL.create_dict(
inds_path, pool_lines)
pool_inds = inds_dict[list(
inds_dict.keys())[0]]
SuPix_scores = superpix_scoring(
overseg_img, pool_inds, scores)
# argsort-ing is not sensitive to
# NaN's, so invert np.inf to np.nan
SuPix_scores[
SuPix_scores==np.inf]=np.nan
# also nan-out the zero superpixels
qSuPix = np.unravel_index(
np.argsort(np.ravel(SuPix_scores)),
SuPix_scores.shape)
qSuPix = np.array([qSuPix[0][:k],
qSuPix[1][:k]])
# when the superpixels are selecte,
# extract their grid-points too
qSuPix_inds = PW_AL.get_SuPix_inds(
overseg_img, qSuPix)
return qSuPix, qSuPix_inds
def superpix_scoring(overseg_img,
inds,
scores):
"""Extending scores of a set of pixels
represented by line numbers in index file,
to a set of overpixels in a given
oversegmentation
:Parameters:
**overseg_img** : 3D array
oversegmentation of the image
containing super-pixels
**inds** : 1D array-like
3D index of the pixels that are
socred
**socres** : 1D array-like
scores that are assigned to pixels
:Returns:
**SuPix_scores** : 2D array
scores assigned to super-pixels,
where each row corresponds to a
slice of the image, and each
column corresponds to a super-pixel;
such that the (i,j)-th element
represents the score assigned to
the super-pixel with label j in
the i-th slice of the over-
segmentation image
If the (i,j)-th element is `np.inf`
it means that the super-pixel with
label j in slice i did not get any
score pixel in its area. And if
it is assigned zero, it means that
the superpixel with label j does
not exist in slice i at all.
"""
# multi-indices of pixel indices
s = overseg_img.shape
multinds = np.unravel_index(inds, s)
Z = np.unique(multinds[2])
SuPix_scores = np.ones(
(s[2],
int(overseg_img.max()+1)))*np.inf
for z in Z:
slice_ = overseg_img[:,:,z]
""" Assigning Min-Itensity of Pixels """
# creatin an image with
# values on the location of