-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtrain.py
130 lines (101 loc) · 3.26 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
import torch
from torch.utils.tensorboard import SummaryWriter
import yaml
from ddsp.model import DDSP
from effortless_config import Config
from os import path
from preprocess import Dataset
from tqdm import tqdm
from ddsp.core import multiscale_fft, safe_log, mean_std_loudness
import soundfile as sf
from einops import rearrange
from ddsp.utils import get_scheduler
import numpy as np
class args(Config):
CONFIG = "config.yaml"
NAME = "debug"
ROOT = "runs"
STEPS = 500000
BATCH = 16
START_LR = 1e-3
STOP_LR = 1e-4
DECAY_OVER = 400000
args.parse_args()
with open(args.CONFIG, "r") as config:
config = yaml.safe_load(config)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = DDSP(**config["model"]).to(device)
dataset = Dataset(config["preprocess"]["out_dir"])
dataloader = torch.utils.data.DataLoader(
dataset,
args.BATCH,
True,
drop_last=True,
)
mean_loudness, std_loudness = mean_std_loudness(dataloader)
config["data"]["mean_loudness"] = mean_loudness
config["data"]["std_loudness"] = std_loudness
writer = SummaryWriter(path.join(args.ROOT, args.NAME), flush_secs=20)
with open(path.join(args.ROOT, args.NAME, "config.yaml"), "w") as out_config:
yaml.safe_dump(config, out_config)
opt = torch.optim.Adam(model.parameters(), lr=args.START_LR)
schedule = get_scheduler(
len(dataloader),
args.START_LR,
args.STOP_LR,
args.DECAY_OVER,
)
# scheduler = torch.optim.lr_scheduler.LambdaLR(opt, schedule)
best_loss = float("inf")
mean_loss = 0
n_element = 0
step = 0
epochs = int(np.ceil(args.STEPS / len(dataloader)))
for e in tqdm(range(epochs)):
for s, p, l in dataloader:
s = s.to(device)
p = p.unsqueeze(-1).to(device)
l = l.unsqueeze(-1).to(device)
l = (l - mean_loudness) / std_loudness
y = model(p, l).squeeze(-1)
ori_stft = multiscale_fft(
s,
config["train"]["scales"],
config["train"]["overlap"],
)
rec_stft = multiscale_fft(
y,
config["train"]["scales"],
config["train"]["overlap"],
)
loss = 0
for s_x, s_y in zip(ori_stft, rec_stft):
lin_loss = (s_x - s_y).abs().mean()
log_loss = (safe_log(s_x) - safe_log(s_y)).abs().mean()
loss = loss + lin_loss + log_loss
opt.zero_grad()
loss.backward()
opt.step()
writer.add_scalar("loss", loss.item(), step)
step += 1
n_element += 1
mean_loss += (loss.item() - mean_loss) / n_element
if not e % 10:
writer.add_scalar("lr", schedule(e), e)
writer.add_scalar("reverb_decay", model.reverb.decay.item(), e)
writer.add_scalar("reverb_wet", model.reverb.wet.item(), e)
# scheduler.step()
if mean_loss < best_loss:
best_loss = mean_loss
torch.save(
model.state_dict(),
path.join(args.ROOT, args.NAME, "state.pth"),
)
mean_loss = 0
n_element = 0
audio = torch.cat([s, y], -1).reshape(-1).detach().cpu().numpy()
sf.write(
path.join(args.ROOT, args.NAME, f"eval_{e:06d}.wav"),
audio,
config["preprocess"]["sampling_rate"],
)