forked from nd-hung/DL4DistancePrediction2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDilatedResNet4Distance.py
1121 lines (889 loc) · 38 KB
/
DilatedResNet4Distance.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
import numpy as np
import theano
import theano.tensor as T
##a new implementation of 1D convolution
class ResConv1DLayer(object):
def __init__(self, rng, input, n_in=0, n_out=0, halfWinSize=0,
dilation=1, activation=T.nnet.relu, mask=None):
""" Initialize the parameters
The input has shape (batchSize, n_in, seqLen)
The output will have shape (batchSize, n_out, seqLen)
"""
self.input = input
self.n_in = n_in
self.n_out = n_out
self.halfWinSize = halfWinSize
windowSize = 2*halfWinSize + 1
self.filter_size = windowSize
self.dilation = (1, dilation)
# reshape input to shape (batchSize, n_in, nRows=1, nCols=seqLen)
in4conv2D = input.dimshuffle(0, 1, 'x', 2)
# initialize the filter, i.e., convoltion kernel
w_shp = (n_out, n_in, 1, windowSize)
if activation == T.nnet.relu:
W_values = np.asarray(rng.normal(scale=np.sqrt(2. / (n_in*windowSize + n_out)), size = w_shp), dtype=theano.config.floatX )
else:
W_values = np.asarray(
rng.uniform(low = - np.sqrt(6. / (n_in*windowSize + n_out)), high = np.sqrt(6. / (n_in*windowSize + n_out)), size = w_shp),
dtype=theano.config.floatX
)
if activation == T.nnet.sigmoid:
W_values *= 4
self.W = theano.shared(value=W_values, name='ResConv1d_W', borrow=True)
b_shp = (n_out,)
self.b = theano.shared(np.asarray( rng.uniform(low=-.0, high=.0, size=b_shp), dtype=input.dtype), name ='ResConv1d_b', borrow=True)
# conv_out and conv_out_bias have shape (batch_size, n_out, 1, nCols)
if dilation > 1:
conv_out = T.nnet.conv2d(in4conv2D, self.W, filter_shape=w_shp, border_mode='half', filter_dilation=(1, dilation) )
else:
conv_out = T.nnet.conv2d(in4conv2D, self.W, filter_shape=w_shp, border_mode='half')
if activation is not None:
conv_out_bias = activation(conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
else:
conv_out_bias = (conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
## out2 has shape (batchSize, n_out, nCols)
out2 = conv_out_bias.dimshuffle(0, 1, 3, 2)[:, :, :, 0]
if mask is not None:
## Zeros are padded at left columns of the input tensor, we need to reset these positions to 0 again after convolution to avoid noise
## mask has shape (batchSize, #positions_to_be_masked)
##take the subtensor of out2 that needs modification
out2_sub = out2[:, :, :mask.shape[1] ]
mask_new = mask.dimshuffle(0, 'x', 1)
self.output = T.set_subtensor(out2_sub, T.mul(out2_sub, mask_new) )
else:
self.output = out2
##self.output has shape (batchSize, n_out, nCols)
# parameters of the model
self.params=[self.W, self.b]
self.paramL1 = abs(self.W).sum() + abs(self.b).sum()
self.paramL2 = (self.W**2).sum() + (self.b**2).sum()
## 2D convolution on a matrix, no pooling is done here
class ResConv2DLayer:
def __init__(self, rng, input, n_in=0, n_out=0, halfWinSize=0,
dilation=1, activation=T.nnet.relu, mask=None):
## input has shape (batchSize, n_in, nRows, nCols) where n_in is the number of input features at each element
## here we assume that input[0], input[1], ...., input[batchSize -1 ] are aligned at the right bottom
## nrows and ncols are unknown
## mask has shape (batchSize, #rows_to_be_masked, nCols), mask is a binary matrix with 0 indicates padding positions and 1 real data position
##halfWinSize is the half window size of the filter shape
##Mask is used to reduce noise introduced by padding. all the matrices in a batch shall be aligned at the right bottom corner
##output shall have shape (batchSize, n_out, nRows, nCols)
self.input = input
self.n_in = n_in
self.halfWinSize = halfWinSize
self.mask = mask
##the window size of our filters is always odd
wSize = 2*halfWinSize + 1
self.filter_size = wSize
self.dilation = (dilation, dilation)
assert n_out >= 0
assert n_in >= 0
W_shape = (n_out, n_in, wSize, wSize)
if activation == T.nnet.relu:
W_values = np.asarray( rng.normal( scale = np.sqrt(2. / (n_in * wSize * wSize + n_out )), size = W_shape), dtype = theano.config.floatX)
else:
W_values = np.asarray(
rng.uniform( low = - np.sqrt(6. / (n_in * wSize * wSize + n_out )), high = np.sqrt(6. / (n_in * wSize * wSize + n_out )), size = W_shape),
dtype = theano.config.floatX
)
if activation == T.nnet.sigmoid:
W_values *= 4
W = theano.shared (value = W_values, name = 'ResConv2dlayer_W', borrow = True)
b_shape = (n_out, )
b_values = np.asarray (rng.uniform(low = -.0, high =.0, size = b_shape), dtype=theano.config.floatX )
b = theano.shared (value = b_values, name = 'ResConv2dlayer_b', borrow = True)
## conv2d_out and out2 have shape (batchSize, n_out, nRows, nCols)
if dilation > 1:
conv2d_out = T.nnet.conv2d(input, W, filter_shape=W_shape, border_mode='half', filter_dilation=(dilation, dilation) )
else:
conv2d_out = T.nnet.conv2d(input, W, filter_shape=W_shape, border_mode='half')
if activation is not None:
out2 = activation(conv2d_out + b.dimshuffle('x', 0, 'x', 'x'))
else:
out2 = conv2d_out + b.dimshuffle('x', 0, 'x', 'x')
if mask is not None:
## mask has shape (batchSize, #rows_to_be_masked, nCols)
## a subtensor of out2 along the horiz direction
out2_sub_horiz = out2[:, :, :mask.shape[1], :]
mask_horiz = mask.dimshuffle(0, 'x', 1, 2)
out3 = T.set_subtensor(out2_sub_horiz, T.mul(out2_sub_horiz, mask_horiz) )
## a subtensor of out3 along the vertical direction
out3_sub_vertical = out3[:, :, :, :mask.shape[1] ]
mask_vertical = mask.dimshuffle(0, 'x', 2, 1)
out4 = T.set_subtensor(out3_sub_vertical, T.mul(out3_sub_vertical, mask_vertical) )
self.output = out4
else:
self.output = out2
self.params = [W, b]
self.paramL1 = (abs(W).sum() + abs(b).sum())
self.paramL2 = ((W**2).sum() + (b**2).sum())
self.n_out = n_out
def batch_norm(x, n_in, mask=None, eps=1e-6):
## This batch_norm calculates the mean and standard deviation by excluding the impact of masked positions in the input
## x is the input and shall have shape (batchSize, n_in, nCols) or (batchSize, n_in, nRows, nCols)
## mask shall have shape (batchSize, #cols_to_be_masked) or (batchSize, #rows_to_be_masked, nCols)
## Assume that all the entries in x to be masked have already been set to 0
gamma = theano.shared(np.asarray(np.ones((n_in,)), dtype=theano.config.floatX), borrow=True)
bias = theano.shared(np.asarray(np.zeros((n_in,)), dtype=theano.config.floatX), borrow=True)
if mask is not None:
if x.ndim == 4:
x_sum = x.sum(axis=[0,2,3], keepdims=True)
## x_num is the number of effective elements in x by excluding the positions to be masked
x_num = mask.sum(dtype=theano.config.floatX) * 2 + T.cast( x.shape[0] * (x.shape[2] - mask.shape[1]) * (x.shape[3] - mask.shape[1]), theano.config.floatX)
## remove repeated count, the 1s in mask[:, :mask.shape[1], :mask.shape[1]] are counted twice, so we need to do adjustment
x_num = x_num - mask[:, :mask.shape[1], :mask.shape[1]].sum(dtype=theano.config.floatX)
x_mean = x_sum / x_num
##calculate the average of x^2
x_sqr = T.sqr(x)
x_sq_sum = x_sqr.sum(axis=[0,2,3], keepdims=True)
x_sq_mean = x_sq_sum / x_num
##calculate the standard devition, eps makes sure that sqrt() will not produce NaN
x_std = (x_sq_mean - T.sqr(x_mean) + eps ).sqrt()
y_temp = T.nnet.bn.batch_normalization(x, gamma[None,:,None,None], bias[None,:,None,None], x_mean, x_std, mode = "low_mem")
## reset the zero-padded entries to 0 along the horizonal direction
y_temp_sub_horiz = y_temp[:, :, :mask.shape[1], :]
mask_horiz = mask.dimshuffle(0, 'x', 1, 2)
y_temp2 = T.set_subtensor(y_temp_sub_horiz, T.mul(y_temp_sub_horiz, mask_horiz) )
## reset the zero-padded entries to 0 along the vertical direction
y_temp_sub_vertical = y_temp2[:, :, :, :mask.shape[1] ]
mask_vertical = mask.dimshuffle(0, 'x', 2, 1)
y = T.set_subtensor(y_temp_sub_vertical, T.mul(y_temp_sub_vertical, mask_vertical) )
elif x.ndim == 3:
x_sum = x.sum(axis=[0,2], keepdims=True)
x_num = mask.sum(dtype=theano.config.floatX) + T.cast( x.shape[0] * (x.shape[2] - mask.shape[1]), theano.config.floatX)
x_mean = x_sum / x_num
x_sq_sum = T.sqr(x).sum(axis=[0,2], keepdims=True)
x_sq_mean = x_sq_sum / x_num
x_std = (x_sq_mean - T.sqr(x_mean) + eps ).sqrt()
y_intermediate = T.nnet.bn.batch_normalization(x, gamma[None,:,None], bias[None,:,None], x_mean, x_std, mode = "low_mem")
## reset the zero-padded entries to 0 again
y_intermediate_sub = y_intermediate[:, :, :mask.shape[1] ]
mask_new = mask.dimshuffle(0, 'x', 1)
y = T.set_subtensor(y_intermediate_sub, T.mul(y_intermediate_sub, mask_new) )
else:
print('the ndim of input for batch_norm can only be 3 or 4!')
sys.exit(-1)
return y, [gamma, bias]
##the below code will be excuted when mask is None
if x.ndim == 4:
x_mean = x.mean(axis=[0,2,3], keepdims=True)
x_std = (x.var(axis=[0,2,3], keepdims=True) + eps ).sqrt()
y = T.nnet.bn.batch_normalization(x, gamma[None,:,None,None], bias[None,:,None,None], x_mean, x_std, mode = "low_mem")
elif x.ndim == 3:
x_mean = x.mean(axis=[0,2], keepdims=True)
x_std = (x.var(axis=[0,2], keepdims=True) + eps ).sqrt()
y = T.nnet.bn.batch_normalization(x, gamma[None,:,None], bias[None,:,None], x_mean, x_std, mode = "low_mem")
else:
print('the ndim of input for batch_norm can only be 3 or 4!')
sys.exit(-1)
return y, [gamma, bias]
class BatchNormLayer:
def __init__(self, input, n_in, mask=None):
self.input = input
self.n_in = n_in
bnout, bnparams = batch_norm(input, n_in, mask=mask)
self.output = bnout
self.params = bnparams
self.n_out = n_in
self.paramL1 = abs(bnparams[0]).sum() + abs(bnparams[1]).sum()
self.paramL2 = (bnparams[0]**2).sum() + (bnparams[1]**2).sum()
def TestBatchNorm3D():
n_in = 20
maxSeqLen = 200
minSeqLen = 190
batchSize = 3
matrices = []
container = np.zeros( (batchSize, n_in, maxSeqLen), dtype=np.float32 )
masks = np.zeros( (batchSize, maxSeqLen - minSeqLen), dtype=np.float32 )
for i in xrange(batchSize):
nRows = np.random.randint(minSeqLen, maxSeqLen)
nCols = nRows
a = np.random.uniform(0, 2, (n_in, nCols))
matrices.append(a)
container[i, :, maxSeqLen - nCols: ] = a
masks[i, maxSeqLen-nCols: ].fill(1)
## calculate the mean of all the matrices
b = np.zeros((n_in, 1))
b2 = np.zeros((n_in, 1))
numElements = 0
for a in matrices:
b = b + np.sum(a, axis=1, keepdims=True)
b2 = b2 + np.sum( np.square(a), axis=1, keepdims=True)
numElements = numElements + a.shape[1]
m = b/numElements
m2 = b2/numElements
std = np.sqrt(m2 - m*m)
newmatrices = []
for a in matrices:
b = (a - m ) /std
newmatrices.append(b)
x = T.tensor3('x')
mask = T.matrix('mask')
y, [gamma, bias] = batch_norm(x, n_in, mask)
f = theano.function([x, mask], y)
c = f(container, masks)
for i in xrange(batchSize):
a = newmatrices[i]
c_sub = c[i][:, c[i].shape[1]-a.shape[1]: ]
c[i][:, c[i].shape[1]-a.shape[1]: ] = c_sub - a
print(np.absolute(c[i]).max())
def TestBatchNorm4D():
n_in = 20
maxSeqLen = 200
minSeqLen = 190
batchSize = 5
matrices = []
container = np.zeros( (batchSize, n_in, maxSeqLen, maxSeqLen), dtype=np.float32 )
masks = np.zeros( (batchSize, maxSeqLen - minSeqLen, maxSeqLen), dtype=np.float32 )
for i in xrange(batchSize):
nRows = np.random.randint(minSeqLen, maxSeqLen)
nCols = nRows
a = np.random.uniform(0, 6, (n_in, nRows, nCols))
matrices.append(a)
container[i, :, maxSeqLen - nRows:, maxSeqLen - nCols: ] = a
masks[i, maxSeqLen-nRows:, maxSeqLen - nRows: ].fill(1)
## calculate the mean of all the matrices
b = np.zeros((n_in, 1, 1))
b2 = np.zeros((n_in, 1, 1))
numElements = 0
for a in matrices:
b = b + np.sum(a, axis=(1, 2), keepdims=True)
b2 = b2 + np.sum( np.square(a), axis=(1, 2), keepdims=True)
numElements = numElements + a.shape[1]*a.shape[2]
m = b/numElements
m2 = b2/numElements
std = np.sqrt(m2 - m*m + 1e-6)
newmatrices = []
for a in matrices:
b = (a - m ) /std
newmatrices.append(b)
x = T.tensor4('x')
mask = T.tensor3('mask')
y, [gamma, bias] = batch_norm(x, n_in, mask)
f = theano.function([x, mask], y)
c = f(container, masks)
for i in xrange(batchSize):
a = newmatrices[i]
#print a
c_sub = c[i][:, c[i].shape[1]-a.shape[1]:, c[i].shape[2]-a.shape[2]: ]
#print c[i]
c[i][:, c[i].shape[1]-a.shape[1]:, c[i].shape[2]-a.shape[2]: ] = c_sub - a
print(np.absolute(c[i]).max())
#print c[i]
class BottleneckBlock:
def __init__(self, rng, input, n_in, halfWinSize=1, mask=None,
n_out=None, n_bottleneck=None, activation=T.tanh,
dim_inc_method='partial_projection', batchNorm=False):
## please make sure that the input has shape (batchSize, n_in, nRows, nCols) or (batchSize, n_in, nCols)
##if n_out is not None, then it shall be no smaller than n_in
if n_out is not None:
assert n_out >= n_in
self.n_out = n_out
else:
self.n_out = n_in
if n_bottleneck is not None:
assert n_bottleneck < n_in
self.n_bottleneck = n_bottleneck
else:
self.n_bottleneck = n_in / 2
self.n_in = n_in
self.input = input
self.mask = mask
self.halfWinSize = halfWinSize
if input.ndim == 3:
ConvLayer = ResConv1DLayer
elif input.ndim == 4:
ConvLayer = ResConv2DLayer
else:
print('the ndim of input can only be 3 or 4')
sys.exit(-1)
if batchNorm:
l1_pre = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_bottleneck, halfWinSize=0, mask=mask, activation=activation)
l1 = BatchNormLayer(l1_pre.output, l1_pre.n_out)
l2_pre = ConvLayer(rng, input=l1.output, n_in=self.n_bottleneck, n_out=self.n_bottleneck, halfWinSize=halfWinSize, mask=mask, activation=activation)
l2 = BatchNormLayer(l2_pre.output, l2_pre.n_out)
l3_pre = ConvLayer(rng, input=l2.output, n_in=self.n_bottleneck, n_out=self.n_out, halfWinSize=0, mask=mask, activation=None)
l3 = BatchNormLayer(l3_pre.output, l3_pre.n_out)
self.layers = [l1_pre, l1, l2_pre, l2, l3_pre, l3]
else:
## if needed, we do dimension increase at the first layer
l1 = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_bottleneck, halfWinSize=0, mask=mask, activation=activation)
l2 = ConvLayer(rng, input=l1.output, n_in=self.n_bottleneck, n_out=self.n_bottleneck, halfWinSize=halfWinSize, mask=mask, activation=activation)
l3 = ConvLayer(rng, input=l2.output, n_in=self.n_bottleneck, n_out=self.n_out, halfWinSize=0, mask=mask, activation=None)
self.layers = [l1, l2, l3]
## intermediate has shape (batchSize, n_out, nRows, nCols) or (batchSize, n_out, nCols)
intermediate = l3.output
if dim_inc_method == 'full_projection':
## we do 1*1 convolution here without any nonlinear transformation
linlayer = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out, halfWinSize=0, mask=mask, activation=None)
intermediate = intermediate + linlayer.output
self.layers.append(linlayer)
#print 'projection is True'
elif dim_inc_method == 'identity':
if input.ndim == 3:
intermediate = T.inc_subtensor(intermediate[:, :n_in, :], input)
elif input.ndim == 4:
intermediate = T.inc_subtensor(intermediate[:, :n_in, :, :], input)
else:
print('the ndim of input can only be 3 or 4')
sys.exit(-1)
print('only projection is supported')
sys.exit(-1)
elif dim_inc_method == 'partial_projection':
if self.n_out == n_in:
intermediate = intermediate + input
else:
if batchNorm:
linlayer_pre = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out - n_in, halfWinSize=0, mask=mask, activation=None)
linlayer = BatchNormLayer(linlayer_pre.output, linlayer_pre.n_out)
self.layers += [linlayer_pre, linlayer]
else:
linlayer = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out - n_in, halfWinSize=0, mask=mask, activation=None)
self.layers.append(linlayer)
intermediate = intermediate + T.concatenate([input, linlayer.output], axis=1)
else:
print('unsupported dimension increase method: ', dim_inc_method)
sys.exit(-1)
if activation is not None:
self.output = activation(intermediate)
else:
self.output = intermediate
## self.output has shape (batchSize, n_out, nRows, nCols)
self.params = []
self.paramL1 = 0
self.paramL2 = 0
for layer in self.layers:
self.params += layer.params
self.paramL1 += layer.paramL1
self.paramL2 += layer.paramL2
class ResBlockV2:
def __init__(self, rng, input, n_in, halfWinSize=0, mask=None,
n_out=None, activation=T.nnet.relu, dim_inc_method='partial_projection',
batchNorm=False, dropout=False):
## please make sure that the input has shape (batchSize, n_in, nRows, nCols) or (batchSize, n_in, nCols)
##if n_out is not None, then it shall be no smaller than n_in
if n_out is not None:
assert n_out >= n_in
self.n_out = n_out
else:
self.n_out = n_in
self.n_in = n_in
self.input = input
self.mask = mask
self.halfWinSize = halfWinSize
if input.ndim == 3:
ConvLayer = ResConv1DLayer
elif input.ndim == 4:
ConvLayer = ResConv2DLayer
else:
print ('the ndim of input can only be 3 or 4')
sys.exit(-1)
if batchNorm:
bnlayer1 = BatchNormLayer(input, n_in, mask=mask)
input1 = activation(input)
l1 = ConvLayer(rng, input=input1, n_in=n_in, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
bnlayer2 = BatchNormLayer(l1.output, l1.n_out, mask=mask)
input2 = activation(bnlayer2.output)
## add dropout code here
l2 = ConvLayer(rng, input=input2, n_in=l1.n_out, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
self.layers = [bnlayer1, l1, bnlayer2, l2]
#self.layers = [ l1, bnlayer2, l2]
else:
input1 = activation(input)
l1 = ConvLayer(rng, input=input1, n_in=n_in, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
input2 = activation(l1.output)
## add dropout code here
l2 = ConvLayer(rng, input=input2, n_in=l1.n_out, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
self.layers = [l1, l2]
## intermediate has shape (batchSize, n_out, nRows, nCols) or (batchSize, n_out, nCols)
intermediate = l2.output
if dim_inc_method == 'full_projection':
## we do 1*1 convolution here without any nonlinear transformation
linlayer = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out, halfWinSize=0, mask=mask, activation=None)
intermediate = intermediate + linlayer.output
self.layers.append(linlayer)
#print 'projection is True'
elif dim_inc_method == 'identity':
if input.ndim == 3:
intermediate = T.inc_subtensor(intermediate[:, :n_in, :], input)
elif input.ndim == 4:
intermediate = T.inc_subtensor(intermediate[:, :n_in, :, :], input)
else:
print('the ndim of input can only be 3 or 4')
sys.exit(-1)
print('only projection is supported')
sys.exit(-1)
elif dim_inc_method == 'partial_projection':
if self.n_out == n_in:
intermediate = intermediate + input
else:
linlayer = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out - n_in, halfWinSize=0, mask=mask, activation=None)
self.layers.append(linlayer)
intermediate = intermediate + T.concatenate([input, linlayer.output], axis=1)
else:
print('unsupported dimension increase method: ', dim_inc_method)
sys.exit(-1)
self.output = intermediate
## self.output has shape (batchSize, n_out, nRows, nCols)
self.params = []
self.paramL1 = 0
self.paramL2 = 0
for layer in self.layers:
self.params += layer.params
self.paramL1 += layer.paramL1
self.paramL2 += layer.paramL2
## ResBlockV23 is almost same as ResBlockV2 except that the former has removed unused batchNormLayers and accordingly model parameters
class ResBlockV23:
def __init__(self, rng, input, n_in, halfWinSize=0, mask=None,
n_out=None, activation=T.nnet.relu, dim_inc_method='partial_projection',
batchNorm=False, dropout=False):
## please make sure that the input has shape (batchSize, n_in, nRows, nCols) or (batchSize, n_in, nCols)
##if n_out is not None, then it shall be no smaller than n_in
if n_out is not None:
assert n_out >= n_in
self.n_out = n_out
else:
self.n_out = n_in
self.n_in = n_in
self.input = input
self.mask = mask
self.halfWinSize = halfWinSize
if input.ndim == 3:
ConvLayer = ResConv1DLayer
elif input.ndim == 4:
ConvLayer = ResConv2DLayer
else:
print('the ndim of input can only be 3 or 4')
sys.exit(-1)
if batchNorm:
input1 = activation(input)
l1 = ConvLayer(rng, input=input1, n_in=n_in, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
bnlayer2 = BatchNormLayer(l1.output, l1.n_out, mask=mask)
input2 = activation(bnlayer2.output)
## add dropout code here
l2 = ConvLayer(rng, input=input2, n_in=l1.n_out, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
##self.layers = [bnlayer1, l1, bnlayer2, l2]
self.layers = [ l1, bnlayer2, l2]
else:
input1 = activation(input)
l1 = ConvLayer(rng, input=input1, n_in=n_in, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
input2 = activation(l1.output)
## add dropout code here
l2 = ConvLayer(rng, input=input2, n_in=l1.n_out, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
self.layers = [l1, l2]
## intermediate has shape (batchSize, n_out, nRows, nCols) or (batchSize, n_out, nCols)
intermediate = l2.output
if dim_inc_method == 'full_projection':
## we do 1*1 convolution here without any nonlinear transformation
linlayer = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out, halfWinSize=0, mask=mask, activation=None)
intermediate = intermediate + linlayer.output
self.layers.append(linlayer)
#print 'projection is True'
elif dim_inc_method == 'identity':
if input.ndim == 3:
intermediate = T.inc_subtensor(intermediate[:, :n_in, :], input)
elif input.ndim == 4:
intermediate = T.inc_subtensor(intermediate[:, :n_in, :, :], input)
else:
print('the ndim of input can only be 3 or 4')
sys.exit(-1)
print ('only projection is supported')
sys.exit(-1)
elif dim_inc_method == 'partial_projection':
if self.n_out == n_in:
intermediate = intermediate + input
else:
linlayer = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out - n_in, halfWinSize=0, mask=mask, activation=None)
self.layers.append(linlayer)
intermediate = intermediate + T.concatenate([input, linlayer.output], axis=1)
else:
print('unsupported dimension increase method: ', dim_inc_method)
exit(-1)
self.output = intermediate
## self.output has shape (batchSize, n_out, nRows, nCols)
self.params = []
self.paramL1 = 0
self.paramL2 = 0
for layer in self.layers:
self.params += layer.params
self.paramL1 += layer.paramL1
self.paramL2 += layer.paramL2
## ResBlockV22 has two batch normalization layers in each residual block while ResBlockV2 and ResBlockV23 have only one
class ResBlockV22:
def __init__(self, rng, input, n_in, halfWinSize=0, mask=None, n_out=None, activation=T.nnet.relu, dim_inc_method='partial_projection', batchNorm=False, dropout=False):
## please make sure that the input has shape (batchSize, n_in, nRows, nCols) or (batchSize, n_in, nCols)
##if n_out is not None, then it shall be no smaller than n_in
if n_out is not None:
assert n_out >= n_in
self.n_out = n_out
else:
self.n_out = n_in
self.n_in = n_in
self.input = input
self.mask = mask
self.halfWinSize = halfWinSize
if input.ndim == 3:
ConvLayer = ResConv1DLayer
elif input.ndim == 4:
ConvLayer = ResConv2DLayer
else:
print('the ndim of input can only be 3 or 4')
sys.exit(-1)
if batchNorm:
bnlayer1 = BatchNormLayer(input, n_in, mask=mask)
input1 = activation(bnlayer1.output)
l1 = ConvLayer(rng, input=input1, n_in=n_in, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
bnlayer2 = BatchNormLayer(l1.output, l1.n_out, mask=mask)
input2 = activation(bnlayer2.output)
## add dropout code here
l2 = ConvLayer(rng, input=input2, n_in=l1.n_out, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
self.layers = [bnlayer1, l1, bnlayer2, l2]
else:
input1 = activation(input)
l1 = ConvLayer(rng, input=input1, n_in=n_in, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
input2 = activation(l1.output)
## add dropout code here
l2 = ConvLayer(rng, input=input2, n_in=l1.n_out, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
self.layers = [l1, l2]
## intermediate has shape (batchSize, n_out, nRows, nCols) or (batchSize, n_out, nCols)
intermediate = l2.output
if dim_inc_method == 'full_projection':
## we do 1*1 convolution here without any nonlinear transformation
linlayer = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out, halfWinSize=0, mask=mask, activation=None)
intermediate = intermediate + linlayer.output
self.layers.append(linlayer)
#print 'projection is True'
elif dim_inc_method == 'identity':
if input.ndim == 3:
intermediate = T.inc_subtensor(intermediate[:, :n_in, :], input)
elif input.ndim == 4:
intermediate = T.inc_subtensor(intermediate[:, :n_in, :, :], input)
else:
print('the ndim of input can only be 3 or 4')
sys.exit(-1)
print('only projection is supported')
sys.exit(-1)
elif dim_inc_method == 'partial_projection':
if self.n_out == n_in:
intermediate = intermediate + input
else:
linlayer = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out - n_in, halfWinSize=0, mask=mask, activation=None)
self.layers.append(linlayer)
intermediate = intermediate + T.concatenate([input, linlayer.output], axis=1)
else:
print ('unsupported dimension increase method: ', dim_inc_method)
exit(-1)
self.output = intermediate
## self.output has shape (batchSize, n_out, nRows, nCols)
self.params = []
self.paramL1 = 0
self.paramL2 = 0
for layer in self.layers:
self.params += layer.params
self.paramL1 += layer.paramL1
self.paramL2 += layer.paramL2
class ResBlockV1:
def __init__(self, rng, input, n_in, halfWinSize=0, mask=None, n_out=None,
activation=T.tanh, dim_inc_method='partial_projection', batchNorm=False):
## please make sure that the input has shape (batchSize, n_in, nRows, nCols) or (batchSize, n_in, nCols)
##if n_out is not None, then it shall be no smaller than n_in
if n_out is not None:
assert n_out >= n_in
self.n_out = n_out
else:
self.n_out = n_in
self.n_in = n_in
self.input = input
self.mask = mask
self.halfWinSize = halfWinSize
if input.ndim == 3:
ConvLayer = ResConv1DLayer
elif input.ndim == 4:
ConvLayer = ResConv2DLayer
else:
print('the ndim of input can only be 3 or 4')
sys.exit(-1)
if batchNorm:
l1_pre = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=activation)
l1 = BatchNormLayer(l1_pre.output, l1_pre.n_out)
l2_pre = ConvLayer(rng, input=l1.output, n_in=self.n_out, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
l2 = BatchNormLayer(l2_pre.output, l2_pre.n_out)
self.layers = [l1_pre, l1, l2_pre, l2]
else:
## if needed, we do dimension increase at the first layer
l1 = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=activation)
l2 = ConvLayer(rng, input=l1.output, n_in=self.n_out, n_out=self.n_out, halfWinSize=halfWinSize, mask=mask, activation=None)
self.layers = [l1, l2]
## intermediate has shape (batchSize, n_out, nRows, nCols) or (batchSize, n_out, nCols)
intermediate = l2.output
if dim_inc_method == 'full_projection':
## we do 1*1 convolution here without any nonlinear transformation
linlayer = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out, halfWinSize=0, mask=mask, activation=None)
intermediate = intermediate + linlayer.output
self.layers.append(linlayer)
#print 'projection is True'
elif dim_inc_method == 'identity':
if input.ndim == 3:
intermediate = T.inc_subtensor(intermediate[:, :n_in, :], input)
elif input.ndim == 4:
intermediate = T.inc_subtensor(intermediate[:, :n_in, :, :], input)
else:
print('the ndim of input can only be 3 or 4')
sys.exit(-1)
print('only projection is supported')
sys.exit(-1)
elif dim_inc_method == 'partial_projection':
if self.n_out == n_in:
intermediate = intermediate + input
else:
if batchNorm:
linlayer_pre = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out - n_in, halfWinSize=0, mask=mask, activation=None)
linlayer = BatchNormLayer(linlayer_pre.output, linlayer_pre.n_out)
self.layers += [linlayer_pre, linlayer]
else:
linlayer = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out - n_in, halfWinSize=0, mask=mask, activation=None)
self.layers.append(linlayer)
intermediate = intermediate + T.concatenate([input, linlayer.output], axis=1)
else:
print('unsupported dimension increase method: ', dim_inc_method)
sys.exit(-1)
if activation is not None:
self.output = activation(intermediate)
else:
self.output = intermediate
## self.output has shape (batchSize, n_out, nRows, nCols)
self.params = []
self.paramL1 = 0
self.paramL2 = 0
for layer in self.layers:
self.params += layer.params
self.paramL1 += layer.paramL1
self.paramL2 += layer.paramL2
class DilatedResBlock:
def __init__(self, rng, input, n_in, halfWinSize=0, dilation=1, mask=None, n_out=None, activation=T.nnet.relu, dim_inc_method='partial_projection', batchNorm=False, dropout=False):
## please make sure that the input has shape (batchSize, n_in, nRows, nCols) or (batchSize, n_in, nCols)
##if n_out is not None, then it shall be no smaller than n_in
if n_out is not None:
assert n_out >= n_in
self.n_out = n_out
else:
self.n_out = n_in
self.n_in = n_in
self.input = input
self.mask = mask
self.halfWinSize = halfWinSize
self.dilation = dilation
if input.ndim == 3:
ConvLayer = ResConv1DLayer
elif input.ndim == 4:
ConvLayer = ResConv2DLayer
else:
print('the ndim of input can only be 3 or 4')
exit(-1)
if batchNorm:
input1 = activation(input)
l1 = ConvLayer(rng, input=input1, n_in=n_in, n_out=self.n_out, halfWinSize=halfWinSize, dilation=dilation, mask=mask, activation=None)
bnlayer2 = BatchNormLayer(l1.output, l1.n_out, mask=mask)
input2 = activation(bnlayer2.output)
## add dropout code here
l2 = ConvLayer(rng, input=input2, n_in=l1.n_out, n_out=self.n_out, halfWinSize=halfWinSize, dilation=dilation, mask=mask, activation=None)
##self.layers = [bnlayer1, l1, bnlayer2, l2]
self.layers = [ l1, bnlayer2, l2]
else:
input1 = activation(input)
l1 = ConvLayer(rng, input=input1, n_in=n_in, n_out=self.n_out, halfWinSize=halfWinSize, dilation=dilation, mask=mask, activation=None)
input2 = activation(l1.output)
## add dropout code here
l2 = ConvLayer(rng, input=input2, n_in=l1.n_out, n_out=self.n_out, halfWinSize=halfWinSize, dilation=dilation, mask=mask, activation=None)
self.layers = [l1, l2]
## intermediate has shape (batchSize, n_out, nRows, nCols) or (batchSize, n_out, nCols)
intermediate = l2.output
if dim_inc_method == 'full_projection':
## we do 1*1 convolution here without any nonlinear transformation
linlayer = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out, halfWinSize=0, mask=mask, activation=None)
intermediate = intermediate + linlayer.output
self.layers.append(linlayer)
#print 'projection is True'
elif dim_inc_method == 'identity':
if input.ndim == 3:
intermediate = T.inc_subtensor(intermediate[:, :n_in, :], input)
elif input.ndim == 4:
intermediate = T.inc_subtensor(intermediate[:, :n_in, :, :], input)
else:
print('the ndim of input can only be 3 or 4')
exit(-1)
print('only projection is supported')
exit(-1)
elif dim_inc_method == 'partial_projection':
if self.n_out == n_in:
intermediate = intermediate + input
else:
## we do 1*1 convolution here without any nonlinear transformation
linlayer = ConvLayer(rng, input=input, n_in=n_in, n_out=self.n_out - n_in, halfWinSize=0, mask=mask, activation=None)
self.layers.append(linlayer)
intermediate = intermediate + T.concatenate([input, linlayer.output], axis=1)
else:
print('unsupported dimension increase method: ', dim_inc_method)
exit(-1)
self.output = intermediate
## self.output has shape (batchSize, n_out, nRows, nCols)
self.params = []
self.paramL1 = 0
self.paramL2 = 0
for layer in self.layers:
self.params += layer.params
self.paramL1 += layer.paramL1
self.paramL2 += layer.paramL2
class DilatedResNet:
def __init__(self, rng, input, n_in, halfWinSize=[0], dilation=[1], mask=None, n_hiddens=None, n_repeats=None, activation=T.nnet.relu, dim_inc_method='partial_projection', batchNorm=True, version='DilatedResNet'):
"""
This DilatedResNet consists of the following components:
1) a start layer with input as input, output has n_hiddens[0] features
2) in total there are len(n_repeats) stacks
3) each stack has 1 + n_repeats[i] blocks
4) the first block of each stack has n_hiddens[i-1] or n_in input features and n_hiddens[i] output features
5) the other blocks of each stack has #inputfeatures = #outputfeatures = n_hiddens[i]
input has shape (batchSize, nRows, nCols, n_in) or (batchSize, nCols, n_in)
output has shape (batchSize, nRows, nCols, n_hiddens[-1]) or (batchSize, nCols, n_hiddens[-1])
n_hiddens needs to be an increasing sequence
n_repeats shall be non-negative
"""
assert n_hiddens is not None
assert n_repeats is not None
assert len(n_hiddens) > 0
assert len(n_hiddens) == len(n_repeats)
assert len(halfWinSize) == len(n_hiddens)
assert len(dilation) == len(n_hiddens)
assert version.startswith('DilatedResNet')
if input.ndim == 3:
ConvLayer = ResConv1DLayer
input2 = input.dimshuffle(0, 2, 1)
elif input.ndim == 4:
ConvLayer = ResConv2DLayer
input2 = input.dimshuffle(0, 3, 1, 2)
else:
print('the ndim of input can only be 3 or 4!')
exit(-1)
ResBlock = DilatedResBlock
blocks = []
startLayer = ConvLayer(rng, input=input2, n_in=n_in, n_out=n_hiddens[0], halfWinSize=halfWinSize[0], dilation=dilation[0], mask=mask, activation=activation)
blocks.append(startLayer)
## repeat a block n_repeats[i] times
curr_block = startLayer
for j in range(n_repeats[0]):
assert (curr_block.n_out == n_hiddens[0])
#bnflag =(batchNorm and j==0)
new_block = ResBlock(rng, input=curr_block.output, n_in=n_hiddens[0], mask=mask, halfWinSize=halfWinSize[0], dilation=dilation[0], activation=activation, dim_inc_method=dim_inc_method, batchNorm=batchNorm)
blocks.append(new_block)
curr_block = new_block
for i in range(1, len(n_hiddens)):
## the start block is in charge of dimension increase
assert (curr_block.n_out == n_hiddens[i-1])
new_block = ResBlock(rng, input=curr_block.output, n_in=n_hiddens[i-1], n_out=n_hiddens[i], mask=mask, halfWinSize=halfWinSize[i], dilation=dilation[i], activation=activation, dim_inc_method=dim_inc_method, batchNorm=batchNorm)
blocks.append(new_block)
curr_block = new_block
## repeat a block n_repeats[i] times
for j in range(n_repeats[i]):
assert (curr_block.n_out == n_hiddens[i])
new_block = ResBlock(rng, input=curr_block.output, n_in=n_hiddens[i], mask=mask, halfWinSize=halfWinSize[i], dilation=dilation[i], activation=activation, dim_inc_method=dim_inc_method, batchNorm=batchNorm)
blocks.append(new_block)
curr_block = new_block
out2 = curr_block.output
self.n_out = curr_block.n_out