forked from naotokui/SpectrogramVAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_and_reconstruct.py
368 lines (294 loc) · 14.1 KB
/
encode_and_reconstruct.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
import matplotlib as mpl
mpl.use('TkAgg')
import tensorflow as tf
import os
import sys
import time
import joblib
from random import shuffle
import numpy as np
import argparse
import json
import librosa
import librosa.display
import matplotlib.pyplot as plt
from spec_reader import *
from util import *
from model_iaf import *
from griffin_lim import griffin_lim
with open('audio_params.json', 'r') as f:
param = json.load(f)
N_FFT = param['N_FFT']
HOP_LENGTH = param['HOP_LENGTH']
SAMPLING_RATE = param['SAMPLING_RATE']
MELSPEC_BANDS = param['MELSPEC_BANDS']
sample_secs = param['sample_secs']
num_samples_dataset = int(sample_secs * SAMPLING_RATE)
logdir = './test_iaf'
max_checkpoints = 5
num_steps = 10000
checkpoint_every = 500
batch_size = 1
model_params = 'params.json'
num_data = -1
encode_batch_size = 128
dataset_file = 'dataset.pkl'
def get_arguments():
def _str_to_bool(s):
"""Convert string to bool (in argparse context)."""
if s.lower() not in ['true', 'false']:
raise ValueError('Argument needs to be a '
'boolean, got {}'.format(s))
return {'true': True, 'false': False}[s.lower()]
# TODO: Some of these paramters clash and if not chosen correctly crash the script
parser = argparse.ArgumentParser(description='Spectrogram VAE')
parser.add_argument('--num_data', type=int, default=num_data,
help='How many data points to process. Default: ' + str(num_data) + '.')
parser.add_argument('--logdir', type=str, default=None,
help='Directory in which to store the logging '
'information for TensorBoard. '
'If the model already exists, it will restore '
'the state and will continue training. '
'Cannot use with --logdir_root and --restore_from.')
parser.add_argument('--model_params', type=str, default=model_params,
help='JSON file with the network parameters. Default: ' + model_params + '.')
parser.add_argument('--audio_file', type=str, default=None,
help='Audiofile to encode and reconstruct. If not specified, will use existing dataset instead.')
parser.add_argument('--dataset_file', type=str, default=dataset_file,
help='Dataset pkl file. Default: ' + dataset_file + '.')
parser.add_argument('--encode_full', type=bool, default=False,
help='Encode and save entire dataset? Default: ' + str(False) + '.')
parser.add_argument('--encode_only_new', type=bool, default=True,
help='Encode only new data points? Default: ' + str(True) + '.')
parser.add_argument('--process_original_audio', type=bool, default=False,
help='Process/copy original audio when saving embeddings? Default: ' + str(False) + '.')
parser.add_argument('--plot_spec', type=bool, default=True,
help='Plot reconstructed spectrograms? Default: ' + str(True) + '.')
parser.add_argument('--rec_audio', type=bool, default=True,
help='Reconstruct and save audio? Default: ' + str(True) + '.')
return parser.parse_args()
def save(saver, sess, logdir, step):
model_name = 'model.ckpt'
checkpoint_path = os.path.join(logdir, model_name)
print('Storing checkpoint to {} ...'.format(logdir), end="")
sys.stdout.flush()
if not os.path.exists(logdir):
os.makedirs(logdir)
saver.save(sess, checkpoint_path, global_step=step)
print(' Done.')
def load(saver, sess, logdir):
print("Trying to restore saved checkpoints from {} ...".format(logdir),
end="")
ckpt = tf.train.get_checkpoint_state(logdir)
if ckpt:
print(" Checkpoint found: {}".format(ckpt.model_checkpoint_path))
global_step = int(ckpt.model_checkpoint_path
.split('/')[-1]
.split('-')[-1])
print(" Global step was: {}".format(global_step))
print(" Restoring...", end="")
saver.restore(sess, ckpt.model_checkpoint_path)
print(" Done.")
return global_step
else:
print(" No checkpoint found.")
return None
def main():
args = get_arguments()
# Load data unless input audiofile is specified
if args.audio_file:
melspecs, _ = get_melspec(args.audio_file, as_tf_input=True)
filename = os.path.basename(args.audio_file)
else:
melspecs, filenames = load_specs(filename='dataset.pkl', return_filenames=True)
melspecs = (np.float32(melspecs) + 80.0) / 80.0
# melspecs = 80.0*(np.random.random((10000,128,126))-1.0)
# print(melspecs[0].shape)
# print(np.expand_dims(np.expand_dims(melspecs[0], 0), 3).shape)
if not os.path.exists(args.logdir):
os.makedirs(args.logdir)
# Look for original parameters
print('Loading existing parameters.')
print(f'{args.logdir}/params.json')
with open(f'{args.logdir}/params.json', 'r') as f:
param = json.load(f)
if args.encode_full:
batch_size = encode_batch_size
full_batches = len(filenames) // batch_size
filename_counter = 0
else:
batch_size = 1
# Set correct batch size in deconvolution shapes
deconv_shape = param['deconv_shape']
for k, s in enumerate(deconv_shape):
actual_shape = s
actual_shape[0] = batch_size
deconv_shape[k] = actual_shape
param['deconv_shape'] = deconv_shape
# Create coordinator.
coord = tf.train.Coordinator()
with tf.name_scope('create_inputs'):
reader = SpectrogramReader(melspecs, coord)
spec_batch = reader.dequeue(batch_size)
# Create network.
net = VAEModel(param,
batch_size)
# Set up session
sess = tf.Session(config=tf.ConfigProto(log_device_placement=False))
init = tf.global_variables_initializer()
sess.run(init)
# Saver for loading checkpoints of the model.
saver = tf.train.Saver(var_list=tf.trainable_variables(), max_to_keep=max_checkpoints)
try:
saved_global_step = load(saver, sess, args.logdir)
except:
print("Something went wrong while restoring checkpoint. "
"We will terminate training to avoid accidentally overwriting "
"the previous model.")
raise
# Check if directory for saving exists
out_dir = f'{args.logdir}/reconstructed-{saved_global_step}'
if not os.path.exists(out_dir):
os.makedirs(out_dir)
if args.encode_full:
out_dir_emb = f'{args.logdir}/embeddings-{saved_global_step}'
if not os.path.exists(out_dir_emb):
os.makedirs(out_dir_emb)
if args.audio_file == None:
if args.num_data == -1:
if args.encode_full:
num_batches = full_batches
else:
num_batches = len(filenames)
else:
num_batches = args.num_data
for step in range(num_batches):
if batch_size == 1:
spec_in = np.expand_dims(np.expand_dims(melspecs[step],0),3)
else:
if step < full_batches:
spec_in = melspecs[step * batch_size:(step + 1) * batch_size]
batch_filenames = filenames[step * batch_size:(step + 1) * batch_size]
else:
spec_in = melspecs[step * batch_size:-1]
batch_size_discrep = batch_size - spec_in.shape[0]
spec_in = np.concatenate([spec_in, np.zeros(batch_size_discrep, spec_in.shape[1])])
batch_filenames = filenames[step * batch_size:-1]
spec_in = np.expand_dims(spec_in,3)
if args.encode_full:
print(f'Batch {step} of {full_batches}.')
# Check if all files exist
exists = []
filename_counter_check = filename_counter
for k, name in enumerate(batch_filenames):
name_no_path = os.path.splitext(os.path.split(name)[1])[0]
dataset_filename_emb = f'{out_dir_emb}/{filename_counter_check} - {name_no_path}.npy'
# Skip if already exists
if os.path.isfile(dataset_filename_emb):
exists.append(True)
else:
exists.append(False)
filename_counter_check += 1
if all(exists):
filename_counter = filename_counter_check
continue
emb, out = net.encode_and_reconstruct(spec_in)
embedding, output = sess.run([emb, out])
del spec_in
print(embedding.shape)
# Save
for k, name in enumerate(batch_filenames):
if args.process_original_audio:
try:
name_no_path = os.path.splitext(os.path.split(name)[1])[0]
dataset_filename = f'{out_dir_emb}/{filename_counter} - {name_no_path}.wav'
dataset_filename_emb = f'{out_dir_emb}/{filename_counter} - {name_no_path}.npy'
# Skip if already exists
if args.encode_only_new and os.path.isfile(dataset_filename_emb):
filename_counter += 1
continue
# Load audio file
y, sr = librosa.core.load(name, sr=SAMPLING_RATE, mono=True, duration=sample_secs)
y_tmp = np.zeros(num_samples_dataset)
# Truncate or pad
if len(y) >= num_samples_dataset:
y_tmp = y[:num_samples_dataset]
else:
y_tmp[:len(y)] = y
# Write to file
librosa.output.write_wav(dataset_filename, y_tmp, sr, norm=True)
np.save(dataset_filename_emb, embedding[k])
# # Also plot reconstruction
# melspec = (np.squeeze(output[k]) - 1.0) * 80.0
# plt.figure()
# ax1 = plt.subplot(2, 1, 1)
#
# librosa.display.specshow((melspecs[step * batch_size + k] - 1.0) * 80.0, sr=SAMPLING_RATE, y_axis='mel',
# x_axis='time',
# hop_length=HOP_LENGTH)
# plt.title('Original: ' + name_no_path)
# ax2 = plt.subplot(2, 1, 2, sharex=ax1)
# librosa.display.specshow(melspec, sr=SAMPLING_RATE, y_axis='mel', x_axis='time',
# hop_length=HOP_LENGTH)
# plt.title('Reconstruction')
# plt.tight_layout()
# plt.savefig(f'{out_dir_emb}/{filename_counter} - {name_no_path}.png')
# plt.close()
filename_counter += 1
except:
pass
else:
dataset_filename_emb = f'{out_dir_emb}/{filename_counter} - {name_no_path}.npy'
# Write to file
np.save(dataset_filename_emb, embedding[k])
filename_counter += 1
del emb, out
del embedding, output
else:
emb, out = net.encode_and_reconstruct(spec_in)
embedding, output = sess.run([emb, out])
melspec = (np.squeeze(output[0])-1.0)*80.0
if args.plot_spec:
plt.figure()
ax1 = plt.subplot(2, 1, 1)
librosa.display.specshow((melspecs[step]-1.0)*80.0, sr=SAMPLING_RATE, y_axis='mel', x_axis='time',
hop_length=HOP_LENGTH)
plt.title('Original: ' + os.path.basename(filenames[step]))
ax2 = plt.subplot(2, 1, 2, sharex=ax1)
librosa.display.specshow(melspec, sr=SAMPLING_RATE, y_axis='mel', x_axis='time',
hop_length=HOP_LENGTH)
plt.title('Reconstruction')
plt.tight_layout()
plt.savefig(f'{out_dir}/reconstructed-{step}.png')
plt.close()
if args.rec_audio:
audio = griffin_lim(melspec)
audio_file = f'{out_dir}/reconstructed-{step}.wav'
librosa.output.write_wav(audio_file, audio/np.max(audio), sr=SAMPLING_RATE)
else:
spec_in = melspecs
emb, out = net.encode_and_reconstruct(spec_in)
embedding, output = sess.run([emb, out])
melspec = (np.squeeze(output[0]) - 1.0) * 80.0
melspec_in = (np.squeeze(melspecs) - 1.0) * 80.0
# Save embeddings
np.save(f'{out_dir}/reconstructed-{filename[:-4]}.npy', embedding[0])
# Plot
plt.figure()
ax1 = plt.subplot(2, 1, 1)
librosa.display.specshow((melspec_in - 1.0) * 80.0, sr=SAMPLING_RATE, y_axis='mel', x_axis='time',
hop_length=HOP_LENGTH)
plt.title('Original: ' + os.path.basename(args.audio_file))
ax2 = plt.subplot(2, 1, 2, sharex=ax1)
librosa.display.specshow(melspec, sr=SAMPLING_RATE, y_axis='mel', x_axis='time',
hop_length=HOP_LENGTH)
plt.title('Reconstruction')
plt.tight_layout()
plt.savefig(f'{out_dir}/reconstructed-{filename[:-4]}.png')
plt.close()
# Reconstruct and save audio
audio = griffin_lim(melspec)
audio_file = f'{out_dir}/reconstructed-{filename[:-4]}.wav'
librosa.output.write_wav(audio_file, audio / np.max(audio), sr=SAMPLING_RATE)
if __name__ == '__main__':
main()