-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPytorchConvSep.py
More file actions
executable file
·587 lines (423 loc) · 23.3 KB
/
PytorchConvSep.py
File metadata and controls
executable file
·587 lines (423 loc) · 23.3 KB
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
from __future__ import print_function
from __future__ import division
import numpy as np
import torch
from torch.autograd import Variable
import torch.nn as nn
from collections import OrderedDict
from data_pipeline import data_gen
import matplotlib.pyplot as plt
import config
import utils
import datetime
import sys, os
import time
import h5py
import stempeg
def loss_calc(inputs, targets, loss_func, autoencoder):
eps=1e-11
#import pdb;pdb.set_trace()
targets = targets *np.linspace(1.0,0.5,513)
targets_cuda = Variable(torch.FloatTensor(targets)).cuda() + eps
inputs = Variable(torch.FloatTensor(inputs)).cuda() + 1e-30
output = autoencoder(inputs) + eps
# import pdb;pdb.set_trace()
vocals = output[:,:2,:,:]
drums = output[:,2:4,:,:]
bass = output[:,4:6,:,:]
others = output[:,6:,:,:]
total_sources = vocals + bass + drums + others
mask_vocals = vocals/total_sources
mask_drums = drums/total_sources
mask_bass = bass/total_sources
mask_others = others/total_sources
out_vocals = inputs * mask_vocals
out_drums = inputs * mask_drums
out_bass = inputs * mask_bass
out_others = inputs * mask_others
targets_vocals = targets_cuda[:,:2,:,:]
targets_drums = targets_cuda[:,2:4,:,:]
targets_bass = targets_cuda[:,4:6,:,:]
targets_others = targets_cuda[:,6:,:,:]
step_loss_vocals = loss_func(out_vocals, targets_vocals)
alpha_diff = config.alpha * loss_func(out_vocals, targets_bass)
alpha_diff += config.alpha * loss_func(out_vocals, targets_drums)
beta_other_voc = config.beta_voc * loss_func(out_vocals, targets_others)
step_loss_drums = loss_func(out_drums, targets_drums)
alpha_diff += config.alpha * loss_func(out_drums, targets_vocals)
alpha_diff += config.alpha * loss_func(out_drums, targets_bass)
beta_other = config.beta * loss_func(out_drums, targets_others)
step_loss_bass = loss_func(out_bass, targets_bass)
alpha_diff += config.alpha * loss_func(out_bass, targets_vocals)
alpha_diff += config.alpha * loss_func(out_bass, targets_drums)
beta_other = config.beta * loss_func(out_bass, targets_others)
return step_loss_vocals, step_loss_drums, step_loss_bass, alpha_diff, beta_other, beta_other_voc
class AutoEncoder(nn.Module):
def __init__(self, conv_hor_in = (1, 513), conv_ver_in = (15, 1)):
'''
I tried to make this as customizable as possible.
INPUT:
- conv_hor_in: size of the kernel filter for the horizontal convolution.
Must be a tuple.
(Height, Width)
- conv_hor_in: same as the conv_hor_in but for the vertical convolution.
Must be a tuple.
(Height, Width)
- out_channels_in: number of channels of features we want the network to create
on each convolution
'''
super(AutoEncoder, self).__init__() # reference current class in each instance
# init conv/deconv filter shapes
self.conv_hor = conv_hor_in
self.conv_ver = conv_ver_in
### ENCODER
# init autoencoder architecture shape
# we need to use sequential, as it's a way to add modules one after the
# another in an ordered way
self.encoder = nn.Sequential(
nn.Conv2d(2, config.num_ch_out_hor, self.conv_hor, stride = 1, padding = 0, bias = True, groups = 1),
nn.Conv2d(config.num_ch_out_hor, config.num_ch_out_ver, self.conv_ver, stride = 1, padding = 0, bias = True, groups = 1)
)
### DECODERS
self.decode_drums = nn.Sequential(
nn.ConvTranspose2d(config.num_ch_out_ver, config.num_ch_out_hor, self.conv_ver, stride = 1, padding = 0, bias = True, groups = 1),
# nn.ReLU(),
nn.ConvTranspose2d(config.num_ch_out_hor, 2, self.conv_hor, stride = 1, padding = 0, bias = True, groups = 1),
# nn.ReLU()
)
self.decode_voice = nn.Sequential(
nn.ConvTranspose2d(config.num_ch_out_ver, config.num_ch_out_hor, self.conv_ver, stride = 1, padding = 0, bias = True, groups = 1),
# nn.ReLU(),
nn.ConvTranspose2d(config.num_ch_out_hor, 2, self.conv_hor, stride = 1, padding = 0, bias = True, groups = 1),
# nn.ReLU()
)
self.decode_bass = nn.Sequential(
nn.ConvTranspose2d(config.num_ch_out_ver, config.num_ch_out_hor, self.conv_ver, stride = 1, padding = 0, bias = True, groups = 1),
# nn.ReLU(),
nn.ConvTranspose2d(config.num_ch_out_hor, 2, self.conv_hor, stride = 1, padding = 0, bias = True, groups = 1),
# nn.ReLU()
)
self.decode_other = nn.Sequential(
nn.ConvTranspose2d(config.num_ch_out_ver, config.num_ch_out_hor, self.conv_ver, stride = 1, padding = 0, bias = True, groups = 1),
# nn.ReLU(),
nn.ConvTranspose2d(config.num_ch_out_hor, 2, self.conv_hor, stride = 1, padding = 0, bias = True, groups = 1),
# nn.ReLU()
)
### FULLY CONNECTED LAYERS
self.layer_first = nn.Sequential(
nn.Linear(config.num_ch_out_ver*16, 128),
nn.ReLU()
)
self.layer_drums = nn.Sequential(
nn.Linear(128,config.num_ch_out_ver*16),
nn.ReLU()
)
self.layer_voice = nn.Sequential(
nn.Linear(128,config.num_ch_out_ver*16),
nn.ReLU()
)
self.layer_bass = nn.Sequential(
nn.Linear(128,config.num_ch_out_ver*16),
nn.ReLU()
)
self.layer_other = nn.Sequential(
nn.Linear(128,config.num_ch_out_ver*16),
nn.ReLU()
)
self.final_output = nn.ReLU()
def forward(self, x):
encode = self.encoder(x)
encode = encode.view(config.batch_size, -1)
layer_output = self.layer_first(encode)
layer_output_voice = self.layer_voice(layer_output)
layer_output_voice = layer_output_voice.view(-1,config.num_ch_out_ver,16,1)
output_voice = self.decode_voice(layer_output_voice)
layer_output_drums = self.layer_drums(layer_output)
layer_output_drums = layer_output_drums.view(-1,config.num_ch_out_ver,16,1)
output_drums = self.decode_drums(layer_output_drums)
layer_output_bass = self.layer_bass(layer_output)
layer_output_bass = layer_output_bass.view(-1,config.num_ch_out_ver,16,1)
output_bass = self.decode_bass(layer_output_bass)
layer_output_other = self.layer_other(layer_output)
layer_output_other = layer_output_other.view(-1,config.num_ch_out_ver,16,1)
output_other = self.decode_other(layer_output_other)
reshape_output = torch.cat((output_voice, output_drums, output_bass, output_other), 1)
output_final = self.final_output(reshape_output)
return output_final
def trainNetwork(save_name = 'model_e' + str(config.num_epochs) + '_b' + str(config.batches_per_epoch_train) + '_bs' + str(config.batch_size) ):
assert torch.cuda.is_available(), "Code only usable with cuda"
#autoencoder = AutoEncoder().cuda()
autoencoder = AutoEncoder().cuda()
autoencoder.load_state_dict(torch.load('./log/model_e8000_b50_bs5_3469.pt'))
optimizer = torch.optim.Adadelta(autoencoder.parameters(), lr = 1, rho=0.95)
loss_func = nn.MSELoss( size_average=False )
#loss_func = nn.L1Loss( size_average=False )
train_evol = []
val_evol = []
count = 0
for epoch in range(config.num_epochs):
start_time = time.time()
generator = data_gen()
val_gen = data_gen(mode= "Val")
train_loss = 0
train_loss_vocals = 0
train_loss_drums = 0
train_loss_bass = 0
train_alpha_diff = 0
train_beta_other = 0
train_beta_other_voc = 0
val_loss = 0
val_loss_vocals = 0
val_loss_drums = 0
val_loss_bass = 0
val_alpha_diff = 0
val_beta_other = 0
val_beta_other_voc = 0
optimizer.zero_grad()
count = 0
for inputs, targets in generator:
step_loss_vocals, step_loss_drums, step_loss_bass, alpha_diff, beta_other, beta_other_voc = loss_calc(inputs, targets, loss_func, autoencoder)
# start_time = time.time()
# add regularization terms from paper
step_loss = abs(step_loss_vocals + step_loss_drums + step_loss_bass - beta_other - alpha_diff - beta_other_voc)
# print time.time()-start_time
# import pdb;pdb.set_trace()
# start_time = time.time()
train_loss += step_loss.item()
if np.isnan(train_loss):
#import pdb;pdb.set_trace()
optimizer.zero_grad()
print ("error output contains NaN")
train_loss_vocals +=step_loss_vocals.item()
train_loss_drums +=step_loss_drums.item()
train_loss_bass +=step_loss_bass.item()
train_alpha_diff += alpha_diff.item()
train_beta_other += beta_other.item()
train_beta_other_voc+=beta_other_voc.item()
step_loss.backward()
#clip gradient
# torch.nn.utils.clip_grad_norm_( autoencoder.parameters(),1)
for p in autoencoder.parameters():
p.grad.data.clamp(-1,1)
optimizer.step()
# print time.time()-start_time
utils.progress(count,config.batches_per_epoch_train, suffix = 'training done')
count+=1
train_loss = train_loss/(config.batches_per_epoch_train*count*config.max_phr_len*513)
train_loss_vocals = train_loss_vocals/(config.batches_per_epoch_train*count*config.max_phr_len*513)
train_loss_drums = train_loss_drums/(config.batches_per_epoch_train*count*config.max_phr_len*513)
train_loss_bass = train_loss_bass/(config.batches_per_epoch_train*count*config.max_phr_len*513)
train_alpha_diff = train_alpha_diff/(config.batches_per_epoch_train*count*config.max_phr_len*513)
train_beta_other = train_beta_other/(config.batches_per_epoch_train*count*config.max_phr_len*513)
train_beta_other_voc= train_beta_other_voc/(config.batches_per_epoch_train*count*config.max_phr_len*513)
train_evol.append([train_loss,train_loss_vocals,train_loss_drums,train_loss_bass,train_alpha_diff,train_beta_other,train_beta_other_voc])
count = 0
for inputs, targets in val_gen:
step_loss_vocals, step_loss_drums, step_loss_bass, alpha_diff, beta_other, beta_other_voc = loss_calc(inputs, targets, loss_func, autoencoder)
# add regularization terms from paper
step_loss = abs(step_loss_vocals + step_loss_drums + step_loss_bass - beta_other - alpha_diff - beta_other_voc)
val_loss += step_loss.item()
val_loss_vocals +=step_loss_vocals.item()
val_loss_drums +=step_loss_drums.item()
val_loss_bass +=step_loss_bass.item()
val_alpha_diff += alpha_diff.item()
val_beta_other += beta_other.item()
val_beta_other_voc+=beta_other_voc.item()
utils.progress(count,config.batches_per_epoch_val, suffix = 'validation done')
count+=1
val_loss = val_loss/(config.batches_per_epoch_val*count*config.max_phr_len*513)
val_loss_vocals = val_loss_vocals/(config.batches_per_epoch_val*count*config.max_phr_len*513)
val_loss_drums = val_loss_drums/(config.batches_per_epoch_val*count*config.max_phr_len*513)
val_loss_bass = val_loss_bass/(config.batches_per_epoch_val*count*config.max_phr_len*513)
val_alpha_diff = val_alpha_diff/(config.batches_per_epoch_val*count*config.max_phr_len*513)
val_beta_other = val_beta_other/(config.batches_per_epoch_val*count*config.max_phr_len*513)
val_beta_other_voc= val_beta_other_voc/(config.batches_per_epoch_val*count*config.max_phr_len*513)
val_evol.append([val_loss,val_loss_vocals,val_loss_drums,val_loss_bass,val_alpha_diff,val_beta_other,val_beta_other_voc])
# import pdb;pdb.set_trace()
duration = time.time()-start_time
if (epoch+1)%config.print_every == 0:
print('epoch %d/%d, took %.2f seconds, epoch total loss: %.7f' % (epoch+1, config.num_epochs, duration, train_loss))
print(' epoch vocal loss: %.7f' % (train_loss_vocals))
print(' epoch drums loss: %.7f' % (train_loss_drums))
print(' epoch bass loss: %.7f' % (train_loss_bass))
print(' epoch alpha diff: %.7f' % (train_alpha_diff))
print(' epoch beta diff: %.7f' % (train_beta_other))
print(' epoch beta2 diff: %.7f' % (train_beta_other_voc))
print(' validation total loss: %.7f' % ( val_loss))
print(' validation vocal loss: %.7f' % (val_loss_vocals))
print(' validation drums loss: %.7f' % (val_loss_drums))
print(' validation bass loss: %.7f' % (val_loss_bass))
print(' validation alpha diff: %.7f' % (val_alpha_diff))
print(' validation beta diff: %.7f' % (val_beta_other))
print(' validation beta2 diff: %.7f' % (val_beta_other_voc))
# import pdb;pdb.set_trace()
if (epoch+1)%config.save_every == 0:
torch.save(autoencoder.state_dict(), config.log_dir+save_name+'_'+str(epoch + 3470)+'.pt')
np.save(config.log_dir+'train_loss',np.array(train_evol))
np.save(config.log_dir+'val_loss',np.array(val_evol))
# import pdb;pdb.set_trace()
torch.save(autoencoder.state_dict(), config.log_dir+save_name+'_'+str(epoch + 99)+'.pt')
def evalNetwork(file_name, load_name='model_e4000_b50_bs5_1709', plot = False, synth = False):
autoencoder_audio = AutoEncoder().cuda()
epoch = 50
eps=1e-30
# autoencoder_audio.load_state_dict(torch.load(config.log_dir+load_name+'_'+str(epoch)+'.pt'))
autoencoder_audio.load_state_dict(torch.load('./log/model_e8000_b50_bs5_3369.pt'))
stat_file = h5py.File(config.stat_dir+'stats.hdf5', mode='r')
'''
import pdb;pdb.set_trace()
enc = autoencoder_audio.encoder
weight = enc[0].weight.data.cpu().numpy()
plt.imshow(weight[0,0,:,:])
'''
max_feat = np.array(stat_file["feats_maximus"])
min_feat = np.array(stat_file["feats_minimus"])
max_feat_tars = max_feat[:8,:].reshape(8,1,513)
min_feat_tars = min_feat[:8,:].reshape(8,1,513)
max_feat_ins = max_feat[-2:,:].reshape(2,1,513)
min_feat_ins = min_feat[-2:,:].reshape(2,1,513)
audio,fs = stempeg.read_stems(os.path.join(config.wav_dir_test,file_name), stem_id=[0,1,2,3,4])
mixture = audio[0]
drums = audio[1]
bass = audio[2]
acc = audio[3]
vocals = audio[4]
mix_stft, mix_phase = utils.stft_stereo(mixture,phase=True)
mix_stft = (mix_stft-min_feat_ins)/(max_feat_ins-min_feat_ins)
drums_stft = utils.stft_stereo(drums)
bass_stft = utils.stft_stereo(bass)
acc_stft = utils.stft_stereo(acc)
voc_stft = utils.stft_stereo(vocals)
in_batches, nchunks_in = utils.generate_overlapadd(mix_stft)
out_batches = []
for in_batch in in_batches:
# import pdb;pdb.set_trace()
in_batch = Variable(torch.FloatTensor(in_batch)).cuda()
out_batch = autoencoder_audio(in_batch)
out_batches.append(np.array(out_batch.data.cpu().numpy()))
out_batches = np.array(out_batches)
#out_batches[out_batches == 0] = 1e-6
vocals = out_batches[:,:,:2,:,:]
drums = out_batches[:,:,2:4,:,:]
bass = out_batches[:,:,4:6,:,:]
others = out_batches[:,:,6:,:,:]
total_sources = vocals + bass + drums + others
total_sources = total_sources
mask_vocals = vocals/total_sources
mask_drums = drums/total_sources
mask_bass = bass/total_sources
mask_others = 1 - (mask_vocals+mask_drums+mask_bass)
out_vocals = in_batches * mask_vocals
out_drums = in_batches * mask_drums
out_bass = in_batches * mask_bass
out_others = in_batches * mask_others
out_vocals = out_vocals*(max_feat_tars[:2,:,:]-min_feat_tars[:2,:,:])+min_feat_tars[:2,:,:]
out_drums = out_drums*(max_feat_tars[2:4,:,:]-min_feat_tars[2:4,:,:])+min_feat_tars[2:4,:,:]
out_bass = out_bass*(max_feat_tars[4:6,:,:]-min_feat_tars[4:6,:,:])+min_feat_tars[4:6,:,:]
out_others = out_others*(max_feat_tars[6:,:,:]-min_feat_tars[6:,:,:])+min_feat_tars[6:,:,:]
out_drums = utils.overlapadd(out_drums, nchunks_in)
out_bass = utils.overlapadd(out_bass, nchunks_in)
out_others = utils.overlapadd(out_others, nchunks_in)
out_vocals = utils.overlapadd(out_vocals, nchunks_in)
if plot:
plt.figure(1)
plt.suptitle(file_name[:-9])
ax1 = plt.subplot(411)
plt.imshow(np.log(drums_stft[0].T),aspect = 'auto', origin = 'lower')
ax1.set_title("Drums Left Channel Ground Truth", fontsize = 10)
ax2 = plt.subplot(412, sharex = ax1, sharey = ax1)
plt.imshow(np.log(out_drums[0].T),aspect = 'auto', origin = 'lower')
ax2.set_title("Drums Left Channel Network Output", fontsize = 10)
ax3 = plt.subplot(413, sharex = ax1, sharey = ax1)
plt.imshow(np.log(drums_stft[1].T),aspect = 'auto', origin = 'lower')
ax3.set_title("Drums Right Channel Ground Truth", fontsize = 10)
ax4 = plt.subplot(414, sharex = ax1, sharey = ax1)
plt.imshow(np.log(out_drums[1].T),aspect = 'auto', origin = 'lower')
ax4.set_title("Drums Right Channel Network Output", fontsize = 10)
plt.figure(2)
plt.suptitle(file_name[:-9])
ax1 = plt.subplot(411)
plt.imshow(np.log(voc_stft[0].T),aspect = 'auto', origin = 'lower')
ax1.set_title("Vocals Left Channel Ground Truth", fontsize = 10)
ax2 = plt.subplot(412, sharex = ax1, sharey = ax1)
plt.imshow(np.log(out_vocals[0].T),aspect = 'auto', origin = 'lower')
ax2.set_title("Vocals Left Channel Network Output", fontsize = 10)
ax3 = plt.subplot(413, sharex = ax1, sharey = ax1)
plt.imshow(np.log(voc_stft[1].T),aspect = 'auto', origin = 'lower')
ax3.set_title("Vocals Right Channel Ground Truth", fontsize = 10)
ax4 = plt.subplot(414, sharex = ax1, sharey = ax1)
plt.imshow(np.log(out_vocals[1].T),aspect = 'auto', origin = 'lower')
ax4.set_title("Vocals Right Channel Network Output", fontsize = 10)
plt.figure(3)
plt.suptitle(file_name[:-9])
ax1 = plt.subplot(411)
plt.imshow(np.log(bass_stft[0].T),aspect = 'auto', origin = 'lower')
ax1.set_title("Bass Left Channel Ground Truth", fontsize = 10)
ax2 = plt.subplot(412, sharex = ax1, sharey = ax1)
plt.imshow(np.log(out_bass[0].T),aspect = 'auto', origin = 'lower')
ax2.set_title("Bass Left Channel Network Output", fontsize = 10)
ax3 = plt.subplot(413, sharex = ax1, sharey = ax1)
plt.imshow(np.log(bass_stft[1].T),aspect = 'auto', origin = 'lower')
ax3.set_title("Bass Right Channel Ground Truth", fontsize = 10)
ax4 = plt.subplot(414, sharex = ax1, sharey = ax1)
plt.imshow(np.log(out_bass[1].T),aspect = 'auto', origin = 'lower')
ax4.set_title("Bass Right Channel Network Output", fontsize = 10)
plt.figure(4)
plt.suptitle(file_name[:-9])
ax1 = plt.subplot(411)
plt.imshow(np.log(acc_stft[0].T),aspect = 'auto', origin = 'lower')
ax1.set_title("Others Left Channel Ground Truth", fontsize = 10)
ax2 = plt.subplot(412, sharex = ax1, sharey = ax1)
plt.imshow(np.log(out_others[0].T),aspect = 'auto', origin = 'lower')
ax2.set_title("Others Left Channel Network Output", fontsize = 10)
ax3 = plt.subplot(413, sharex = ax1, sharey = ax1)
plt.imshow(np.log(acc_stft[1].T),aspect = 'auto', origin = 'lower')
ax3.set_title("Others Right Channel Ground Truth", fontsize = 10)
ax4 = plt.subplot(414, sharex = ax1, sharey = ax1)
plt.imshow(np.log(out_others[1].T),aspect = 'auto', origin = 'lower')
ax4.set_title("Others Right Channel Network Output", fontsize = 10)
plt.show()
if synth:
# import pdb;pdb.set_trace()
utils.inverse_stft_write(out_drums[:,:mix_phase.shape[1],:],mix_phase,config.out_dir+file_name+"_drums.wav")
utils.inverse_stft_write(out_bass[:,:mix_phase.shape[1],:],mix_phase,config.out_dir+file_name+"_bass.wav")
utils.inverse_stft_write(out_vocals[:,:mix_phase.shape[1],:],mix_phase,config.out_dir+file_name+"_vocals.wav")
utils.inverse_stft_write(out_others[:,:mix_phase.shape[1],:],mix_phase,config.out_dir+file_name+"_others.wav")
def plot_loss():
train_loss = np.load(config.log_dir+'train_loss.npy')
val_loss = np.load(config.log_dir+'val_loss.npy')
plt.plot(train_loss)
plt.show()
if __name__ == '__main__':
if sys.argv[1] == '-train' or sys.argv[1] == '--train' or sys.argv[1] == '--t' or sys.argv[1] == '-t':
print("Training")
trainNetwork()
elif sys.argv[1] == '-synth' or sys.argv[1] == '--synth' or sys.argv[1] == '--s' or sys.argv[1] == '-s':
if len(sys.argv)<3:
print("Please give a file to synthesize")
else:
file_name = sys.argv[2]
if not file_name.endswith('.stem.mp4'):
file_name = file_name+'.stem.mp4'
print("Synthesizing File %s"% file_name)
if '-p' in sys.argv or '--p' in sys.argv or '-plot' in sys.argv or '--plot' in sys.argv:
if '-ns' in sys.argv or '--ns' in sys.argv:
print("Just showing plots for File %s"% sys.argv[2])
evalNetwork(file_name,plot=True, synth =False)
else:
print("Showing Plots And Synthesizing File %s"% sys.argv[2])
evalNetwork(file_name,plot=True, synth =True)
else:
evalNetwork(file_name,plot=False, synth =True)
elif sys.argv[1] == '-plot' or sys.argv[1] == '--pl' or sys.argv[1] == '--plot_loss':
plot_loss()
# else:
# print("Synthesizing File %s, Not Showing Plots"% sys.argv[2])
# synth_file(file_name,show_plots=False, save_file=True)
elif sys.argv[1] == '-help' or sys.argv[1] == '--help' or sys.argv[1] == '--h' or sys.argv[1] == '-h':
print("%s --train to train the model"%sys.argv[0])
print("%s --synth <filename> to synthesize file"%sys.argv[0])
print("%s --synth <filename> -- plot to synthesize file and show plots"%sys.argv[0])
print("%s --synth <filename> -- plot --ns to just show plots"%sys.argv[0])
else:
print("Unable to decipher inputs please use %s --help for help on how to use this function"%sys.argv[0])