-
Notifications
You must be signed in to change notification settings - Fork 1
/
layers.py
142 lines (114 loc) · 4.91 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
# A Wavenet For Speech Denoising - Dario Rethage - 19.05.2017
# Layers.py
import tensorflow as tf
from tensorflow import keras
class AddSingletonDepth(keras.layers.Layer):
def call(self, x, mask=None):
x = keras.backend.expand_dims(x, -1) # add a dimension of the right
if keras.backend.ndim(x) == 4:
return keras.backend.permute_dimensions(x, (0, 3, 1, 2))
else:
return x
def get_output_shape_for(self, input_shape):
if len(input_shape) == 3:
return input_shape[0], 1, input_shape[1], input_shape[2]
else:
return input_shape[0], input_shape[1], 1
class Subtract(keras.layers.Layer):
def __init__(self, **kwargs):
super(Subtract, self).__init__(**kwargs)
def call(self, x, mask=None):
return x[0] - x[1]
def get_output_shape_for(self, input_shape):
return input_shape[0]
class CorrectFourier(keras.layers.Layer):
def __init__(self, units=32,filter_size =500,stride = 1,padding ="SAME",**kwargs):
self.units = units
self.stride = stride
self.padding = padding
self.filter_size = filter_size
super(CorrectFourier, self).__init__(**kwargs)
def get_config(self):
config = super().get_config().copy()
config.update({
# 'selector': self.selector,
'units': self.units,
'stride': self.stride,
'padding': self.padding,
'filter_size': self.filter_size,
})
return config
def build(self, input_shape):
assert input_shape[-1] == 1
w_init = tf.random_uniform_initializer(0,3.14)
self.w = tf.Variable(
initial_value=w_init(shape=(1, self.units), dtype=tf.float32),
trainable=True,
)
self.coeff = tf.constant(tf.range(self.filter_size,dtype=tf.float32),shape=(self.filter_size,1))
def call(self, inputs):
wn = tf.matmul(self.coeff, self.w)
sin_kernels = tf.expand_dims(tf.sin(wn),1)
cos_kernels = tf.expand_dims(tf.cos(wn),1)
conv_sin = tf.nn.conv1d(inputs,sin_kernels,stride=self.stride , padding = self.padding)
conv_cos = tf.nn.conv1d(inputs,cos_kernels,stride=self.stride , padding = self.padding)
return tf.math.log(conv_sin**2 + conv_cos**2)
class CosineExtractor(keras.layers.Layer):
def __init__(self, units=32,filter_size =500,stride = 1,padding ="SAME",**kwargs):
self.units = units
self.stride = stride
self.padding = padding
self.filter_size = filter_size
super(CosineExtractor, self).__init__(**kwargs)
def get_config(self):
config = super().get_config().copy()
config.update({
# 'selector': self.selector,
'units': self.units,
'stride': self.stride,
'padding': self.padding,
'filter_size': self.filter_size,
})
return config
def build(self, input_shape):
assert input_shape[-1] == 1
w_init = tf.random_uniform_initializer(0,3.14)
self.w = tf.Variable(
initial_value=w_init(shape=(1, self.units), dtype=tf.float32),
trainable=True,
)
self.coeff = tf.constant(tf.range(self.filter_size,dtype=tf.float32),shape=(self.filter_size,1))
def call(self, inputs):
wn = tf.matmul(self.coeff, self.w)
cos_kernels = tf.expand_dims(tf.cos(wn),1)
conv_cos = tf.nn.conv1d(inputs,cos_kernels,stride=self.stride , padding = self.padding)
return tf.nn.conv1d(inputs,cos_kernels,stride=self.stride , padding = self.padding) / float(self.filter_size/2)
class Slice(keras.layers.Layer):
def __init__(self, selector, output_shape, **kwargs):
self.selector = selector
self.desired_output_shape = output_shape
super(Slice, self).__init__(**kwargs)
def get_config(self):
config = super().get_config().copy()
config.update({
# 'selector': self.selector,
'desired_output_shape': self.desired_output_shape,
})
return config
def call(self, x, mask=None):
selector = self.selector
if len(self.selector) == 2 and not type(self.selector[1]) is slice and not type(self.selector[1]) is int:
x = keras.backend.permute_dimensions(x, [0, 2, 1])
selector = (self.selector[1], self.selector[0])
y = x[selector]
if len(self.selector) == 2 and not type(self.selector[1]) is slice and not type(self.selector[1]) is int:
y = keras.backend.permute_dimensions(y, [0, 2, 1])
return y
def get_output_shape_for(self, input_shape):
output_shape = (None,)
for i, dim_length in enumerate(self.desired_output_shape):
if dim_length == Ellipsis:
output_shape = output_shape + (input_shape[i+1],)
else:
output_shape = output_shape + (dim_length,)
return output_shape