forked from shirgur/PixelRNN
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Layers.py
728 lines (600 loc) · 29 KB
/
Layers.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
"""
A Keras implementation of PixelRNN by Van Den Oord et al. Google
arxiv.org/pdf/1601.06759
Copyright Shir Gur, 2016
"""
import numpy as np
import theano
from theano import tensor as T
from keras.engine import Layer, InputSpec
from keras import backend as K
from keras.backend.common import _FLOATX
from keras import activations, initializations, regularizers, constraints
import keras.layers.convolutional as K_conv
import Utils
class ColRecurrent(Layer):
def __init__(self, weights=None, reverse=False,
return_sequences=False, go_backwards=False, stateful=False,
nb_filter=None, filter_length=None,
unroll=False, consume_less='cpu',
input_dim=None, input_length=None, **kwargs):
self.return_sequences = return_sequences
self.initial_weights = weights
self.go_backwards = go_backwards
self.stateful = stateful
self.unroll = unroll
self.consume_less = consume_less
self.reverse = reverse
self.nb_filter = nb_filter
self.filter_length = filter_length
self.supports_masking = True
self.input_spec = [InputSpec(ndim=4)]
self.input_dim = input_dim
self.input_length = input_length
if self.input_dim:
kwargs['input_shape'] = (self.input_length, self.input_dim)
super(ColRecurrent, self).__init__(**kwargs)
def get_output_shape_for(self, input_shape):
if self.return_sequences:
return (input_shape[0], input_shape[1], self.nb_filter, input_shape[3])
else:
return (input_shape[0], self.nb_filter, input_shape[3])
def compute_mask(self, input, mask):
if self.return_sequences:
return mask
else:
return None
def step(self, x, states):
raise NotImplementedError
def get_constants(self, x):
return []
def get_initial_states(self, x):
init_h = self.init_h.dimshuffle(('x', 0, 1))
init_h = T.extra_ops.repeat(init_h, x.shape[0], axis=0)
init_c = self.init_c.dimshuffle(('x', 0, 1))
init_c = T.extra_ops.repeat(init_c, x.shape[0], axis=0)
return [init_h, init_c]
def preprocess_input(self, x):
return x
def call(self, x, mask=None):
# input shape: (nb_samples, time (padded with zeros), input_dim)
# note that the .build() method of subclasses MUST define
# self.input_spec with a complete input shape.
input_shape = self.input_spec[0].shape
if K._BACKEND == 'tensorflow':
if not input_shape[1]:
raise Exception('When using TensorFlow, you should define '
'explicitly the number of timesteps of '
'your sequences.\n'
'If your first layer is an Embedding, '
'make sure to pass it an "input_length" '
'argument. Otherwise, make sure '
'the first layer has '
'an "input_shape" or "batch_input_shape" '
'argument, including the time axis. '
'Found input shape at layer ' + self.name +
': ' + str(input_shape))
if self.stateful:
initial_states = self.states
else:
initial_states = self.get_initial_states(x)
constants = self.get_constants(x)
preprocessed_input = self.preprocess_input(x)
last_output, outputs, states = K.rnn(self.step, preprocessed_input,
initial_states,
go_backwards=self.go_backwards,
mask=mask,
constants=constants,
unroll=self.unroll,
input_length=input_shape[1])
if self.stateful:
self.updates = []
for i in range(len(states)):
self.updates.append((self.states[i], states[i]))
if self.return_sequences:
return outputs
else:
return last_output
def get_config(self):
config = {'return_sequences': self.return_sequences,
'go_backwards': self.go_backwards,
'stateful': self.stateful,
'unroll': self.unroll,
'consume_less': self.consume_less}
if self.stateful:
config['batch_input_shape'] = self.input_spec[0].shape
else:
config['input_dim'] = self.input_dim
config['input_length'] = self.input_length
base_config = super(ColRecurrent, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class PyramidSTM(ColRecurrent):
def __init__(self, nb_filter, filter_length, direction='Down',
init='glorot_uniform', inner_init='orthogonal',
forget_bias_init='one', activation='tanh',
inner_activation='hard_sigmoid',
border_mode="same", sub_sample=(1, 1),
W_regularizer=None, U_regularizer=None, b_regularizer=None,
dropout_W=0., dropout_U=0., **kwargs):
self.nb_filter = nb_filter
self.filter_length = filter_length
self.border_mode = border_mode
self.subsample = sub_sample
self.direction = direction
self.init = initializations.get(init)
self.inner_init = initializations.get(inner_init)
self.forget_bias_init = initializations.get(forget_bias_init)
self.activation = activations.get(activation)
self.inner_activation = activations.get(inner_activation)
self.W_regularizer = regularizers.get(W_regularizer)
self.U_regularizer = regularizers.get(U_regularizer)
self.b_regularizer = regularizers.get(b_regularizer)
self.dropout_W, self.dropout_U = dropout_W, dropout_U
kwargs["nb_filter"] = nb_filter
kwargs["filter_length"] = filter_length
if self.dropout_W or self.dropout_U:
self.uses_learning_phase = True
super(PyramidSTM, self).__init__(**kwargs)
def call(self, x, mask=None):
if self.direction == 'Down':
X = K.permute_dimensions(x, (0, 2, 1, 3))
elif self.direction == 'Right':
X = K.permute_dimensions(x, (0, 3, 1, 2))
else:
raise Exception('ERROR: Unknown direction')
if self.direction == 'Down':
return K.permute_dimensions(super(PyramidSTM, self).call(X, mask), (0, 2, 1, 3))
elif self.direction == 'Right':
return K.permute_dimensions(super(PyramidSTM, self).call(X, mask), (0, 2, 3, 1))
else:
raise Exception('ERROR: Unknown direction')
def get_output_shape_for(self, input_shape):
if self.direction == 'Down':
dim_out = 3
elif self.direction == 'Right':
dim_out = 2
else:
raise Exception('ERROR: Unknown direction')
if self.return_sequences:
return (input_shape[0], self.nb_filter, input_shape[2], input_shape[3])
else:
return (input_shape[0], self.nb_filter, input_shape[dim_out])
def build(self, input_shape):
# Input shape :: (samples, channels, height, width)
self.input_spec = [InputSpec(shape=input_shape)]
if self.direction == 'Down':
dims = self.input_spec[0].shape
self.shuffeled_dims = (dims[0], dims[2], dims[1], dims[3])
elif self.direction == 'Right':
dims = self.input_spec[0].shape
self.shuffeled_dims = (dims[0], dims[3], dims[1], dims[2])
else:
raise Exception('ERROR: Unknown direction')
input_dim = self.shuffeled_dims[2]
self.input_dim = input_dim
self.Shape = (4*self.nb_filter, input_dim, 1, 1)
self.Shape1 = (4*self.nb_filter, self.nb_filter, 3, 1)
self.Shape2 = (self.nb_filter, self.shuffeled_dims[3])
self.W_iof = self.init(self.Shape)
self.U_iof = self.init(self.Shape1)
self.b_iof = K.zeros((4*self.nb_filter,))
self.init_h = K.zeros(self.Shape2)
self.init_c = K.zeros(self.Shape2)
if self.stateful:
self.reset_states()
else:
self.states = [None, None]
self.regularizers = []
if self.W_regularizer:
self.W_regularizer.set_param(self.W_iof)
self.regularizers.append(self.W_regularizer)
if self.U_regularizer:
self.U_regularizer.set_param(self.U_iof)
self.regularizers.append(self.U_regularizer)
if self.b_regularizer:
self.b_regularizer.set_param(self.b_iof)
self.regularizers.append(self.b_regularizer)
self.trainable_weights = [self.W_iof, self.U_iof, self.b_iof,
self.init_h, self.init_c]
if self.initial_weights is not None:
self.set_weights(self.initial_weights)
del self.initial_weights
def reset_states(self):
assert self.stateful, 'Layer must be stateful.'
def conv_step(self, x, W, b=None, border_mode="valid", filter_shape=None, mask_type='b'):
mask = np.ones(filter_shape, dtype=_FLOATX)
in_third = self.input_dim//3
out_third = self.nb_filter//3
mask[:out_third,in_third:,0,0] = 0
mask[out_third:2*out_third,2*in_third:,0,0] = 0
W = W * mask
input_shape = self.shuffeled_dims
x = K.expand_dims(x, -1) # add a dimension of the right
conv_out = T.nnet.conv2d(x, W, subsample=self.subsample,
border_mode='half',
filter_flip=False,
input_shape=(input_shape[0],
input_shape[2],
input_shape[3],
1),
filter_shape=filter_shape)
if b:
conv_out = conv_out + K.reshape(b, (1, filter_shape[0], 1, 1))
conv_out = K.squeeze(conv_out, 3) # remove the dummy 3rd dimension
return conv_out
def conv_step_hidden(self, x, W, border_mode="valid", filters=None, filter_shape=None):
input_shape = self.shuffeled_dims
if filters == None:
filters = self.nb_filter
x = K.expand_dims(x, -1) # add a dimension of the right
conv_out = T.nnet.conv2d(x, W, subsample=(1, 1),
border_mode='half',
filter_flip=False,
input_shape=(input_shape[0],
filters,
input_shape[3],
1),
filter_shape=filter_shape)
conv_out = K.squeeze(conv_out, 3) # remove the dummy 3rd dimension
return conv_out
def step(self, x, states):
h_tm1 = states[0]
c_tm1 = states[1]
input_to_state = self.conv_step(x, self.W_iof, self.b_iof, border_mode=self.border_mode, filter_shape=self.Shape)
state_to_state = self.conv_step_hidden(h_tm1, self.U_iof, border_mode="same", filters=4*self.nb_filter, filter_shape=self.Shape1)
gates = input_to_state + state_to_state
o_f_i =self.inner_activation(gates[:,:3*self.nb_filter,:])
o = o_f_i[:,0*self.nb_filter:1*self.nb_filter,:]
f = o_f_i[:,1*self.nb_filter:2*self.nb_filter,:]
i = o_f_i[:,2*self.nb_filter:3*self.nb_filter,:]
g = self.activation(gates[:,3*self.nb_filter:4*self.nb_filter,:])
c = (f * c_tm1) + (i * g)
h = o * self.activation(c)
return h, [h ,c]
def get_config(self):
config = {"output_dim": self.output_dim,
"init": self.init.__name__,
"inner_init": self.inner_init.__name__,
"forget_bias_init": self.forget_bias_init.__name__,
"activation": self.activation.__name__,
"inner_activation": self.inner_activation.__name__,
"W_regularizer": self.W_regularizer.get_config() if self.W_regularizer else None,
"U_regularizer": self.U_regularizer.get_config() if self.U_regularizer else None,
"b_regularizer": self.b_regularizer.get_config() if self.b_regularizer else None,
"dropout_W": self.dropout_W,
"dropout_U": self.dropout_U}
base_config = super(PyramidSTM, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class DiagLSTM(ColRecurrent):
def __init__(self, nb_filter, filter_length, direction='Down',
init='glorot_uniform', inner_init='orthogonal',
forget_bias_init='one', activation='tanh',
inner_activation='hard_sigmoid',
border_mode="same", sub_sample=(1, 1),
W_regularizer=None, U_regularizer=None, b_regularizer=None,
dropout_W=0., dropout_U=0., **kwargs):
self.nb_filter = nb_filter
self.filter_length = filter_length
self.border_mode = border_mode
self.subsample = sub_sample
self.direction = direction
self.init = initializations.get(init)
self.inner_init = initializations.get(inner_init)
self.forget_bias_init = initializations.get(forget_bias_init)
self.activation = activations.get(activation)
self.inner_activation = activations.get(inner_activation)
self.W_regularizer = regularizers.get(W_regularizer)
self.U_regularizer = regularizers.get(U_regularizer)
self.b_regularizer = regularizers.get(b_regularizer)
self.dropout_W, self.dropout_U = dropout_W, dropout_U
kwargs["nb_filter"] = nb_filter
kwargs["filter_length"] = filter_length
if self.dropout_W or self.dropout_U:
self.uses_learning_phase = True
super(DiagLSTM, self).__init__(**kwargs)
def call(self, x, mask=None):
if self.direction == 'Down':
X = K.permute_dimensions(x, (0, 3, 1, 2))
elif self.direction == 'Right':
X = K.permute_dimensions(x, (0, 2, 1, 3))
else:
raise Exception('ERROR: Unknown direction')
if self.stateful:
super(DiagLSTM, self).call(X, mask)
else:
if self.reverse:
X = X[:,::-1,:,:]
X = Utils.Skew(X)
res = super(DiagLSTM, self).call(X, mask)
unskew = Utils.Unskew(res)
if self.reverse:
unskew = unskew[:,::-1,:,:]
if self.direction == 'Down':
return K.permute_dimensions(unskew, (0, 2, 3, 1))
elif self.direction == 'Right':
return K.permute_dimensions(unskew, (0, 2, 1, 3))
else:
raise Exception('ERROR: Unknown direction')
def get_output_shape_for(self, input_shape):
if self.direction == 'Down':
dim_out = 2
elif self.direction == 'Right':
dim_out = 3
else:
raise Exception('ERROR: Unknown direction')
if self.return_sequences:
return (input_shape[0], self.nb_filter, input_shape[2], input_shape[3])
else:
return (input_shape[0], self.nb_filter, input_shape[dim_out])
def build(self, input_shape):
# Input shape :: (samples, channels, height, width)
self.input_spec = [InputSpec(shape=input_shape)]
if self.direction == 'Down':
dims = self.input_spec[0].shape
self.shuffeled_dims = (dims[0], dims[3], dims[1], dims[2])
elif self.direction == 'Right':
dims = self.input_spec[0].shape
self.shuffeled_dims = (dims[0], dims[2], dims[1], dims[3])
else:
raise Exception('ERROR: Unknown direction')
input_dim = self.shuffeled_dims[2]
self.input_dim = input_dim
self.Shape = (4*self.nb_filter, input_dim, 1, 1)
self.Shape1 = (4*self.nb_filter, self.nb_filter, 2, 1)
self.Shape2 = (self.nb_filter, self.shuffeled_dims[3])
self.W_iof = self.init(self.Shape)
self.U_iof = self.init(self.Shape1)
self.b_iof = K.zeros((4*self.nb_filter,))
self.init_h = K.zeros(self.Shape2)
self.init_c = K.zeros(self.Shape2)
if self.stateful:
self.reset_states()
else:
self.states = [None, None]
self.regularizers = []
if self.W_regularizer:
self.W_regularizer.set_param(self.W_iof)
self.regularizers.append(self.W_regularizer)
if self.U_regularizer:
self.U_regularizer.set_param(self.U_iof)
self.regularizers.append(self.U_regularizer)
if self.b_regularizer:
self.b_regularizer.set_param(self.b_iof)
self.regularizers.append(self.b_regularizer)
self.trainable_weights = [self.W_iof, self.U_iof, self.b_iof,
self.init_h, self.init_c]
if self.initial_weights is not None:
self.set_weights(self.initial_weights)
del self.initial_weights
def reset_states(self):
assert self.stateful, 'Layer must be stateful.'
def conv_step(self, x, W, b=None, border_mode="valid", filter_shape=None, mask_type='b'):
mask = np.ones(filter_shape, dtype=_FLOATX)
in_third = self.input_dim//3
out_third = self.nb_filter//3
mask[:out_third,in_third:,0,0] = 0
mask[out_third:2*out_third,2*in_third:,0,0] = 0
W = W * mask
input_shape = self.shuffeled_dims
x = K.expand_dims(x, -1) # add a dimension of the right
conv_out = T.nnet.conv2d(x, W, subsample=self.subsample,
border_mode='half',
filter_flip=False,
input_shape=(input_shape[0],
input_shape[2],
input_shape[3],
1),
filter_shape=filter_shape)
if b:
conv_out = conv_out + K.reshape(b, (1, filter_shape[0], 1, 1))
conv_out = K.squeeze(conv_out, 3) # remove the dummy 3rd dimension
return conv_out
def conv_step_hidden(self, x, W, border_mode="valid", filters=None, filter_shape=None):
input_shape = self.shuffeled_dims
if filters == None:
filters = self.nb_filter
x = K.expand_dims(x, -1) # add a dimension of the right
conv_out = T.nnet.conv2d(x, W, subsample=(1, 1),
border_mode='valid',
filter_flip=False,
input_shape=(input_shape[0],
filters,
input_shape[3],
1),
filter_shape=filter_shape)
conv_out = K.squeeze(conv_out, 3) # remove the dummy 3rd dimension
return conv_out
def step(self, x, states):
h_tm1 = states[0]
h_tm1 = T.concatenate([
T.zeros((K.shape(h_tm1)[0], K.shape(h_tm1)[1], 1), theano.config.floatX),
h_tm1
], axis=-1)
c_tm1 = states[1]
input_to_state = self.conv_step(x, self.W_iof, self.b_iof, border_mode=self.border_mode, filter_shape=self.Shape)
state_to_state = self.conv_step_hidden(h_tm1, self.U_iof, border_mode="same", filters=4*self.nb_filter, filter_shape=self.Shape1)
gates = input_to_state + state_to_state
o_f_i =self.inner_activation(gates[:,:3*self.nb_filter,:])
o = o_f_i[:,0*self.nb_filter:1*self.nb_filter,:]
f = o_f_i[:,1*self.nb_filter:2*self.nb_filter,:]
i = o_f_i[:,2*self.nb_filter:3*self.nb_filter,:]
g = self.activation(gates[:,3*self.nb_filter:4*self.nb_filter,:])
c = (f * c_tm1) + (i * g)
h = o * self.activation(c)
return h, [h ,c]
def get_config(self):
config = {"output_dim": self.output_dim,
"init": self.init.__name__,
"inner_init": self.inner_init.__name__,
"forget_bias_init": self.forget_bias_init.__name__,
"activation": self.activation.__name__,
"inner_activation": self.inner_activation.__name__,
"W_regularizer": self.W_regularizer.get_config() if self.W_regularizer else None,
"U_regularizer": self.U_regularizer.get_config() if self.U_regularizer else None,
"b_regularizer": self.b_regularizer.get_config() if self.b_regularizer else None,
"dropout_W": self.dropout_W,
"dropout_U": self.dropout_U}
base_config = super(DiagLSTM, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class MaskedConvolution2D(Layer):
def __init__(self, nb_filter, nb_row, nb_col, mask_type=None, direction='Down',
init='glorot_uniform', activation='linear', weights=None,
border_mode='valid', subsample=(1, 1), dim_ordering='th',
W_regularizer=None, b_regularizer=None, activity_regularizer=None,
W_constraint=None, b_constraint=None,
bias=True, **kwargs):
self.mask_type = mask_type
self.direction = direction
if border_mode not in {'valid', 'same'}:
raise Exception('Invalid border mode for Convolution2D:', border_mode)
self.nb_filter = nb_filter
self.nb_row = nb_row
self.nb_col = nb_col
self.init = initializations.get(init, dim_ordering=dim_ordering)
self.activation = activations.get(activation)
assert border_mode in {'valid', 'same'}, 'border_mode must be in {valid, same}'
self.border_mode = border_mode
self.subsample = tuple(subsample)
assert dim_ordering in {'tf', 'th'}, 'dim_ordering must be in {tf, th}'
self.dim_ordering = dim_ordering
self.W_regularizer = regularizers.get(W_regularizer)
self.b_regularizer = regularizers.get(b_regularizer)
self.activity_regularizer = regularizers.get(activity_regularizer)
self.W_constraint = constraints.get(W_constraint)
self.b_constraint = constraints.get(b_constraint)
self.bias = bias
self.input_spec = [InputSpec(ndim=4)]
self.initial_weights = weights
super(MaskedConvolution2D, self).__init__(**kwargs)
def build(self, input_shape):
if self.dim_ordering == 'th':
stack_size = input_shape[1]
self.input_dim = input_shape[1]
self.W_shape = (self.nb_filter, stack_size, self.nb_row, self.nb_col)
elif self.dim_ordering == 'tf':
stack_size = input_shape[3]
self.input_dim = input_shape[3]
self.W_shape = (self.nb_row, self.nb_col, stack_size, self.nb_filter)
else:
raise Exception('Invalid dim_ordering: ' + self.dim_ordering)
self.W = self.init(self.W_shape, name='{}_W'.format(self.name))
if self.bias:
self.b = K.zeros((self.nb_filter,), name='{}_b'.format(self.name))
self.trainable_weights = [self.W, self.b]
else:
self.trainable_weights = [self.W]
self.regularizers = []
if self.W_regularizer:
self.W_regularizer.set_param(self.W)
self.regularizers.append(self.W_regularizer)
if self.bias and self.b_regularizer:
self.b_regularizer.set_param(self.b)
self.regularizers.append(self.b_regularizer)
if self.activity_regularizer:
self.activity_regularizer.set_layer(self)
self.regularizers.append(self.activity_regularizer)
self.constraints = {}
if self.W_constraint:
self.constraints[self.W] = self.W_constraint
if self.bias and self.b_constraint:
self.constraints[self.b] = self.b_constraint
if self.initial_weights is not None:
self.set_weights(self.initial_weights)
del self.initial_weights
def get_output_shape_for(self, input_shape):
if self.dim_ordering == 'th':
rows = input_shape[2]
cols = input_shape[3]
elif self.dim_ordering == 'tf':
rows = input_shape[1]
cols = input_shape[2]
else:
raise Exception('Invalid dim_ordering: ' + self.dim_ordering)
rows = K_conv.conv_output_length(rows, self.nb_row,
self.border_mode, self.subsample[0])
cols = K_conv.conv_output_length(cols, self.nb_col,
self.border_mode, self.subsample[1])
if self.dim_ordering == 'th':
return (input_shape[0], self.nb_filter, rows, cols)
elif self.dim_ordering == 'tf':
return (input_shape[0], rows, cols, self.nb_filter)
else:
raise Exception('Invalid dim_ordering: ' + self.dim_ordering)
def call(self, x, mask=None):
mask = np.ones(self.W_shape, dtype=_FLOATX)
center_col = self.nb_col//2
center_row = self.nb_row//2
if self.direction == 'Down':
for i in range(self.nb_col):
for j in range(self.nb_row):
if (j > center_row) or (j>i) or ((i - self.nb_col + j)>0):
mask[:, :, j, i] = 0
elif self.direction == 'Right':
for i in range(self.nb_col):
for j in range(self.nb_row):
if (i > center_col) or (i>j) or ((j - self.nb_row + i)>0):
mask[:, :, j, i] = 0
else:
raise Exception('ERROR: Unknown direction')
in_third = self.input_dim//3
out_third = self.nb_filter//3
if self.mask_type == 'a':
mask[:out_third,:,0,0] = 0
mask[out_third:2*out_third,in_third:,center_row,center_col] = 0
mask[2*out_third:3*out_third,2*in_third:,center_row,center_col] = 0
elif self.mask_type == 'b':
mask[:out_third,in_third:,0,0] = 0
mask[out_third:2*out_third,2*in_third:,center_row,center_col] = 0
W = self.W * mask
output = T.nnet.conv2d(x, W, subsample=self.subsample,
border_mode='half',
filter_flip=False,
filter_shape=self.W_shape)
if self.bias:
if self.dim_ordering == 'th':
output += K.reshape(self.b, (1, self.nb_filter, 1, 1))
elif self.dim_ordering == 'tf':
output += K.reshape(self.b, (1, 1, 1, self.nb_filter))
else:
raise Exception('Invalid dim_ordering: ' + self.dim_ordering)
output = self.activation(output)
return output
def get_config(self):
config = {'nb_filter': self.nb_filter,
'nb_row': self.nb_row,
'nb_col': self.nb_col,
'init': self.init.__name__,
'activation': self.activation.__name__,
'border_mode': self.border_mode,
'subsample': self.subsample,
'dim_ordering': self.dim_ordering,
'W_regularizer': self.W_regularizer.get_config() if self.W_regularizer else None,
'b_regularizer': self.b_regularizer.get_config() if self.b_regularizer else None,
'activity_regularizer': self.activity_regularizer.get_config() if self.activity_regularizer else None,
'W_constraint': self.W_constraint.get_config() if self.W_constraint else None,
'b_constraint': self.b_constraint.get_config() if self.b_constraint else None,
'bias': self.bias}
base_config = super(MaskedConvolution2D, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class GetColors(Layer):
def __init__(self, color, **kwargs):
assert color in (0,1,2)
self.color = color
super(GetColors, self).__init__(**kwargs)
def call(self, x, mask=None):
X = K.permute_dimensions(x, (0, 2, 3, 1))
return X[:,:,:,(self.color*256):(self.color+1)*256]
def get_output_shape_for(self, input_shape):
output = list(input_shape)
return (output[0],output[2],output[3],256)
class SoftmaxLayer(Layer):
def __init__(self, **kwargs):
super(SoftmaxLayer, self).__init__(**kwargs)
def get_output_shape_for(self, input_shape):
return (input_shape[0], input_shape[1]*input_shape[2], input_shape[3])
def call(self, x, mask=None):
X = K.T.reshape(x, (-1, K.shape(x)[-1]))
y = K.softmax(X)
y = K.T.reshape(y, (-1, K.shape(x)[1]*K.shape(x)[2] ,K.shape(x)[-1]))
return y