-
Notifications
You must be signed in to change notification settings - Fork 37
/
layers.py
620 lines (490 loc) · 23.2 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
"""
ShadeSketch
https://github.com/qyzdao/ShadeSketch
Learning to Shadow Hand-drawn Sketches
Qingyuan Zheng, Zhuoru Li, Adam W. Bargteil
Copyright (C) 2020 The respective authors and Project HAT. All rights reserved.
Licensed under MIT license.
"""
import tensorflow as tf
# import keras
keras = tf.keras
K = keras.backend
Layer = keras.layers.Layer
Conv2D = keras.layers.Conv2D
InputSpec = keras.layers.InputSpec
image_data_format = K.image_data_format
activations = keras.activations
initializers = keras.initializers
regularizers = keras.regularizers
constraints = keras.constraints
class Composite(Layer):
def __init__(self,
data_format='channels_last',
**kwargs):
self.data_format = data_format
super(Composite, self).__init__(**kwargs)
def call(self, inputs):
line_inputs, shade_inputs = inputs
return line_inputs + (shade_inputs + 1) * 0.25
def compute_output_shape(self, input_shape):
return input_shape[0]
class PixelwiseConcat(Layer):
def __init__(self,
data_format='channels_last',
**kwargs):
self.data_format = data_format
super(PixelwiseConcat, self).__init__(**kwargs)
def call(self, inputs):
pixel_inputs, unit_inputs = inputs
if self.data_format == 'channels_first':
repeated_unit_inputs = tf.tile(
K.expand_dims(K.expand_dims(unit_inputs, 2), 2),
[1, K.shape(pixel_inputs)[2], K.shape(pixel_inputs)[3], 1]
)
elif self.data_format == 'channels_last':
repeated_unit_inputs = tf.tile(
K.expand_dims(K.expand_dims(unit_inputs, 1), 1),
[1, K.shape(pixel_inputs)[1], K.shape(pixel_inputs)[2], 1]
)
return K.concatenate([pixel_inputs, repeated_unit_inputs])
def compute_output_shape(self, input_shape):
if self.data_format == 'channels_first':
return (input_shape[0][0], input_shape[0][1] + input_shape[1][1], input_shape[0][2], input_shape[0][3])
elif self.data_format == 'channels_last':
return (input_shape[0][0], input_shape[0][1], input_shape[0][2], input_shape[0][3] + input_shape[1][1])
class SubPixelConv2D(Conv2D):
def __init__(self,
filters,
kernel_size,
r,
padding='same',
data_format=None,
strides=(1, 1),
activation=None,
use_bias=True,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
**kwargs):
super(SubPixelConv2D, self).__init__(
filters=r * r * filters,
kernel_size=kernel_size,
strides=strides,
padding=padding,
data_format=data_format,
activation=activation,
use_bias=use_bias,
kernel_initializer=kernel_initializer,
bias_initializer=bias_initializer,
kernel_regularizer=kernel_regularizer,
bias_regularizer=bias_regularizer,
activity_regularizer=activity_regularizer,
kernel_constraint=kernel_constraint,
bias_constraint=bias_constraint,
**kwargs)
self.r = r
if hasattr(tf.nn, 'depth_to_space'):
self.depth_to_space = tf.nn.depth_to_space
else:
self.depth_to_space = tf.depth_to_space
def phase_shift(self, I):
if self.data_format == 'channels_first':
return self.depth_to_space(I, self.r, data_format="NCHW")
elif self.data_format == 'channels_last':
return self.depth_to_space(I, self.r, data_format="NHWC")
def call(self, inputs):
return self.phase_shift(super(SubPixelConv2D, self).call(inputs))
def compute_output_shape(self, input_shape):
if self.data_format == 'channels_first':
n, c, h, w = super(SubPixelConv2D, self).compute_output_shape(input_shape)
elif self.data_format == 'channels_last':
n, h, w, c = super(SubPixelConv2D, self).compute_output_shape(input_shape)
if h is not None:
h = int(self.r * h)
if w is not None:
w = int(self.r * w)
c = int(c / (self.r * self.r))
if self.data_format == 'channels_first':
return (n, c, h, w)
elif self.data_format == 'channels_last':
return (n, h, w, c)
def get_config(self):
config = super(Conv2D, self).get_config()
config.pop('rank')
config.pop('dilation_rate')
config['filters'] /= self.r * self.r
config['r'] = self.r
return config
class SelfAttention(Layer):
def __init__(self,
data_format='channels_last',
activation=None,
use_bias=True,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
**kwargs):
super(SelfAttention, self).__init__(**kwargs)
self.data_format = data_format
self.activation = activations.get(activation)
self.use_bias = use_bias
self.kernel_initializer = initializers.get(kernel_initializer)
self.bias_initializer = initializers.get(bias_initializer)
self.kernel_regularizer = regularizers.get(kernel_regularizer)
self.bias_regularizer = regularizers.get(bias_regularizer)
self.activity_regularizer = regularizers.get(activity_regularizer)
self.kernel_constraint = constraints.get(kernel_constraint)
self.bias_constraint = constraints.get(bias_constraint)
def build(self, input_shape):
if self.data_format == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
kernel_size = (1, 1)
self.filters = int(input_shape[channel_axis])
self.kernel_f = self.add_weight(shape=kernel_size + (self.filters, self.filters // 8),
initializer=self.kernel_initializer,
name='kernel_f',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
self.kernel_g = self.add_weight(shape=kernel_size + (self.filters, self.filters // 8),
initializer=self.kernel_initializer,
name='kernel_g',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
self.kernel_h = self.add_weight(shape=kernel_size + (self.filters, self.filters),
initializer=self.kernel_initializer,
name='kernel_h',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
if self.use_bias:
self.bias_f = self.add_weight(shape=(self.filters // 8,),
initializer=self.bias_initializer,
name='bias_f',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
self.bias_g = self.add_weight(shape=(self.filters // 8,),
initializer=self.bias_initializer,
name='bias_g',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
self.bias_h = self.add_weight(shape=(self.filters,),
initializer=self.bias_initializer,
name='bias_h',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
else:
self.bias_f = None
self.bias_g = None
self.bias_h = None
self.gamma = self.add_weight(
name='gamma',
shape=(1,),
initializer=initializers.Constant(0)
)
super(SelfAttention, self).build(input_shape)
def call(self, inputs):
f = K.conv2d(inputs,
self.kernel_f,
data_format=self.data_format,
strides=(1, 1),
dilation_rate=(1, 1)) # [bs, h, w, c']
g = K.conv2d(inputs,
self.kernel_g,
data_format=self.data_format,
strides=(1, 1),
dilation_rate=(1, 1)) # [bs, h, w, c']
h = K.conv2d(inputs,
self.kernel_h,
data_format=self.data_format,
strides=(1, 1),
dilation_rate=(1, 1)) # [bs, h, w, c]
if self.use_bias:
f = K.bias_add(f, self.bias_f, data_format=self.data_format) # [bs, h, w, c']
g = K.bias_add(g, self.bias_g, data_format=self.data_format) # [bs, h, w, c']
h = K.bias_add(h, self.bias_h, data_format=self.data_format) # [bs, h, w, c]
# N = h * w
s = K.dot(K.batch_flatten(g), K.transpose(K.batch_flatten(f))) # # [bs, N, N]
beta = K.softmax(s) # attention map
o = K.dot(beta, K.batch_flatten(h)) # [bs, N, C]
o = K.reshape(o, K.shape(inputs)) # [bs, h, w, C]
return self.activation(self.gamma * o + inputs)
def compute_output_shape(self, input_shape):
return input_shape
def get_config(self):
config = {
'activation': activations.serialize(self.activation),
'data_format': self.data_format,
'use_bias': self.use_bias,
'kernel_initializer': initializers.serialize(self.kernel_initializer),
'bias_initializer': initializers.serialize(self.bias_initializer),
'kernel_regularizer': regularizers.serialize(self.kernel_regularizer),
'bias_regularizer': regularizers.serialize(self.bias_regularizer),
'activity_regularizer': regularizers.serialize(self.activity_regularizer),
'kernel_constraint': constraints.serialize(self.kernel_constraint),
'bias_constraint': constraints.serialize(self.bias_constraint)
}
base_config = super(SelfAttention, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
"""
Implementation of Coordinate Channel
keras-coordconv
MIT License
Copyright (c) 2018 Somshubra Majumdar
https://github.com/titu1994/keras-coordconv/blob/master/coord.py
"""
class _CoordinateChannel(Layer):
""" Adds Coordinate Channels to the input tensor.
# Arguments
rank: An integer, the rank of the input data-uniform,
e.g. "2" for 2D convolution.
use_radius: Boolean flag to determine whether the
radius coordinate should be added for 2D rank
inputs or not.
data_format: A string,
one of `"channels_last"` or `"channels_first"`.
The ordering of the dimensions in the inputs.
`"channels_last"` corresponds to inputs with shape
`(batch, ..., channels)` while `"channels_first"` corresponds to
inputs with shape `(batch, channels, ...)`.
It defaults to the `image_data_format` value found in your
Keras config file at `~/.keras/keras.json`.
If you never set it, then it will be "channels_last".
# Input shape
ND tensor with shape:
`(samples, channels, *)`
if `data_format` is `"channels_first"`
or ND tensor with shape:
`(samples, *, channels)`
if `data_format` is `"channels_last"`.
# Output shape
ND tensor with shape:
`(samples, channels + 2, *)`
if `data_format` is `"channels_first"`
or 5D tensor with shape:
`(samples, *, channels + 2)`
if `data_format` is `"channels_last"`.
# References:
- [An Intriguing Failing of Convolutional Neural Networks and the CoordConv Solution](https://arxiv.org/abs/1807.03247)
"""
def __init__(self, rank,
use_radius=False,
data_format='channels_last',
**kwargs):
super(_CoordinateChannel, self).__init__(**kwargs)
if data_format not in [None, 'channels_first', 'channels_last']:
raise ValueError('`data_format` must be either "channels_last", "channels_first" '
'or None.')
self.rank = rank
self.use_radius = use_radius
self.data_format = data_format
self.axis = 1 if image_data_format() == 'channels_first' else -1
self.input_spec = InputSpec(min_ndim=2)
self.supports_masking = True
def build(self, input_shape):
assert len(input_shape) >= 2
input_dim = input_shape[self.axis]
self.input_spec = InputSpec(min_ndim=self.rank + 2,
axes={self.axis: input_dim})
self.built = True
def call(self, inputs, training=None, mask=None):
input_shape = K.shape(inputs)
if self.rank == 1:
input_shape = [input_shape[i] for i in range(3)]
batch_shape, dim, channels = input_shape
xx_range = tf.tile(K.expand_dims(K.arange(0, dim), axis=0),
K.stack([batch_shape, 1]))
xx_range = K.expand_dims(xx_range, axis=-1)
xx_channels = K.cast(xx_range, K.floatx())
xx_channels = xx_channels / K.cast(dim - 1, K.floatx())
xx_channels = (xx_channels * 2) - 1.
outputs = K.concatenate([inputs, xx_channels], axis=-1)
if self.rank == 2:
if self.data_format == 'channels_first':
inputs = K.permute_dimensions(inputs, [0, 2, 3, 1])
input_shape = K.shape(inputs)
input_shape = [input_shape[i] for i in range(4)]
batch_shape, dim1, dim2, channels = input_shape
xx_ones = tf.ones(K.stack([batch_shape, dim2]), dtype='int32')
xx_ones = K.expand_dims(xx_ones, axis=-1)
xx_range = tf.tile(K.expand_dims(K.arange(0, dim1), axis=0),
K.stack([batch_shape, 1]))
xx_range = K.expand_dims(xx_range, axis=1)
xx_channels = K.batch_dot(xx_ones, xx_range, axes=[2, 1])
xx_channels = K.expand_dims(xx_channels, axis=-1)
xx_channels = K.permute_dimensions(xx_channels, [0, 2, 1, 3])
yy_ones = tf.ones(K.stack([batch_shape, dim1]), dtype='int32')
yy_ones = K.expand_dims(yy_ones, axis=1)
yy_range = tf.tile(K.expand_dims(K.arange(0, dim2), axis=0),
K.stack([batch_shape, 1]))
yy_range = K.expand_dims(yy_range, axis=-1)
yy_channels = K.batch_dot(yy_range, yy_ones, axes=[2, 1])
yy_channels = K.expand_dims(yy_channels, axis=-1)
yy_channels = K.permute_dimensions(yy_channels, [0, 2, 1, 3])
xx_channels = K.cast(xx_channels, K.floatx())
xx_channels = xx_channels / K.cast(dim1 - 1, K.floatx())
xx_channels = (xx_channels * 2) - 1.
yy_channels = K.cast(yy_channels, K.floatx())
yy_channels = yy_channels / K.cast(dim2 - 1, K.floatx())
yy_channels = (yy_channels * 2) - 1.
outputs = K.concatenate([inputs, xx_channels, yy_channels], axis=-1)
if self.use_radius:
rr = K.sqrt(K.square(xx_channels - 0.5) +
K.square(yy_channels - 0.5))
outputs = K.concatenate([outputs, rr], axis=-1)
if self.data_format == 'channels_first':
outputs = K.permute_dimensions(outputs, [0, 3, 1, 2])
if self.rank == 3:
if self.data_format == 'channels_first':
inputs = K.permute_dimensions(inputs, [0, 2, 3, 4, 1])
input_shape = K.shape(inputs)
input_shape = [input_shape[i] for i in range(5)]
batch_shape, dim1, dim2, dim3, channels = input_shape
xx_ones = tf.ones(K.stack([batch_shape, dim3]), dtype='int32')
xx_ones = K.expand_dims(xx_ones, axis=-1)
xx_range = tf.tile(K.expand_dims(K.arange(0, dim2), axis=0),
K.stack([batch_shape, 1]))
xx_range = K.expand_dims(xx_range, axis=1)
xx_channels = K.batch_dot(xx_ones, xx_range, axes=[2, 1])
xx_channels = K.expand_dims(xx_channels, axis=-1)
xx_channels = K.permute_dimensions(xx_channels, [0, 2, 1, 3])
xx_channels = K.expand_dims(xx_channels, axis=1)
xx_channels = tf.tile(xx_channels,
[1, dim1, 1, 1, 1])
yy_ones = tf.ones(K.stack([batch_shape, dim2]), dtype='int32')
yy_ones = K.expand_dims(yy_ones, axis=1)
yy_range = tf.tile(K.expand_dims(K.arange(0, dim3), axis=0),
K.stack([batch_shape, 1]))
yy_range = K.expand_dims(yy_range, axis=-1)
yy_channels = K.batch_dot(yy_range, yy_ones, axes=[2, 1])
yy_channels = K.expand_dims(yy_channels, axis=-1)
yy_channels = K.permute_dimensions(yy_channels, [0, 2, 1, 3])
yy_channels = K.expand_dims(yy_channels, axis=1)
yy_channels = tf.tile(yy_channels,
[1, dim1, 1, 1, 1])
zz_range = tf.tile(K.expand_dims(K.arange(0, dim1), axis=0),
K.stack([batch_shape, 1]))
zz_range = K.expand_dims(zz_range, axis=-1)
zz_range = K.expand_dims(zz_range, axis=-1)
zz_channels = tf.tile(zz_range,
[1, 1, dim2, dim3])
zz_channels = K.expand_dims(zz_channels, axis=-1)
xx_channels = K.cast(xx_channels, K.floatx())
xx_channels = xx_channels / K.cast(dim2 - 1, K.floatx())
xx_channels = xx_channels * 2 - 1.
yy_channels = K.cast(yy_channels, K.floatx())
yy_channels = yy_channels / K.cast(dim3 - 1, K.floatx())
yy_channels = yy_channels * 2 - 1.
zz_channels = K.cast(zz_channels, K.floatx())
zz_channels = zz_channels / K.cast(dim1 - 1, K.floatx())
zz_channels = zz_channels * 2 - 1.
outputs = K.concatenate([inputs, zz_channels, xx_channels, yy_channels],
axis=-1)
if self.data_format == 'channels_first':
outputs = K.permute_dimensions(outputs, [0, 4, 1, 2, 3])
return outputs
def compute_output_shape(self, input_shape):
assert input_shape and len(input_shape) >= 2
assert input_shape[self.axis]
if self.use_radius and self.rank == 2:
channel_count = 3
else:
channel_count = self.rank
output_shape = list(input_shape)
output_shape[self.axis] = input_shape[self.axis] + channel_count
return tuple(output_shape)
def get_config(self):
config = {
'rank': self.rank,
'use_radius': self.use_radius,
'data_format': self.data_format
}
base_config = super(_CoordinateChannel, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class CoordinateChannel1D(_CoordinateChannel):
""" Adds Coordinate Channels to the input tensor of rank 1.
# Arguments
data_format: A string,
one of `"channels_last"` or `"channels_first"`.
The ordering of the dimensions in the inputs.
`"channels_last"` corresponds to inputs with shape
`(batch, ..., channels)` while `"channels_first"` corresponds to
inputs with shape `(batch, channels, ...)`.
It defaults to the `image_data_format` value found in your
Keras config file at `~/.keras/keras.json`.
If you never set it, then it will be "channels_last".
# Input shape
3D tensor with shape: `(batch_size, steps, input_dim)`
# Output shape
3D tensor with shape: `(batch_size, steps, input_dim + 2)`
# References:
- [An Intriguing Failing of Convolutional Neural Networks and the CoordConv Solution](https://arxiv.org/abs/1807.03247)
"""
def __init__(self, data_format=None, **kwargs):
super(CoordinateChannel1D, self).__init__(
rank=1,
use_radius=False,
data_format=data_format,
**kwargs
)
def get_config(self):
config = super(CoordinateChannel1D, self).get_config()
config.pop('rank')
config.pop('use_radius')
return config
class CoordinateChannel2D(_CoordinateChannel):
""" Adds Coordinate Channels to the input tensor.
# Arguments
use_radius: Boolean flag to determine whether the
radius coordinate should be added for 2D rank
inputs or not.
data_format: A string,
one of `"channels_last"` or `"channels_first"`.
The ordering of the dimensions in the inputs.
`"channels_last"` corresponds to inputs with shape
`(batch, ..., channels)` while `"channels_first"` corresponds to
inputs with shape `(batch, channels, ...)`.
It defaults to the `image_data_format` value found in your
Keras config file at `~/.keras/keras.json`.
If you never set it, then it will be "channels_last".
# Input shape
4D tensor with shape:
`(samples, channels, rows, cols)`
if `data_format` is `"channels_first"`
or 4D tensor with shape:
`(samples, rows, cols, channels)`
if `data_format` is `"channels_last"`.
# Output shape
4D tensor with shape:
`(samples, channels + 2/3, rows, cols)`
if `data_format` is `"channels_first"`
or 4D tensor with shape:
`(samples, rows, cols, channels + 2/3)`
if `data_format` is `"channels_last"`.
If `use_radius` is set, then will have 3 additional filers,
else only 2 additional filters will be added.
# References:
- [An Intriguing Failing of Convolutional Neural Networks and the CoordConv Solution](https://arxiv.org/abs/1807.03247)
"""
def __init__(self, use_radius=False,
data_format=None,
**kwargs):
super(CoordinateChannel2D, self).__init__(
rank=2,
use_radius=use_radius,
data_format=data_format,
**kwargs
)
def get_config(self):
config = super(CoordinateChannel2D, self).get_config()
config.pop('rank')
return config