-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
252 lines (200 loc) · 9.11 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
# -*- coding: utf-8 -*-
"""
-----------------------------------------------------------------------------
Copyright (c) 2019 Descript Inc.
Please see LICENSE-melgan-neurips.md.
-----------------------------------------------------------------------------
Add notes
"""
from mel2wav.dataset import AudioDataset
from mel2wav.modules import Generator, Discriminator, Audio2Mel
from mel2wav.utils import save_sample
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import yaml
import numpy as np
import time
import argparse
from pathlib import Path
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--save_path", required=True)
parser.add_argument("--load_path", default=None)
parser.add_argument("--n_mel_channels", type=int, default=80)
parser.add_argument("--ngf", type=int, default=32)
parser.add_argument("--n_residual_layers", type=int, default=3)
parser.add_argument("--ndf", type=int, default=16)
parser.add_argument("--num_D", type=int, default=3)
parser.add_argument("--n_layers_D", type=int, default=4)
parser.add_argument("--downsamp_factor", type=int, default=4)
parser.add_argument("--lambda_feat", type=float, default=10)
parser.add_argument("--cond_disc", action="store_true")
parser.add_argument("--data_path", default=None, type=Path)
parser.add_argument("--batch_size", type=int, default=16)
parser.add_argument("--seq_len", type=int, default=8192)
parser.add_argument("--epochs", type=int, default=3000)
parser.add_argument("--log_interval", type=int, default=100)
parser.add_argument("--save_interval", type=int, default=1000)
parser.add_argument("--n_test_samples", type=int, default=8)
args = parser.parse_args()
return args
def main():
args = parse_args()
root = Path(args.save_path)
load_root = Path(args.load_path) if args.load_path else None
root.mkdir(parents=True, exist_ok=True)
####################################
# Dump arguments and create logger #
####################################
with open(root / "args.yml", "w") as f: # args.ymlに色々な設定情報が入っている。
yaml.dump(args, f)
writer = SummaryWriter(str(root))
#######################
# Load PyTorch Models #
#######################
netG = Generator(args.n_mel_channels, args.ngf, args.n_residual_layers).cuda() # Generatorを定義
netD = Discriminator( # Deneratorを定義
args.num_D, args.ndf, args.n_layers_D, args.downsamp_factor
).cuda()
fft = Audio2Mel(n_mel_channels=args.n_mel_channels).cuda() # 生wavをMelスペクトルに変換する関数
print(netG)
print(netD)
#####################
# Create optimizers #
#####################
optG = torch.optim.Adam(netG.parameters(), lr=1e-4, betas=(0.5, 0.9))
optD = torch.optim.Adam(netD.parameters(), lr=1e-4, betas=(0.5, 0.9))
if load_root and load_root.exists(): # 過去の係数の読み込み。もし、前回のptが存在していれば。
netG.load_state_dict(torch.load(load_root / "netG.pt"))
optG.load_state_dict(torch.load(load_root / "optG.pt"))
netD.load_state_dict(torch.load(load_root / "netD.pt"))
optD.load_state_dict(torch.load(load_root / "optD.pt"))
#######################
# Create data loaders #
#######################
# wavデータのLOADERと 訓練用と試験用のデータセットの準備
train_set = AudioDataset(
Path(args.data_path) / "train_files.txt", args.seq_len, sampling_rate=22050
)
test_set = AudioDataset(
Path(args.data_path) / "test_files.txt",
22050 * 4,
sampling_rate=22050,
augment=False,
)
train_loader = DataLoader(train_set, batch_size=args.batch_size, num_workers=4)
test_loader = DataLoader(test_set, batch_size=1)
##########################
# Dumping original audio #
##########################
test_voc = []
test_audio = []
for i, x_t in enumerate(test_loader):
x_t = x_t.cuda()
s_t = fft(x_t).detach()
test_voc.append(s_t.cuda())
test_audio.append(x_t)
audio = x_t.squeeze().cpu()
save_sample(root / ("original_%d.wav" % i), 22050, audio) # 試験用のWAVの保存
writer.add_audio("original/sample_%d.wav" % i, audio, 0, sample_rate=22050)
if i == args.n_test_samples - 1:
break
costs = []
start = time.time()
# enable cudnn autotuner to speed up training
torch.backends.cudnn.benchmark = True #
best_mel_reconst = 1000000
steps = 0
#--------------------------------------------------------------------------------
# START EPOCH HERE ! epoch default is 3000
#
for epoch in range(1, args.epochs + 1):
for iterno, x_t in enumerate(train_loader):
x_t = x_t.cuda()
s_t = fft(x_t).detach()
x_pred_t = netG(s_t.cuda())
with torch.no_grad():
s_pred_t = fft(x_pred_t.detach())
s_error = F.l1_loss(s_t, s_pred_t).item() # s_error : 本物とGeneratorで作ったFFTスペクトルの差
#######################
# Train Discriminator #
#######################
D_fake_det = netD(x_pred_t.cuda().detach())
D_real = netD(x_t.cuda())
loss_D = 0
for scale in D_fake_det:
loss_D += F.relu(1 + scale[-1]).mean()
for scale in D_real:
loss_D += F.relu(1 - scale[-1]).mean()
netD.zero_grad()
loss_D.backward()
optD.step() # Discrimator だけ opt.stepしている。
###################
# Train Generator #
###################
D_fake = netD(x_pred_t.cuda())
loss_G = 0
for scale in D_fake:
loss_G += -scale[-1].mean()
loss_feat = 0
feat_weights = 4.0 / (args.n_layers_D + 1)
D_weights = 1.0 / args.num_D
wt = D_weights * feat_weights
for i in range(args.num_D):
for j in range(len(D_fake[i]) - 1):
loss_feat += wt * F.l1_loss(D_fake[i][j], D_real[i][j].detach()) # loss_feat:L1ロスを計算している
netG.zero_grad()
(loss_G + args.lambda_feat * loss_feat).backward() # Loss GとL1ロスを加算している
optG.step() # Generator だけ opt.stepしている。
######################
# Update tensorboard #
######################
costs.append([loss_D.item(), loss_G.item(), loss_feat.item(), s_error])
writer.add_scalar("loss/discriminator", costs[-1][0], steps)
writer.add_scalar("loss/generator", costs[-1][1], steps)
writer.add_scalar("loss/feature_matching", costs[-1][2], steps)
writer.add_scalar("loss/mel_reconstruction", costs[-1][3], steps)
steps += 1
#-------------------------------------------------
# parser.add_argument("--log_interval", type=int, default=100)
# parser.add_argument("--save_interval", type=int, default=1000)
#
if steps % args.save_interval == 0:
st = time.time()
with torch.no_grad():
for i, (voc, _) in enumerate(zip(test_voc, test_audio)):
pred_audio = netG(voc)
pred_audio = pred_audio.squeeze().cpu()
save_sample(root / ("generated_%d.wav" % i), 22050, pred_audio)
writer.add_audio(
"generated/sample_%d.wav" % i,
pred_audio,
epoch,
sample_rate=22050,
)
torch.save(netG.state_dict(), root / "netG.pt")
torch.save(optG.state_dict(), root / "optG.pt")
torch.save(netD.state_dict(), root / "netD.pt")
torch.save(optD.state_dict(), root / "optD.pt")
if np.asarray(costs).mean(0)[-1] < best_mel_reconst:
best_mel_reconst = np.asarray(costs).mean(0)[-1]
torch.save(netD.state_dict(), root / "best_netD.pt")
torch.save(netG.state_dict(), root / "best_netG.pt")
print("Took %5.4fs to generate samples" % (time.time() - st))
print("-" * 100)
if steps % args.log_interval == 0:
print(
"Epoch {} | Iters {} / {} | ms/batch {:5.2f} | loss {}".format(
epoch,
iterno,
len(train_loader),
1000 * (time.time() - start) / args.log_interval,
np.asarray(costs).mean(0),
)
)
costs = []
start = time.time()
if __name__ == "__main__":
main()