forked from gudgud96/music-fader-nets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer_cvae.py
executable file
·320 lines (244 loc) · 10.8 KB
/
trainer_cvae.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
'''
CVAE model.
'''
import json
import torch
import os
import numpy as np
from model_v2 import *
from torch import optim
from torch.distributions import kl_divergence, Normal
from torch.nn import functional as F
from torch.optim.lr_scheduler import ExponentialLR
from sklearn.model_selection import train_test_split
from ptb_v2 import *
# initialization
with open('model_config_v2.json') as f:
args = json.load(f)
if not os.path.isdir('log'):
os.mkdir('log')
if not os.path.isdir('params'):
os.mkdir('params')
save_path = 'params/{}.pt'.format(args['name'])
from datetime import datetime
timestamp = str(datetime.now())
save_path_timing = 'params/{}.pt'.format(args['name'] + "_" + timestamp)
# model dimensions
EVENT_DIMS = 342
RHYTHM_DIMS = 3
NOTE_DIMS = 16
CHROMA_DIMS = 24
model = MusicAttrCVAE(roll_dims=EVENT_DIMS, rhythm_dims=RHYTHM_DIMS, note_dims=NOTE_DIMS,
chroma_dims=CHROMA_DIMS,
hidden_dims=args['hidden_dim'], z_dims=args['z_dim'],
n_step=args['time_step'])
if os.path.exists(save_path):
print("Loading {}".format(save_path))
model.load_state_dict(torch.load(save_path))
else:
print("Save path: {}".format(save_path))
optimizer = optim.Adam(model.parameters(), lr=args['lr'])
if torch.cuda.is_available():
print('Using: ', torch.cuda.get_device_name(torch.cuda.current_device()))
model.cuda()
else:
print('CPU mode')
step, pre_epoch = 0, 0
batch_size = args["batch_size"]
model.train()
# dataloaders
is_shuffle = True
data_lst, rhythm_lst, note_density_lst, chroma_lst = get_classic_piano()
tlen, vlen = int(0.8 * len(data_lst)), int(0.9 * len(data_lst))
train_ds_dist = YamahaDataset(data_lst, rhythm_lst, note_density_lst,
chroma_lst, mode="train")
train_dl_dist = DataLoader(train_ds_dist, batch_size=batch_size, shuffle=is_shuffle, num_workers=0)
val_ds_dist = YamahaDataset(data_lst, rhythm_lst, note_density_lst,
chroma_lst, mode="val")
val_dl_dist = DataLoader(val_ds_dist, batch_size=batch_size, shuffle=is_shuffle, num_workers=0)
test_ds_dist = YamahaDataset(data_lst, rhythm_lst, note_density_lst,
chroma_lst, mode="test")
test_dl_dist = DataLoader(test_ds_dist, batch_size=batch_size, shuffle=is_shuffle, num_workers=0)
dl = train_dl_dist
print("Train / Validation / Test")
print(len(train_ds_dist), len(val_ds_dist), len(test_ds_dist))
def std_normal(shape):
N = Normal(torch.zeros(shape), torch.ones(shape))
if torch.cuda.is_available():
N.loc = N.loc.cuda()
N.scale = N.scale.cuda()
return N
def loss_function(out, d,
dis,
step,
beta=.1):
# anneal beta
if step < 1000:
beta0 = 0
else:
beta0 = min((step - 10000) / 10000 * beta, beta)
CE_X = F.nll_loss(out.view(-1, out.size(-1)),
d.view(-1), reduction='mean')
# all distribution conform to standard gaussian
inputs = dis
normal = std_normal(dis.mean.size())
KLD = kl_divergence(dis, normal).mean()
return CE_X + beta0 * KLD, CE_X
def train(step, d_oh, r_oh, n_oh, d, r, n, c, r_density, n_density):
optimizer.zero_grad()
res = model(d_oh, r_oh, n_oh, c, r_density, n_density)
# package output
out, dis, z = res
# calculate loss
loss, CE_X = loss_function(out, d, dis, step, beta=args['beta'])
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1)
optimizer.step()
step += 1
output = loss.item(), CE_X.item()
return step, output
def evaluate(d_oh, r_oh, n_oh, d, r, n, c, r_density, n_density):
# calculate rhythm and note density first
r_density = torch.Tensor([Counter(k.cpu().detach().numpy())[1] / len(k) for k in r]).cuda()
n_density = torch.Tensor([sum(k.cpu().detach().numpy()) / len(k) for k in n]).cuda()
r_density = r_density.unsqueeze(-1)
n_density = n_density.unsqueeze(-1)
res = model(d_oh, r_oh, n_oh, c, r_density, n_density)
# package output
out, dis, z = res
# calculate loss
loss, CE_X = loss_function(out, d, dis, step, beta=args['beta'])
output = loss.item(), CE_X.item()
return output
def convert_to_one_hot(input, dims):
if len(input.shape) > 1:
input_oh = torch.zeros((input.shape[0], input.shape[1], dims)).cuda()
input_oh = input_oh.scatter_(-1, input.unsqueeze(-1), 1.)
else:
input_oh = torch.zeros((input.shape[0], dims)).cuda()
input_oh = input_oh.scatter_(-1, input.unsqueeze(-1), 1.)
return input_oh
def training_phase(step):
print("D - Data, R - Rhythm, N - Note, RD - Reg. Rhythm Density, ND- Reg. Note Density")
for i in range(1, args['n_epochs'] + 1):
print("Epoch {} / {}".format(i, args['n_epochs']))
batch_loss, batch_test_loss = 0, 0
b_CE_X, b_CE_R, b_CE_N = 0, 0, 0
t_CE_X, t_CE_R, t_CE_N = 0, 0, 0
b_l_r, b_l_n, t_l_r, t_l_n = 0, 0, 0, 0
for j, x in tqdm(enumerate(train_dl_dist), total=len(train_dl_dist)):
d, r, n, c, r_density, n_density = x
d, r, n, c = d.cuda().long(), r.cuda().long(), \
n.cuda().long(), c.cuda().float()
r_density, n_density = r_density.cuda().float().unsqueeze(-1), \
n_density.cuda().float().unsqueeze(-1)
d_oh = convert_to_one_hot(d, EVENT_DIMS)
r_oh = convert_to_one_hot(r, RHYTHM_DIMS)
n_oh = convert_to_one_hot(n, NOTE_DIMS)
step, loss = train(step, d_oh, r_oh, n_oh,
d, r, n, c, r_density, n_density)
loss, CE_X = loss
batch_loss += loss
b_CE_X += CE_X
for j, x in tqdm(enumerate(val_dl_dist), total=len(val_dl_dist)):
d, r, n, c, r_density, n_density = x
d, r, n, c = d.cuda().long(), r.cuda().long(), \
n.cuda().long(), c.cuda().float()
r_density, n_density = r_density.cuda().float().unsqueeze(-1), \
n_density.cuda().float().unsqueeze(-1)
d_oh = convert_to_one_hot(d, EVENT_DIMS)
r_oh = convert_to_one_hot(r, RHYTHM_DIMS)
n_oh = convert_to_one_hot(n, NOTE_DIMS)
loss = evaluate(d_oh, r_oh, n_oh,
d, r, n, c, r_density, n_density)
loss, CE_X = loss
batch_test_loss += loss
t_CE_X += CE_X
print('batch loss: {:.5f} {:.5f}'.format(batch_loss / len(train_dl_dist),
batch_test_loss / len(val_dl_dist)))
print("train loss by term - D: {:.4f} R: {:.4f} N: {:.4f} RD: {:.4f} ND: {:.4f}".format(
b_CE_X / len(train_dl_dist), b_CE_R / len(train_dl_dist),
b_CE_N / len(train_dl_dist),
b_l_r / len(train_dl_dist), b_l_n / len(train_dl_dist)
))
print("test loss by term - D: {:.4f} R: {:.4f} N: {:.4f} RD: {:.4f} ND: {:.4f}".format(
t_CE_X / len(val_dl_dist), t_CE_R / len(val_dl_dist),
t_CE_N / len(val_dl_dist),
t_l_r / len(val_dl_dist), t_l_n / len(val_dl_dist),
))
torch.save(model.cpu().state_dict(), save_path)
timestamp = str(datetime.now())
save_path_timing = 'params/{}.pt'.format(args['name'] + "_" + timestamp)
torch.save(model.cpu().state_dict(), save_path_timing)
if torch.cuda.is_available():
model.cuda()
print('Model saved as {}!'.format(save_path))
def evaluation_phase():
if torch.cuda.is_available():
model.cuda()
if os.path.exists(save_path):
print("Loading {}".format(save_path))
model.load_state_dict(torch.load(save_path))
def run(dl):
t_CE_X, t_CE_R, t_CE_N = 0, 0, 0
t_l_r, t_l_n = 0, 0
t_acc_x, t_acc_r, t_acc_n = 0, 0, 0
data_len = 0
for i, x in tqdm(enumerate(dl), total=len(dl)):
d, r, n, c, r_density, n_density = x
d, r, n, c = d.cuda().long(), r.cuda().long(), \
n.cuda().long(), c.cuda().float()
r_density, n_density = r_density.cuda().float().unsqueeze(-1), \
n_density.cuda().float().unsqueeze(-1)
d_oh = convert_to_one_hot(d, EVENT_DIMS)
r_oh = convert_to_one_hot(r, RHYTHM_DIMS)
n_oh = convert_to_one_hot(n, NOTE_DIMS)
res = model(d_oh, r_oh, n_oh, c, r_density, n_density)
# package output
out, dis, z = res
# calculate loss
loss, CE_X = loss_function(out, d, dis, step, beta=args['beta'])
# update
t_CE_X += CE_X.item()
# calculate accuracy
def acc(a, b, t, trim=False):
a = torch.argmax(a, dim=-1).squeeze().cpu().detach().numpy()
b = b.squeeze().cpu().detach().numpy()
b_acc = 0
for i in range(len(a)):
a_batch = a[i]
b_batch = b[i]
if trim:
b_batch = np.trim_zeros(b_batch)
a_batch = a_batch[:len(b_batch)]
correct = 0
for j in range(len(a_batch)):
if a_batch[j] == b_batch[j]:
correct += 1
acc = correct / len(a_batch)
b_acc += acc
return b_acc
acc_x = acc(out, d, "d", trim=True)
data_len += out.shape[0]
# accuracy update store
t_acc_x += acc_x
# Print results
print("CE: {:.4} {:.4} {:.4}".format(t_CE_X / len(dl),
t_CE_R / len(dl),
t_CE_N / len(dl)))
print("Regularized: {:.4} {:.4}".format(t_l_r / len(dl),
t_l_n / len(dl)))
print("Adversarial: {:.4} {:.4}".format(t_l_adv_r / len(dl),
t_l_adv_n / len(dl)))
print("Acc: {:.4} {:.4} {:.4}".format(t_acc_x / data_len,
t_acc_r / data_len,
t_acc_n / data_len))
if is_class:
print("Class acc: {:.4} {:.4}".format(c_acc_r / data_len,
c_acc_n / data_len))
dl = DataLoader(train_ds_dist, batch_size=128, shuffle=False, num_workers=0)
run(dl)
dl = DataLoader(test_ds_dist, batch_size=128, shuffle=False, num_workers=0)
run(dl)
training_phase(step)
evaluation_phase()