-
Notifications
You must be signed in to change notification settings - Fork 1
/
voice_conversion.py
930 lines (773 loc) · 31.7 KB
/
voice_conversion.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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
#We'll be using TF 2.1 and torchaudio
from __future__ import print_function, division
import tensorflow as tf
import os
from glob import glob
import scipy
import soundfile as sf
import matplotlib.pyplot as plt
from IPython.display import clear_output
from tensorflow.keras.layers import Input, Dense, Reshape, Flatten, Concatenate, Conv2D, Conv2DTranspose, GlobalAveragePooling2D, UpSampling2D, LeakyReLU, ReLU, Add, Multiply, Lambda, Dot, BatchNormalization, Activation, ZeroPadding2D, Cropping2D, Cropping1D
from tensorflow.keras.models import Sequential, Model, load_model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.initializers import TruncatedNormal, he_normal
import tensorflow.keras.backend as K
import datetime
import numpy as np
import random
import matplotlib.pyplot as plt
import collections
from PIL import Image
#from skimage.transform import resize
#import imageio
import librosa
import librosa.display
from librosa.feature import melspectrogram
import os
#import time
import IPython
import numpy as np
#import pytsmod as tsm
import soundfile as sf
import sys
import wave
import scipy.io.wavfile
from numpy import *
import shutil
#Hyperparameters
hop=192 #hop size (window size = 6*hop)
sr=16000 #sampling rate
min_level_db=-100 #reference values to normalize data
ref_level_db=20
shape=24 #length of time axis of split specrograms to feed to generator
vec_len=128 #length of vector generated by siamese vector
bs = 16 #batch size
delta = 2. #constant for siamese loss
#There seems to be a problem with Tensorflow STFT, so we'll be using pytorch to handle offline mel-spectrogram generation and waveform reconstruction
#For waveform reconstruction, a gradient-based method is used:
''' Decorsière, Rémi, Peter L. Søndergaard, Ewen N. MacDonald, and Torsten Dau.
"Inversion of auditory spectrograms, traditional spectrograms, and other envelope representations."
IEEE/ACM Transactions on Audio, Speech, and Language Processing 23, no. 1 (2014): 46-56.'''
#ORIGINAL CODE FROM https://github.com/yoyololicon/spectrogram-inversion
import torch
import torch.nn as nn
import torch.nn.functional as F
from tqdm import tqdm
from functools import partial
import math
import heapq
from torchaudio.transforms import MelScale, Spectrogram
#torch.set_default_tensor_type('torch.cuda.FloatTensor')
specobj = Spectrogram(n_fft=6*hop, win_length=6*hop, hop_length=hop, pad=0, power=2, normalized=True)
specfunc = specobj.forward
melobj = MelScale(n_mels=hop, sample_rate=sr, f_min=0.)
melfunc = melobj.forward
def melspecfunc(waveform):
specgram = specfunc(waveform)
mel_specgram = melfunc(specgram)
return mel_specgram
def spectral_convergence(input, target):
return 20 * ((input - target).norm().log10() - target.norm().log10())
def GRAD(spec, transform_fn, samples=None, init_x0=None, maxiter=1000, tol=1e-6, verbose=1, evaiter=10, lr=0.003):
spec = torch.Tensor(spec)
samples = (spec.shape[-1]*hop)-hop
if init_x0 is None:
init_x0 = spec.new_empty((1,samples)).normal_(std=1e-6)
x = nn.Parameter(init_x0)
T = spec
criterion = nn.L1Loss()
optimizer = torch.optim.Adam([x], lr=lr)
bar_dict = {}
metric_func = spectral_convergence
bar_dict['spectral_convergence'] = 0
metric = 'spectral_convergence'
init_loss = None
with tqdm(total=maxiter, disable=not verbose) as pbar:
for i in range(maxiter):
optimizer.zero_grad()
V = transform_fn(x)
loss = criterion(V, T)
loss.backward()
optimizer.step()
lr = lr*0.9999
for param_group in optimizer.param_groups:
param_group['lr'] = lr
if i % evaiter == evaiter - 1:
with torch.no_grad():
V = transform_fn(x)
bar_dict[metric] = metric_func(V, spec).item()
l2_loss = criterion(V, spec).item()
pbar.set_postfix(**bar_dict, loss=l2_loss)
pbar.update(evaiter)
return x.detach().view(-1).cpu()
def normalize(S):
return np.clip((((S - min_level_db) / -min_level_db)*2.)-1., -1, 1)
def denormalize(S):
return (((np.clip(S, -1, 1)+1.)/2.) * -min_level_db) + min_level_db
def prep(wv,hop=192):
S = np.array(torch.squeeze(melspecfunc(torch.Tensor(wv).view(1,-1))).detach().cpu())
S = librosa.power_to_db(S)-ref_level_db
return normalize(S)
def deprep(S):
S = denormalize(S)+ref_level_db
S = librosa.db_to_power(S)
wv = GRAD(np.expand_dims(S,0), melspecfunc, maxiter=2000, evaiter=10, tol=1e-8)
return np.array(np.squeeze(wv))
#Helper functions
#Generate spectrograms from waveform array
def tospec(data):
specs=np.empty(data.shape[0], dtype=object)
for i in range(data.shape[0]):
x = data[i]
S=prep(x)
S = np.array(S, dtype=np.float32)
specs[i]=np.expand_dims(S, -1)
print(specs.shape)
return specs
#Generate multiple spectrograms with a determined length from single wav file
def tospeclong(path, length=4*16000):
x, sr = librosa.load(path,sr=16000)
x,_ = librosa.effects.trim(x)
loudls = librosa.effects.split(x, top_db=50)
xls = np.array([])
for interv in loudls:
xls = np.concatenate((xls,x[interv[0]:interv[1]]))
x = xls
num = x.shape[0]//length
specs=np.empty(num, dtype=object)
for i in range(num-1):
a = x[i*length:(i+1)*length]
S = prep(a)
S = np.array(S, dtype=np.float32)
try:
sh = S.shape
specs[i]=S
except AttributeError:
print('spectrogram failed')
print(specs.shape)
return specs
#Waveform array from path of folder containing wav files
def audio_array(path):
ls = glob(f'{path}/*.wav')
adata = []
for i in range(len(ls)):
x, sr = tf.audio.decode_wav(tf.io.read_file(ls[i]), 1)
x = np.array(x, dtype=np.float32)
adata.append(x)
return np.array(adata)
#Concatenate spectrograms in array along the time axis
def testass(a):
but=False
con = np.array([])
nim = a.shape[0]
for i in range(nim):
im = a[i]
im = np.squeeze(im)
if not but:
con=im
but=True
else:
con = np.concatenate((con,im), axis=1)
return np.squeeze(con)
#Split spectrograms in chunks with equal size
def splitcut(data):
ls = []
mini = 0
minifinal = 10*shape #max spectrogram length
for i in range(data.shape[0]-1):
if data[i].shape[1]<=data[i+1].shape[1]:
mini = data[i].shape[1]
else:
mini = data[i+1].shape[1]
if mini>=3*shape and mini<minifinal:
minifinal = mini
for i in range(data.shape[0]):
x = data[i]
if x.shape[1]>=3*shape:
for n in range(x.shape[1]//minifinal):
ls.append(x[:,n*minifinal:n*minifinal+minifinal,:])
ls.append(x[:,-minifinal:,:])
return np.array(ls)
@tf.function
def proc(x):
return tf.image.random_crop(x, size=[hop, 3*shape, 1])
#Adding Spectral Normalization to convolutional layers
from tensorflow.python.keras.utils import conv_utils
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import sparse_ops
from tensorflow.python.ops import gen_math_ops
from tensorflow.python.ops import standard_ops
from tensorflow.python.eager import context
from tensorflow.python.framework import tensor_shape
def l2normalize(v, eps=1e-12):
return v / (tf.norm(v) + eps)
class ConvSN2D(tf.keras.layers.Conv2D):
def __init__(self, filters, kernel_size, power_iterations=1, **kwargs):
super(ConvSN2D, self).__init__(filters, kernel_size, **kwargs)
self.power_iterations = power_iterations
def build(self, input_shape):
super(ConvSN2D, self).build(input_shape)
if self.data_format == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
self.u = self.add_weight(self.name + '_u',
shape=tuple([1, self.kernel.shape.as_list()[-1]]),
initializer=tf.initializers.RandomNormal(0, 1),
trainable=False
)
def compute_spectral_norm(self, W, new_u, W_shape):
for _ in range(self.power_iterations):
new_v = l2normalize(tf.matmul(new_u, tf.transpose(W)))
new_u = l2normalize(tf.matmul(new_v, W))
sigma = tf.matmul(tf.matmul(new_v, W), tf.transpose(new_u))
W_bar = W/sigma
with tf.control_dependencies([self.u.assign(new_u)]):
W_bar = tf.reshape(W_bar, W_shape)
return W_bar
def call(self, inputs):
W_shape = self.kernel.shape.as_list()
W_reshaped = tf.reshape(self.kernel, (-1, W_shape[-1]))
new_kernel = self.compute_spectral_norm(W_reshaped, self.u, W_shape)
outputs = self.convolution_op(inputs, new_kernel)
if self.use_bias:
if self.data_format == 'channels_first':
outputs = tf.nn.bias_add(outputs, self.bias, data_format='NCHW')
else:
outputs = tf.nn.bias_add(outputs, self.bias, data_format='NHWC')
if self.activation is not None:
return self.activation(outputs)
return outputs
class ConvSN2DTranspose(tf.keras.layers.Conv2DTranspose):
def __init__(self, filters, kernel_size, power_iterations=1, **kwargs):
super(ConvSN2DTranspose, self).__init__(filters, kernel_size, **kwargs)
self.power_iterations = power_iterations
def build(self, input_shape):
super(ConvSN2DTranspose, self).build(input_shape)
if self.data_format == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
self.u = self.add_weight(self.name + '_u',
shape=tuple([1, self.kernel.shape.as_list()[-1]]),
initializer=tf.initializers.RandomNormal(0, 1),
trainable=False
)
def compute_spectral_norm(self, W, new_u, W_shape):
for _ in range(self.power_iterations):
new_v = l2normalize(tf.matmul(new_u, tf.transpose(W)))
new_u = l2normalize(tf.matmul(new_v, W))
sigma = tf.matmul(tf.matmul(new_v, W), tf.transpose(new_u))
W_bar = W/sigma
with tf.control_dependencies([self.u.assign(new_u)]):
W_bar = tf.reshape(W_bar, W_shape)
return W_bar
def call(self, inputs):
W_shape = self.kernel.shape.as_list()
W_reshaped = tf.reshape(self.kernel, (-1, W_shape[-1]))
new_kernel = self.compute_spectral_norm(W_reshaped, self.u, W_shape)
inputs_shape = array_ops.shape(inputs)
batch_size = inputs_shape[0]
if self.data_format == 'channels_first':
h_axis, w_axis = 2, 3
else:
h_axis, w_axis = 1, 2
height, width = inputs_shape[h_axis], inputs_shape[w_axis]
kernel_h, kernel_w = self.kernel_size
stride_h, stride_w = self.strides
if self.output_padding is None:
out_pad_h = out_pad_w = None
else:
out_pad_h, out_pad_w = self.output_padding
out_height = conv_utils.deconv_output_length(height,
kernel_h,
padding=self.padding,
output_padding=out_pad_h,
stride=stride_h,
dilation=self.dilation_rate[0])
out_width = conv_utils.deconv_output_length(width,
kernel_w,
padding=self.padding,
output_padding=out_pad_w,
stride=stride_w,
dilation=self.dilation_rate[1])
if self.data_format == 'channels_first':
output_shape = (batch_size, self.filters, out_height, out_width)
else:
output_shape = (batch_size, out_height, out_width, self.filters)
output_shape_tensor = array_ops.stack(output_shape)
outputs = K.conv2d_transpose(
inputs,
new_kernel,
output_shape_tensor,
strides=self.strides,
padding=self.padding,
data_format=self.data_format,
dilation_rate=self.dilation_rate)
if not context.executing_eagerly():
out_shape = self.compute_output_shape(inputs.shape)
outputs.set_shape(out_shape)
if self.use_bias:
outputs = tf.nn.bias_add(
outputs,
self.bias,
data_format=conv_utils.convert_data_format(self.data_format, ndim=4))
if self.activation is not None:
return self.activation(outputs)
return outputs
class DenseSN(Dense):
def build(self, input_shape):
super(DenseSN, self).build(input_shape)
self.u = self.add_weight(self.name + '_u',
shape=tuple([1, self.kernel.shape.as_list()[-1]]),
initializer=tf.initializers.RandomNormal(0, 1),
trainable=False)
def compute_spectral_norm(self, W, new_u, W_shape):
new_v = l2normalize(tf.matmul(new_u, tf.transpose(W)))
new_u = l2normalize(tf.matmul(new_v, W))
sigma = tf.matmul(tf.matmul(new_v, W), tf.transpose(new_u))
W_bar = W/sigma
with tf.control_dependencies([self.u.assign(new_u)]):
W_bar = tf.reshape(W_bar, W_shape)
return W_bar
def call(self, inputs):
W_shape = self.kernel.shape.as_list()
W_reshaped = tf.reshape(self.kernel, (-1, W_shape[-1]))
new_kernel = self.compute_spectral_norm(W_reshaped, self.u, W_shape)
rank = len(inputs.shape)
if rank > 2:
outputs = standard_ops.tensordot(inputs, new_kernel, [[rank - 1], [0]])
if not context.executing_eagerly():
shape = inputs.shape.as_list()
output_shape = shape[:-1] + [self.units]
outputs.set_shape(output_shape)
else:
inputs = math_ops.cast(inputs, self._compute_dtype)
if K.is_sparse(inputs):
outputs = sparse_ops.sparse_tensor_dense_matmul(inputs, new_kernel)
else:
outputs = gen_math_ops.mat_mul(inputs, new_kernel)
if self.use_bias:
outputs = tf.nn.bias_add(outputs, self.bias)
if self.activation is not None:
return self.activation(outputs)
return outputs
#Networks Architecture
init = tf.keras.initializers.he_uniform()
def conv2d(layer_input, filters, kernel_size=4, strides=2, padding='same', leaky=True, bnorm=True, sn=True):
if leaky:
Activ = LeakyReLU(alpha=0.2)
else:
Activ = ReLU()
if sn:
d = ConvSN2D(filters, kernel_size=kernel_size, strides=strides, padding=padding, kernel_initializer=init, use_bias=False)(layer_input)
else:
d = Conv2D(filters, kernel_size=kernel_size, strides=strides, padding=padding, kernel_initializer=init, use_bias=False)(layer_input)
if bnorm:
d = BatchNormalization()(d)
d = Activ(d)
return d
def deconv2d(layer_input, layer_res, filters, kernel_size=4, conc=True, scalev=False, bnorm=True, up=True, padding='same', strides=2):
if up:
u = UpSampling2D((1,2))(layer_input)
u = ConvSN2D(filters, kernel_size, strides=(1,1), kernel_initializer=init, use_bias=False, padding=padding)(u)
else:
u = ConvSN2DTranspose(filters, kernel_size, strides=strides, kernel_initializer=init, use_bias=False, padding=padding)(layer_input)
if bnorm:
u = BatchNormalization()(u)
u = LeakyReLU(alpha=0.2)(u)
if conc:
u = Concatenate()([u,layer_res])
return u
#Extract function: splitting spectrograms
def extract_image(im):
im1 = Cropping2D(((0,0), (0, 2*(im.shape[2]//3))))(im)
im2 = Cropping2D(((0,0), (im.shape[2]//3,im.shape[2]//3)))(im)
im3 = Cropping2D(((0,0), (2*(im.shape[2]//3), 0)))(im)
return im1,im2,im3
#Assemble function: concatenating spectrograms
def assemble_image(lsim):
im1,im2,im3 = lsim
imh = Concatenate(2)([im1,im2,im3])
return imh
#U-NET style architecture
def build_generator(input_shape):
h,w,c = input_shape
inp = Input(shape=input_shape)
#downscaling
g0 = tf.keras.layers.ZeroPadding2D((0,1))(inp)
g1 = conv2d(g0, 256, kernel_size=(h,3), strides=1, padding='valid')
g2 = conv2d(g1, 256, kernel_size=(1,9), strides=(1,2))
g3 = conv2d(g2, 256, kernel_size=(1,7), strides=(1,2))
#upscaling
g4 = deconv2d(g3,g2, 256, kernel_size=(1,7), strides=(1,2))
g5 = deconv2d(g4,g1, 256, kernel_size=(1,9), strides=(1,2), bnorm=False)
g6 = ConvSN2DTranspose(1, kernel_size=(h,1), strides=(1,1), kernel_initializer=init, padding='valid', activation='tanh')(g5)
return Model(inp,g6, name='G')
#Siamese Network
def build_siamese(input_shape):
h,w,c = input_shape
inp = Input(shape=input_shape)
g1 = conv2d(inp, 256, kernel_size=(h,3), strides=1, padding='valid', sn=False)
g2 = conv2d(g1, 256, kernel_size=(1,9), strides=(1,2), sn=False)
g3 = conv2d(g2, 256, kernel_size=(1,7), strides=(1,2), sn=False)
g4 = Flatten()(g3)
g5 = Dense(vec_len)(g4)
return Model(inp, g5, name='S')
#Discriminator (Critic) Network
def build_critic(input_shape):
h,w,c = input_shape
inp = Input(shape=input_shape)
g1 = conv2d(inp, 512, kernel_size=(h,3), strides=1, padding='valid', bnorm=False)
g2 = conv2d(g1, 512, kernel_size=(1,9), strides=(1,2), bnorm=False)
g3 = conv2d(g2, 512, kernel_size=(1,7), strides=(1,2), bnorm=False)
g4 = Flatten()(g3)
g4 = DenseSN(1, kernel_initializer=init)(g4)
return Model(inp, g4, name='C')
#Load past models from path to resume training or test
def load(path):
gen = build_generator((hop,shape,1))
siam = build_siamese((hop,shape,1))
critic = build_critic((hop,3*shape,1))
gen.load_weights(path+'/gen.h5')
critic.load_weights(path+'/critic.h5')
siam.load_weights(path+'/siam.h5')
return gen,critic,siam
#Build models
def build():
gen = build_generator((hop,shape,1))
siam = build_siamese((hop,shape,1))
critic = build_critic((hop,3*shape,1)) #the discriminator accepts as input spectrograms of triple the width of those generated by the generator
return gen,critic,siam
#Generate a random batch to display current training results
def testgena():
sw = True
while sw:
a = np.random.choice(aspec)
if a.shape[1]//shape!=1:
sw=False
dsa = []
if a.shape[1]//shape>6:
num=6
else:
num=a.shape[1]//shape
rn = np.random.randint(a.shape[1]-(num*shape))
for i in range(num):
im = a[:,rn+(i*shape):rn+(i*shape)+shape]
im = np.reshape(im, (im.shape[0],im.shape[1],1))
dsa.append(im)
return np.array(dsa, dtype=np.float32)
#Show results mid-training
def save_test_image_full(path):
a = testgena()
print(a.shape)
ab = gen(a, training=False)
ab = testass(ab)
a = testass(a)
abwv = deprep(ab)
awv = deprep(a)
sf.write(path+'/new_file.wav', abwv, sr)
IPython.display.display(IPython.display.Audio(np.squeeze(abwv), rate=sr))
IPython.display.display(IPython.display.Audio(np.squeeze(awv), rate=sr))
fig, axs = plt.subplots(ncols=2)
axs[0].imshow(np.flip(a, -2), cmap=None)
axs[0].axis('off')
axs[0].set_title('Source')
axs[1].imshow(np.flip(ab, -2), cmap=None)
axs[1].axis('off')
axs[1].set_title('Generated')
plt.show()
#Save in training loop
def save_end(epoch,gloss,closs,mloss,n_save=3,save_path='../content/'): #use custom save_path (i.e. Drive '../content/drive/My Drive/')
if epoch % n_save == 0:
print('Saving...')
path = f'{save_path}/MELGANVC-{str(gloss)[:9]}-{str(closs)[:9]}-{str(mloss)[:9]}'
os.mkdir(path)
gen.save_weights(path+'/gen.h5')
critic.save_weights(path+'/critic.h5')
siam.save_weights(path+'/siam.h5')
save_test_image_full(path)
#Losses
def mae(x,y):
return tf.reduce_mean(tf.abs(x-y))
def mse(x,y):
return tf.reduce_mean((x-y)**2)
def loss_travel(sa,sab,sa1,sab1):
l1 = tf.reduce_mean(((sa-sa1) - (sab-sab1))**2)
l2 = tf.reduce_mean(tf.reduce_sum(-(tf.nn.l2_normalize(sa-sa1, axis=[-1]) * tf.nn.l2_normalize(sab-sab1, axis=[-1])), axis=-1))
return l1+l2
def loss_siamese(sa,sa1):
logits = tf.sqrt(tf.reduce_sum((sa-sa1)**2, axis=-1, keepdims=True))
return tf.reduce_mean(tf.square(tf.maximum((delta - logits), 0)))
def d_loss_f(fake):
return tf.reduce_mean(tf.maximum(1 + fake, 0))
def d_loss_r(real):
return tf.reduce_mean(tf.maximum(1 - real, 0))
def g_loss_f(fake):
return tf.reduce_mean(- fake)
#Get models and optimizers
def get_networks(shape, load_model=False, path=None):
if not load_model:
gen,critic,siam = build()
else:
gen,critic,siam = load(path)
print('Built networks')
opt_gen = Adam(0.0001, 0.5)
opt_disc = Adam(0.0001, 0.5)
return gen,critic,siam, [opt_gen,opt_disc]
#Set learning rate
def update_lr(lr):
opt_gen.learning_rate = lr
opt_disc.learning_rate = lr
#Training Functions
#Train Generator, Siamese and Critic
@tf.function
def train_all(a,b):
#splitting spectrogram in 3 parts
aa,aa2,aa3 = extract_image(a)
bb,bb2,bb3 = extract_image(b)
with tf.GradientTape() as tape_gen, tf.GradientTape() as tape_disc:
#translating A to B
fab = gen(aa, training=True)
fab2 = gen(aa2, training=True)
fab3 = gen(aa3, training=True)
#identity mapping B to B COMMENT THESE 3 LINES IF THE IDENTITY LOSS TERM IS NOT NEEDED
fid = gen(bb, training=True)
fid2 = gen(bb2, training=True)
fid3 = gen(bb3, training=True)
#concatenate/assemble converted spectrograms
fabtot = assemble_image([fab,fab2,fab3])
#feed concatenated spectrograms to critic
cab = critic(fabtot, training=True)
cb = critic(b, training=True)
#feed 2 pairs (A,G(A)) extracted spectrograms to Siamese
sab = siam(fab, training=True)
sab2 = siam(fab3, training=True)
sa = siam(aa, training=True)
sa2 = siam(aa3, training=True)
#identity mapping loss
loss_id = (mae(bb,fid)+mae(bb2,fid2)+mae(bb3,fid3))/3. #loss_id = 0. IF THE IDENTITY LOSS TERM IS NOT NEEDED
#travel loss
loss_m = loss_travel(sa,sab,sa2,sab2)+loss_siamese(sa,sa2)
#generator and critic losses
loss_g = g_loss_f(cab)
loss_dr = d_loss_r(cb)
loss_df = d_loss_f(cab)
loss_d = (loss_dr+loss_df)/2.
#generator+siamese total loss
lossgtot = loss_g+10.*loss_m+0.5*loss_id #CHANGE LOSS WEIGHTS HERE (COMMENT OUT +w*loss_id IF THE IDENTITY LOSS TERM IS NOT NEEDED)
#computing and applying gradients
grad_gen = tape_gen.gradient(lossgtot, gen.trainable_variables+siam.trainable_variables)
opt_gen.apply_gradients(zip(grad_gen, gen.trainable_variables+siam.trainable_variables))
grad_disc = tape_disc.gradient(loss_d, critic.trainable_variables)
opt_disc.apply_gradients(zip(grad_disc, critic.trainable_variables))
return loss_dr,loss_df,loss_g,loss_id
#Train Critic only
@tf.function
def train_d(a,b):
aa,aa2,aa3 = extract_image(a)
with tf.GradientTape() as tape_disc:
fab = gen(aa, training=True)
fab2 = gen(aa2, training=True)
fab3 = gen(aa3, training=True)
fabtot = assemble_image([fab,fab2,fab3])
cab = critic(fabtot, training=True)
cb = critic(b, training=True)
loss_dr = d_loss_r(cb)
loss_df = d_loss_f(cab)
loss_d = (loss_dr+loss_df)/2.
grad_disc = tape_disc.gradient(loss_d, critic.trainable_variables)
opt_disc.apply_gradients(zip(grad_disc, critic.trainable_variables))
return loss_dr,loss_df
#After Training, use these functions to convert data with the generator and save the results
#Assembling generated Spectrogram chunks into final Spectrogram
def specass(a,spec):
but=False
con = np.array([])
nim = a.shape[0]
for i in range(nim-1):
im = a[i]
im = np.squeeze(im)
if not but:
con=im
but=True
else:
con = np.concatenate((con,im), axis=1)
diff = spec.shape[1]-(nim*shape)
a = np.squeeze(a)
con = np.concatenate((con,a[-1,:,-diff:]), axis=1)
return np.squeeze(con)
#Splitting input spectrogram into different chunks to feed to the generator
def chopspec(spec):
dsa=[]
for i in range(spec.shape[1]//shape):
im = spec[:,i*shape:i*shape+shape]
im = np.reshape(im, (im.shape[0],im.shape[1],1))
dsa.append(im)
imlast = spec[:,-shape:]
imlast = np.reshape(imlast, (imlast.shape[0],imlast.shape[1],1))
dsa.append(imlast)
return np.array(dsa, dtype=np.float32)
#Converting from source Spectrogram to target Spectrogram
def towave(number, spec, name, path='../content/', show=False):
specarr = chopspec(spec)
print(specarr.shape)
a = specarr
print('Generating...')
ab = gen(a, training=False)
print('Assembling and Converting...')
#a = specass(a,spec)
ab = specass(ab,spec)
#awv = deprep(a)
abwv = deprep(ab)
print('Saving...')
pathfin = f'{path}/{name}'
os.mkdir(pathfin)
sf.write(pathfin+'/AB.wav', abwv, sr)
#sf.write(pathfin+'/A.wav', awv, sr)
print('Saved WAV!')
IPython.display.display(IPython.display.Audio(np.squeeze(abwv), rate=sr))
#IPython.display.display(IPython.display.Audio(np.squeeze(awv), rate=sr))
# if show:
# fig, axs = plt.subplots(ncols=2)
# axs[0].imshow(np.flip(a, -2), cmap=None)
# axs[0].axis('off')
# axs[0].set_title('Source')
# axs[1].imshow(np.flip(ab, -2), cmap=None)
# axs[1].axis('off')
# axs[1].set_title('Generated')
# plt.show()
return abwv
model_path = "C:\\Users\\User\\Desktop\\21-2_school\\capstone_project\\project\\backend\\MELGANVC-0.5553046-0.5153603-0.1086449"
#model_path = "/MELGANVC-0.5553046-0.5153603-0.1086449"
gen, critic, siam, [opt_gen, opt_disc] = get_networks(shape, load_model=True, path=model_path)
#Wav to wav conversion
def voice_conversion(number, target):
# 폴더 있으면 폴더 지우기
if os.path.isdir("./conversion_output"):
shutil.rmtree("./conversion_output")
os.makedirs("conversion_output")
if target == "Man":
#model_path = "C:\\Users\\User\\Desktop\\21-2_school\\capstone_project\\project\\backend\\MELGANVC-0.5553046-0.5153603-0.1086449"
model_path = "./MELGANVC-0.5553046-0.5153603-0.1086449"
# gen,critic,siam, [opt_gen,opt_disc] = get_networks(shape, load_model=True, path='../content/drive/MyDrive/male_male_checkpoint/MELGANVC-0.5553046-0.5153603-0.1086449/')
else:
#model_path = "C:\\Users\\User\\Desktop\\21-2_school\\capstone_project\\project\\backend\\MELGANVC-0.5380363-0.5506637-0.0765312"
model_path = "./MELGANVC-0.5380363-0.5506637-0.0765312"
gen, critic, siam, [opt_gen, opt_disc] = get_networks(shape, load_model=True, path=model_path)
for i in range(number-1):
if i %2==1:
# Wav to wav conversion
vocal_path = "./output/soundtrack"+str(i)+"/vocals.wav"
wv, sr = librosa.core.load(vocal_path, sr=24000) # Load waveform
speca = prep(wv) #Waveform to Spectrogram
#abwv = towave(i, speca, name='convert'+str(i), path='C:\\Users\\User\\Desktop\\21-2_school\\capstone_project\\project\\backend\\conversion_output')
abwv = towave(i, speca, name='convert'+str(i), path='./conversion_output')
#new_song_path = 'C:\\Users\\User\\Desktop\\21-2_school\\capstone_project\\project\\backend\\conversion_output\\convert'+str(i)+'\\AB.wav'
new_song_path = './conversion_output/convert'+str(i)+'/AB.wav'
song_length = librosa.get_duration(filename=new_song_path)
org_song_length = librosa.get_duration(filename=vocal_path)
print("result", song_length, "org", org_song_length)
(samplerate,smp)=load_wav(new_song_path)
y_third = librosa.effects.pitch_shift(smp, samplerate, n_steps= 8) #-4키로 바꾸기
paulstretch(samplerate,y_third, org_song_length/song_length ,0.25,"./conversion_output/convert"+str(i)+"/coverted_and_length_squeeze.wav")
# print(song_length2, song_length1)
#squeeze = song_length1/song_length2
#y, sr = librosa.core.load('out.wav', sr=24000) #여기에 fitch 바꿀음원파일넣기
# y_third = librosa.effects.pitch_shift(y, sr, n_steps= -4) #-4키로 바꾸기
# y_third_length = librosa.get_duration(y= y_third)
# y_third2 = librosa.effects.time_stretch(y_third, squeeze) #20초->40초 : 20/40, 23초 -> 20초 : 23/20
# y_forth = librosa.effects.pitch_shift(y_third2, sr, n_steps=+24) #-4키로 바꾸기
#speca = prep(y_third) #Waveform to Spectrogram
#abwv = towave2(speca, name='voice_conversion_pitch_right', path='C:\\Users\\User\\Desktop\\21-2_school\\capstone_project\\Flask_Prac\\voice_conversion_result') #Convert and save wav
def towave2(spec, name, path='../content/', show=False):
specarr = chopspec(spec)
print(specarr.shape)
a = specarr
print('Generating...')
print('Assembling and Converting...')
a = specass(a,spec)
awv = deprep(a)
print('Saving...')
pathfin = f'{path}/{name}'
os.mkdir(pathfin)
sf.write(pathfin+'/shift_fitch.wav', awv, sr)
print('Saved WAV!')
IPython.display.display(IPython.display.Audio(np.squeeze(awv), rate=sr))
# if show:
# fig, axs = plt.subplots(ncols=2)
# axs[0].imshow(np.flip(a, -2), cmap=None)
# axs[0].axis('off')
# axs[0].set_title('Source')
# plt.show()
return awv
def load_wav(filename):
try:
wavedata=scipy.io.wavfile.read(filename)
samplerate=int(wavedata[0])
smp=wavedata[1]*(1.0/32768.0)
if len(smp.shape)>1: #convert to mono
smp=(smp[:,0]+smp[:,1])*0.5
return (samplerate,smp)
except:
print ("Error loading wav: "+filename)
return None
########################################
def paulstretch(samplerate,smp,stretch,windowsize_seconds,outfilename):
outfile=wave.open(outfilename,"wb")
outfile.setsampwidth(2)
outfile.setframerate(samplerate)
outfile.setnchannels(1)
#make sure that windowsize is even and larger than 16
windowsize=int(windowsize_seconds*samplerate)
if windowsize<16:
windowsize=16
windowsize=int(windowsize/2)*2
half_windowsize=int(windowsize/2)
#correct the end of the smp
end_size=int(samplerate*0.05)
if end_size<16:
end_size=16
smp[len(smp)-end_size:len(smp)]*=linspace(1,0,end_size)
#compute the displacement inside the input file
start_pos=0.0
displace_pos=(windowsize*0.5)/stretch
#create Hann window
window=0.5-cos(arange(windowsize,dtype='float')*2.0*pi/(windowsize-1))*0.5
old_windowed_buf=zeros(windowsize)
hinv_sqrt2=(1+sqrt(0.5))*0.5
hinv_buf=hinv_sqrt2-(1.0-hinv_sqrt2)*cos(arange(half_windowsize,dtype='float')*2.0*pi/half_windowsize)
while True:
#get the windowed buffer
istart_pos=int(floor(start_pos))
buf=smp[istart_pos:istart_pos+windowsize]
if len(buf)<windowsize:
buf=append(buf,zeros(windowsize-len(buf)))
buf=buf*window
#get the amplitudes of the frequency components and discard the phases
freqs=abs(fft.rfft(buf))
#randomize the phases by multiplication with a random complex number with modulus=1
ph=random.uniform(0,2*pi,len(freqs))*1j
freqs=freqs*exp(ph)
#do the inverse FFT
buf=fft.irfft(freqs)
#window again the output buffer
buf*=window
#overlap-add the output
output=buf[0:half_windowsize]+old_windowed_buf[half_windowsize:windowsize]
old_windowed_buf=buf
#remove the resulted amplitude modulation
output*=hinv_buf
#clamp the values to -1..1
output[output>1.0]=1.0
output[output<-1.0]=-1.0
#write the output to wav file
outfile.writeframes(int16(output*32767.0).tostring())
start_pos+=displace_pos
if start_pos>=len(smp):
print ("100 %")
break
sys.stdout.write ("%d %% \r" % int(100.0*start_pos/len(smp)))
sys.stdout.flush()
outfile.close()