forked from hujinsen/StarGAN-Voice-Conversion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
executable file
·268 lines (205 loc) · 10.8 KB
/
train.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
import os
import numpy as np
import argparse
import time
import librosa
import glob
from preprocess import *
from model import *
from sklearn.utils import shuffle
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from utility import *
def get_files_labels(pattern: str):
files = glob.glob(pattern)
names = []
for f in files:
t = os.path.normpath(f).rsplit(os.sep, maxsplit=1)[1] #'./data/processed/SF2-100008_11.npy'
name = t.rsplit('.', maxsplit=1)[0]
names.append(name)
return files, names
def train(processed_dir: str, test_wav_dir: str):
timestr = time.strftime("%Y-%m-%d-%H-%M", time.localtime()) #like '2018-10-10-14-47'
all_speaker = get_speakers()
label_enc = LabelEncoder()
label_enc.fit(all_speaker)
lambda_cycle = 10
lambda_identity = 5
lambda_classifier = 3
generator_learning_rate = 0.0001
generator_learning_rate_decay = generator_learning_rate / 20000
discriminator_learning_rate = 0.0001
discriminator_learning_rate_decay = discriminator_learning_rate / 20000
domain_classifier_learning_rate = 0.0001
domain_classifier_learning_rate_decay = domain_classifier_learning_rate / 20000
#====================load data================#
print('Loading Data...')
files, names = get_files_labels(os.path.join(processed_dir, '*.npy'))
assert len(files) > 0
normlizer = Normalizer()
exclude_dict = {} #key that not appear in the value list.(eg. SF1:[TM1**.wav,TM2**.wav,SF2**.wav ... ])
for s in all_speaker:
p = os.path.join(processed_dir, '*.npy') #'./data/processed/*.npy'
temp = [fn for fn in glob.glob(p) if fn.find(s) == -1]
exclude_dict[s] = temp
print('Loading Data Done.')
#====================create model=============#
BATCHSIZE = 1
model = StarGANVC(num_features=FEATURE_DIM, frames=FRAMES, batchsize=BATCHSIZE)
#====================start train==============#
EPOCH = 201
num_samples = len(files)
for epoch in range(1, EPOCH+1, 1):
start_time_epoch = time.time()
files_shuffled, names_shuffled = shuffle(files, names)
for i in range(num_samples // BATCHSIZE):
num_iterations = num_samples // BATCHSIZE * (epoch-1) + i
if num_iterations > 100000:
domain_classifier_learning_rate = max(0, domain_classifier_learning_rate - domain_classifier_learning_rate_decay)
generator_learning_rate = max(0, generator_learning_rate - generator_learning_rate_decay)
discriminator_learning_rate = max(0, discriminator_learning_rate - discriminator_learning_rate_decay)
if discriminator_learning_rate == 0 or generator_learning_rate == 0:
print('Early stop training.')
break
start = i * BATCHSIZE
end = (i + 1) * BATCHSIZE
if end > num_samples:
end = num_samples
X, X_t, y, y_t = [], [], [], []
#get target file paths
batchnames = names_shuffled[start:end]
pre_targets = []
for name in batchnames:
name = name.split(sep='-')[0] #SF1
t = np.random.choice(exclude_dict[name], 1)[0]
pre_targets.append(t)
#one batch train data
for one_filename, one_name, one_target in zip(files_shuffled[start:end], names_shuffled[start:end], pre_targets):
#target name
t = os.path.normpath(one_target).rsplit(os.sep, maxsplit=1)[1] #'./data/processed/SF2-100008_11.npy'
target_speaker_name = t.rsplit('.', maxsplit=1)[0].split('-')[0]
#source name
speaker_name = one_name.split('-')[0] #SF1
#shape [36,512]
one_file = np.load(one_filename)
one_file = normlizer.forward_process(one_file, speaker_name)
#shape [36,512,1]
one_file = np.reshape(one_file, [one_file.shape[0], one_file.shape[1], 1])
X.append(one_file)
#source label
temp_index = label_enc.transform([speaker_name])[0]
temp_arr_s = np.zeros([
len(all_speaker),
])
temp_arr_s[temp_index] = 1
y.append(temp_arr_s)
#load target files and labels
one_file_t = np.load(one_target)
one_file_t = normlizer.forward_process(one_file_t, target_speaker_name)
#[36,512,1]
one_file_t = np.reshape(one_file_t, [one_file_t.shape[0], one_file_t.shape[1], 1])
X_t.append(one_file_t)
#target label
temp_index_t = label_enc.transform([target_speaker_name])[0]
temp_arr_t = np.zeros([
len(all_speaker),
])
temp_arr_t[temp_index_t] = 1
y_t.append(temp_arr_t)
generator_loss, discriminator_loss, domain_classifier_loss = model.train(\
input_source=X, input_target=X_t, source_label=y, \
target_label=y_t, generator_learning_rate=generator_learning_rate,\
discriminator_learning_rate=discriminator_learning_rate,\
classifier_learning_rate=domain_classifier_learning_rate, \
lambda_identity=lambda_identity, lambda_cycle=lambda_cycle,\
lambda_classifier=lambda_classifier
)
if num_iterations % 10 == 0:
print('Iteration: {:07d},Generator Loss : {:.3f}, Discriminator Loss : {:.3f}, domain_classifier_loss: {:.3f}'\
.format(num_iterations, generator_loss, discriminator_loss, domain_classifier_loss))
#=======================test model==========================
file_path = os.path.join('out/', f'{epoch}_{timestr}')
if epoch % 10 == 0:
print('============test model============')
#out put path
os.makedirs(file_path, exist_ok=True)
tempfiles = []
for one_speaker in all_speaker:
p = os.path.join(test_wav_dir, f'{one_speaker}/*.wav')
wavs = glob.glob(p)
tempfiles.append(wavs[0])
tempfiles.append(wavs[1]) #'./data/fourspeakers_test/200006.wav'
for one_file in tempfiles:
_, speaker, name = os.path.normpath(one_file).rsplit(os.sep, maxsplit=2)
wav_, fs = librosa.load(one_file, sr=SAMPLE_RATE, mono=True, dtype=np.float64)
wav, pad_length = pad_wav_to_get_fixed_frames(wav_, frames=FRAMES)
f0, timeaxis = pyworld.harvest(wav, fs)
sp = pyworld.cheaptrick(wav, f0, timeaxis, fs, fft_size=FFTSIZE)
ap = pyworld.d4c(wav, f0, timeaxis, fs, fft_size=FFTSIZE)
coded_sp = pyworld.code_spectral_envelope(sp, fs, FEATURE_DIM)
#one audio file to multiple slices(that's one_test_sample),every slice is an input
one_test_sample = []
csp_transpose = coded_sp.T #36x512 36x128...
for i in range(0, csp_transpose.shape[1] - FRAMES + 1, FRAMES):
t = csp_transpose[:, i:i + FRAMES]
t = normlizer.forward_process(t, speaker)
t = np.reshape(t, [t.shape[0], t.shape[1], 1])
one_test_sample.append(t)
#target label 1->2, 2->3, 3->0, 0->1
one_test_sample_label = np.zeros([len(one_test_sample), len(all_speaker)])
temp_index = label_enc.transform([speaker])[0]
temp_index = (temp_index + 2) % len(all_speaker)
for i in range(len(one_test_sample)):
one_test_sample_label[i][temp_index] = 1
#get conversion target name ,like SF1
target_name = label_enc.inverse_transform([temp_index])[0]
generated_results = model.test(one_test_sample, one_test_sample_label)
reshpaped_res = []
for one in generated_results:
t = np.reshape(one, [one.shape[0], one.shape[1]])
t = normlizer.backward_process(t, target_name)
reshpaped_res.append(t)
#collect the generated slices, and concate the array to be a whole representation of the whole audio
c = []
for one_slice in reshpaped_res:
one_slice = np.ascontiguousarray(one_slice.T, dtype=np.float64)
decoded_sp = pyworld.decode_spectral_envelope(one_slice, SAMPLE_RATE, fft_size=FFTSIZE)
c.append(decoded_sp)
concated = np.concatenate((c), axis=0)
#f0 convert
f0 = normlizer.pitch_conversion(f0, speaker, target_name)
synwav = pyworld.synthesize(f0, concated, ap, fs)
#remove synthesized wav paded length
synwav = synwav[:-pad_length]
#save synthesized wav to file
wavname = f'{speaker}-{target_name}+{name}'
wavpath = os.path.join(file_path, 'wavs')
if not os.path.exists(wavpath):
os.makedirs(wavpath, exist_ok=True)
librosa.output.write_wav(f'{wavpath}/{wavname}', synwav, sr=fs)
print(f'[save]:{wavpath}/{wavname}')
print('============test finished!============')
if epoch % 10 == 0:
print('============save model============')
model_path = os.path.join(file_path, 'model')
os.makedirs(model_path, exist_ok=True)
print(f'[save]: {model_path}')
model.save(directory=model_path, filename=MODEL_NAME)
end_time_epoch = time.time()
time_elapsed_epoch = end_time_epoch - start_time_epoch
print('Time Elapsed for Epoch %d: %02d:%02d:%02d' % (epoch, time_elapsed_epoch // 3600, (time_elapsed_epoch % 3600 // 60),
(time_elapsed_epoch % 60 // 1)))
if __name__ == '__main__':
processed_dir = './data/processed'
test_wav_dir = './data/fourspeakers_test'
parser = argparse.ArgumentParser(description='Train StarGAN Voice conversion model.')
parser.add_argument('--processed_dir', type=str, help='train dataset directory that contains processed npy and npz files', default=processed_dir)
parser.add_argument('--test_wav_dir', type=str, help='test directory that contains raw audios', default=test_wav_dir)
argv = parser.parse_args()
processed_dir = argv.processed_dir
test_wav_dir = argv.test_wav_dir
start_time = time.time()
train(processed_dir, test_wav_dir)
end_time = time.time()
time_elapsed = end_time - start_time
print('Training Time: %02d:%02d:%02d' % \
(time_elapsed // 3600, (time_elapsed % 3600 // 60), (time_elapsed % 60 // 1)))