forked from igul222/speech
-
Notifications
You must be signed in to change notification settings - Fork 1
/
two_tier.py
277 lines (223 loc) · 8.48 KB
/
two_tier.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
"""
RNN Speech Generation Model
Ishaan Gulrajani
"""
import os, sys
sys.path.append(os.getcwd())
try: # This only matters on Ishaan's computer
import gpu_queue
# gpu_queue.delay(60*60*3)
gpu_queue.wait_for_gpu(high_priority=False)
except ImportError:
pass
import numpy
numpy.random.seed(123)
import random
random.seed(123)
import dataset
import theano
import theano.tensor as T
import theano.ifelse
import lib
import lasagne
import scipy.io.wavfile
import time
import functools
import itertools
# Hyperparams
BATCH_SIZE = 128
N_FRAMES = 64 # How many 'frames' to include in each truncated BPTT pass
FRAME_SIZE = 4 # How many samples per frame
DIM = 512 # Model dimensionality. 512 is sufficient for model development; 1024 if you want good samples.
N_GRUS = 3 # How many GRUs to stack in the frame-level model
Q_LEVELS = 256 # How many levels to use when discretizing samples. e.g. 256 = 8-bit scalar quantization
GRAD_CLIP = 1 # Elementwise grad clip threshold
# Dataset
DATA_PATH = '/media/seagate/blizzard/parts'
N_FILES = 141703
BITRATE = 16000
# Other constants
TRAIN_MODE = 'iters' # 'iters' to use PRINT_ITERS and STOP_ITERS, 'time' to use PRINT_TIME and STOP_TIME
PRINT_ITERS = 10000 # Print cost, generate samples, save model checkpoint every N iterations.
STOP_ITERS = 100000 # Stop after this many iterations
PRINT_TIME = 60*60 # Print cost, generate samples, save model checkpoint every N seconds.
STOP_TIME = 60*60*12 # Stop after this many seconds of actual training (not including time req'd to generate samples etc.)
TEST_SET_SIZE = 128 # How many audio files to use for the test set
SEQ_LEN = N_FRAMES * FRAME_SIZE # Total length (# of samples) of each truncated BPTT sequence
Q_ZERO = numpy.int32(Q_LEVELS//2) # Discrete value correponding to zero amplitude
def frame_level_rnn(input_sequences, h0, reset):
"""
input_sequences.shape: (batch size, n frames * FRAME_SIZE)
h0.shape: (batch size, N_GRUS, DIM)
reset.shape: ()
output.shape: (batch size, n frames * FRAME_SIZE, DIM)
"""
if N_GRUS != 3:
raise Exception('N_GRUS must be 3, at least for now')
learned_h0 = lib.param(
'FrameLevel.h0',
numpy.zeros((N_GRUS, DIM), dtype=theano.config.floatX)
)
learned_h0 = T.alloc(learned_h0, h0.shape[0], N_GRUS, DIM)
h0 = theano.ifelse.ifelse(reset, learned_h0, h0)
frames = input_sequences.reshape((
input_sequences.shape[0],
input_sequences.shape[1] / FRAME_SIZE,
FRAME_SIZE
))
# Rescale frames from ints in [0, Q_LEVELS) to floats in [-2, 2]
# (a reasonable range to pass as inputs to the RNN)
frames = (frames.astype('float32') / lib.floatX(Q_LEVELS/2)) - lib.floatX(1)
frames *= lib.floatX(2)
gru1 = lib.ops.LowMemGRU('FrameLevel.GRU1', FRAME_SIZE, DIM, frames, h0=h0[:, 0])
gru2 = lib.ops.LowMemGRU('FrameLevel.GRU2', DIM, DIM, gru1, h0=h0[:, 1])
gru3 = lib.ops.LowMemGRU('FrameLevel.GRU3', DIM, DIM, gru2, h0=h0[:, 2])
output = lib.ops.Linear(
'FrameLevel.Output',
DIM,
FRAME_SIZE * DIM,
gru3,
initialization='he'
)
output = output.reshape((output.shape[0], output.shape[1] * FRAME_SIZE, DIM))
last_hidden = T.stack([gru1[:, -1], gru2[:, -1], gru3[:, -1]], axis=1)
return (output, last_hidden)
def sample_level_predictor(frame_level_outputs, prev_samples):
"""
frame_level_outputs.shape: (batch size, DIM)
prev_samples.shape: (batch size, FRAME_SIZE)
output.shape: (batch size, Q_LEVELS)
"""
prev_samples = lib.ops.Embedding(
'SampleLevel.Embedding',
Q_LEVELS,
Q_LEVELS,
prev_samples
).reshape((-1, FRAME_SIZE * Q_LEVELS))
out = lib.ops.Linear(
'SampleLevel.L1_PrevSamples',
FRAME_SIZE * Q_LEVELS,
DIM,
prev_samples,
biases=False,
initialization='he'
)
out += frame_level_outputs
out = T.nnet.relu(out)
out = lib.ops.Linear('SampleLevel.L2', DIM, DIM, out, initialization='he')
out = T.nnet.relu(out)
out = lib.ops.Linear('SampleLevel.L3', DIM, DIM, out, initialization='he')
out = T.nnet.relu(out)
# We apply the softmax later
return lib.ops.Linear('SampleLevel.Output', DIM, Q_LEVELS, out)
sequences = T.imatrix('sequences')
h0 = T.tensor3('h0')
reset = T.iscalar('reset')
input_sequences = sequences[:, :-FRAME_SIZE]
target_sequences = sequences[:, FRAME_SIZE:]
frame_level_outputs, new_h0 = frame_level_rnn(input_sequences, h0, reset)
prev_samples = sequences[:, :-1]
prev_samples = prev_samples.reshape((1, BATCH_SIZE, 1, -1))
prev_samples = T.nnet.neighbours.images2neibs(prev_samples, (1, FRAME_SIZE), neib_step=(1, 1), mode='valid')
prev_samples = prev_samples.reshape((BATCH_SIZE * SEQ_LEN, FRAME_SIZE))
sample_level_outputs = sample_level_predictor(
frame_level_outputs.reshape((BATCH_SIZE * SEQ_LEN, DIM)),
prev_samples
)
cost = T.nnet.categorical_crossentropy(
T.nnet.softmax(sample_level_outputs),
target_sequences.flatten()
).mean()
# By default we report cross-entropy cost in bits.
# Switch to nats by commenting out this line:
cost = cost * lib.floatX(1.44269504089)
params = lib.search(cost, lambda x: hasattr(x, 'param'))
lib._train.print_params_info(cost, params)
grads = T.grad(cost, wrt=params, disconnected_inputs='warn')
grads = [T.clip(g, lib.floatX(-GRAD_CLIP), lib.floatX(GRAD_CLIP)) for g in grads]
updates = lasagne.updates.adam(grads, params)
train_fn = theano.function(
[sequences, h0, reset],
[cost, new_h0],
updates=updates,
on_unused_input='warn'
)
frame_level_generate_fn = theano.function(
[sequences, h0, reset],
frame_level_rnn(sequences, h0, reset),
on_unused_input='warn'
)
frame_level_outputs = T.matrix('frame_level_outputs')
prev_samples = T.imatrix('prev_samples')
sample_level_generate_fn = theano.function(
[frame_level_outputs, prev_samples],
lib.ops.softmax_and_sample(
sample_level_predictor(
frame_level_outputs,
prev_samples
)
),
on_unused_input='warn'
)
def generate_and_save_samples(tag):
def write_audio_file(name, data):
data = data.astype('float32')
data -= data.min()
data /= data.max()
data -= 0.5
data *= 0.95
scipy.io.wavfile.write(name+'.wav', BITRATE, data)
# Generate 5 sample files, each 5 seconds long
N_SEQS = 10
LENGTH = 5*BITRATE
samples = numpy.zeros((N_SEQS, LENGTH), dtype='int32')
samples[:, :FRAME_SIZE] = Q_ZERO
h0 = numpy.zeros((N_SEQS, N_GRUS, DIM), dtype='float32')
frame_level_outputs = None
for t in xrange(FRAME_SIZE, LENGTH):
if t % FRAME_SIZE == 0:
frame_level_outputs, h0 = frame_level_generate_fn(
samples[:, t-FRAME_SIZE:t],
h0,
numpy.int32(t == FRAME_SIZE)
)
samples[:, t] = sample_level_generate_fn(
frame_level_outputs[:, t % FRAME_SIZE],
samples[:, t-FRAME_SIZE:t]
)
for i in xrange(N_SEQS):
write_audio_file("sample_{}_{}".format(tag, i), samples[i])
print "Training!"
total_iters = 0
total_time = 0.
last_print_time = 0.
last_print_iters = 0
for epoch in itertools.count():
h0 = numpy.zeros((BATCH_SIZE, N_GRUS, DIM), dtype='float32')
costs = []
data_feeder = dataset.feed_epoch(DATA_PATH, N_FILES, BATCH_SIZE, SEQ_LEN, FRAME_SIZE, Q_LEVELS, Q_ZERO)
for seqs, reset in data_feeder:
start_time = time.time()
cost, h0 = train_fn(seqs, h0, reset)
total_time += time.time() - start_time
total_iters += 1
costs.append(cost)
if (TRAIN_MODE=='iters' and total_iters-last_print_iters == PRINT_ITERS) or \
(TRAIN_MODE=='time' and total_time-last_print_time >= PRINT_TIME):
print "epoch:{}\ttotal iters:{}\ttrain cost:{}\ttotal time:{}\ttime per iter:{}".format(
epoch,
total_iters,
numpy.mean(costs),
total_time,
total_time / total_iters
)
tag = "iters{}_time{}".format(total_iters, total_time)
generate_and_save_samples(tag)
lib.save_params('params_{}.pkl'.format(tag))
costs = []
last_print_time += PRINT_TIME
last_print_iters += PRINT_ITERS
if (TRAIN_MODE=='iters' and total_iters == STOP_ITERS) or \
(TRAIN_MODE=='time' and total_time >= STOP_TIME):
print "Done!"
sys.exit()