-
Notifications
You must be signed in to change notification settings - Fork 5
/
exp_train_rbm.py
1408 lines (1203 loc) · 56.8 KB
/
exp_train_rbm.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
# Trains an RBM.
#
# Copyright (c) 2016 Gijs van Tulder / Erasmus MC, the Netherlands
# This code is licensed under the MIT license. See LICENSE for details.
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from collections import OrderedDict
import scipy.io as sio
from scipy import ndimage
import scipy.misc
import morb
from morb import rbms, stats, updaters, trainers, monitors, units, parameters, prediction, objectives
import theano
import theano.tensor as T
import numpy as np
import gzip, cPickle, time
import json, sys, os, time, os.path, math
import re
import gc
from utils import one_hot
from plot_filters import plot_filters, filter_dot_distance
from confmat import Confmat
import borderconvparameters
from theano import ProfileMode
mode = None
theano.config.floatX = 'float32'
# do not use scientific notation
np.set_printoptions(suppress=True)
# mode = theano.compile.DebugMode(require_matching_strides=False)
theano.config.exception_verbosity = 'high'
############################################
# SETTINGS
############################################
visible_maps = 1
hidden_maps = 32 # 8*8 # 100 # 50
filter_height = 5 # pic_w
filter_width = 5 # pic_w
offsets = filter_height
import argparse
parser = argparse.ArgumentParser(description="")
parser.add_argument("--experiment-id", metavar="ID", help="experiment ID")
parser.add_argument("--previous-layer", metavar="PKL", type=str, nargs="+")
parser.add_argument("--subsample", metavar="PX", type=int,
help="subsample first layer",
default=1)
parser.add_argument("--previous-layer-binary", action="store_true",
help="previous layer output as binary")
parser.add_argument("--beta", metavar="BETA", type=float,
help="proportion of generative learning",
default=0.9)
parser.add_argument("--beta-decay", metavar="BETA_DECAY", type=float,
help="actual beta = beta * (decay**epoch)",
default=1)
parser.add_argument("--learning-rate", metavar="RATE", type=float,
help="learning rate",
default=0.0001)
parser.add_argument("--learning-rate-decay-start", metavar="EPOCH", type=int,
help="learning rate decay, decay start")
parser.add_argument("--learning-rate-decay-end", metavar="EPOCH", type=int,
help="learning rate decay, decay end")
parser.add_argument("--learning-rate-decay-end-rate", metavar="RATE", type=float,
help="learning rate decay, final rate")
parser.add_argument("--learning-rate-bias", metavar="RATE", type=float,
help="learning rate for the bias")
parser.add_argument("--target-sparsity", metavar="ACTIVATION", type=float,
help="desired mean activation per feature map",
default=None)
parser.add_argument("--sparsity-lambda", metavar="LAMBDA", type=float,
help="lambda parameter",
default=None)
parser.add_argument("--goh-target-sparsity", metavar="ACTIVATION", type=float,
help="desired mean activation per feature map",
default=None)
parser.add_argument("--goh-sparsity-lambda", metavar="LAMBDA", type=float,
help="lambda parameter",
default=None)
parser.add_argument("--fixed-hidden-bias", action="store_true")
parser.add_argument("--non-shared-bias", action="store_true")
parser.add_argument("--no-U-pooling", action="store_true")
parser.add_argument("--multiple-hidden-bias", type=int,
help="number of hidden biases per filter",
default=None)
parser.add_argument("--bias-decay", metavar="BIAS_DECAY", type=float,
help="hidden bias -= BIAS_DECAY",
default=None)
parser.add_argument("--hidden-maps", metavar="N", type=int,
help="number of hidden maps",
default=hidden_maps)
parser.add_argument("--filter-height", metavar="H", type=int,
help="filter height",
default=filter_height)
parser.add_argument("--filter-width", metavar="W", type=int,
help="filter width",
default=filter_width)
parser.add_argument("--epochs", metavar="N", type=int,
help="epochs",
default=3000)
parser.add_argument("--train-scans", metavar="S", type=str,
help="scans used for training",
default="069,048")
parser.add_argument("--test-scans", metavar="S", type=str,
help="scans used for testing",
default="002,007")
parser.add_argument("--only-class", metavar="C", type=int,
help="train/test for one class",
default=None)
parser.add_argument("--n-states", metavar="N", type=int,
help="number of classes",
default=4)
parser.add_argument("--mb-size", metavar="MB", type=int, default=10)
parser.add_argument("--image-size", metavar="PX", type=int)
parser.add_argument("--image-width", metavar="PX", type=int)
parser.add_argument("--image-height", metavar="PX", type=int)
parser.add_argument("--subpatches", metavar="PX", type=int)
parser.add_argument("--convolution-type", required=True,
choices=["no","full","fullnoborder"])
parser.add_argument("--gaussian-blur", metavar="SIGMA", type=float,
help="Gaussian blur with sigma",
default=None)
parser.add_argument("--zca-whiten", action="store_true",
help="Apply ZCA whitening",
default=None)
parser.add_argument("--global-normalisation", action="store_true",
help="Use global normalisation, not per-patch")
parser.add_argument("--k-train", metavar="N", type=int,
help="CD steps for training",
default=1)
parser.add_argument("--k-eval", metavar="N", type=int,
help="CD steps for evaluation",
default=1)
parser.add_argument("--subset", metavar="PROP", type=float,
help="Train and test on a small subset",
default=None)
parser.add_argument("--subset-noshuffle", metavar="PROP", type=float,
help="Train and test on the first PROP of samples",
default=None)
parser.add_argument("--train-label-only-discriminative",
nargs="?", type=float,
const=-1000, default=None)
parser.add_argument("--ignore-labels", action="store_true",
help="Label units have no effects on filter learning")
parser.add_argument("--weight-w-init-std", type=float, default=None)
parser.add_argument("--weight-u-init-std", type=float, default=None)
parser.add_argument("--evaluate-every", metavar="EPOCHS", type=int, default=10)
parser.add_argument("--plot-every", metavar="EPOCHS", type=int, default=20)
parser.add_argument("--test-every", metavar="EPOCHS", type=int, default=100)
parser.add_argument("--resume-run", metavar="RUN_ID", type=str)
parser.add_argument("--rng-seed", metavar="SEED", type=int, default=123)
args = parser.parse_args()
hidden_maps = args.hidden_maps
filter_height = args.filter_height
filter_width = args.filter_width
offsets = filter_height
epochs = args.epochs
learning_rate = args.learning_rate
if args.image_size:
pic_w = args.image_size
pic_h = args.image_size
else:
pic_w = 15
pic_h = 15
if args.image_width:
pic_w = args.image_width
if args.image_height:
pic_h = args.image_height
mb_size = args.mb_size
n_states = args.n_states
if args.convolution_type == "no":
pic_h = filter_height
pic_w = filter_width
shape_info = {
'hidden_maps': hidden_maps,
'visible_maps': visible_maps,
'filter_height': filter_height,
'filter_width': filter_width,
'visible_height': pic_h,
'visible_width': pic_w,
'mb_size': mb_size
}
pooling_operator = T.sum
if args.train_label_only_discriminative and args.train_label_only_discriminative < 0:
args.train_label_only_discriminative = args.learning_rate
run_id = "%s-%s-%d" % (args.experiment_id, time.strftime("%Y%m%d-%H%M%S"), os.getpid())
if args.only_class:
run_id = "%s-class%d" % (run_id, args.only_class)
print "run: %s" % run_id
print "experiment: %s" % args.experiment_id
print
print "previous layer: ", args.previous_layer
print "subsample: ", args.subsample
print "previous layer binary: ", args.previous_layer_binary
print "shape info: ", shape_info
print "beta: ", args.beta
print "beta decay: ", args.beta_decay
print "pooling oper: ", pooling_operator
print "convolution: ", args.convolution_type
print "subpatches: ", args.subpatches
print "gaussian blur:", args.gaussian_blur
print "zca whiten: ", args.zca_whiten
print "learning rate:", args.learning_rate
print " decay start:", args.learning_rate_decay_start
print " decay end: ", args.learning_rate_decay_end
print " final rate: ", args.learning_rate_decay_end_rate
print " for bias: ", args.learning_rate_bias
print "discrim. lrat:", args.train_label_only_discriminative
print "sparsity: ", args.target_sparsity
print "sparsity lamb:", args.sparsity_lambda
print "goh sparsity: ", args.goh_target_sparsity
print "goh sparslamb:", args.goh_sparsity_lambda
print "fix hidd.bias:", args.fixed_hidden_bias
print "non-shared bias:", args.non_shared_bias
print "no U-pooling: ", args.no_U_pooling
print "multi hidbias:", args.multiple_hidden_bias
print "bias decay: ", args.bias_decay
print "mb_size: ", mb_size
print "epochs: ", epochs
print "only class: ", args.only_class
print "subset: ", args.subset
print "subset noshuf:", args.subset_noshuffle
print "ignore labels:", args.ignore_labels
print "weight W init std: ", args.weight_w_init_std
print "weight U init std: ", args.weight_u_init_std
print "rng seed: ", args.rng_seed
numpy_rng = np.random.RandomState(args.rng_seed)
############################################
# DATA
############################################
# load data
print ">> Loading dataset..."
# scans with 15x15 pixels
scans = ["002","007","032","048","069","072","075","087","108","109","112","114","137","139","156","175","188","189","190","201","205","224","233","250","272","279","281","283","306","312","318","320","326","329","351","362","363","367","373","388","430"]
scans = ["002","007","032","048","069","072","075","087","108","109","112","114","137"]
scans = ["002","007","032","048"] # ,"069","072","075","087","108","109","112","114","137"]
# test_scans = sys.argv[1:]
# train_scans = [s for s in scans if not s in test_scans]
# train_scans = train_scans[0:2]
# test_scans = ["002","007"]
# train_scans = ["069","048"]
train_scans = args.train_scans.split(",")
test_scans = args.test_scans.split(",")
print "train: "+(",".join(train_scans))
print "test: "+(",".join(test_scans))
print
if "032" in test_scans or "032" in train_scans:
raise Exception("032 not okay.")
# load train scans
train_data = []
train_labels = []
for s in train_scans:
if ".mat" in s:
m = sio.loadmat(s)
elif ".jpg" in s:
s_label, s_filename = s.split(":")
m = scipy.misc.imread(s_filename)
train_data.append(np.expand_dims(m,0).astype(theano.config.floatX))
train_labels.append(np.reshape(np.double(s_label),[1,1]).astype(theano.config.floatX))
pic_h = m.shape[0]
pic_w = m.shape[1]
elif args.convolution_type == "full" or args.previous_layer:
m = sio.loadmat("SALD-cells-with-borders/"+s+".mat")
else:
m = sio.loadmat("SALD-cells/"+s+".mat")
if ".jpg" in s:
pass
elif 'image' in m:
train_data.append(m['image'].astype(theano.config.floatX))
elif args.convolution_type == "full" or args.previous_layer:
train_data.append(np.transpose(np.double(m['neighbourhoods']).astype(theano.config.floatX)))
else:
train_data.append(np.transpose(np.double(m['cells']).astype(theano.config.floatX)))
if not ".jpg" in s:
train_labels.append(np.transpose(m['labels'].astype(theano.config.floatX)))
m = None
train_data = np.concatenate(train_data)
train_labels = np.concatenate(train_labels)
if args.only_class:
subset = (train_labels[:,0] == args.only_class)
train_data = train_data[subset]
train_labels = train_labels[subset]
# remove any extra classes
subset = (train_labels[:,0] <= args.n_states)
train_data = train_data[subset]
train_labels = train_labels[subset]
if args.subset_noshuffle:
n = min(train_data.shape[0], int(args.subset_noshuffle * n))
train_data = train_data[0:(np.floor_divide(n, mb_size) * mb_size)]
train_labels = train_labels[0:(np.floor_divide(n, mb_size) * mb_size)]
order = numpy_rng.permutation(train_data.shape[0])
n = train_data.shape[0]
if not args.subset is None:
n = min(n, int(args.subset * n))
order = order[0:(np.floor_divide(n, mb_size) * mb_size)]
train_data = train_data[order]
train_labels = train_labels[order]
print "train shape:", train_data.shape
print "train shape:", train_labels.shape
# load test scans
test_data = []
test_labels = []
for s in test_scans:
if ".mat" in s:
m = sio.loadmat(s)
elif ".jpg" in s:
s_label, s_filename = s.split(":")
m = scipy.misc.imread(s_filename)
test_data.append(np.expand_dims(m,0).astype(theano.config.floatX))
test_labels.append(np.reshape(np.double(s_label),[1,1]).astype(theano.config.floatX))
elif args.convolution_type == "full" or args.previous_layer:
m = sio.loadmat("SALD-cells-with-borders/"+s+".mat")
else:
m = sio.loadmat("SALD-cells/"+s+".mat")
if ".jpg" in s:
pass
elif 'image' in m:
test_data.append(np.double(m['image']).astype(theano.config.floatX))
elif args.convolution_type == "full" or args.previous_layer:
test_data.append(np.transpose(np.double(m['neighbourhoods']).astype(theano.config.floatX)))
else:
test_data.append(np.transpose(np.double(m['cells']).astype(theano.config.floatX)))
if not ".jpg" in s:
test_labels.append(np.transpose(m['labels'].astype(theano.config.floatX)))
m = None
test_data = np.concatenate(test_data)
test_labels = np.concatenate(test_labels)
if args.only_class:
subset = (test_labels[:,0] == args.only_class)
test_data = test_data[subset]
test_labels = test_labels[subset]
# remove any extra classes
subset = (test_labels[:,0] <= args.n_states)
test_data = test_data[subset]
test_labels = test_labels[subset]
if args.subset_noshuffle:
n = min(test_data.shape[0], int(args.subset_noshuffle * n))
test_data = test_data[0:(np.floor_divide(n, mb_size) * mb_size)]
test_labels = test_labels[0:(np.floor_divide(n, mb_size) * mb_size)]
order = numpy_rng.permutation(test_data.shape[0])
n = test_data.shape[0]
if not args.subset is None:
n = min(n, int(args.subset * n))
order = order[0:(np.floor_divide(n, mb_size) * mb_size)]
test_data = test_data[order]
test_labels = test_labels[order]
print "test shape: ", test_data.shape
print "test shape: ", test_labels.shape
print
train_distr = [ sum(sum(train_labels==i)) for i in np.sort(np.unique(train_labels)) ]
test_distr = [ sum(sum(test_labels==i)) for i in np.sort(np.unique(test_labels)) ]
print "train distribution: ", train_distr
print "train distribution: ", np.asarray(train_distr, dtype=float) / sum(train_distr)
print "test distribution: ", test_distr
print "test distribution: ", np.asarray(test_distr, dtype=float) / sum(test_distr)
print
# garbage collection
gc.collect()
############################################
# CONVERT TO INPUTS
############################################
if train_data.ndim == 2:
pic_h_from_data = int(np.sqrt(train_data.shape[1]))
pic_w_from_data = int(np.sqrt(train_data.shape[1]))
train_set_x = train_data.reshape((train_data.shape[0], 1, pic_h_from_data, pic_w_from_data))
else:
train_set_x = train_data.reshape([train_data.shape[0], 1] + list(train_data.shape[1:100]))
pic_h_from_data = int(train_set_x.shape[2])
pic_w_from_data = int(train_set_x.shape[3])
if test_data.ndim == 2:
test_set_x = test_data.reshape((test_data.shape[0], 1, pic_h_from_data, pic_w_from_data))
else:
test_set_x = test_data.reshape([test_data.shape[0], 1] + list(test_data.shape[1:100]))
if args.subpatches:
print ">> Extracting subpatches"
print " Before: train ", train_set_x.shape
print " test ", test_set_x.shape
# create subpatches
cells_sub = []
labels_sub = []
for i in xrange(train_set_x.shape[0]):
for j in xrange(args.subpatches):
x = numpy_rng.randint(0, pic_w_from_data - pic_w)
y = numpy_rng.randint(0, pic_h_from_data - pic_h)
cells_sub.append(train_set_x[i:(i+1), :, y:(y+pic_h), x:(x+pic_w)])
labels_sub.append(train_labels[i:(i+1), :])
train_set_x = np.concatenate(cells_sub)
train_labels = np.concatenate(labels_sub)
order = numpy_rng.permutation(train_set_x.shape[0])
train_set_x = train_set_x[order]
train_labels = train_labels[order]
cells_sub = []
labels_sub = []
for i in xrange(test_set_x.shape[0]):
for j in xrange(args.subpatches):
x = numpy_rng.randint(0, pic_w_from_data - pic_w)
y = numpy_rng.randint(0, pic_h_from_data - pic_h)
cells_sub.append(test_set_x[i:(i+1), :, y:(y+pic_h), x:(x+pic_w)])
labels_sub.append(test_labels[i:(i+1), :])
test_set_x = np.concatenate(cells_sub)
test_labels = np.concatenate(labels_sub)
order = numpy_rng.permutation(test_set_x.shape[0])
test_set_x = test_set_x[order]
test_labels = test_labels[order]
cells_sub = None
labels_sub = None
print " After: train ", train_set_x.shape
print " test ", test_set_x.shape
pic_h_from_data = pic_h
pic_w_from_data = pic_w
if not args.convolution_type == "full" and not args.previous_layer:
assert pic_h == pic_h_from_data
assert pic_w == pic_w_from_data
# release
train_data = None
test_data = None
train_set_y = one_hot(train_labels[0:train_set_x.shape[0],:] - 1, n_states)
test_set_y = one_hot(test_labels[0:test_set_x.shape[0],:] - 1, n_states)
if args.gaussian_blur:
print ">> Applying Gaussian blur"
for i in xrange(0, train_set_x.shape[0]):
for j in xrange(0, train_set_x.shape[1]):
train_set_x[i,j,:,:] = ndimage.gaussian_filter(train_set_x[i,j,:,:], sigma=args.gaussian_blur)
for i in xrange(0, test_set_x.shape[0]):
for j in xrange(0, test_set_x.shape[1]):
test_set_x[i,j,:,:] = ndimage.gaussian_filter(test_set_x[i,j,:,:], sigma=args.gaussian_blur)
if args.zca_whiten:
# http://ufldl.stanford.edu/wiki/index.php/Exercise:PCA_and_Whitening
import scipy.linalg
print ">> ZCA whitening"
n_samples = train_set_x.shape[0]
train_set_rows = train_set_x.reshape(n_samples, -1)
mu = np.mean(train_set_rows, axis=1).reshape(n_samples, 1)
train_set_rows -= mu
sigma = np.dot(np.transpose(train_set_rows), train_set_rows) / n_samples
U, s, Vh = scipy.linalg.svd(sigma)
k = np.where(np.cumsum(s) / np.sum(s) > 0.90)[0][0] # explain 90% of variance
epsilon = 0.1
xZCAwhite = np.dot(np.dot(np.dot(U, np.diag(1.0 / np.sqrt(s + epsilon))), np.transpose(U)), np.transpose(train_set_rows))
train_set_x = np.transpose(xZCAwhite).reshape(n_samples, 1, pic_h_from_data, pic_w_from_data)
n_samples = test_set_x.shape[0]
test_set_rows = test_set_x.reshape(n_samples, -1)
mu = np.mean(test_set_rows, axis=1).reshape(n_samples, 1)
test_set_rows -= mu
xZCAwhite = np.dot(np.dot(np.dot(U, np.diag(1.0 / np.sqrt(s + epsilon))), np.transpose(U)), np.transpose(test_set_rows))
test_set_x = np.transpose(xZCAwhite).reshape(n_samples, 1, pic_h_from_data, pic_w_from_data)
xZCAwhite = None
if args.global_normalisation:
# normalise / whiten
global_mu = np.mean(train_set_x)
global_sigma = np.std(train_set_x)
train_set_x -= global_mu
train_set_x /= (0.25 * global_sigma)
test_set_x -= global_mu
test_set_x /= (0.25 * global_sigma)
else:
# normalise / whiten
print ">> Normalising training data..."
n_samples = train_set_x.shape[0]
# pic_h = train_set_x.shape[2]
# pic_w = train_set_x.shape[3]
train_set_rows = train_set_x.reshape(n_samples, -1)
mu = np.mean(train_set_rows, axis=1).reshape(n_samples, 1, 1, 1)
sigma = np.std(train_set_rows, axis=1).reshape(n_samples, 1, 1, 1)
train_set_x -= mu
train_set_x /= (0.25 * sigma)
# release
train_set_rows = None
print ">> Normalising testing data..."
n_samples = test_set_x.shape[0]
# pic_h = test_set_x.shape[2]
# pic_w = test_set_x.shape[3]
test_set_rows = test_set_x.reshape(n_samples, -1)
mu = np.mean(test_set_rows, axis=1).reshape(n_samples, 1, 1, 1)
sigma = np.std(test_set_rows, axis=1).reshape(n_samples, 1, 1, 1)
test_set_x -= mu
test_set_x /= (0.25 * sigma)
# release
test_set_rows = None
if not train_set_x.dtype == theano.config.floatX:
train_set_x = train_set_x.astype(theano.config.floatX)
if not test_set_x.dtype == theano.config.floatX:
test_set_x = test_set_x.astype(theano.config.floatX)
if args.convolution_type == "full" or args.previous_layer:
# determine border margin
margin_h = filter_height - 1
margin_w = filter_width - 1
train_set_x_border = train_set_x[:,:,(pic_h-margin_h):(2*pic_h+margin_h),(pic_w-margin_h):(2*pic_w+margin_w)]
test_set_x_border = test_set_x[:,:,(pic_h-margin_h):(2*pic_h+margin_h),(pic_w-margin_h):(2*pic_w+margin_w)]
train_set_x = train_set_x[:,:,pic_h:(2*pic_h),pic_w:(2*pic_w)]
test_set_x = test_set_x[:,:,pic_h:(2*pic_h),pic_w:(2*pic_w)]
print ">> After removing borders:"
print "train_set_x_border: ", train_set_x_border.shape
print "test_set_x_border: ", test_set_x_border.shape
print "train_set_x: ", train_set_x.shape
print "test_set_x: ", test_set_x.shape
# garbage collection
gc.collect()
############################################
# APPLY FIRST LAYER CONVO
############################################
if args.previous_layer:
from morb import activation_functions
from theano.tensor.nnet import conv
for prev_layer in args.previous_layer:
print ">> Processing layer: ", prev_layer
train_set_x_conv_border_collect = []
test_set_x_conv_border_collect = []
for fname in prev_layer.split(","):
print " >> Filter set: ", fname
with open(fname, "r") as f:
prev_layer_params = cPickle.load(f)
prev_W = prev_layer_params["W"]
prev_bh = prev_layer_params["bh"]
print " prev_W.shape: ", prev_W.shape
print " prev_bh.shape: ", prev_bh.shape
if args.convolution_type == "no":
# TODO
print "TODO"
sys.exit()
print " Applying convolution..."
V = T.dtensor4()
W = T.dtensor4()
bh = T.dvector()
W_flipped = W[:, :, ::-1, ::-1]
subsample = (args.subsample, args.subsample)
reshaped_bh = bh.dimshuffle('x',0,'x','x')
c = conv.conv2d(V, W_flipped, border_mode="full", subsample=subsample)
c_act = activation_functions.sigmoid(c + reshaped_bh)
conv_f = theano.function([V, W, bh], [ c_act ])
train_set_x_conv = []
for i in xrange(0, train_set_x.shape[0]):
cvf = conv_f(train_set_x_border[i:i+1, :,:,:], prev_W, prev_bh)[0]
if not cvf.dtype == theano.config.floatX:
cvf = cvf.astype(theano.config.floatX)
train_set_x_conv.append(cvf)
train_set_x_conv = np.concatenate(train_set_x_conv)
test_set_x_conv = []
for i in xrange(0, test_set_x.shape[0]):
cvf = conv_f(test_set_x_border[i:i+1, :,:,:], prev_W, prev_bh)[0]
if not cvf.dtype == theano.config.floatX:
cvf = cvf.astype(theano.config.floatX)
test_set_x_conv.append(cvf)
test_set_x_conv = np.concatenate(test_set_x_conv)
# release
prev_layer_params = None
prev_W = None
prev_bh = None
conv_f = None
cvf = None
print " After this layer:"
print " train_set_x_conv: ", train_set_x_conv.shape
print " test_set_x_conv: ", test_set_x_conv.shape
# determine border margin
margin_h = (train_set_x_conv.shape[2] - train_set_x_border.shape[2]) / 2
margin_w = (train_set_x_conv.shape[3] - train_set_x_border.shape[3]) / 2
train_set_x_conv_border = train_set_x_conv[:,:,(margin_h):(train_set_x_conv.shape[2]-margin_h),(margin_w):(train_set_x_conv.shape[3]-margin_w)]
test_set_x_conv_border = test_set_x_conv[:,:,(margin_h):(test_set_x_conv.shape[2]-margin_h),(margin_w):(test_set_x_conv.shape[3]-margin_w)]
if not train_set_x_conv_border.dtype == theano.config.floatX:
train_set_x_conv_border = train_set_x_conv_border.astype(theano.config.floatX)
if not test_set_x_conv_border.dtype == theano.config.floatX:
test_set_x_conv_border = test_set_x_conv_border.astype(theano.config.floatX)
train_set_x_conv_border_collect.append(train_set_x_conv_border)
test_set_x_conv_border_collect.append(test_set_x_conv_border)
# release
train_set_x_conv_border = None
test_set_x_conv_border = None
train_set_x_border = np.concatenate(train_set_x_conv_border_collect, axis=1)
test_set_x_border = np.concatenate(test_set_x_conv_border_collect, axis=1)
# release
train_set_x_conv_border_collect = None
test_set_x_conv_border_collect = None
print " After this layer:"
print " train_set_x_border: ", train_set_x_border.shape
print " test_set_x_border: ", test_set_x_border.shape
# remove borders
margin_h = (train_set_x_border.shape[2] - pic_h) / 2
margin_w = (train_set_x_border.shape[3] - pic_w) / 2
train_set_x = train_set_x_border[:,:,(margin_h):(pic_h + margin_h),(margin_w):(pic_w + margin_w)]
test_set_x = test_set_x_border[:,:,(margin_h):(pic_h + margin_h),(margin_w):(pic_w + margin_w)]
print " After removing borders:"
print " train_set_x: ", train_set_x.shape
print " test_set_x: ", test_set_x.shape
visible_maps = train_set_x.shape[1]
shape_info['visible_maps'] = visible_maps
# normalise / whiten
if not args.previous_layer_binary:
if args.global_normalisation:
# normalise / whiten
mu = np.mean(train_set_x)
sigma = np.std(train_set_x)
train_set_x_border -= mu
train_set_x_border /= (0.25 * sigma)
test_set_x_border -= mu
test_set_x_border /= (0.25 * sigma)
else:
print " Normalising layer output (training data)..."
n_samples = train_set_x.shape[0]
# pic_h = train_set_x.shape[2]
# pic_w = train_set_x.shape[3]
train_set_rows = train_set_x.reshape(n_samples, -1)
mu = np.mean(train_set_rows, axis=1).reshape(n_samples, 1, 1, 1)
sigma = np.std(train_set_rows, axis=1).reshape(n_samples, 1, 1, 1)
train_set_x_border -= mu
train_set_x_border /= (0.25 * sigma)
# release
train_set_rows = None
print " Normalising layer output (testing data)..."
n_samples = test_set_x.shape[0]
# pic_h = test_set_x.shape[2]
# pic_w = test_set_x.shape[3]
test_set_rows = test_set_x.reshape(n_samples, -1)
mu = np.mean(test_set_rows, axis=1).reshape(n_samples, 1, 1, 1)
sigma = np.std(test_set_rows, axis=1).reshape(n_samples, 1, 1, 1)
test_set_x_border -= mu
test_set_x_border /= (0.25 * sigma)
# release
test_set_rows = None
if not train_set_x_border.dtype == theano.config.floatX:
train_set_x_border = train_set_x_border.astype(theano.config.floatX)
if not test_set_x_border.dtype == theano.config.floatX:
test_set_x_border = test_set_x_border.astype(theano.config.floatX)
# remove borders again,
# so numpy can make train_set_x a view of train_set_x_border
margin_h = (train_set_x_border.shape[2] - pic_w) / 2
margin_w = (train_set_x_border.shape[3] - pic_w) / 2
train_set_x = train_set_x_border[:,:,(margin_h):(pic_h + margin_h),(margin_w):(pic_w + margin_w)]
test_set_x = test_set_x_border[:,:,(margin_h):(pic_h + margin_h),(margin_w):(pic_w + margin_w)]
print " After removing borders:"
print " train_set_x: ", train_set_x.shape
print " test_set_x: ", test_set_x.shape
# garbage collection
gc.collect()
############################################
# CONSTRUCT RBM
############################################
print ">> Constructing RBM..."
fan_in = visible_maps * filter_height * filter_width
if args.weight_w_init_std:
weight_w_std = args.weight_w_init_std
else:
weight_w_std = 0.5 / np.sqrt(fan_in)
if args.weight_u_init_std:
weight_u_std = args.weight_u_init_std
else:
weight_u_std = 4*np.sqrt(6./(hidden_maps+n_states))
# initial values
if args.convolution_type == "no":
initial_W = np.asarray(
numpy_rng.normal(
0, weight_w_std,
size = (visible_maps, pic_h, pic_w, hidden_maps)
), dtype=theano.config.floatX)
initial_U = np.asarray(
numpy_rng.uniform(
low = -weight_u_std,
high = weight_u_std,
size = (n_states, hidden_maps)
), dtype=theano.config.floatX)
if args.no_U_pooling:
raise "No U pooling not supported with no convolution"
elif args.multiple_hidden_bias:
initial_W = np.asarray(
numpy_rng.normal(
0, weight_w_std,
size = (hidden_maps, visible_maps, filter_height, filter_width)
), dtype=theano.config.floatX)
initial_U = np.asarray(
numpy_rng.uniform(
low = -weight_u_std,
high = weight_u_std,
size = (n_states, hidden_maps, args.multiple_hidden_bias)
), dtype=theano.config.floatX)
if args.no_U_pooling:
raise "No U pooling not supported with multiple hidden bias"
else:
initial_W = np.asarray(
numpy_rng.normal(
0, weight_w_std,
size = (hidden_maps, visible_maps, filter_height, filter_width)
), dtype=theano.config.floatX)
if args.no_U_pooling:
margin_h = shape_info['filter_height'] - 1
margin_w = shape_info['filter_width'] - 1
hidden_height = shape_info['visible_height'] + (2 * margin_h) - shape_info['filter_height'] + 1
hidden_width = shape_info['visible_width'] + (2 * margin_w) - shape_info['filter_width'] + 1
initial_U = np.asarray(
numpy_rng.uniform(
low = -weight_u_std,
high = weight_u_std,
size = (n_states, hidden_maps, hidden_height, hidden_width)
), dtype=theano.config.floatX)
else:
initial_U = np.asarray(
numpy_rng.uniform(
low = -weight_u_std,
high = weight_u_std,
size = (n_states, hidden_maps)
), dtype=theano.config.floatX)
initial_bv = np.zeros(visible_maps, dtype = theano.config.floatX)
initial_by = np.zeros(n_states, dtype = theano.config.floatX)
if args.non_shared_bias:
if not args.convolution_type == "full":
raise "Unsupported: non-shared bias needs full border convolution"
margin_h = shape_info['filter_height'] - 1
margin_w = shape_info['filter_width'] - 1
hidden_height = shape_info['visible_height'] + (2 * margin_h) - shape_info['filter_height'] + 1
hidden_width = shape_info['visible_width'] + (2 * margin_w) - shape_info['filter_width'] + 1
initial_bh = np.zeros((hidden_maps, hidden_height, hidden_width), dtype = theano.config.floatX)
elif args.multiple_hidden_bias:
initial_bh = np.zeros((hidden_maps, args.multiple_hidden_bias), dtype = theano.config.floatX)
else:
initial_bh = np.zeros(hidden_maps, dtype = theano.config.floatX)
# units
# rbms.SigmoidBinaryRBM(n_visible, n_hidden)
rbm = morb.base.RBM()
if args.previous_layer_binary:
rbm.v = units.BinaryUnits(rbm, name='v') # visibles
else:
rbm.v = units.GaussianUnits(rbm, name='v') # visibles
# rbm.v = units.BinaryUnits(rbm, name='v') # visibles
rbm.h = units.BinaryUnits(rbm, name='h') # hiddens
# rbm.y = units.BinaryUnits(rbm, name='y')
if not args.ignore_labels:
rbm.y = units.SoftmaxUnits(rbm, name='y')
if args.convolution_type == "full":
class DummyUnits(object):
def __init__(self, name):
self.name = name
self.proxy_units = []
def __repr__(self):
return self.name
rbm.v_border = DummyUnits(name="v border dummy")
context_units = [rbm.v_border]
else:
context_units = []
pmap = {
"W": theano.shared(value=initial_W, name="W"),
"bv": theano.shared(value=initial_bv, name="bv"),
"bh": theano.shared(value=initial_bh, name="bh")
}
if not args.ignore_labels:
pmap["U"] =theano.shared(value=initial_U, name="U")
pmap["by"] =theano.shared(value=initial_by, name="by")
# parameters
if not args.previous_layer_binary:
parameters.FixedBiasParameters(rbm, rbm.v.precision_units)
if args.multiple_hidden_bias:
if args.convolution_type == "full":
raise "Unsupported: multiple hidden bias needs borderconv"
rbm.W = borderconvparameters.Convolutional2DParameters(rbm, [rbm.v, rbm.h], 'W', name='W', shape_info=shape_info, var_fixed_border=rbm.v_border, shared_hidden_dims=1, alternative_gradient=True)
elif args.convolution_type == "full":
rbm.W = borderconvparameters.Convolutional2DParameters(rbm, [rbm.v, rbm.h], 'W', name='W', shape_info=shape_info, var_fixed_border=rbm.v_border, alternative_gradient=True)
elif args.convolution_type == "no":
rbm.W = parameters.AdvancedProdParameters(rbm, [rbm.v, rbm.h], [3,1], 'W', name='W')
elif args.convolution_type == "fullnoborder":
rbm.W = borderconvparameters.Convolutional2DParameters(rbm, [rbm.v, rbm.h], 'W', name='W', shape_info=shape_info)
# one bias per map (so shared across width and height):
if args.previous_layer_binary:
rbm.bv = parameters.SharedBiasParameters(rbm, rbm.v, 3, 2, 'bv', name='bv')
else:
rbm.bv = parameters.SharedQuadraticBiasParameters(rbm, rbm.v, 3, 2, 'bv', name='bv')
if args.non_shared_bias:
rbm.bh = parameters.AdvancedBiasParameters(rbm, rbm.h, 3, 'bh', name='bh')
elif args.fixed_hidden_bias:
rbm.bh = parameters.FixedBiasParameters(rbm, rbm.h, value=-10)
rbm.bh.var = theano.shared(value=initial_bh, name='bh')
elif args.convolution_type == "no":
rbm.bh = parameters.BiasParameters(rbm, rbm.h, 'bh', name='bh')
elif args.multiple_hidden_bias:
rbm.bh = parameters.SharedBiasParameters(rbm, rbm.h, 4, 2, 'bh', name='bh')
else:
rbm.bh = parameters.SharedBiasParameters(rbm, rbm.h, 3, 2, 'bh', name='bh')
# labels
if args.ignore_labels:
pass
elif args.convolution_type == "no":
rbm.U = parameters.ProdParameters(rbm, [rbm.y, rbm.h], 'U', name='U')
elif args.multiple_hidden_bias:
if args.no_U_pooling:
rbm.U = parameters.AdvancedProdParameters(rbm, [rbm.y, rbm.h], [1, 4], 'U', name='U')
else:
rbm.U = parameters.SharedProdParameters(rbm, [rbm.y, rbm.h], 4, 2, 'U', name='U', pooling_operator=pooling_operator)
else:
if args.no_U_pooling:
rbm.U = parameters.AdvancedProdParameters(rbm, [rbm.y, rbm.h], [1, 3], 'U', name='U')
else:
rbm.U = parameters.SharedProdParameters(rbm, [rbm.y, rbm.h], 3, 2, 'U', name='U', pooling_operator=pooling_operator)
if not args.ignore_labels:
rbm.by = parameters.BiasParameters(rbm, rbm.y, 'by', name='by')
initial_vmap = { rbm.v: T.tensor4('v') }
if not args.ignore_labels:
initial_vmap[rbm.y] = T.matrix('y')
if args.convolution_type == "full":
initial_vmap[rbm.v_border] = T.tensor4('v border')
print rbm
if not args.ignore_labels:
dlo = objectives.discriminative_learning_objective(rbm, \
visible_units = [rbm.v], \
hidden_units = [rbm.h], \
label_units = [rbm.y], \
vmap = initial_vmap,
pmap = pmap)
else:
dlo = theano.shared(value=np.cast['float32'](0)) * theano.shared(value=np.cast['float32'](0))
# learning rate decay
learning_rate_with_decay = theano.shared(value=np.cast[theano.config.floatX](learning_rate), name='learning_rate')
# try to calculate weight updates using CD-1 stats
print ">> Constructing contrastive divergence updaters..."
k_train = args.k_train
k_eval = args.k_eval
print "k_train=%d k_eval=%d" % (k_train, k_eval)
if not args.ignore_labels:
# s = stats.cd_stats(rbm, initial_vmap, visible_units=[rbm.v, rbm.y], hidden_units=[rbm.h], context_units=context_units, \
# k=k_train, mean_field_for_stats=[rbm.v, rbm.y], mean_field_for_gibbs=[rbm.v, rbm.y, rbm.h])
s = stats.cd_stats(rbm, initial_vmap, pmap, visible_units=[rbm.v, rbm.y], hidden_units=[rbm.h], context_units=context_units, \
k=k_train, mean_field_for_stats=[rbm.v, rbm.y], mean_field_for_gibbs=[rbm.v, rbm.y])
else:
# s = stats.cd_stats(rbm, initial_vmap, visible_units=[rbm.v], hidden_units=[rbm.h], context_units=context_units, \
# k=k_train, mean_field_for_stats=[rbm.v], mean_field_for_gibbs=[rbm.v, rbm.h])
s = stats.cd_stats(rbm, initial_vmap, pmap, visible_units=[rbm.v], hidden_units=[rbm.h], context_units=context_units, \
k=k_train, mean_field_for_stats=[rbm.v], mean_field_for_gibbs=[rbm.v])
print "mean_field_for_gibbs without rbm.h !"
print "Stats eval"
if not args.ignore_labels:
s_eval = stats.cd_stats(rbm, initial_vmap, pmap, visible_units=[rbm.v, rbm.y], hidden_units=[rbm.h], context_units=context_units, k=k_eval) # , mean_field_for_gibbs=[rbm.v], mean_field_for_stats=[rbm.v, rbm.h])
else:
s_eval = stats.cd_stats(rbm, initial_vmap, pmap, visible_units=[rbm.v], hidden_units=[rbm.h], context_units=context_units, k=k_eval) # , mean_field_for_gibbs=[rbm.v], mean_field_for_stats=[rbm.v, rbm.h])
decayed_beta = theano.shared(value=np.cast['float32'](args.beta), name='beta')
def sparsity_penalty(rbm, hidden_units, v0_vmap, pmap, target):
"""
Customised to support convolutional RBM's multiple hidden maps.
"""
# complete units lists
hidden_units = rbm.complete_units_list(hidden_units)
# complete the supplied vmap
v0_vmap = rbm.complete_vmap(v0_vmap)
hidden_vmap = rbm.mean_field(hidden_units, v0_vmap, pmap)
if len(hidden_units) > 1:
raise "More than one set of hidden units."
penalty_terms = []
for hu in hidden_units:
mean_activation = T.mean(hidden_vmap[hu], [0,2,3]) # mean over minibatch,x,y
# penalty_terms.append(T.sum((mean_activation - target) ** 2))
penalty_terms.append(target - mean_activation)
total_penalty = penalty_terms[0] # sum(penalty_terms)
return total_penalty
class LeeSparsityUpdater(morb.base.Updater):
def get_update(self):
return theano.printing.Print("sparsity update")(sparsity_penalty(rbm, [rbm.h], initial_vmap, pmap, args.target_sparsity))
if args.sparsity_lambda and args.target_sparsity:
calc_sparsity_penalty = sparsity_penalty(rbm, [rbm.h], initial_vmap, pmap, args.target_sparsity)
# dlo = calc_sparsity_penalty
umap = {}
for var in rbm.variables:
learning_rate_for_var = learning_rate_with_decay
if args.learning_rate_bias and (var == rbm.bh.var or var == rbm.bv.var):
learning_rate_for_var = args.learning_rate_bias
pu = pmap[var]
if args.ignore_labels:
pu += learning_rate_for_var * (decayed_beta * updaters.CDUpdater(rbm, var, s))
elif args.train_label_only_discriminative and var in (rbm.U.var, rbm.by.var):
pu += args.train_label_only_discriminative * updaters.GradientUpdater(dlo, var, pmap=pmap)
else:
pu += learning_rate_for_var * (decayed_beta * updaters.CDUpdater(rbm, var, s) + (1 - decayed_beta) * updaters.GradientUpdater(dlo, var, pmap=pmap))
if args.sparsity_lambda and args.target_sparsity and var == rbm.bh.var:
print "using sparsity penalty!"
# pu += args.sparsity_lambda * updaters.SelfUpdater(calc_sparsity_penalty) # , var, pmap=pmap)