-
Notifications
You must be signed in to change notification settings - Fork 21
/
trainerGAN.py
402 lines (323 loc) · 13.1 KB
/
trainerGAN.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
# Reference (https://github.com/kan-bayashi/ParallelWaveGAN/)
"""Template GAN training flow."""
import logging
import os
import abc
import torch
from collections import defaultdict
from tensorboardX import SummaryWriter
from tqdm import tqdm
class TrainerGAN(abc.ABC):
def __init__(
self,
steps,
epochs,
data_loader,
model,
criterion,
optimizer,
scheduler,
config,
device=torch.device("cpu"),
):
"""Initialize trainer.
Args:
steps (int): Initial global steps.
epochs (int): Initial global epochs.
data_loader (dict): Dict of data loaders. It must contrain "train" and "dev" loaders.
model (dict): Dict of models. It must contrain "generator" and "discriminator" models.
criterion (dict): Dict of criterions. It must contrain "stft" and "mse" criterions.
optimizer (dict): Dict of optimizers. It must contrain "generator" and "discriminator" optimizers.
scheduler (dict): Dict of schedulers. It must contrain "generator" and "discriminator" schedulers.
config (dict): Config dict loaded from yaml format configuration file.
device (torch.deive): Pytorch device instance.
"""
self.steps = steps
self.epochs = epochs
self.data_loader = data_loader
self.model = model
self.criterion = criterion
self.optimizer = optimizer
self.scheduler = scheduler
self.config = config
self.device = device
self.writer = SummaryWriter(config["outdir"])
self.total_train_loss = defaultdict(float)
self.total_eval_loss = defaultdict(float)
self.train_max_steps = config.get("train_max_steps", 0)
@abc.abstractmethod
def _train_step(self, batch):
"""Single step of training."""
pass
@abc.abstractmethod
def _eval_step(self, batch):
"""Single step of evaluation."""
pass
def run(self):
"""Run training."""
self.finish_train = False
self.tqdm = tqdm(
initial=self.steps, total=self.train_max_steps, desc="[train]"
)
while True:
self._train_epoch()
# check whether training is finished
if self.finish_train:
break
self.tqdm.close()
logging.info("Finished training.")
def save_checkpoint(self, checkpoint_path):
"""Save checkpoint.
Args:
checkpoint_path (str): Checkpoint path to be saved.
"""
state_dict = {
"optimizer": {
"generator": self.optimizer["generator"].state_dict(),
"discriminator": self.optimizer["discriminator"].state_dict(),
},
"scheduler": {
"generator": self.scheduler["generator"].state_dict(),
"discriminator": self.scheduler["discriminator"].state_dict(),
},
"steps": self.steps,
"epochs": self.epochs,
}
state_dict["model"] = {
"generator": self.model["generator"].state_dict(),
"discriminator": self.model["discriminator"].state_dict(),
}
if not os.path.exists(os.path.dirname(checkpoint_path)):
os.makedirs(os.path.dirname(checkpoint_path))
torch.save(state_dict, checkpoint_path)
def load_checkpoint(self, checkpoint_path, strict=True, load_only_params=False, load_discriminator=True):
"""Load checkpoint.
Args:
checkpoint_path (str): Checkpoint path to be loaded.
load_only_params (bool): Whether to load only model parameters.
load_discriminator (bool): Whether to load optimizer and scheduler of the discriminators.
"""
state_dict = torch.load(checkpoint_path, map_location="cpu")
self.model["generator"].load_state_dict(
state_dict["model"]["generator"], strict=strict)
self.model["discriminator"].load_state_dict(
state_dict["model"]["discriminator"], strict=strict)
if not load_only_params:
self.steps = state_dict["steps"]
self.epochs = state_dict["epochs"]
self.optimizer["generator"].load_state_dict(
state_dict["optimizer"]["generator"])
self.scheduler["generator"].load_state_dict(
state_dict["scheduler"]["generator"])
if load_discriminator:
self.optimizer["discriminator"].load_state_dict(
state_dict["optimizer"]["discriminator"])
self.scheduler["discriminator"].load_state_dict(
state_dict["scheduler"]["discriminator"])
def _train_epoch(self):
"""One epoch of training."""
for train_steps_per_epoch, batch in enumerate(self.data_loader["train"], 1):
# train one step
self._train_step(batch)
# check interval
self._check_log_interval()
self._check_eval_interval()
self._check_save_interval()
# check whether training is finished
if self.finish_train:
return
# update
self.epochs += 1
self.train_steps_per_epoch = train_steps_per_epoch
if train_steps_per_epoch > 200:
logging.info(
f"(Steps: {self.steps}) Finished {self.epochs} epoch training "
f"({self.train_steps_per_epoch} steps per epoch)."
)
def _eval_epoch(self):
"""One epoch of evaluation."""
logging.info(f"(Steps: {self.steps}) Start evaluation.")
# change mode
for key in self.model.keys():
self.model[key].eval()
# calculate loss for each batch
for eval_steps_per_epoch, batch in enumerate(
tqdm(self.data_loader["dev"], desc="[eval]"), 1
):
# eval one step
self._eval_step(batch)
logging.info(
f"(Steps: {self.steps}) Finished evaluation "
f"({eval_steps_per_epoch} steps per epoch)."
)
# average loss
for key in self.total_eval_loss.keys():
self.total_eval_loss[key] /= eval_steps_per_epoch
logging.info(
f"(Steps: {self.steps}) {key} = {self.total_eval_loss[key]:.4f}."
)
# record
self._write_to_tensorboard(self.total_eval_loss)
# reset
self.total_eval_loss = defaultdict(float)
# restore mode
for key in self.model.keys():
self.model[key].train()
def _metric_loss(self, predict_y, natural_y, mode='train'):
"""Metric losses."""
metric_loss=0.0
# mel spectrogram loss
if self.config.get('use_mel_loss', False):
mel_loss = self.criterion["mel"](predict_y, natural_y)
mel_loss *= self.config["lambda_mel_loss"]
self._record_loss('mel_loss', mel_loss, mode=mode)
metric_loss += mel_loss
# multi-resolution sfft loss
if self.config.get('use_stft_loss', False):
sc_loss, mag_loss = self.criterion["stft"](predict_y, natural_y)
sc_loss *= self.config["lambda_stft_loss"]
mag_loss *= self.config["lambda_stft_loss"]
self._record_loss('spectral_convergence_loss', sc_loss, mode=mode)
self._record_loss('log_stft_magnitude_loss', mag_loss, mode=mode)
metric_loss += (sc_loss + mag_loss)
# waveform shape loss
if self.config.get("use_shape_loss", False):
shape_loss = self.criterion["shape"](predict_y, natural_y)
shape_loss *= self.config["lambda_shape_loss"]
self._record_loss('shape_loss', shape_loss, mode=mode)
metric_loss += shape_loss
return metric_loss
def _adv_loss(self, predict_p, natural_p=None, mode='train'):
"""Adversarial loss."""
adv_loss = self.criterion["gen_adv"](predict_p)
# feature matching loss
if natural_p is not None:
fm_loss = self.criterion["feat_match"](predict_p, natural_p)
self._record_loss('feature_matching_loss', fm_loss, mode=mode)
adv_loss += self.config["lambda_feat_match"] * fm_loss
adv_loss *= self.config["lambda_adv"]
self._record_loss('adversarial_loss', adv_loss, mode=mode)
return adv_loss
def _dis_loss(self, predict_p, natural_p, mode='train'):
"""Discriminator loss."""
real_loss, fake_loss = self.criterion["dis_adv"](predict_p, natural_p)
dis_loss = real_loss + fake_loss
self._record_loss('real_loss', real_loss, mode=mode)
self._record_loss('fake_loss', fake_loss, mode=mode)
self._record_loss('discriminator_loss', dis_loss, mode=mode)
return dis_loss
def _update_generator(self, gen_loss):
"""Update generator."""
self.optimizer["generator"].zero_grad()
gen_loss.backward()
if self.config["generator_grad_norm"] > 0:
torch.nn.utils.clip_grad_norm_(
self.model["generator"].parameters(),
self.config["generator_grad_norm"],
)
self.optimizer["generator"].step()
self.scheduler["generator"].step()
def _update_discriminator(self, dis_loss):
"""Update discriminator."""
self.optimizer["discriminator"].zero_grad()
dis_loss.backward()
if self.config["discriminator_grad_norm"] > 0:
torch.nn.utils.clip_grad_norm_(
self.model["discriminator"].parameters(),
self.config["discriminator_grad_norm"],
)
self.optimizer["discriminator"].step()
self.scheduler["discriminator"].step()
def _record_loss(self, name, loss, mode='train'):
"""Record loss."""
if torch.is_tensor(loss):
loss = loss.item()
if mode == 'train':
self.total_train_loss[f"train/{name}"] += loss
elif mode == 'eval':
self.total_eval_loss[f"eval/{name}"] += loss
else:
raise NotImplementedError(f"Mode ({mode}) is not supported!")
def _write_to_tensorboard(self, loss):
"""Write to tensorboard."""
for key, value in loss.items():
self.writer.add_scalar(key, value, self.steps)
def _check_save_interval(self):
if self.steps and (self.steps % self.config["save_interval_steps"] == 0):
self.save_checkpoint(
os.path.join(self.config["outdir"], f"checkpoint-{self.steps}steps.pkl")
)
logging.info(f"Successfully saved checkpoint @ {self.steps} steps.")
def _check_eval_interval(self):
if self.steps % self.config["eval_interval_steps"] == 0:
self._eval_epoch()
def _check_log_interval(self):
if self.steps % self.config["log_interval_steps"] == 0:
for key in self.total_train_loss.keys():
self.total_train_loss[key] /= self.config["log_interval_steps"]
logging.info(
f"(Steps: {self.steps}) {key} = {self.total_train_loss[key]:.4f}."
)
self._write_to_tensorboard(self.total_train_loss)
# reset
self.total_train_loss = defaultdict(float)
def _check_train_finish(self):
if self.steps >= self.train_max_steps:
self.finish_train = True
else:
self.finish_train = False
return self.finish_train
class TrainerVQGAN(TrainerGAN):
def __init__(
self,
steps,
epochs,
data_loader,
model,
criterion,
optimizer,
scheduler,
config,
device=torch.device("cpu"),
):
super(TrainerVQGAN, self).__init__(
steps=steps,
epochs=epochs,
data_loader=data_loader,
model=model,
criterion=criterion,
optimizer=optimizer,
scheduler=scheduler,
config=config,
device=device,
)
# perplexity info
def _perplexity(self, perplexity, label=None, mode='train'):
if label:
name = f"{mode}/ppl_{label}"
else:
name = f"{mode}/ppl"
if torch.numel(perplexity) > 1:
perplexity = perplexity.tolist()
for idx, ppl in enumerate(perplexity):
self._record_loss(f"{name}_{idx}", ppl, mode=mode)
else:
self._record_loss(name, perplexity, mode=mode)
# vq loss
def _vq_loss(self, vqloss, label=None, mode='train'):
if label:
name = f"{mode}/vqloss_{label}"
else:
name = f"{mode}/vqloss"
vqloss = torch.sum(vqloss)
vqloss *= self.config["lambda_vq_loss"]
self._record_loss(name, vqloss, mode=mode)
return vqloss