-
Notifications
You must be signed in to change notification settings - Fork 22
/
model_run.py
1357 lines (1172 loc) · 63.3 KB
/
model_run.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch
import pickle
import argparse
import math
import numpy as np
import shutil
import os
import matplotlib
matplotlib.use('Agg')
import pyScoreParser.xml_matching as xml_matching
import pyScoreParser.performanceWorm as perf_worm
import data_process as dp
import copy
import random
import nnModel
import model_parameters as param
import model_constants as cons
import sys
from pathlib import Path
sys.modules['xml_matching'] = xml_matching
parser = argparse.ArgumentParser()
parser.add_argument("-mode", "--sessMode", type=str, default='test', help="train or test or testAll")
parser.add_argument("-path", "--testPath", type=str, default="./test_pieces/bps_5_1/", help="folder path of test mat")
parser.add_argument("-data", "--dataName", type=str, default="training_data", help="dat file name")
parser.add_argument("--resume", type=str, default="_best.pth.tar", help="best model path")
parser.add_argument("-tempo", "--startTempo", type=int, default=0, help="start tempo. zero to use xml first tempo")
parser.add_argument("-trill", "--trainTrill", default=False, type=lambda x: (str(x).lower() == 'true'), help="train trill")
parser.add_argument("-slur", "--slurEdge", default=False, type=lambda x: (str(x).lower() == 'true'), help="slur edge in graph")
parser.add_argument("-voice", "--voiceEdge", default=True, type=lambda x: (str(x).lower() == 'true'), help="network in voice level")
parser.add_argument("-vel", "--velocity", type=str, default='50,65', help="mean velocity of piano and forte")
parser.add_argument("-dev", "--device", type=int, default=0, help="cuda device number")
parser.add_argument("-code", "--modelCode", type=str, default='isgn', help="code name for saving the model")
parser.add_argument("-tCode", "--trillCode", type=str, default='trill_default', help="code name for loading trill model")
parser.add_argument("-comp", "--composer", type=str, default='Beethoven', help="composer name of the input piece")
parser.add_argument("--latent", type=float, default=0, help='initial_z value')
parser.add_argument("-bp", "--boolPedal", default=False, type=lambda x: (str(x).lower() == 'true'), help='make pedal value zero under threshold')
parser.add_argument("-loss", "--trainingLoss", type=str, default='MSE', help='type of training loss')
parser.add_argument("-reTrain", "--resumeTraining", default=False, type=lambda x: (str(x).lower() == 'true'), help='resume training after loading model')
parser.add_argument("-perf", "--perfName", default='Anger_sub1', type=str, help='resume training after loading model')
parser.add_argument("-delta", "--deltaLoss", default=False, type=lambda x: (str(x).lower() == 'true'), help="network in voice level")
parser.add_argument("-hCode", "--hierCode", type=str, default='han_ar_measure', help="code name for loading hierarchy model")
parser.add_argument("-intermd", "--intermediateLoss", default=True, type=lambda x: (str(x).lower() == 'true'), help="intermediate loss in ISGN")
parser.add_argument("-randtr", "--randomTrain", default=True, type=lambda x: (str(x).lower() == 'true'), help="use random train")
parser.add_argument("-dskl", "--disklavier", default=True, type=lambda x: (str(x).lower() == 'true'), help="save midi for disklavier")
parser.add_argument("-multi", "--multi_instruments", default=False, type=lambda x: (str(x).lower() == 'true'), help="save midi for disklavier")
random.seed(0)
args = parser.parse_args()
LOSS_TYPE = args.trainingLoss
HIERARCHY = False
IN_HIER = False
HIER_MEAS = False
HIER_BEAT = False
HIER_MODEL = None
RAND_TRAIN = args.randomTrain
TRILL = False
if 'measure' in args.modelCode or 'beat' in args.modelCode:
HIERARCHY = True
elif 'note' in args.modelCode:
IN_HIER = True # In hierarchy mode
if HIERARCHY or IN_HIER:
if 'measure' in args.modelCode or 'measure' in args.hierCode:
HIER_MEAS = True
elif 'beat' in args.modelCode or 'beat' in args.hierCode:
HIER_BEAT = True
if 'trill' in args.modelCode:
TRILL = True
### parameters
learning_rate = 0.0003
TIME_STEPS = 500
VALID_STEPS = 5000
DELTA_WEIGHT = 2
NUM_UPDATED = 0
WEIGHT_DECAY = 1e-5
GRAD_CLIP = 5
KLD_MAX = 0.01
KLD_SIG = 20e4
if args.sessMode == 'train':
print('Learning Rate: {}, Time_steps: {}, Delta weight: {}, Weight decay: {}, Grad clip: {}, KLD max: {}, KLD sig step: {}'.format
(learning_rate, TIME_STEPS, DELTA_WEIGHT, WEIGHT_DECAY, GRAD_CLIP, KLD_MAX, KLD_SIG))
num_epochs = 100
num_key_augmentation = 1
NUM_INPUT = 78
NUM_PRIME_PARAM = 11
if HIERARCHY:
NUM_OUTPUT = 2
elif TRILL:
NUM_INPUT += NUM_PRIME_PARAM
NUM_OUTPUT = 5
else:
NUM_OUTPUT = 11
if IN_HIER:
NUM_INPUT += 2
NUM_TEMPO_PARAM = 1
VEL_PARAM_IDX = 1
DEV_PARAM_IDX = 2
PEDAL_PARAM_IDX = 3
num_second_param = 0
num_trill_param = 5
num_voice_feed_param = 0 # velocity, onset deviation
num_tempo_info = 0
num_dynamic_info = 0 # distance from marking, dynamics vector 4, mean_piano, forte marking and velocity = 4
is_trill_index_score = -11
is_trill_index_concated = -11 - (NUM_PRIME_PARAM + num_second_param)
with open(args.dataName + "_stat.dat", "rb") as f:
u = pickle._Unpickler(f)
u.encoding = 'latin1'
if args.trainingLoss == 'CE':
MEANS, STDS, BINS = u.load()
new_prime_param = 0
new_trill_param = 0
for i in range(NUM_PRIME_PARAM):
new_prime_param += len(BINS[i]) - 1
for i in range(NUM_PRIME_PARAM, NUM_PRIME_PARAM + num_trill_param - 1):
new_trill_param += len(BINS[i]) - 1
NUM_PRIME_PARAM = new_prime_param
print('New NUM_PRIME_PARAM: ', NUM_PRIME_PARAM)
num_trill_param = new_trill_param + 1
NUM_OUTPUT = NUM_PRIME_PARAM + num_trill_param
NUM_TEMPO_PARAM = len(BINS[0]) - 1
else:
MEANS, STDS = u.load()
QPM_INDEX = 0
# VOICE_IDX = 11
TEMPO_IDX = 26
QPM_PRIMO_IDX = 4
TEMPO_PRIMO_IDX = -2
GRAPH_KEYS = ['onset', 'forward', 'melisma', 'rest']
if args.slurEdge:
GRAPH_KEYS.append('slur')
if args.voiceEdge:
GRAPH_KEYS.append('voice')
N_EDGE_TYPE = len(GRAPH_KEYS) * 2
# mean_vel_start_index = 7
# vel_vec_start_index = 33
batch_size = 1
torch.cuda.set_device(args.device)
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if args.sessMode == 'train' and not args.resumeTraining:
NET_PARAM = param.initialize_model_parameters_by_code(args.modelCode)
NET_PARAM.num_edge_types = N_EDGE_TYPE
NET_PARAM.training_args = args
param.save_parameters(NET_PARAM, args.modelCode + '_param')
elif args.resumeTraining:
NET_PARAM = param.load_parameters(args.modelCode + '_param')
else:
NET_PARAM = param.load_parameters(args.modelCode + '_param')
TrillNET_Param = param.load_parameters(args.trillCode + '_param')
# if not hasattr(NET_PARAM, 'num_edge_types'):
# NET_PARAM.num_edge_types = 10
# if not hasattr(TrillNET_Param, 'num_edge_types'):
# TrillNET_Param.num_edge_types = 10
TRILL_MODEL = nnModel.TrillRNN(TrillNET_Param, DEVICE).to(DEVICE)
if 'isgn' in args.modelCode:
MODEL = nnModel.ISGN(NET_PARAM, DEVICE).to(DEVICE)
elif 'han' in args.modelCode:
if 'ar' in args.modelCode:
step_by_step = True
else:
step_by_step = False
MODEL = nnModel.HAN_Integrated(NET_PARAM, DEVICE, step_by_step).to(DEVICE)
elif 'trill' in args.modelCode:
MODEL = nnModel.TrillRNN(NET_PARAM, DEVICE).to(DEVICE)
else:
print('Error: Unclassified model code')
# Model = nnModel.HAN_VAE(NET_PARAM, DEVICE, False).to(DEVICE)
optimizer = torch.optim.Adam(MODEL.parameters(), lr=learning_rate, weight_decay=WEIGHT_DECAY)
if LOSS_TYPE == 'MSE':
def criterion(pred, target, aligned_status=1):
if isinstance(aligned_status, int):
data_size = pred.shape[-2] * pred.shape[-1]
else:
data_size = torch.sum(aligned_status).item() * pred.shape[-1]
if data_size == 0:
data_size = 1
if target.shape != pred.shape:
print('Error: The shape of the target and prediction for the loss calculation is different')
print(target.shape, pred.shape)
return torch.zeros(1).to(DEVICE)
return torch.sum(((target - pred) ** 2) * aligned_status) / data_size
elif LOSS_TYPE == 'CE':
# criterion = nn.CrossEntropyLoss()
def criterion(pred, target, aligned_status=1):
if isinstance(aligned_status, int):
data_size = pred.shape[-2] * pred.shape[-1]
else:
data_size = torch.sum(aligned_status).item() * pred.shape[-1]
if data_size ==0:
data_size = 1
print('data size for loss calculation is zero')
return -1 * torch.sum((target * torch.log(pred) + (1-target) * torch.log(1-pred)) * aligned_status) / data_size
def save_checkpoint(state, is_best, filename=args.modelCode, model_name='prime'):
save_name = model_name + '_' + filename + '_checkpoint.pth.tar'
torch.save(state, save_name)
if is_best:
best_name = model_name + '_' + filename + '_best.pth.tar'
shutil.copyfile(save_name, best_name)
def edges_to_matrix(edges, num_notes):
if not MODEL.is_graph:
return None
num_keywords = len(GRAPH_KEYS)
matrix = np.zeros((N_EDGE_TYPE, num_notes, num_notes))
for edg in edges:
if edg[2] not in GRAPH_KEYS:
continue
edge_type = GRAPH_KEYS.index(edg[2])
matrix[edge_type, edg[0], edg[1]] = 1
if edge_type != 0:
matrix[edge_type+num_keywords, edg[1], edg[0]] = 1
else:
matrix[edge_type, edg[1], edg[0]] = 1
matrix[num_keywords, :,:] = np.identity(num_notes)
matrix = torch.Tensor(matrix)
return matrix
def edges_to_matrix_short(edges, slice_index):
if not MODEL.is_graph:
return None
num_keywords = len(GRAPH_KEYS)
num_notes = slice_idx[1] - slice_idx[0]
matrix = np.zeros((N_EDGE_TYPE, num_notes, num_notes))
start_edge_index = xml_matching.binary_index_for_edge(edges, slice_index[0])
end_edge_index = xml_matching.binary_index_for_edge(edges, slice_index[1] + 1)
for i in range(start_edge_index, end_edge_index):
edg = edges[i]
if edg[2] not in GRAPH_KEYS:
continue
if edg[1] >= slice_index[1]:
continue
edge_type = GRAPH_KEYS.index(edg[2])
matrix[edge_type, edg[0]-slice_index[0], edg[1]-slice_index[0]] = 1
if edge_type != 0:
matrix[edge_type+num_keywords, edg[1]-slice_index[0], edg[0]-slice_index[0]] = 1
else:
matrix[edge_type, edg[1]-slice_index[0], edg[0]-slice_index[0]] = 1
matrix[num_keywords, :,:] = np.identity(num_notes)
matrix = torch.Tensor(matrix)
return matrix
def edges_to_sparse_tensor(edges):
num_keywords = len(GRAPH_KEYS)
edge_list = []
edge_type_list = []
for edg in edges:
edge_type = GRAPH_KEYS.index(edg[2])
edge_list.append(edg[0:2])
edge_list.append([edg[1], edg[0]])
edge_type_list.append(edge_type)
if edge_type != 0:
edge_type_list.append(edge_type+num_keywords)
else:
edge_type_list.append(edge_type)
edge_list = torch.LongTensor(edge_list)
edge_type_list = torch.FloatTensor(edge_type_list)
matrix = torch.sparse.FloatTensor(edge_list.t(), edge_type_list)
return matrix
def categorize_value_to_vector(y, bins):
vec_length = sum([len(x) for x in bins])
num_notes = len(y)
y_categorized = []
num_categorized_params = len(bins)
for i in range(num_notes):
note = y[i]
total_vec = []
for j in range(num_categorized_params):
temp_vec = [0] * (len(bins[j]) - 1)
temp_vec[int(note[j])] = 1
total_vec += temp_vec
total_vec.append(note[-1]) # add up trill
y_categorized.append(total_vec)
return y_categorized
def scale_model_prediction_to_original(prediction, MEANS, STDS):
for i in range(len(STDS)):
for j in range(len(STDS[i])):
if STDS[i][j] < 1e-4:
STDS[i][j] = 1
prediction = np.squeeze(np.asarray(prediction.cpu()))
num_notes = len(prediction)
if LOSS_TYPE == 'MSE':
for i in range(11):
prediction[:, i] *= STDS[1][i]
prediction[:, i] += MEANS[1][i]
for i in range(11, 15):
prediction[:, i] *= STDS[1][i+4]
prediction[:, i] += MEANS[1][i+4]
elif LOSS_TYPE == 'CE':
prediction_in_value = np.zeros((num_notes, 16))
for i in range(num_notes):
bin_range_start = 0
for j in range(15):
feature_bin_size = len(BINS[j]) - 1
feature_class = np.argmax(prediction[i, bin_range_start:bin_range_start + feature_bin_size])
feature_value = (BINS[j][feature_class] + BINS[j][feature_class + 1]) / 2
prediction_in_value[i, j] = feature_value
bin_range_start += feature_bin_size
prediction_in_value[i, 15] = prediction[i, -1]
prediction = prediction_in_value
return prediction
def load_file_and_generate_performance(path_name, composer=args.composer, z=args.latent,
start_tempo=args.startTempo, return_features=False, multi_instruments=args.multi_instruments):
vel_pair = (int(args.velocity.split(',')[0]), int(args.velocity.split(',')[1]))
test_x, xml_notes, xml_doc, edges, note_locations, part_names = xml_matching.read_xml_to_array(path_name, MEANS, STDS,
start_tempo, composer,
vel_pair)
batch_x = torch.Tensor(test_x)
num_notes = len(test_x)
input_y = torch.zeros(1, num_notes, NUM_OUTPUT).to(DEVICE)
if type(z) is dict:
initial_z = z['z']
qpm_change = z['qpm']
z = z['key']
batch_x[:,QPM_PRIMO_IDX] = batch_x[:,QPM_PRIMO_IDX] + qpm_change
else:
initial_z = 'zero'
if IN_HIER:
batch_x = batch_x.to(DEVICE).view(1, -1, HIER_MODEL.input_size)
graph = edges_to_matrix(edges, batch_x.shape[1])
MODEL.is_teacher_force = False
if type(initial_z) is list:
hier_z = initial_z[0]
final_z = initial_z[1]
else:
# hier_z = [z] * HIER_MODEL_PARAM.encoder.size
hier_z = 'zero'
final_z = initial_z
hier_input_y = torch.zeros(1, num_notes, HIER_MODEL.output_size)
hier_output, _ = run_model_in_steps(batch_x, hier_input_y, graph, note_locations, initial_z=hier_z, model=HIER_MODEL)
if 'measure' in args.hierCode:
hierarchy_numbers = [x.measure for x in note_locations]
else:
hierarchy_numbers = [x.section for x in note_locations]
hier_output_spanned = HIER_MODEL.span_beat_to_note_num(hier_output, hierarchy_numbers, len(test_x), 0)
combined_x = torch.cat((batch_x, hier_output_spanned), 2)
prediction, _ = run_model_in_steps(combined_x, input_y, graph, note_locations, initial_z=final_z, model=MODEL)
else:
if type(initial_z) is list:
initial_z = initial_z[0]
batch_x = batch_x.to(DEVICE).view(1, -1, NUM_INPUT)
graph = edges_to_matrix(edges, batch_x.shape[1])
prediction, _ = run_model_in_steps(batch_x, input_y, graph, note_locations, initial_z=initial_z, model=MODEL)
trill_batch_x = torch.cat((batch_x, prediction), 2)
trill_prediction, _ = run_model_in_steps(trill_batch_x, torch.zeros(1, num_notes, cons.num_trill_param), graph, note_locations, model=TRILL_MODEL)
prediction = torch.cat((prediction, trill_prediction), 2)
prediction = scale_model_prediction_to_original(prediction, MEANS, STDS)
output_features = xml_matching.model_prediction_to_feature(prediction)
output_features = xml_matching.add_note_location_to_features(output_features, note_locations)
if return_features:
return output_features
output_xml = xml_matching.apply_tempo_perform_features(xml_doc, xml_notes, output_features, start_time=1,
predicted=True)
output_midi, midi_pedals = xml_matching.xml_notes_to_midi(output_xml, multi_instruments)
piece_name = path_name.split('/')
save_dir = Path('test_result')
save_dir.mkdir(exist_ok=True)
save_name = str(save_dir / (piece_name[-2] + '_by_' + args.modelCode + '_z' + str(z)))
perf_worm.plot_performance_worm(output_features, save_name + '.png')
xml_matching.save_midi_notes_as_piano_midi(output_midi, midi_pedals, save_name + '.mid', part_names=part_names,
bool_pedal=args.boolPedal, disklavier=args.disklavier)
def load_file_and_encode_style(path, perf_name, composer_name):
test_x, test_y, edges, note_locations = xml_matching.read_score_perform_pair(path, perf_name, composer_name, MEANS, STDS)
qpm_primo = test_x[0][4]
test_x, test_y = handle_data_in_tensor(test_x, test_y, hierarchy_test=IN_HIER)
edges = edges_to_matrix(edges, test_x.shape[0])
if IN_HIER:
test_x = test_x.view((1, -1, HIER_MODEL.input_size))
hier_y = test_y[0].view(1, -1, HIER_MODEL.output_size)
perform_z_high = encode_performance_style_vector(test_x, hier_y, edges, note_locations, model=HIER_MODEL)
hier_outputs, _ = run_model_in_steps(test_x, hier_y, edges, note_locations, model=HIER_MODEL)
if HIER_MEAS:
hierarchy_numbers = [x.measure for x in note_locations]
elif HIER_BEAT:
hierarchy_numbers = [x.beat for x in note_locations]
hier_outputs_spanned = HIER_MODEL.span_beat_to_note_num(hier_outputs, hierarchy_numbers, test_x.shape[1], 0)
input_concat = torch.cat((test_x, hier_outputs_spanned), 2)
batch_y = test_y[1].view(1, -1, MODEL.output_size)
perform_z_note = encode_performance_style_vector(input_concat, batch_y, edges, note_locations, model=MODEL)
perform_z = [perform_z_high, perform_z_note]
else:
batch_x = test_x.view((1, -1, NUM_INPUT))
batch_y = test_y.view((1, -1, NUM_OUTPUT))
perform_z = encode_performance_style_vector(batch_x, batch_y, edges, note_locations)
perform_z = [perform_z]
return perform_z, qpm_primo
def encode_performance_style_vector(input, input_y, edges, note_locations, model=MODEL):
with torch.no_grad():
model_eval = model.eval()
if edges is not None:
edges = edges.to(DEVICE)
encoded_z = model_eval(input, input_y, edges,
note_locations=note_locations, start_index=0, return_z=True)
return encoded_z
def encode_all_emotionNet_data(path_list, style_keywords):
perform_z_by_emotion = []
perform_z_list_by_subject = []
qpm_list_by_subject = []
num_style = len(style_keywords)
if IN_HIER:
num_model = 2
else:
num_model = 1
for pair in path_list:
subject_num = pair[2]
for sub_idx in range(subject_num):
indiv_perform_z = []
indiv_qpm = []
path = cons.emotion_folder_path + pair[0] + '/'
composer_name = pair[1]
for key in style_keywords:
perf_name = key + '_sub' + str(sub_idx+1)
perform_z_li, qpm_primo = load_file_and_encode_style(path, perf_name, composer_name)
indiv_perform_z.append(perform_z_li)
indiv_qpm.append(qpm_primo)
for i in range(1, num_style):
for j in range(num_model):
indiv_perform_z[i][j] = indiv_perform_z[i][j] - indiv_perform_z[0][j]
indiv_qpm[i] = indiv_qpm[i] - indiv_qpm[0]
perform_z_list_by_subject.append(indiv_perform_z)
qpm_list_by_subject.append(indiv_qpm)
for i in range(num_style):
z_by_models = []
for j in range(num_model):
emotion_mean_z = []
for z_list in perform_z_list_by_subject:
emotion_mean_z.append(z_list[i][j])
mean_perform_z = torch.mean(torch.stack(emotion_mean_z), 0, True)
z_by_models.append(mean_perform_z)
if i != 0:
emotion_qpm = []
for qpm_change in qpm_list_by_subject:
emotion_qpm.append(qpm_change[i])
mean_qpm_change = np.mean(emotion_qpm)
else:
mean_qpm_change = 0
print(style_keywords[i], z_by_models, mean_qpm_change)
perform_z_by_emotion.append({'z': z_by_models, 'key': style_keywords[i], 'qpm': mean_qpm_change})
return perform_z_by_emotion
# with open(args.testPath + args.perfName + '_style' + '.dat', 'wb') as f:
# pickle.dump(mean_perform_z, f, protocol=2)
def run_model_in_steps(input, input_y, edges, note_locations, initial_z=False, model=MODEL):
num_notes = input.shape[1]
with torch.no_grad(): # no need to track history in validation
model_eval = model.eval()
total_output = []
total_z = []
measure_numbers = [x.measure for x in note_locations]
slice_indexes = dp.make_slicing_indexes_by_measure(num_notes, measure_numbers, steps=VALID_STEPS, overlap=False)
# if edges is not None:
# edges = edges.to(DEVICE)
for slice_idx in slice_indexes:
batch_start, batch_end = slice_idx
if edges is not None:
batch_graph = edges[:, batch_start:batch_end, batch_start:batch_end].to(DEVICE)
else:
batch_graph = None
batch_input = input[:, batch_start:batch_end, :].view(1,-1,model.input_size)
batch_input_y = input_y[:, batch_start:batch_end, :].view(1,-1,model.output_size)
temp_outputs, perf_mu, perf_var, _ = model_eval(batch_input, batch_input_y, batch_graph,
note_locations=note_locations, start_index=batch_start, initial_z=initial_z)
total_z.append((perf_mu, perf_var))
total_output.append(temp_outputs)
outputs = torch.cat(total_output, 1)
return outputs, total_z
def batch_time_step_run(data, model, batch_size=batch_size):
batch_start, batch_end = training_data['slice_idx']
batch_x, batch_y = handle_data_in_tensor(data['x'][batch_start:batch_end], data['y'][batch_start:batch_end])
batch_x = batch_x.view((batch_size, -1, NUM_INPUT))
batch_y = batch_y.view((batch_size, -1, NUM_OUTPUT))
align_matched = torch.Tensor(data['align_matched'][batch_start:batch_end]).view((batch_size, -1, 1)).to(DEVICE)
pedal_status = torch.Tensor(data['pedal_status'][batch_start:batch_end]).view((batch_size, -1, 1)).to(DEVICE)
if training_data['graphs'] is not None:
edges = training_data['graphs']
if edges.shape[1] == batch_end - batch_start:
edges = edges.to(DEVICE)
else:
edges = edges[:, batch_start:batch_end, batch_start:batch_end].to(DEVICE)
else:
edges = training_data['graphs']
prime_batch_x = batch_x
if HIERARCHY:
prime_batch_y = batch_y
else:
prime_batch_y = batch_y[:, :, 0:NUM_PRIME_PARAM]
model_train = model.train()
outputs, perform_mu, perform_var, total_out_list \
= model_train(prime_batch_x, prime_batch_y, edges, note_locations, batch_start)
if HIERARCHY:
if HIER_MEAS:
hierarchy_numbers = [x.measure for x in note_locations]
elif HIER_BEAT:
hierarchy_numbers = [x.beat for x in note_locations]
tempo_in_hierarchy = MODEL.note_tempo_infos_to_beat(batch_y, hierarchy_numbers, batch_start, 0)
dynamics_in_hierarchy = MODEL.note_tempo_infos_to_beat(batch_y, hierarchy_numbers, batch_start, 1)
tempo_loss = criterion(outputs[:,:,0:1], tempo_in_hierarchy)
vel_loss = criterion(outputs[:,:,1:2], dynamics_in_hierarchy)
if args.deltaLoss and outputs.shape[1] > 1:
vel_out_delta = outputs[:, 1:, 1:2] - outputs[:, :-1, 1:2]
vel_true_delta = dynamics_in_hierarchy[:, 1:, :] - dynamics_in_hierarchy[:, :-1, :]
vel_loss += criterion(vel_out_delta, vel_true_delta) * DELTA_WEIGHT
vel_loss /= 1 + DELTA_WEIGHT
total_loss = tempo_loss + vel_loss
elif TRILL:
trill_bool = batch_x[:, :, is_trill_index_concated:is_trill_index_concated + 1]
if torch.sum(trill_bool) > 0:
total_loss = criterion(outputs, batch_y, trill_bool)
else:
return torch.zeros(1), torch.zeros(1), torch.zeros(1), torch.zeros(1), torch.zeros(1), torch.zeros(1), torch.zeros(1)
else:
if 'isgn' in args.modelCode and args.intermediateLoss:
total_loss = torch.zeros(1).to(DEVICE)
for out in total_out_list:
if model.is_baseline:
tempo_loss = criterion(out[:, :, 0:1],
prime_batch_y[:, :, 0:1], align_matched)
else:
tempo_loss = cal_tempo_loss_in_beat(out, prime_batch_y, note_locations, batch_start)
vel_loss = criterion(out[:, :, VEL_PARAM_IDX:DEV_PARAM_IDX],
prime_batch_y[:, :, VEL_PARAM_IDX:DEV_PARAM_IDX], align_matched)
dev_loss = criterion(out[:, :, DEV_PARAM_IDX:PEDAL_PARAM_IDX],
prime_batch_y[:, :, DEV_PARAM_IDX:PEDAL_PARAM_IDX], align_matched)
articul_loss = criterion(out[:, :, PEDAL_PARAM_IDX:PEDAL_PARAM_IDX+1],
prime_batch_y[:,:,PEDAL_PARAM_IDX:PEDAL_PARAM_IDX+1], pedal_status)
pedal_loss = criterion(out[:, :, PEDAL_PARAM_IDX+1:], prime_batch_y[:, :, PEDAL_PARAM_IDX+1:],
align_matched)
total_loss += (tempo_loss + vel_loss + dev_loss + articul_loss + pedal_loss * 7) / 11
total_loss /= len(total_out_list)
else:
if model.is_baseline:
tempo_loss = criterion(outputs[:, :, 0:1],
prime_batch_y[:, :, 0:1], align_matched)
else:
tempo_loss = cal_tempo_loss_in_beat(outputs, prime_batch_y, note_locations, batch_start)
vel_loss = criterion(outputs[:, :, VEL_PARAM_IDX:DEV_PARAM_IDX],
prime_batch_y[:, :, VEL_PARAM_IDX:DEV_PARAM_IDX], align_matched)
dev_loss = criterion(outputs[:, :, DEV_PARAM_IDX:PEDAL_PARAM_IDX],
prime_batch_y[:, :, DEV_PARAM_IDX:PEDAL_PARAM_IDX], align_matched)
articul_loss = criterion(outputs[:, :, PEDAL_PARAM_IDX:PEDAL_PARAM_IDX + 1],
prime_batch_y[:, :, PEDAL_PARAM_IDX:PEDAL_PARAM_IDX + 1], pedal_status)
pedal_loss = criterion(outputs[:, :, PEDAL_PARAM_IDX + 1:], prime_batch_y[:, :, PEDAL_PARAM_IDX + 1:],
align_matched)
total_loss = (tempo_loss + vel_loss + dev_loss + articul_loss + pedal_loss * 7) / 11
if isinstance(perform_mu, bool):
perform_kld = torch.zeros(1)
else:
perform_kld = -0.5 * torch.sum(1 + perform_var - perform_mu.pow(2) - perform_var.exp())
total_loss += perform_kld * kld_weight
optimizer.zero_grad()
total_loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), GRAD_CLIP)
optimizer.step()
if HIERARCHY:
return tempo_loss, vel_loss, torch.zeros(1), torch.zeros(1), torch.zeros(1), torch.zeros(1), perform_kld
elif TRILL:
return torch.zeros(1), torch.zeros(1), torch.zeros(1), torch.zeros(1), torch.zeros(1), total_loss, torch.zeros(1)
else:
return tempo_loss, vel_loss, dev_loss, articul_loss, pedal_loss, torch.zeros(1), perform_kld
# loss = criterion(outputs, batch_y)
# tempo_loss = criterion(prime_outputs[:, :, 0], prime_batch_y[:, :, 0])
def cal_tempo_loss_in_beat(pred_x, true_x, note_locations, start_index):
previous_beat = -1
num_notes = pred_x.shape[1]
start_beat = note_locations[start_index].beat
num_beats = note_locations[num_notes+start_index-1].beat - start_beat + 1
pred_beat_tempo = torch.zeros([num_beats, NUM_TEMPO_PARAM]).to(DEVICE)
true_beat_tempo = torch.zeros([num_beats, NUM_TEMPO_PARAM]).to(DEVICE)
for i in range(num_notes):
current_beat = note_locations[i+start_index].beat
if current_beat > previous_beat:
previous_beat = current_beat
if 'baseline' in args.modelCode:
for j in range(i, num_notes):
if note_locations[j+start_index].beat > current_beat:
break
if not i == j:
pred_beat_tempo[current_beat - start_beat] = torch.mean(pred_x[0, i:j, QPM_INDEX])
true_beat_tempo[current_beat - start_beat] = torch.mean(true_x[0, i:j, QPM_INDEX])
else:
pred_beat_tempo[current_beat-start_beat] = pred_x[0,i,QPM_INDEX:QPM_INDEX + NUM_TEMPO_PARAM]
true_beat_tempo[current_beat-start_beat] = true_x[0,i,QPM_INDEX:QPM_INDEX + NUM_TEMPO_PARAM]
tempo_loss = criterion(pred_beat_tempo, true_beat_tempo)
if args.deltaLoss and pred_beat_tempo.shape[0] > 1:
prediction_delta = pred_beat_tempo[1:] - pred_beat_tempo[:-1]
true_delta = true_beat_tempo[1:] - true_beat_tempo[:-1]
delta_loss = criterion(prediction_delta, true_delta)
tempo_loss = (tempo_loss + delta_loss * DELTA_WEIGHT) / (1 + DELTA_WEIGHT)
return tempo_loss
def handle_data_in_tensor(x, y, hierarchy_test=False):
x = torch.Tensor(x)
y = torch.Tensor(y)
if HIER_MEAS:
hierarchy_output = y[:,cons.MEAS_TEMPO_IDX:cons.MEAS_TEMPO_IDX+2]
elif HIER_BEAT:
hierarchy_output = y[:,cons.BEAT_TEMPO_IDX:cons.BEAT_TEMPO_IDX+2]
if hierarchy_test:
y = y[:, :NUM_PRIME_PARAM]
return x.to(DEVICE), (hierarchy_output.to(DEVICE), y.to(DEVICE))
if HIERARCHY:
y = hierarchy_output
elif IN_HIER:
x = torch.cat((x,hierarchy_output), 1)
y = y[:, :NUM_PRIME_PARAM]
elif TRILL:
x = torch.cat((x, y[:, :NUM_PRIME_PARAM]), 1)
y = y[: ,-num_trill_param:]
else:
y = y[:, :NUM_PRIME_PARAM]
return x.to(DEVICE), y.to(DEVICE)
def sigmoid(x, gain=1):
return 1 / (1 + math.exp(-gain*x))
class TraningSample():
def __init__(self, index):
self.index = index
self.slice_indexes = None
### training
if args.sessMode == 'train':
model_parameters = filter(lambda p: p.requires_grad, MODEL.parameters())
params = sum([np.prod(p.size()) for p in model_parameters])
print('Number of Network Parameters is ', params)
best_prime_loss = float("inf")
best_second_loss = float("inf")
best_trill_loss = float("inf")
start_epoch = 0
if args.resumeTraining and not args.trainTrill:
if os.path.isfile('prime_' + args.modelCode + args.resume):
print("=> loading checkpoint '{}'".format(args.modelCode + args.resume))
# model_codes = ['prime', 'trill']
filename = 'prime_' + args.modelCode + args.resume
checkpoint = torch.load(filename, map_location=DEVICE)
best_valid_loss = checkpoint['best_valid_loss']
MODEL.load_state_dict(checkpoint['state_dict'])
MODEL.device = DEVICE
optimizer.load_state_dict(checkpoint['optimizer'])
NUM_UPDATED = checkpoint['training_step']
print("=> loaded checkpoint '{}' (epoch {})"
.format(filename, checkpoint['epoch']))
start_epoch = checkpoint['epoch'] - 1
best_prime_loss = checkpoint['best_valid_loss']
print('Best valid loss was ', best_prime_loss)
# load data
print('Loading the training data...')
training_data_name = args.dataName + ".dat"
if not os.path.isfile(training_data_name):
training_data_name = '/mnt/ssd1/jdasam_data/' + training_data_name
with open(training_data_name, "rb") as f:
u = pickle._Unpickler(f)
u.encoding = 'latin1'
# p = u.load()
# complete_xy = pickle.load(f)
complete_xy = u.load()
train_xy = complete_xy['train']
test_xy = complete_xy['valid']
print('number of train performances: ', len(train_xy), 'number of valid perf: ', len(test_xy))
print('training sample example', train_xy[0][0][0])
train_model = MODEL
# total_step = len(train_loader)
for epoch in range(start_epoch, num_epochs):
print('current training step is ', NUM_UPDATED)
tempo_loss_total = []
vel_loss_total = []
dev_loss_total = []
articul_loss_total = []
pedal_loss_total = []
trill_loss_total = []
kld_total = []
if RAND_TRAIN:
num_perf_data = len(train_xy)
remaining_samples = []
for i in range(num_perf_data):
remaining_samples.append(TraningSample(i))
while len(remaining_samples) > 0:
new_index = random.randrange(0, len(remaining_samples))
selected_sample = remaining_samples[new_index]
train_x = train_xy[selected_sample.index][0]
train_y = train_xy[selected_sample.index][1]
if args.trainingLoss == 'CE':
train_y = categorize_value_to_vector(train_y, BINS)
note_locations = train_xy[selected_sample.index][2]
align_matched = train_xy[selected_sample.index][3]
pedal_status = train_xy[selected_sample.index][4]
edges = train_xy[selected_sample.index][5]
data_size = len(train_x)
if selected_sample.slice_indexes is None:
measure_numbers = [x.measure for x in note_locations]
if HIER_MEAS and HIERARCHY:
selected_sample.slice_indexes = dp.make_slice_with_same_measure_number(data_size,
measure_numbers,
measure_steps=TIME_STEPS)
else:
selected_sample.slice_indexes = dp.make_slicing_indexes_by_measure(data_size, measure_numbers, steps=TIME_STEPS)
num_slice = len(selected_sample.slice_indexes)
selected_idx = random.randrange(0,num_slice)
slice_idx = selected_sample.slice_indexes[selected_idx]
if MODEL.is_graph:
graphs = edges_to_matrix_short(edges, slice_idx)
else:
graphs = None
key_lists = [0]
key = 0
for i in range(num_key_augmentation):
while key in key_lists:
key = random.randrange(-5, 7)
key_lists.append(key)
for i in range(num_key_augmentation+1):
key = key_lists[i]
temp_train_x = dp.key_augmentation(train_x, key)
kld_weight = sigmoid((NUM_UPDATED - KLD_SIG) / (KLD_SIG/10)) * KLD_MAX
training_data = {'x': temp_train_x, 'y': train_y, 'graphs': graphs,
'note_locations': note_locations,
'align_matched': align_matched, 'pedal_status': pedal_status,
'slice_idx': slice_idx, 'kld_weight': kld_weight}
tempo_loss, vel_loss, dev_loss, articul_loss, pedal_loss, trill_loss, kld = \
batch_time_step_run(training_data, model=train_model)
tempo_loss_total.append(tempo_loss.item())
vel_loss_total.append(vel_loss.item())
dev_loss_total.append(dev_loss.item())
articul_loss_total.append(articul_loss.item())
pedal_loss_total.append(pedal_loss.item())
trill_loss_total.append(trill_loss.item())
kld_total.append(kld.item())
NUM_UPDATED += 1
del selected_sample.slice_indexes[selected_idx]
if len(selected_sample.slice_indexes) == 0:
# print('every slice in the sample is trained')
del remaining_samples[new_index]
else:
for xy_tuple in train_xy:
train_x = xy_tuple[0]
train_y = xy_tuple[1]
if args.trainingLoss == 'CE':
train_y = categorize_value_to_vector(train_y, BINS)
note_locations = xy_tuple[2]
align_matched = xy_tuple[3]
pedal_status = xy_tuple[4]
edges = xy_tuple[5]
data_size = len(note_locations)
if MODEL.is_graph:
graphs = edges_to_matrix(edges, data_size)
else:
graphs = None
measure_numbers = [x.measure for x in note_locations]
# graphs = edges_to_sparse_tensor(edges)
total_batch_num = int(math.ceil(data_size / (TIME_STEPS * batch_size)))
key_lists = [0]
key = 0
for i in range(num_key_augmentation):
while key in key_lists:
key = random.randrange(-5, 7)
key_lists.append(key)
for i in range(num_key_augmentation+1):
key = key_lists[i]
temp_train_x = dp.key_augmentation(train_x, key)
slice_indexes = dp.make_slicing_indexes_by_measure(data_size, measure_numbers, steps=TIME_STEPS)
kld_weight = sigmoid((NUM_UPDATED - KLD_SIG) / (KLD_SIG/10)) * KLD_MAX
for slice_idx in slice_indexes:
training_data = {'x': temp_train_x, 'y': train_y, 'graphs': graphs,
'note_locations': note_locations,
'align_matched': align_matched, 'pedal_status': pedal_status,
'slice_idx': slice_idx, 'kld_weight': kld_weight}
tempo_loss, vel_loss, dev_loss, articul_loss, pedal_loss, trill_loss, kld = \
batch_time_step_run(training_data, model=train_model)
tempo_loss_total.append(tempo_loss.item())
vel_loss_total.append(vel_loss.item())
dev_loss_total.append(dev_loss.item())
articul_loss_total.append(articul_loss.item())
pedal_loss_total.append(pedal_loss.item())
trill_loss_total.append(trill_loss.item())
kld_total.append(kld.item())
NUM_UPDATED += 1
print('Epoch [{}/{}], Loss - Tempo: {:.4f}, Vel: {:.4f}, Deviation: {:.4f}, Articulation: {:.4f}, Pedal: {:.4f}, Trill: {:.4f}, KLD: {:.4f}'
.format(epoch + 1, num_epochs, np.mean(tempo_loss_total), np.mean(vel_loss_total),
np.mean(dev_loss_total), np.mean(articul_loss_total), np.mean(pedal_loss_total), np.mean(trill_loss_total), np.mean(kld_total)))
## Validation
tempo_loss_total =[]
vel_loss_total =[]
deviation_loss_total =[]
articul_loss_total = []
pedal_loss_total = []
trill_loss_total = []
kld_loss_total = []
for xy_tuple in test_xy:
test_x = xy_tuple[0]
test_y = xy_tuple[1]
note_locations = xy_tuple[2]
align_matched = xy_tuple[3]
pedal_status = xy_tuple[4]
edges = xy_tuple[5]
if MODEL.is_graph:
graphs = edges_to_matrix(edges, len(test_x))
else:
graphs = None
if LOSS_TYPE == 'CE':
test_y = categorize_value_to_vector(test_y, BINS)
batch_x, batch_y = handle_data_in_tensor(test_x, test_y)
batch_x = batch_x.view(1, -1, NUM_INPUT)
batch_y = batch_y.view(1, -1, NUM_OUTPUT)
# input_y = torch.Tensor(prev_feature).view((1, -1, TOTAL_OUTPUT)).to(DEVICE)
align_matched = torch.Tensor(align_matched).view(1, -1, 1).to(DEVICE)
pedal_status = torch.Tensor(pedal_status).view(1,-1,1).to(DEVICE)
outputs, total_z = run_model_in_steps(batch_x, batch_y, graphs, note_locations)
# valid_loss = criterion(outputs[:,:,NUM_TEMPO_PARAM:-num_trill_param], batch_y[:,:,NUM_TEMPO_PARAM:-num_trill_param], align_matched)
if HIERARCHY:
if HIER_MEAS:
hierarchy_numbers = [x.measure for x in note_locations]
elif HIER_BEAT:
hierarchy_numbers = [x.beat for x in note_locations]
tempo_y = MODEL.note_tempo_infos_to_beat(batch_y, hierarchy_numbers, 0, 0)
vel_y = MODEL.note_tempo_infos_to_beat(batch_y, hierarchy_numbers, 0, 1)
tempo_loss = criterion(outputs[:, :, 0:1], tempo_y)
vel_loss = criterion(outputs[:, :, 1:2], vel_y)
if args.deltaLoss:
tempo_out_delta = outputs[:, 1:, 0:1] - outputs[:, :-1, 0:1]
tempo_true_delta = tempo_y[:, 1:, :] - tempo_y[:, :-1, :]
vel_out_delta = outputs[:, 1:, 1:2] - outputs[:, :-1, 1:2]
vel_true_delta = vel_y[:, 1:, :] - vel_y[:, :-1, :]
tempo_loss += criterion(tempo_out_delta, tempo_true_delta) * DELTA_WEIGHT
vel_loss += criterion(vel_out_delta, vel_true_delta) * DELTA_WEIGHT
deviation_loss = torch.zeros(1)
articul_loss = torch.zeros(1)
pedal_loss = torch.zeros(1)
trill_loss = torch.zeros(1)
for z in total_z:
perform_mu, perform_var = z
kld_loss = -0.5 * torch.sum(1 + perform_var - perform_mu.pow(2) - perform_var.exp())
kld_loss_total.append(kld_loss.item())
elif TRILL:
trill_bool = batch_x[:,:,is_trill_index_concated] == 1
trill_bool = trill_bool.float().view(1,-1,1).to(DEVICE)
trill_loss = criterion(outputs, batch_y, trill_bool)
tempo_loss = torch.zeros(1)
vel_loss = torch.zeros(1)
deviation_loss = torch.zeros(1)
articul_loss = torch.zeros(1)
pedal_loss = torch.zeros(1)
kld_loss = torch.zeros(1)
kld_loss_total.append(kld_loss.item())
else:
tempo_loss = cal_tempo_loss_in_beat(outputs, batch_y, note_locations, 0)
if LOSS_TYPE =='CE':
vel_loss = criterion(outputs[:,:,NUM_TEMPO_PARAM:NUM_TEMPO_PARAM+len(BINS[1])], batch_y[:,:,NUM_TEMPO_PARAM:NUM_TEMPO_PARAM+len(BINS[1])], align_matched)
deviation_loss = criterion(outputs[:,:,NUM_TEMPO_PARAM+len(BINS[1]):NUM_TEMPO_PARAM+len(BINS[1])+len(BINS[2])],
batch_y[:,:,NUM_TEMPO_PARAM+len(BINS[1]):NUM_TEMPO_PARAM+len(BINS[1])+len(BINS[2])])
pedal_loss = criterion(outputs[:,:,NUM_TEMPO_PARAM+len(BINS[1])+len(BINS[2]):-num_trill_param],
batch_y[:,:,NUM_TEMPO_PARAM+len(BINS[1])+len(BINS[2]):-num_trill_param])
trill_loss = criterion(outputs[:,:,-num_trill_param:], batch_y[:,:,-num_trill_param:])
else:
vel_loss = criterion(outputs[:, :, VEL_PARAM_IDX], batch_y[:, :, VEL_PARAM_IDX], align_matched)
deviation_loss = criterion(outputs[:, :, DEV_PARAM_IDX], batch_y[:, :, DEV_PARAM_IDX], align_matched)
articul_loss = criterion(outputs[:, :, PEDAL_PARAM_IDX], batch_y[:, :, PEDAL_PARAM_IDX], pedal_status)
pedal_loss = criterion(outputs[:, :, PEDAL_PARAM_IDX+1:], batch_y[:, :, PEDAL_PARAM_IDX+1:], align_matched)
trill_loss = torch.zeros(1)
for z in total_z:
perform_mu, perform_var = z
kld_loss = -0.5 * torch.sum(1 + perform_var - perform_mu.pow(2) - perform_var.exp())
kld_loss_total.append(kld_loss.item())
# valid_loss_total.append(valid_loss.item())
tempo_loss_total.append(tempo_loss.item())
vel_loss_total.append(vel_loss.item())
deviation_loss_total.append(deviation_loss.item())
articul_loss_total.append(articul_loss.item())
pedal_loss_total.append(pedal_loss.item())
trill_loss_total.append(trill_loss.item())