-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodel.py
225 lines (174 loc) · 7.69 KB
/
model.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
from __future__ import division, print_function
__author__ = "Lauri Juvela, lauri.juvela@aalto.fi"
import numpy as np
import tensorflow as tf
_FLOATX = tf.float32
def get_weight_variable(name, shape=None, use_spectral_norm=False, update_u=True, initializer=tf.contrib.layers.xavier_initializer_conv2d()):
if shape is None:
return tf.get_variable(name)
else:
return tf.get_variable(name, shape=shape, dtype=_FLOATX, initializer=initializer)
def get_bias_variable(name, shape=None, initializer=tf.constant_initializer(value=0.0, dtype=_FLOATX)):
if shape is None:
return tf.get_variable(name)
else:
return tf.get_variable(name, shape=shape, dtype=_FLOATX, initializer=initializer)
class WaveNet():
def __init__(self,
name,
input_channels=40,
output_channels=1,
residual_channels=64,
postnet_channels=256,
filter_width=3,
dilations=[1, 2, 4, 8, 1, 2, 4, 8],
cond_channels=None,
cond_embed_dim=64,
use_dropout=False,
dropout_keep_prob=0.95,
mask_center_input=False):
self.input_channels = input_channels
self.output_channels = output_channels
self.filter_width = filter_width
self.dilations = dilations
self.residual_channels = residual_channels
self.postnet_channels = postnet_channels
self.use_dropout = use_dropout
self.dropout_keep_prob = dropout_keep_prob
self.mask_center_input = mask_center_input
if cond_channels is not None:
self._use_cond = True
self.cond_embed_dim = cond_embed_dim
self.cond_channels = cond_channels
else:
self._use_cond = False
self._name = name
#self._create_variables()
def get_receptive_field(self):
# returns one sided receptive field of the model
# symmetric receptive field is actually 2 * get_receptive_field() - 1
# from dilated stack
receptive_field = sum(self.dilations) * ((self.filter_width - 1) // 2)
# from input layer
receptive_field += (self.filter_width - 1) // 2 - 1
# current sample
receptive_field += 1
return receptive_field
def get_variable_list(self):
theta = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self._name)
if len(theta) == 0:
raise RuntimeError("Model '{}' variabiles are not allocated until the first call of forward_pass".format(self._name))
return theta
def _input_layer(self, main_input):
with tf.variable_scope('input_layer'):
fw = self.filter_width
r = self.residual_channels
W = get_weight_variable('W', (fw, self.input_channels, r))
b = get_bias_variable('b', (r))
if self.mask_center_input:
assert fw % 2 == 1, "Filter width must be odd if masking the center value"
ones = tf.ones(
shape=(fw//2, self.input_channels, r), dtype=W.dtype)
zeros = tf.zeros(
shape=(1, self.input_channels, r), dtype=W.dtype)
M = tf.concat([ones, zeros, ones], axis=0)
W = M*W
X = main_input
Y = tf.nn.convolution(X, W, padding='SAME')
Y += b
if self.use_dropout:
Y = tf.nn.dropout(Y, keep_prob=self.dropout_keep_prob)
Y = tf.tanh(Y)
return Y
def _embed_cond(self, cond_input):
with tf.variable_scope('embed_cond'):
W = get_weight_variable(
'W', (1, self.cond_channels, self.cond_embed_dim))
b = get_bias_variable('b', (self.cond_embed_dim))
Y = tf.nn.convolution(
cond_input, W, padding='SAME') # 1x1 convolution
Y += b
return tf.tanh(Y)
def _conv_module(self, main_input, residual_input, module_idx, dilation, cond_input=None):
with tf.variable_scope('conv_modules'):
with tf.variable_scope('module{}'.format(module_idx)):
fw = self.filter_width
r = self.residual_channels
s = self.postnet_channels
X = main_input
# convolution
W = get_weight_variable('filter_gate_W', (fw, r, 2*r))
b = get_bias_variable('filter_gate_b', (2*r))
Y = tf.nn.convolution(
X, W, padding='SAME', dilation_rate=[dilation])
Y += b
if self._use_cond:
W = get_weight_variable(
'cond_filter_gate_W', (1, self.cond_embed_dim, 2*r))
b = get_bias_variable('cond_filter_gate_b', (2*r))
C = tf.nn.convolution(
cond_input, W, padding='SAME') # 1x1 convolution
C += b
Y += C
if self.use_dropout:
Y = tf.nn.dropout(Y, keep_prob=self.dropout_keep_prob)
# filter and gate
Y = tf.tanh(Y[:, :, :r])*tf.sigmoid(Y[:, :, r:])
#Y = Y[:, :, :r]*tf.sigmoid(Y[:, :, r:]) # GLU
# skip 1x1 convolution
W = get_weight_variable('skip_W', (1, r, s))
b = get_bias_variable('skip_b', (s))
skip_out = tf.nn.convolution(X, W, padding='SAME')
skip_out += b
# output 1x1 convolution
W = get_weight_variable('output_W', (1, r, r))
b = get_bias_variable('output_b', r)
Y = tf.nn.convolution(Y, W, padding='SAME')
Y += b
Y += X
return Y, skip_out
def _postproc_module(self, skip_outputs):
with tf.variable_scope('postproc_module'):
s = self.postnet_channels
# sum of residual module outputs
X = tf.zeros_like(skip_outputs[0])
for R in skip_outputs:
#R = tf.nn.relu(R) # activation maybe
X += R
# 1x1 convolution
W1 = get_weight_variable('W1', shape=(1, s, s))
b1 = get_bias_variable('b1', shape=s)
Y = tf.nn.convolution(X, W1, padding='SAME')
Y += b1
Y = tf.nn.tanh(Y)
# final output
if type(self.output_channels) is list:
output_list = []
for i, c in enumerate(self.output_channels):
W = get_weight_variable('W_out{}'.format(i), (1, s, c))
b = get_bias_variable('b_out{}'.format(i), c)
out = tf.nn.convolution(Y, W, padding='SAME')
out += b
output_list.append(out)
Y = output_list
else:
W = get_weight_variable('W_out', (1, s, self.output_channels))
b = get_bias_variable('b_out', self.output_channels)
out = tf.nn.convolution(Y, W, padding='SAME')
out += b
Y = out
return Y
def forward_pass(self, X_input, cond_input=None):
skip_outputs = []
with tf.variable_scope(self._name, reuse=tf.AUTO_REUSE):
if self._use_cond:
C = self._embed_cond(cond_input)
else:
C = None
R = self._input_layer(X_input)
X = R
for i, dilation in enumerate(self.dilations):
X, skip = self._conv_module(X, R, i, dilation, cond_input=C)
skip_outputs.append(skip)
Y = self._postproc_module(skip_outputs)
return Y