-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodel_v2.py
executable file
·587 lines (476 loc) · 19.3 KB
/
model_v2.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
import torch
from torch import nn
from torch.nn import functional as F
from torch.distributions import Normal
from torch.autograd import Function
from collections import Counter
class MusicAttrRegVAE(nn.Module):
'''
Music FaderNets, vanilla VAE model.
Regularization loss can be GLSR or Pati et al. in trainer section.
'''
def __init__(self,
roll_dims,
rhythm_dims,
note_dims,
chroma_dims,
hidden_dims,
z_dims,
n_step,
k=1000):
super(MusicAttrRegVAE, self).__init__()
# encoder
self.gru_r = nn.GRU(roll_dims, hidden_dims, batch_first=True, bidirectional=True)
self.gru_n = nn.GRU(roll_dims, hidden_dims, batch_first=True, bidirectional=True)
self.gru_c = nn.GRU(roll_dims, hidden_dims, batch_first=True, bidirectional=True)
# sub-decoder
self.gru_d_r = nn.GRU(z_dims + rhythm_dims, hidden_dims, batch_first=True)
self.gru_d_n = nn.GRU(z_dims + note_dims, hidden_dims, batch_first=True)
self.gru_d_c = nn.GRU(z_dims + chroma_dims, hidden_dims, batch_first=True)
# classifiers
self.c_r = nn.Linear(z_dims, 3)
self.c_n = nn.Linear(z_dims, 3)
# mu and logvar
self.mu_r, self.var_r = nn.Linear(hidden_dims * 2, z_dims), nn.Linear(hidden_dims * 2, z_dims)
self.mu_n, self.var_n = nn.Linear(hidden_dims * 2, z_dims), nn.Linear(hidden_dims * 2, z_dims)
self.mu_c, self.var_c = nn.Linear(hidden_dims * 2, z_dims), nn.Linear(hidden_dims * 2, z_dims)
# global decoder
num_dims = 2
cdtl_dims = 24
self.linear_init_global = nn.Linear(z_dims * num_dims + cdtl_dims, hidden_dims)
self.grucell_g = nn.GRUCell(z_dims * num_dims + cdtl_dims + roll_dims, hidden_dims)
self.grucell_g_2 = nn.GRUCell(hidden_dims, hidden_dims)
# linear init before sub-decoder
self.linear_init_r = nn.Linear(z_dims, hidden_dims)
self.linear_init_n = nn.Linear(z_dims, hidden_dims)
self.linear_init_c = nn.Linear(z_dims, hidden_dims)
# linear out after sub-decoder
self.linear_out_r = nn.Linear(hidden_dims, rhythm_dims)
self.linear_out_n = nn.Linear(hidden_dims, note_dims)
self.linear_out_c = nn.Linear(z_dims, chroma_dims)
self.linear_out_g = nn.Linear(hidden_dims, roll_dims)
self.n_step = n_step
self.roll_dims = roll_dims
self.hidden_dims = hidden_dims
self.eps = 100
self.rhythm_dims = rhythm_dims
self.sample = None
self.iteration = 0
self.z_dims = z_dims
self.k = torch.FloatTensor([k])
def _sampling(self, x):
idx = x.max(1)[1]
x = torch.zeros_like(x)
arange = torch.arange(x.size(0)).long()
if torch.cuda.is_available():
arange = arange.cuda()
x[arange, idx] = 1
return x
def encoder(self, x):
# rhythm encoder
x_r = self.gru_r(x)[-1]
x_r = x_r.transpose_(0, 1).contiguous().view(x_r.size(0), -1)
mu_r, var_r = self.mu_r(x_r), self.var_r(x_r).exp_()
# note encoder
x_n = self.gru_n(x)[-1]
x_n = x_n.transpose_(0, 1).contiguous().view(x_n.size(0), -1)
mu_n, var_n = self.mu_n(x_n), self.var_n(x_n).exp_()
dis_r = Normal(mu_r, var_r)
dis_n = Normal(mu_n, var_n)
output = (dis_r, dis_n)
return output
def sub_decoders(self, rhythm, z_r, note, z_n):
def get_hidden_and_concat_latent(input, z_latent):
z_latent_stack = torch.stack([z_latent] * input.shape[1], dim=1)
input_in = torch.cat([input, z_latent_stack], dim=-1)
return input_in
rhythm_in = get_hidden_and_concat_latent(rhythm, z_r)
h_r = self.linear_init_r(z_r).unsqueeze(0)
rhythm_out = self.gru_d_r(rhythm_in, h_r)[0]
rhythm_out = F.log_softmax(self.linear_out_r(rhythm_out), 1)
note_in = get_hidden_and_concat_latent(note, z_n)
h_n = self.linear_init_n(z_n).unsqueeze(0)
note_out = self.gru_d_n(note_in, h_n)[0]
note_out = F.log_softmax(self.linear_out_n(note_out), 1)
return rhythm_out, note_out
def global_decoder(self, z, steps):
out = torch.zeros((z.size(0), self.roll_dims)).cuda()
out[:, -1] = 1.
x, hx = [], [None, None]
t = self.linear_init_global(z)
hx[0] = t
if torch.cuda.is_available():
out = out.cuda()
for i in range(steps):
out = torch.cat([out, z], 1)
hx[0] = self.grucell_g(out, hx[0])
if i == 0:
hx[1] = hx[0]
hx[1] = self.grucell_g_2(hx[0], hx[1])
out = F.log_softmax(self.linear_out_g(hx[1]), 1)
x.append(out)
if self.training:
p = torch.rand(1).item()
if p < self.eps:
out = self.sample[:, i, :]
else:
out = self._sampling(out)
else:
out = self._sampling(out)
return torch.stack(x, 1)
def forward(self, x, rhythm, note, chroma):
if self.training:
self.sample = x
self.iteration += 1
dis_r, dis_n = self.encoder(x)
def repar(mu, stddev, sigma=1):
eps = Normal(0, sigma).sample(sample_shape=stddev.size()).cuda()
z = mu + stddev * eps # reparameterization trick
return z
z_r = repar(dis_r.mean, dis_r.stddev)
z_n = repar(dis_n.mean, dis_n.stddev)
# get sub decoders output
r_out, n_out = self.sub_decoders(rhythm, z_r, note, z_n)
# packaging output
z = torch.cat([z_r, z_n, chroma], dim=1)
out = self.global_decoder(z, steps=x.shape[1])
output = (out, r_out, n_out)
dis = (dis_r, dis_n)
z_out = (z_r, z_n)
res = (output, dis, z_out)
return res
class MusicAttrSingleVAE(nn.Module):
'''
Single encoder VAE with reg. loss by Pati et al. (2019).
'''
def __init__(self,
roll_dims,
rhythm_dims,
note_dims,
chroma_dims,
hidden_dims,
z_dims,
n_step,
k=1000):
super(MusicAttrSingleVAE, self).__init__()
# encoder
self.gru = nn.GRU(roll_dims, hidden_dims, batch_first=True, bidirectional=True)
# dropouts
self.e_dropout = nn.Dropout(p=0.3)
# no sub-decoder -- only latent regularization in loss function
# mu and logvar -- use 2 * z_dims to ensure same capacity with disentangled models
self.mu, self.var = nn.Linear(hidden_dims * 2, z_dims * 2), nn.Linear(hidden_dims * 2, z_dims * 2)
# global decoder
num_dims = 2
cdtl_dims = 24
self.linear_init_global = nn.Linear(z_dims * num_dims + cdtl_dims, hidden_dims)
self.grucell_g = nn.GRUCell(z_dims * num_dims + cdtl_dims + roll_dims, hidden_dims)
self.grucell_g_2 = nn.GRUCell(hidden_dims, hidden_dims)
self.linear_out_g = nn.Linear(hidden_dims, roll_dims)
self.n_step = n_step
self.roll_dims = roll_dims
self.hidden_dims = hidden_dims
self.eps = 100
self.rhythm_dims = rhythm_dims
self.sample = None
self.iteration = 0
self.z_dims = z_dims
self.k = torch.FloatTensor([k])
def _sampling(self, x):
idx = x.max(1)[1]
x = torch.zeros_like(x)
arange = torch.arange(x.size(0)).long()
if torch.cuda.is_available():
arange = arange.cuda()
x[arange, idx] = 1
return x
def encoder(self, x):
# encoder
x = self.gru(x)[-1]
x = x.transpose_(0, 1).contiguous().view(x.size(0), -1)
mu, var = self.mu(x), self.var(x).exp_()
return Normal(mu, var)
def global_decoder(self, z, steps):
out = torch.zeros((z.size(0), self.roll_dims)).cuda()
out[:, -1] = 1.
x, hx = [], [None, None]
t = self.linear_init_global(z)
hx[0] = t
if torch.cuda.is_available():
out = out.cuda()
for i in range(steps):
out = torch.cat([out, z], 1)
hx[0] = self.grucell_g(out, hx[0])
if i == 0:
hx[1] = hx[0]
hx[1] = self.grucell_g_2(hx[0], hx[1])
out = F.log_softmax(self.linear_out_g(hx[1]), 1)
x.append(out)
if self.training:
p = torch.rand(1).item()
if p < self.eps:
out = self.sample[:, i, :]
else:
out = self._sampling(out)
# self.eps = self.k / \
# (self.k + torch.exp(self.iteration / self.k))
else:
out = self._sampling(out)
return torch.stack(x, 1)
def forward(self, x, chroma):
if self.training:
self.sample = x
self.iteration += 1
# residual or without
dis = self.encoder(x)
def repar(mu, stddev, sigma=1):
eps = Normal(0, sigma).sample(sample_shape=stddev.size()).cuda()
z = mu + stddev * eps # reparameterization trick
return z
z = repar(dis.mean, dis.stddev)
# packaging output
z = torch.cat([z, chroma], dim=1)
out = self.global_decoder(z, steps=x.shape[1])
res = (out, dis, z)
return res
class MusicAttrCVAE(nn.Module):
'''
CVAE model - one encoder, decode with concatenated conditions.
'''
def __init__(self,
roll_dims,
rhythm_dims,
note_dims,
chroma_dims,
hidden_dims,
z_dims,
n_step,
k=1000):
super(MusicAttrCVAE, self).__init__()
# encoder
self.gru_e = nn.GRU(roll_dims + 2, hidden_dims, batch_first=True, bidirectional=True)
# classifiers
self.c_r = nn.Linear(z_dims, 3)
self.c_n = nn.Linear(z_dims, 3)
# mu and logvar
self.mu, self.var = nn.Linear(hidden_dims * 2, z_dims), nn.Linear(hidden_dims * 2, z_dims)
# global decoder
num_dims = 1
cdtl_dims = 2
self.linear_init_global = nn.Linear(z_dims * num_dims + cdtl_dims, hidden_dims)
self.grucell_g = nn.GRUCell(z_dims * num_dims + cdtl_dims + roll_dims, hidden_dims)
self.grucell_g_2 = nn.GRUCell(hidden_dims, hidden_dims)
# linear out after sub-decoder
self.linear_out_g = nn.Linear(hidden_dims, roll_dims)
self.n_step = n_step
self.roll_dims = roll_dims
self.hidden_dims = hidden_dims
self.eps = 100
self.rhythm_dims = rhythm_dims
self.sample = None
self.iteration = 0
self.z_dims = z_dims
self.k = torch.FloatTensor([k])
def _sampling(self, x):
idx = x.max(1)[1]
x = torch.zeros_like(x)
arange = torch.arange(x.size(0)).long()
if torch.cuda.is_available():
arange = arange.cuda()
x[arange, idx] = 1
return x
def encoder(self, x, r_density, n_density, chroma):
r_density_rpt = torch.stack([r_density] * x.shape[1], dim=1)
n_density_rpt = torch.stack([n_density] * x.shape[1], dim=1)
x_in = torch.cat([x, r_density_rpt, n_density_rpt], dim=-1)
# 1 encoder
h = self.gru_e(x_in)[-1]
h = h.transpose_(0, 1).contiguous().view(h.size(0), -1)
mu, var = self.mu(h), self.var(h).exp_()
dis = Normal(mu, var)
return dis
def sub_decoders(self, rhythm, z_r, note, z_n):
def get_hidden_and_concat_latent(input, z_latent):
z_latent_stack = torch.stack([z_latent] * input.shape[1], dim=1)
input_in = torch.cat([input, z_latent_stack], dim=-1)
return input_in
rhythm_in = get_hidden_and_concat_latent(rhythm, z_r)
h_r = self.linear_init_r(z_r).unsqueeze(0)
rhythm_out = self.gru_d_r(rhythm_in, h_r)[0]
rhythm_out = F.log_softmax(self.linear_out_r(rhythm_out), 1)
note_in = get_hidden_and_concat_latent(note, z_n)
h_n = self.linear_init_n(z_n).unsqueeze(0)
note_out = self.gru_d_n(note_in, h_n)[0]
note_out = F.log_softmax(self.linear_out_n(note_out), 1)
return rhythm_out, note_out, 0, 0
def global_decoder(self, z, steps):
out = torch.zeros((z.size(0), self.roll_dims)).cuda()
out[:, -1] = 1.
x, hx = [], [None, None]
t = self.linear_init_global(z)
hx[0] = t
if torch.cuda.is_available():
out = out.cuda()
for i in range(steps):
out = torch.cat([out, z], 1)
hx[0] = self.grucell_g(out, hx[0])
if i == 0:
hx[1] = hx[0]
hx[1] = self.grucell_g_2(hx[0], hx[1])
out = F.log_softmax(self.linear_out_g(hx[1]), 1)
x.append(out)
if self.training:
p = torch.rand(1).item()
if p < self.eps:
out = self.sample[:, i, :]
else:
out = self._sampling(out)
else:
out = self._sampling(out)
return torch.stack(x, 1)
def forward(self, x, rhythm, note, chroma, r_density, n_density):
if self.training:
self.sample = x
self.iteration += 1
# residual or without
dis = self.encoder(x, r_density, n_density, chroma)
def repar(mu, stddev, sigma=1):
eps = Normal(0, sigma).sample(sample_shape=stddev.size()).cuda()
z = mu + stddev * eps # reparameterization trick
return z
z = repar(dis.mean, dis.stddev)
# packaging output
z = torch.cat([z, r_density, n_density], dim=-1)
out = self.global_decoder(z, steps=x.shape[1])
res = (out, dis, z)
return res
class ReverseLayerF(Function):
@staticmethod
def forward(ctx, x, alpha=1):
ctx.alpha = alpha
return x.view_as(x)
@staticmethod
def backward(ctx, grad_output):
output = grad_output.neg() * ctx.alpha
return output, None
class MusicAttrFaderNets(nn.Module):
'''
Fader Networks model - basically a CVAE with adversarial loss training.
'''
def __init__(self,
roll_dims,
rhythm_dims,
note_dims,
chroma_dims,
hidden_dims,
z_dims,
n_step,
k=1000):
super(MusicAttrFaderNets, self).__init__()
# encoder
self.gru_e = nn.GRU(roll_dims, hidden_dims, batch_first=True, bidirectional=True)
# classifiers
self.c_r = nn.Linear(z_dims, 3)
self.c_n = nn.Linear(z_dims, 3)
# mu and logvar
self.mu, self.var = nn.Linear(hidden_dims * 2, z_dims), nn.Linear(hidden_dims * 2, z_dims)
# discriminator
self.discriminator_r = nn.Linear(z_dims, 1)
self.discriminator_n = nn.Linear(z_dims, 1)
self.dropout = nn.Dropout(p=0.3)
# global decoder
num_dims = 1
cdtl_dims = 2
self.linear_init_global = nn.Linear(z_dims * num_dims + cdtl_dims, hidden_dims)
self.grucell_g = nn.GRUCell(z_dims * num_dims + cdtl_dims + roll_dims, hidden_dims)
self.grucell_g_2 = nn.GRUCell(hidden_dims, hidden_dims)
# linear out after sub-decoder
self.linear_out_g = nn.Linear(hidden_dims, roll_dims)
self.n_step = n_step
self.roll_dims = roll_dims
self.hidden_dims = hidden_dims
self.eps = 100
self.rhythm_dims = rhythm_dims
self.sample = None
self.iteration = 0
self.z_dims = z_dims
self.k = torch.FloatTensor([k])
def _sampling(self, x):
idx = x.max(1)[1]
x = torch.zeros_like(x)
arange = torch.arange(x.size(0)).long()
if torch.cuda.is_available():
arange = arange.cuda()
x[arange, idx] = 1
return x
def encoder(self, x):
h = self.gru_e(x)[-1]
h = h.transpose_(0, 1).contiguous().view(h.size(0), -1)
mu, var = self.mu(h), self.var(h).exp_()
dis = Normal(mu, var)
return dis
def sub_decoders(self, rhythm, z_r, note, z_n):
def get_hidden_and_concat_latent(input, z_latent):
z_latent_stack = torch.stack([z_latent] * input.shape[1], dim=1)
input_in = torch.cat([input, z_latent_stack], dim=-1)
return input_in
rhythm_in = get_hidden_and_concat_latent(rhythm, z_r)
h_r = self.linear_init_r(z_r).unsqueeze(0)
rhythm_out = self.gru_d_r(rhythm_in, h_r)[0]
rhythm_out = F.log_softmax(self.linear_out_r(rhythm_out), 1)
note_in = get_hidden_and_concat_latent(note, z_n)
h_n = self.linear_init_n(z_n).unsqueeze(0)
note_out = self.gru_d_n(note_in, h_n)[0]
note_out = F.log_softmax(self.linear_out_n(note_out), 1)
# chroma_out = torch.nn.Sigmoid()(self.linear_out_c(z_c)) # BCE
return rhythm_out, note_out, 0, 0
def global_decoder(self, z, steps):
out = torch.zeros((z.size(0), self.roll_dims)).cuda()
out[:, -1] = 1.
x, hx = [], [None, None]
t = self.linear_init_global(z)
hx[0] = t
if torch.cuda.is_available():
out = out.cuda()
# if not self.training:
# print("not training mode")
for i in range(steps):
out = torch.cat([out, z], 1)
hx[0] = self.grucell_g(out, hx[0])
if i == 0:
hx[1] = hx[0]
hx[1] = self.grucell_g_2(hx[0], hx[1])
out = F.log_softmax(self.linear_out_g(hx[1]), 1)
x.append(out)
if self.training:
p = torch.rand(1).item()
if p < self.eps:
out = self.sample[:, i, :]
else:
out = self._sampling(out)
# self.eps = self.k / \
# (self.k + torch.exp(self.iteration / self.k))
else:
out = self._sampling(out)
return torch.stack(x, 1)
def forward(self, x, rhythm, note, chroma, r_density, n_density):
if self.training:
self.sample = x
self.iteration += 1
# residual or without
dis = self.encoder(x)
def repar(mu, stddev, sigma=1):
eps = Normal(0, sigma).sample(sample_shape=stddev.size()).cuda()
z = mu + stddev * eps # reparameterization trick
return z
z = repar(dis.mean, dis.stddev)
# discriminator part
r_z = ReverseLayerF.apply(z)
r_out = self.dropout(F.relu(self.discriminator_r(r_z)))
n_out = self.dropout(F.relu(self.discriminator_n(r_z)))
# packaging output
z = torch.cat([z, r_density, n_density], dim=-1)
out = self.global_decoder(z, steps=x.shape[1])
output = (out, r_out, n_out)
res = (output, dis, z)
return res