forked from YatingMusic/ddsp-singing-vocoders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
executable file
·277 lines (228 loc) · 9.48 KB
/
solver.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
import os
import sys
import time
import shutil
import numpy as np
import soundfile as sf
import torch
from logger.saver import Saver
from logger import utils
def render(args, model, path_mel_dir, path_gendir='gen', is_part=False):
print(' [*] rendering...')
model.eval()
# list files
files = utils.traverse_dir(
path_mel_dir,
extension='npy',
is_ext=False,
is_sort=True,
is_pure=True)
num_files = len(files)
print(' > num_files:', num_files)
# run
with torch.no_grad():
for fidx in range(num_files):
fn = files[fidx]
print('--------')
print('{}/{} - {}'.format(fidx, num_files, fn))
path_mel = os.path.join(path_mel_dir, fn) + '.npy'
mel = np.load(path_mel)
mel = torch.from_numpy(mel).float().to(args.device).unsqueeze(0)
print(' mel:', mel.shape)
# forward
signal, f0_pred, _, (s_h, s_n) = model(mel)
# path
path_pred = os.path.join(path_gendir, 'pred', fn + '.wav')
if is_part:
path_pred_n = os.path.join(path_gendir, 'part', fn + '-noise.wav')
path_pred_h = os.path.join(path_gendir, 'part', fn + '-harmonic.wav')
print(' > path_pred:', path_pred)
os.makedirs(os.path.dirname(path_pred), exist_ok=True)
if is_part:
os.makedirs(os.path.dirname(path_pred_h), exist_ok=True)
# to numpy
pred = utils.convert_tensor_to_numpy(signal)
if is_part:
pred_n = utils.convert_tensor_to_numpy(s_n)
pred_h = utils.convert_tensor_to_numpy(s_h)
# save
sf.write(path_pred, pred, args.data.sampling_rate)
if is_part:
sf.write(path_pred_n, pred_n, args.data.sampling_rate)
sf.write(path_pred_h, pred_h, args.data.sampling_rate)
def test(args, model, loss_func, loader_test, path_gendir='gen', is_part=False):
print(' [*] testing...')
print(' [*] output folder:', path_gendir)
model.eval()
# losses
test_loss = 0.
test_loss_mss = 0.
test_loss_f0 = 0.
# intialization
num_batches = len(loader_test)
os.makedirs(path_gendir, exist_ok=True)
rtf_all = []
# run
with torch.no_grad():
for bidx, data in enumerate(loader_test):
fn = data['name'][0]
print('--------')
print('{}/{} - {}'.format(bidx, num_batches, fn))
# unpack data
for k in data.keys():
if k != 'name':
data[k] = data[k].to(args.device).float()
print('>>', data['name'][0])
# forward
st_time = time.time()
signal, f0_pred, _, (s_h, s_n) = model(data['mel'])
ed_time = time.time()
# crop
min_len = np.min([signal.shape[1], data['audio'].shape[1]])
signal = signal[:,:min_len]
data['audio'] = data['audio'][:,:min_len]
# RTF
run_time = ed_time - st_time
song_time = data['audio'].shape[-1] / args.data.sampling_rate
rtf = run_time / song_time
print('RTF: {} | {} / {}'.format(rtf, run_time, song_time))
rtf_all.append(rtf)
# loss
loss, (loss_mss, loss_f0) = loss_func(
signal, data['audio'], f0_pred, data['f0'])
test_loss += loss.item()
test_loss_mss += loss_mss.item()
test_loss_f0 += loss_f0.item()
# path
path_pred = os.path.join(path_gendir, 'pred', fn + '.wav')
path_anno = os.path.join(path_gendir, 'anno', fn + '.wav')
if is_part:
path_pred_n = os.path.join(path_gendir, 'part', fn + '-noise.wav')
path_pred_h = os.path.join(path_gendir, 'part', fn + '-harmonic.wav')
print(' > path_pred:', path_pred)
print(' > path_anno:', path_anno)
os.makedirs(os.path.dirname(path_pred), exist_ok=True)
os.makedirs(os.path.dirname(path_anno), exist_ok=True)
if is_part:
os.makedirs(os.path.dirname(path_pred_h), exist_ok=True)
# to numpy
pred = utils.convert_tensor_to_numpy(signal)
anno = utils.convert_tensor_to_numpy(data['audio'])
if is_part:
pred_n = utils.convert_tensor_to_numpy(s_n)
pred_h = utils.convert_tensor_to_numpy(s_h)
# save
sf.write(path_pred, pred, args.data.sampling_rate)
sf.write(path_anno, anno, args.data.sampling_rate)
if is_part:
sf.write(path_pred_n, pred_n, args.data.sampling_rate)
sf.write(path_pred_h, pred_h, args.data.sampling_rate)
# report
test_loss /= num_batches
test_loss_mss /= num_batches
test_loss_f0 /= num_batches
# check
print(' [test_loss] test_loss:', test_loss)
print(' Real Time Factor', np.mean(rtf_all))
return test_loss, test_loss_mss, test_loss_f0
def train(args, model, loss_func, loader_train, loader_test):
# saver
saver = Saver(args)
# model size
params_count = utils.get_network_paras_amount({'model': model})
saver.log_info('--- model size ---')
saver.log_info(params_count)
# optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=args.train.lr)
# run
best_loss = np.inf
num_batches = len(loader_train)
model.train()
prev_save_time = -1
saver.log_info('======= start training =======')
for epoch in range(args.train.epochs):
for batch_idx, data in enumerate(loader_train):
saver.global_step_increment()
optimizer.zero_grad()
# unpack data
for k in data.keys():
if k != 'name':
data[k] = data[k].to(args.device).float()
# forward
signal, f0_pred, _, _, = model(data['mel'])
# loss
loss, (loss_mss, loss_f0) = loss_func(
signal, data['audio'], f0_pred, data['f0'])
# handle nan loss
if torch.isnan(loss):
raise ValueError(' [x] nan loss ')
else:
# backpropagate
loss.backward()
optimizer.step()
# log loss
if saver.global_step % args.train.interval_log == 0:
saver.log_info(
'epoch: {}/{} {:3d}/{:3d} | {} | t: {:.2f} | loss: {:.6f} | time: {} | counter: {}'.format(
epoch,
args.train.epochs,
batch_idx,
num_batches,
saver.expdir,
saver.get_interval_time(),
loss.item(),
saver.get_total_time(),
saver.global_step
)
)
saver.log_info(
' > mss loss: {:.6f}, f0: {:.6f}'.format(
loss_mss.item(),
loss_f0.item(),
)
)
y, s = signal, data['audio']
saver.log_info(
"pred: max:{:.5f}, min:{:.5f}, mean:{:.5f}, rms: {:.5f}\n" \
"anno: max:{:.5f}, min:{:.5f}, mean:{:.5f}, rms: {:.5f}".format(
torch.max(y), torch.min(y), torch.mean(y), torch.mean(y** 2) ** 0.5,
torch.max(s), torch.min(s), torch.mean(s), torch.mean(s** 2) ** 0.5))
saver.log_value({
'train loss': loss.item(),
'train loss mss': loss_mss.item(),
'train loss f0': loss_f0.item(),
})
# validation
# if saver.global_step % args.train.interval_val == 0:
cur_hour = saver.get_total_time(to_str=False) // 3600
if cur_hour != prev_save_time:
# save latest
saver.save_models(
{'vocoder': model}, postfix=f'{saver.global_step}_{cur_hour}')
prev_save_time = cur_hour
# run testing set
path_testdir_runtime = os.path.join(
args.env.expdir,
'runtime_gen',
f'gen_{saver.global_step}_{cur_hour}')
test_loss, test_loss_mss, test_loss_f0 = test(
args, model, loss_func, loader_test,
path_gendir=path_testdir_runtime)
saver.log_info(
' --- <validation> --- \nloss: {:.6f}. mss loss: {:.6f}, f0: {:.6f}'.format(
test_loss, test_loss_mss, test_loss_f0
)
)
saver.log_value({
'valid loss': test_loss,
'valid loss mss': test_loss_mss,
'valid loss f0': test_loss_f0,
})
model.train()
# save best model
if test_loss < best_loss:
saver.log_info(' [V] best model updated.')
saver.save_models(
{'vocoder': model}, postfix='best')
test_loss = best_loss
saver.make_report()