-
Notifications
You must be signed in to change notification settings - Fork 6
/
train.py
312 lines (246 loc) · 13.2 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
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
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import itertools
import os
import time
import argparse
import json
import torch
from scheduler import WarmupCosineLrScheduler
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DistributedSampler, DataLoader
import torch.multiprocessing as mp
from torch.distributed import init_process_group
from torch.nn.parallel import DistributedDataParallel
from utils import AttrDict, build_env
from utils import get_dataset_filelist, mel_spectrogram
from utils import plot_spectrogram, scan_checkpoint, load_checkpoint, save_checkpoint
from supercodec import Supercodec
from data import SoundDataset, get_dataloader
from msstftd import MultiScaleSTFTDiscriminator
from losses import total_loss, disc_loss
torch.backends.cudnn.benchmark = True
def accum_log(log, new_logs):
for key, new_value in new_logs.items():
old_value = log.get(key, 0.)
log[key] = old_value + new_value
return log
def train(rank, a, h):
if h.num_gpus > 1:
init_process_group(backend=h.dist_config['dist_backend'], init_method=h.dist_config['dist_url'], world_size=h.dist_config['world_size'] * h.num_gpus, rank=rank)
torch.cuda.manual_seed(h.seed)
device = torch.device('cuda:{:d}'.format(rank))
supercodec = Supercodec(
codebook_size=h.codebook_size,
codebook_dim=h.codebook_dim,
rq_num_quantizers=h.rq_num_quantizers,
shared_codebook = False,
strides=h.strides,
channel_mults=h.channel_mults,
training=True
)
supercodec = supercodec.to(device)
# DDP(model, device_ids=[rank], find_unused_parameters=True)
disc_model = MultiScaleSTFTDiscriminator(filters=h.filters)
disc_model = disc_model.to(device)
if rank == 0:
os.makedirs(a.checkpoint_path, exist_ok=True)
print("checkpoints directory : ", a.checkpoint_path)
if os.path.isdir(a.checkpoint_path):
cp_g = scan_checkpoint(a.checkpoint_path, 'g_')
cp_do = scan_checkpoint(a.checkpoint_path, 'do_')
steps = 0
if cp_g is None or cp_do is None:
state_dict_do = None
last_epoch = -1
else:
state_dict_g = load_checkpoint(cp_g, device)
state_dict_do = load_checkpoint(cp_do, device)
supercodec.load_state_dict(state_dict_g['generator'])
disc_model.load_state_dict(state_dict_do['discriminator'])
steps = state_dict_do['steps'] + 1
last_epoch = state_dict_do['epoch']
if h.num_gpus > 1:
supercodec = DistributedDataParallel(supercodec, device_ids=[rank], find_unused_parameters=True).to(device)
disc_model = DistributedDataParallel(disc_model, device_ids=[rank], find_unused_parameters=True).to(device)
params = [p for p in supercodec.parameters() if p.requires_grad]
disc_params = [p for p in disc_model.parameters() if p.requires_grad]
# optim_g = torch.optim.AdamW([supercodec.encoder.parameters(), supercodec.decoder.parameters()], h.learning_rate, betas=[h.adam_b1, h.adam_b2])
optim_g = torch.optim.Adam([{'params': params, 'lr': h.lr}], betas=(0.5, 0.9))
optim_d = torch.optim.Adam([{'params': disc_params, 'lr': h.disc_lr}], betas=(0.5, 0.9))
if state_dict_do is not None:
optim_g.load_state_dict(state_dict_do['optim_g'])
optim_d.load_state_dict(state_dict_do['optim_d'])
training_filelist, validation_filelist = get_dataset_filelist(a)
trainset = SoundDataset(
training_filelist,
split=True,
shuffle=True,
segment_size=h.segment_size,
max_length=a.data_max_length
)
train_sampler = DistributedSampler(trainset) if h.num_gpus > 1 else None
train_loader = DataLoader(trainset, num_workers=h.num_workers, shuffle=False,
sampler=train_sampler,
batch_size=h.batch_size,
pin_memory=True,
drop_last=True)
if rank == 0:
validset = SoundDataset(
validation_filelist,
split=False,
shuffle=True,
segment_size=h.segment_size,
max_length=a.data_max_length
)
validation_loader = DataLoader(validset, num_workers=1, shuffle=False,
sampler=None,
batch_size=1,
pin_memory=True,
drop_last=True)
sw = SummaryWriter(os.path.join(a.checkpoint_path, 'logs'))
apply_grad_penalty = a.apply_grad_penalty_every > 0 and not (steps % a.apply_grad_penalty_every)
scheduler_g = WarmupCosineLrScheduler(optim_g, max_iter=h.max_epoch * len(train_loader), eta_ratio=0.1,
warmup_iter=h.warmup_epoch * len(train_loader),
warmup_ratio=1e-4)
scheduler_d = WarmupCosineLrScheduler(optim_d, max_iter=h.max_epoch * len(train_loader),
eta_ratio=0.1,
warmup_iter=h.warmup_epoch * len(train_loader),
warmup_ratio=1e-4)
supercodec.train()
disc_model.train()
logs = {}
for epoch in range(max(0, last_epoch), a.training_epochs):
if rank == 0:
start = time.time()
print("Epoch: {}".format(epoch + 1))
if h.num_gpus > 1:
train_sampler.set_epoch(epoch)
for i, batch in enumerate(train_loader):
if rank == 0:
start_b = time.time()
wave = batch
wave = torch.autograd.Variable(wave.to(device, non_blocking=True))
recon_g, loss_w = supercodec(wave, return_recons_only=True)
wave = wave.unsqueeze(1)
#Discriminator
logits_real, fmap_real = disc_model(wave)
optim_d.zero_grad()
logits_fake, fmap_fake = disc_model(recon_g.detach())
loss_disc = disc_loss([logit_real for logit_real in logits_real], logits_fake)
loss_disc.backward(retain_graph=True)
optim_d.step()
# Generator
optim_g.zero_grad()
logits_real, fmap_real = disc_model(wave)
logits_fake, fmap_fake = disc_model(recon_g)
loss_g = total_loss(fmap_real, logits_fake, fmap_fake, wave, recon_g)
wave_mel = mel_spectrogram(wave.squeeze(1), h.n_fft, h.num_mels, h.sampling_rate, h.hop_size, h.win_size,
h.fmin, h.fmax_for_loss)
recon_g_mel = mel_spectrogram(recon_g.squeeze(1), h.n_fft, h.num_mels, h.sampling_rate, h.hop_size, h.win_size,
h.fmin, h.fmax_for_loss)
loss_mel = F.l1_loss(wave_mel, recon_g_mel) * 15
loss = loss_g + loss_w + loss_mel
loss.backward()
optim_g.step()
scheduler_g.step()
scheduler_d.step()
if rank == 0:
# STDOUT logging
if steps % a.stdout_interval == 0:
with torch.no_grad():
mel_error = F.l1_loss(wave_mel,recon_g_mel).item()
print('Steps : {:d}, Gen Loss: {:.3f}, VQ. Loss : {:.3f}, Mel-Spec. Error : {:4.3f}, Disc. Error : {:4.3f}, s/b : {:4.3f}'.
format(steps, loss_g.item(), loss_w.item(),
mel_error, loss_disc.item(), time.time() - start_b))
# checkpointing
if steps % a.checkpoint_interval == 0 and steps != 0:
checkpoint_path = "{}/g_{:08d}".format(a.checkpoint_path, steps)
save_checkpoint(checkpoint_path,
{'generator': (supercodec.module if h.num_gpus > 1 else supercodec).state_dict()})
checkpoint_path = "{}/do_{:08d}".format(a.checkpoint_path, steps)
save_checkpoint(checkpoint_path,
{
'discriminator': (
disc_model.module if h.num_gpus > 1 else disc_model).state_dict(),
'optim_g': optim_g.state_dict(), 'optim_d': optim_d.state_dict(), 'steps': steps,
'epoch': epoch})
# Tensorboard summary logging
if steps % a.summary_interval == 0:
sw.add_scalar("training/mel_spec_error", mel_error, steps)
sw.add_scalar("training/disc_loss", loss_disc, steps)
sw.add_scalar("training/loss_g", loss_g, steps)
sw.add_scalar("training/all_commit_loss", loss_w, steps)
# Validation
if steps % a.validation_interval == 0: # and steps != 0:
supercodec.eval()
torch.cuda.empty_cache()
val_err_tot = 0
with torch.no_grad():
for j, batch in enumerate(validation_loader):
wave = batch
wave = wave.to(device)
recons = supercodec(wave, return_recons_only = True)
wave_mel = mel_spectrogram(wave.squeeze(1), h.n_fft, h.num_mels, h.sampling_rate,
h.hop_size, h.win_size,
h.fmin, h.fmax_for_loss)
y_g_hat_mel = mel_spectrogram(recons.squeeze(1), h.n_fft, h.num_mels, h.sampling_rate,
h.hop_size, h.win_size,
h.fmin, h.fmax_for_loss)
val_err_tot += F.l1_loss(wave_mel, y_g_hat_mel).item()
if j <= 4:
if steps == 0:
sw.add_audio('gt/y_{}'.format(j), wave[0], steps, h.sampling_rate)
sw.add_figure('gt/y_spec_{}'.format(j), plot_spectrogram(wave_mel.squeeze(0).cpu().numpy()), steps)
sw.add_audio('generated/y_hat_{}'.format(j), recons[0], steps, h.sampling_rate)
y_hat_spec = mel_spectrogram(recons.squeeze(1), h.n_fft, h.num_mels,
h.sampling_rate, h.hop_size, h.win_size,
h.fmin, h.fmax)
sw.add_figure('generated/y_hat_spec_{}'.format(j),
plot_spectrogram(y_hat_spec.squeeze(0).cpu().numpy()), steps)
val_err = val_err_tot / (j + 1)
sw.add_scalar("validation/mel_spec_error", val_err, steps)
supercodec.train()
steps += 1
if rank == 0:
print('Time taken for epoch {} is {} sec\n'.format(epoch + 1, int(time.time() - start)))
def main():
print('Initializing Training Process..')
parser = argparse.ArgumentParser()
parser.add_argument('--group_name', default=None)
parser.add_argument('--input_wavs_dir', default="")
parser.add_argument('--input_wavs_dir_validation', default="")
parser.add_argument('--input_training_file', default="")
parser.add_argument('--input_validation_file',
default="")
parser.add_argument('--checkpoint_path', default='test/')
parser.add_argument('--config', default='config_v1.json')
parser.add_argument('--training_epochs', default=3100, type=int)
parser.add_argument('--apply_grad_penalty_every', default=4, type=int)
parser.add_argument('--data_max_length', default=32000, type=int)
parser.add_argument('--stdout_interval', default=5, type=int)
parser.add_argument('--checkpoint_interval', default=5000, type=int)
parser.add_argument('--summary_interval', default=100, type=int)
parser.add_argument('--validation_interval', default=1000, type=int)
parser.add_argument('--fine_tuning', default=True, type=bool)
a = parser.parse_args()
with open(a.config) as f:
data = f.read()
json_config = json.loads(data)
h = AttrDict(json_config)
build_env(a.config, 'config.json', a.checkpoint_path)
torch.manual_seed(h.seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(h.seed)
h.num_gpus = torch.cuda.device_count()
h.batch_size = int(h.batch_size / h.num_gpus)
print('Batch size per GPU :', h.batch_size)
else:
pass
if h.num_gpus > 1:
mp.spawn(train, nprocs=h.num_gpus, args=(a, h,))
else:
train(0, a, h)
if __name__ == '__main__':
main()