-
Notifications
You must be signed in to change notification settings - Fork 0
/
train2.py
1342 lines (1187 loc) · 58.1 KB
/
train2.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 glob
import os
import numpy
import torch
from tqdm import tqdm
from music21 import converter, instrument, note, chord, stream, duration, pitch
import generation2
from data import NoteData, MidiDataset, NetworkData
from generation2 import create_midi_track
from model import *
import os
from torch.cuda.amp import autocast
def get_notes(directory, get_flat=False):
data = NoteData()
for file in glob.glob(f'{directory}*.mid'):
print("Parsing: ", file)
try:
midi = converter.parse(file)
except Exception as e:
print(f"Warning: could not parse {file}. Skipping. Error: {e}")
continue
if not get_flat:
try: # file has instrument parts
instruments = instrument.partitionByInstrument(midi)
notes_to_parse = instruments.parts[0].recurse()
except: # file has notes in a flat structure
notes_to_parse = midi.flat.notes
else:
notes_to_parse = midi.flat.notes
prev_event_end = -(data.get_random_off())
notes = []
for event in notes_to_parse:
if isinstance(event, note.Note) or isinstance(event, chord.Chord):
offset = event.offset - prev_event_end # Calc offset (distance from last note)
if offset < 0:
offset = 0
try:
inst = event.activeSite.getInstrument()
if inst and inst.midiProgram is not None and inst.midiProgram >= 110: # Filter out percussion
continue
except:
continue
note_val = None
durr_val = None
vel_val = None
if isinstance(event, note.Note):
note_val = str(event.pitch.nameWithOctave)
durr_val = float(event.duration.quarterLength)
vel_val = event.volume.velocity if event.volume else None
elif isinstance(event, chord.Chord):
note_val = '.'.join(str(p.nameWithOctave) for p in event.pitches)
durr_val = float(event.duration.quarterLength)
vel_val = event.volume.velocity if event.volume else None
if note_val is not None and durr_val is not None and vel_val is not None:
note_val = data.add_note_if_absent(note_val)
durr_val = data.add_durr_if_absent(durr_val)
offset = data.add_offs_if_absent(offset)
vel_val = data.add_vel_if_absent(vel_val)
notes.append((note_val, offset, durr_val, vel_val))
prev_event_end = event.offset
data.training_notes.append(notes)
data.calc_vocab()
print(f'train:{data.training_notes}')
print(f'Notes{data.note_table}')
print(f'Offsets{data.offset_table}')
print(f'Durations{data.duration_table}')
print(f'Velocities{data.velocity_table}')
return data
class Step:
def __init__(self, max):
self.channels = numpy.zeros((4, max), dtype=int)
self.max = max
self.idx = 0
def add_note(self, note_tuple):
if self.idx == self.max - 1:
return
for i in range(0, self.max):
if self.channels[0][i] == note_tuple[0]:
return
for i in range (0,4):
self.channels[i][self.idx] = note_tuple[i]
self.idx += 1
def get_step(self):
# Bubble sort columns using first row as the key
for i in range(self.max):
for j in range(self.max - i - 1):
if self.channels[0][j] > self.channels[0][j + 1]:
for k in range(4):
self.channels[k][j], self.channels[k][j + 1] = self.channels[k][j + 1], self.channels[k][j]
# Rotate until zeros in first row are at the end
while self.channels[0][0] == 0:
self.channels = np.roll(self.channels, -1, axis=1)
return self.channels
def get_notes_single(directory, max_chan, get_flat=True):
data = NoteData()
x = 0
for file in glob.glob(f'{directory}*.mid'):
print("Parsing: ", file)
try:
midi = converter.parse(file)
except Exception as e:
print(f"Warning: could not parse {file}. Skipping. Error: {e}")
continue
#
# if not get_flat:
# try: # file has instrument parts
# instruments = instrument.partitionByInstrument(midi)
# notes_to_parse = instruments.parts[0].recurse()
# except: # file has notes in a flat structure
# notes_to_parse = midi.flat.notes
# else:
notes_to_parse = midi.flat.notes
prev_event_offset = -(data.get_random_off())
notes = []
step = None
for event in notes_to_parse:
if isinstance(event, note.Note) or isinstance(event, chord.Chord):
offset = event.offset - prev_event_offset # Calc offset (distance from last note)
if offset < 0:
offset = 0
try:
inst = event.activeSite.getInstrument()
if inst and inst.midiProgram is not None and inst.midiProgram >= 110: # Filter out percussion
continue
except:
continue
if offset > 0:
if step is not None:
notes.append(step.get_step())
step = Step(max_chan)
if isinstance(event, note.Note):
# note_val = data.add_note_if_absent(str(event.pitch.nameWithOctave))
note_val = data.add_note_if_absent(event.pitch.midi)
durr_val = data.add_durr_if_absent(float(event.duration.quarterLength))
vel_val = data.add_vel_if_absent(event.volume.velocity) if event.volume else None
off_val = data.add_offs_if_absent(offset)
tup = (note_val, off_val, durr_val, vel_val)
if not any(item is None for item in tup):
step.add_note(tup)
elif isinstance(event, chord.Chord):
p = event.pitches
for i in range(0, len(event.pitches)):
note_val = data.add_note_if_absent(p[i].midi)
durr_val = data.add_durr_if_absent(event.duration.quarterLength)
vel_val = data.add_vel_if_absent(event.volume.velocity if event.volume else None)
off_val = data.add_offs_if_absent(offset)
tup = (note_val, off_val, durr_val, vel_val)
if not any(item is None for item in tup):
step.add_note(tup)
prev_event_offset = event.offset
data.training_notes.append(notes)
# pred =[]
# for step in notes:
# ns = []
# os = []
# ds = []
# vs = []
# for i in range(6):
# ns.append(data.get_note(step[0][i]))
# os.append(data.get_offset(step[1][i]))
# ds.append(data.get_duration(step[2][i]))
# vs.append(data.get_velocity(step[3][i]))
# pred.append((ns,os,ds,vs))
#
# file = file.split("/")
# output_file = f"test_{file[2]}.mid"
# create_midi_track(pred, output_file=output_file)
# x += 1
data.calc_vocab()
return data
def prepare_sequences(note_data, device=torch.device("cpu"), sequence_length=64, skip_amount=1):
sequence_length = sequence_length
network_input = []
network_output_notes = []
network_output_offsets = []
network_output_durations = []
network_output_velocities = []
# create input sequences and the corresponding outputs
for notes in note_data.training_notes:
for i in range(0, len(notes) - sequence_length - 1, skip_amount):
sequence_in = notes[i:i + sequence_length]
sequence_out = notes[i + sequence_length]
network_input.append([[x[0], x[1], x[2], x[3]] for x in sequence_in])
network_output_notes.append(sequence_out[0])
network_output_offsets.append(sequence_out[1])
network_output_durations.append(sequence_out[2])
network_output_velocities.append(sequence_out[3])
network_input = torch.tensor(network_input, dtype=torch.long).to(device)
# Shape for cross_entropy: (N) where N is batch size.
# network_output_notes = torch.tensor(network_output_notes, torch.long).view(-1).to(device)
# network_output_offsets = torch.tensor(network_output_offsets, torch.long).view(-1).to(device)
# network_output_durations = torch.tensor(network_output_durations, torch.long).view(-1).to(device)
# network_output_velocities = torch.tensor(network_output_velocities, torch.long).view(-1).to(device)
network_output_notes = torch.tensor(network_output_notes, dtype=torch.long).to(device)
network_output_offsets = torch.tensor(network_output_offsets, dtype=torch.long).to(device)
network_output_durations = torch.tensor(network_output_durations, dtype=torch.long).to(device)
network_output_velocities = torch.tensor(network_output_velocities, dtype=torch.long).to(device)
return NetworkData(network_input, network_output_notes, network_output_offsets, network_output_durations,
network_output_velocities)
def generate_seed_from_int(seed_int, seq_length, note_data):
# Create a random number generator with the provided seed
rng = np.random.default_rng(seed_int)
# Generate random indices within the range of each vocabulary
note_indices = rng.integers(note_data.n_vocab, size=seq_length)
offset_indices = rng.integers(note_data.o_vocab, size=seq_length)
duration_indices = rng.integers(note_data.d_vocab, size=seq_length)
velocity_indices = rng.integers(note_data.v_vocab, size=seq_length)
# Stack the indices into a single sequence and reshape it to the required shape
seed_sequence = np.vstack([note_indices, offset_indices, duration_indices, velocity_indices])
seed_sequence = seed_sequence.T.reshape(1, seq_length, 4)
# Convert to a PyTorch tensor
seed_sequence = torch.tensor(seed_sequence, dtype=torch.float16)
return seed_sequence
#
# def train(model, train_loader, criterion, optimizer, device, note_data, scheduler=None, clip_value=None):
# model.train()
# running_loss = 0.0
# correct_predictions = 0
# total_predictions = 0
#
# for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(train_loader):
# inputs = inputs.to(device)
# targets_note = targets_note.to(device)
# targets_offset = targets_offset.to(device)
# targets_duration = targets_duration.to(device)
# targets_velocity = targets_velocity.to(device)
#
# # Forward pass
# output_note, output_offset, output_duration, output_velocity = model(inputs)
#
# # Calculate loss
# loss_note = criterion(output_note.view(-1, note_data.n_vocab), targets_note.view(-1).long())
# loss_offset = criterion(output_offset.view(-1, note_data.o_vocab), targets_offset.view(-1).long())
# loss_duration = criterion(output_duration.view(-1, note_data.d_vocab), targets_duration.view(-1).long())
# loss_velocity = criterion(output_velocity.view(-1, note_data.v_vocab), targets_velocity.view(-1).long())
#
# loss = loss_note + loss_offset + loss_duration + loss_velocity
#
# # Backward pass and optimization
# optimizer.zero_grad()
# loss.backward()
#
# # Gradient clip
# if clip_value is not None: # Gradient clipping
# torch.nn.utils.clip_grad_norm_(model.parameters(), clip_value)
#
# optimizer.step()
#
# if scheduler is not None:
# scheduler.step()
#
# running_loss += loss.item() * inputs.size(0)
#
# # Calculate accuracy
# _, predicted_notes = torch.max(output_note.data, 1)
# _, predicted_offsets = torch.max(output_offset.data, 1)
# _, predicted_durations = torch.max(output_duration.data, 1)
# _, predicted_velocities = torch.max(output_velocity.data, 1)
#
# total_predictions += targets_note.size(0)
# correct_predictions += (predicted_notes == targets_note).sum().item()
# correct_predictions += (predicted_offsets == targets_offset).sum().item()
# correct_predictions += (predicted_durations == targets_duration).sum().item()
# correct_predictions += (predicted_velocities == targets_velocity).sum().item()
#
# accuracy = correct_predictions / (total_predictions * 4)
#
# return (running_loss / len(train_loader.dataset)) / 4, accuracy
#
# def validate(model, valid_loader, criterion, device, note_data):
# model.eval()
# running_loss = 0.0
# correct_predictions = 0
# total_predictions = 0
#
# with torch.no_grad():
# for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(valid_loader):
# inputs = inputs.to(device)
# targets_note = targets_note.to(device)
# targets_offset = targets_offset.to(device)
# targets_duration = targets_duration.to(device)
# targets_velocity = targets_velocity.to(device)
#
# # Forward pass
# output_note, output_offset, output_duration, output_velocity = model(inputs)
#
# # Calculate loss
# loss_note = criterion(output_note.view(-1, note_data.n_vocab), targets_note.view(-1).long())
# loss_offset = criterion(output_offset.view(-1, note_data.o_vocab), targets_offset.view(-1).long())
# loss_duration = criterion(output_duration.view(-1, note_data.d_vocab), targets_duration.view(-1).long())
# loss_velocity = criterion(output_velocity.view(-1, note_data.v_vocab), targets_velocity.view(-1).long())
#
# loss = loss_note + loss_offset + loss_duration + loss_velocity
#
# running_loss += loss.item() * inputs.size(0)
#
# # Calculate accuracy
# _, predicted_notes = torch.max(output_note.data, 1)
# _, predicted_offsets = torch.max(output_offset.data, 1)
# _, predicted_durations = torch.max(output_duration.data, 1)
# _, predicted_velocities = torch.max(output_velocity.data, 1)
#
# total_predictions += targets_note.size(0)
# correct_predictions += (predicted_notes == targets_note).sum().item()
# correct_predictions += (predicted_offsets == targets_offset).sum().item()
# correct_predictions += (predicted_durations == targets_duration).sum().item()
# correct_predictions += (predicted_velocities == targets_velocity).sum().item()
#
# accuracy = correct_predictions / (total_predictions * 4)
#
# return (running_loss / len(valid_loader.dataset)) / 4, accuracy
# def train(model, train_loader, criterion, optimizer, device, note_data, scaler, scheduler=None, clip_value=None):
# model.train()
# running_loss = 0.0
# total_predictions = 0
#
# for inputs, targets in tqdm(train_loader):
# inputs = inputs.to(device)
# targets = [target.to(device) for target in targets] # move each target to the device
#
# # Forward pass
# with autocast():
# outputs = model(inputs) # outputs is a list of tensors
#
# # Calculate loss
#
# losses = [criterion(output, target) for output, target in zip(outputs, targets)]
# loss = sum(losses)
#
# # Backward pass and optimization
# optimizer.zero_grad()
# scaler.scale(loss).backward()
#
# # Gradient clipping
# if clip_value is not None:
# scaler.unscale_(optimizer)
# torch.nn.utils.clip_grad_norm_(model.parameters(), clip_value)
#
# scaler.step(optimizer)
# scaler.update()
#
# if scheduler is not None:
# scheduler.step()
#
# running_loss += loss.item() * inputs.size(0)
#
# # Calculate accuracy
# correct_predictions = [(output.argmax(dim=1) == target).sum().item() for output, target in zip(outputs, targets)]
# total_predictions += inputs.size(0)
# accuracy = sum(correct_predictions) / (total_predictions * len(outputs))
#
# return running_loss / len(train_loader.dataset), accuracy
#
#
#
# def validate(model, valid_loader, criterion, device, note_data):
# model.eval()
# running_loss = 0.0
# correct_predictions = 0
# total_predictions = 0
#
# with torch.no_grad():
# for inputs, targets in tqdm(valid_loader):
# inputs = inputs.to(device)
# targets = targets.to(device)
#
# # Forward pass
# with autocast():
# outputs = model(inputs)
#
# # Calculate loss
# loss = criterion(outputs, targets)
#
# running_loss += loss.item() * inputs.size(0)
#
# # Calculate accuracy
# _, predicted = torch.max(outputs.data, 1)
# total_predictions += targets.size(0)
# correct_predictions += (predicted == targets).sum().item()
#
# accuracy = correct_predictions / total_predictions
#
# return running_loss / len(valid_loader.dataset), accuracy
#
# def train(model, train_loader, criterion, optimizer, device, note_data, scalar, scheduler=None, clip_value=None):
# model.train()
# running_loss = 0.0
# correct_predictions = 0
# total_predictions = 0
#
# for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(train_loader):
# inputs = inputs.to(device)
# targets_note = targets_note.to(device)
# targets_offset = targets_offset.to(device)
# targets_duration = targets_duration.to(device)
# targets_velocity = targets_velocity.to(device)
#
# # Forward pass
# output_note, output_offset, output_duration, output_velocity = model(inputs.to(device))
#
# # Calculate loss
# loss_note = criterion(output_note.view(-1, note_data.n_vocab), targets_note.view(-1).long())
# loss_offset = criterion(output_offset.view(-1, note_data.o_vocab), targets_offset.view(-1).long())
# loss_duration = criterion(output_duration.view(-1, note_data.d_vocab), targets_duration.view(-1).long())
# loss_velocity = criterion(output_velocity.view(-1, note_data.v_vocab), targets_velocity.view(-1).long())
#
# loss = loss_note + loss_offset + loss_duration + loss_velocity
#
# # Backward pass and optimization
# optimizer.zero_grad()
# loss.backward()
#
# # Gradient clip
# if clip_value is not None: # Gradient clipping
# torch.nn.utils.clip_grad_norm_(model.parameters(), clip_value)
#
# optimizer.step()
#
# if scheduler is not None:
# scheduler.step()
#
# running_loss += loss.item() * inputs.size(0)
#
# # Calculate accuracy
# _, predicted_notes = torch.max(output_note.data, 1)
# _, predicted_offsets = torch.max(output_offset.data, 1)
# _, predicted_durations = torch.max(output_duration.data, 1)
# _, predicted_velocities = torch.max(output_velocity.data, 1)
#
# total_predictions += targets_note.size(0)
# correct_predictions += (predicted_notes == targets_note).sum().item()
# correct_predictions += (predicted_offsets == targets_offset).sum().item()
# correct_predictions += (predicted_durations == targets_duration).sum().item()
# correct_predictions += (predicted_velocities == targets_velocity).sum().item()
#
# accuracy = correct_predictions / (total_predictions * 4)
#
# return (running_loss / len(train_loader.dataset)) / 4, accuracy
# #
#
#
#
#
# Evaluation function
# def evaluate(model, val_loader, criterion, device, note_data):
# model.eval()
# running_loss = 0.0
# correct_predictions = 0
# total_predictions = 0
#
# with torch.no_grad():
# for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(val_loader):
# inputs = inputs.to(device)
# targets_note = targets_note.to(device)
# targets_offset = targets_offset.to(device)
# targets_duration = targets_duration.to(device)
# targets_velocity = targets_velocity.to(device)
#
# # Forward pass
# output_note, output_offset, output_duration, output_velocity = model(inputs)
#
# # Calculate loss
# loss_note = criterion(output_note.view(-1, note_data.n_vocab), targets_note.view(-1).long())
# loss_offset = criterion(output_offset.view(-1, note_data.o_vocab), targets_offset.view(-1).long())
# loss_duration = criterion(output_duration.view(-1, note_data.d_vocab), targets_duration.view(-1).long())
# loss_velocity = criterion(output_velocity.view(-1, note_data.v_vocab), targets_velocity.view(-1).long())
#
# loss = loss_note + loss_offset + loss_duration + loss_velocity
#
# running_loss += loss.item() * inputs.size(0)
#
# # Calculate accuracy
# _, predicted_notes = torch.max(output_note.data, 1)
# _, predicted_offsets = torch.max(output_offset.data, 1)
# _, predicted_durations = torch.max(output_duration.data, 1)
# _, predicted_velocities = torch.max(output_velocity.data, 1)
#
# total_predictions += targets_note.size(0)
# correct_predictions += (predicted_notes == targets_note).sum().item()
# correct_predictions += (predicted_offsets == targets_offset).sum().item()
# correct_predictions += (predicted_durations == targets_duration).sum().item()
# correct_predictions += (predicted_velocities == targets_velocity).sum().item()
#
# accuracy = correct_predictions / (total_predictions * 4)
#
# return (running_loss / len(val_loader.dataset)) / 4, accuracy
# Training function
# def train(model, train_loader, criterion, optimizer, device, note_data, scalar, bathc_size, scheduler=None, clip_value=2):
# model.train()
# running_loss = 0.0
# correct_predictions = 0
# total_predictions = 0
#
#
# hidden = model.init_hidden(device, batch_size=bathc_size)
# for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(train_loader):
# optimizer.zero_grad()
# # hidden = model.detach_hidden(hidden)
#
#
#
# hidden = model.init_hidden(device, bathc_size)
# inputs = inputs.to(device)
# targets_note = targets_note.to(device)
# targets_offset = targets_offset.to(device)
# targets_duration = targets_duration.to(device)
# targets_velocity = targets_velocity.to(device)
#
#
# # Create weights for each target feature
# weight_note = torch.where(targets_note != 0, 1.2, 0.2).to(device)
# weight_offset = torch.where(targets_offset != 0, 1.2, 0.2).to(device)
# weight_duration = torch.where(targets_duration != 0, 1, 0.2).to(device)
# weight_velocity = torch.where(targets_velocity != 0, 0.5, 0.1).to(device)
#
#
# # Forward pass
# output_note, output_offset, output_duration, output_velocity, hidden = model(inputs.to(device), hidden)
#
# # Calculate loss for each prediction
# loss_note = 0
# loss_offset = 0
# loss_duration = 0
# loss_velocity = 0
#
# for i in range(6):
# element_loss_note = criterion(output_note[:, i, :], targets_note[:, i])
# element_loss_offset = criterion(output_offset[:, i, :], targets_offset[:, i])
# element_loss_duration = criterion(output_duration[:, i, :], targets_duration[:, i])
# element_loss_velocity = criterion(output_velocity[:, i, :], targets_velocity[:, i])
#
# weighted_loss_note = element_loss_note * weight_note[:, i]
# weighted_loss_offset = element_loss_offset * weight_offset[:, i]
# weighted_loss_duration = element_loss_duration * weight_duration[:, i]
# weighted_loss_velocity = element_loss_velocity * weight_velocity[:, i]
#
# loss_note += torch.mean(weighted_loss_note)
# loss_offset += torch.mean(weighted_loss_offset)
# loss_duration += torch.mean(weighted_loss_duration)
# loss_velocity += torch.mean(weighted_loss_velocity)
# #
# # Calculate accuracy
# _, predicted_notes = torch.max(output_note.data, 2)
# _, predicted_offsets = torch.max(output_offset.data, 2)
# _, predicted_durations = torch.max(output_duration.data, 2)
# _, predicted_velocities = torch.max(output_velocity.data, 2)
#
# total_predictions += targets_note.size(0) * targets_note.size(1)
# correct_predictions += (predicted_notes == targets_note).sum().item()
# correct_predictions += (predicted_offsets == targets_offset).sum().item()
# correct_predictions += (predicted_durations == targets_duration).sum().item()
# correct_predictions += (predicted_velocities == targets_velocity).sum().item()
#
#
# loss = loss_note + loss_offset + loss_duration + loss_velocity
#
#
# # Backward pass and optimization
#
# loss.backward()
#
# # Gradient clip
# if clip_value is not None: # Gradient clipping
# torch.nn.utils.clip_grad_norm_(model.parameters(), clip_value)
#
# optimizer.step()
#
# if scheduler is not None:
# scheduler.step()
#
# running_loss += loss.item() * inputs.size(0)
#
#
#
# accuracy = correct_predictions / total_predictions
#
# return running_loss / len(train_loader.dataset), accuracy
# def train(model, train_loader, criterion, optimizer, device, note_data, scalar, batch_size, scheduler=None, clip_value=2):
# model.train()
# running_loss = 0.0
# correct_predictions = 0
# total_predictions = 0
#
# hidden = model.init_hidden(device, batch_size=batch_size)
# for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(train_loader):
# optimizer.zero_grad()
#
# hidden = model.init_hidden(device, batch_size)
# inputs = inputs.to(device)
# targets_note = targets_note.to(device)
# targets_offset = targets_offset.to(device)
# targets_duration = targets_duration.to(device)
# targets_velocity = targets_velocity.to(device)
#
# # Create weights for each target feature (once per batch)
# weight_note = torch.where(targets_note != 0, 1, 0.2).to(device)
# weight_offset = torch.where(targets_offset != 0, 1, 0.2).to(device)
# weight_duration = torch.where(targets_duration != 0, 0.8, 0.2).to(device)
# weight_velocity = torch.where(targets_velocity != 0, 0.5, 0.1).to(device)
#
# # Forward pass
# output_note, output_offset, output_duration, output_velocity, hidden = model(inputs.to(device), hidden)
#
# # Calculate loss for each prediction
# loss_note = 0
# loss_offset = 0
# loss_duration = 0
# loss_velocity = 0
#
# for i in range(6):
# # Calculate loss for this timestamp
# element_loss_note = criterion(output_note[:, i, :], targets_note[:, i])
# element_loss_offset = criterion(output_offset[:, i, :], targets_offset[:, i])
# element_loss_duration = criterion(output_duration[:, i, :], targets_duration[:, i])
# element_loss_velocity = criterion(output_velocity[:, i, :], targets_velocity[:, i])
#
# # Apply weights
# weighted_loss_note = element_loss_note * weight_note[:, i]
# weighted_loss_offset = element_loss_offset * weight_offset[:, i]
# weighted_loss_duration = element_loss_duration * weight_duration[:, i]
# weighted_loss_velocity = element_loss_velocity * weight_velocity[:, i]
#
# # Accumulate losses
# loss_note += torch.mean(weighted_loss_note)
# loss_offset += torch.mean(weighted_loss_offset)
# loss_duration += torch.mean(weighted_loss_duration)
# loss_velocity += torch.mean(weighted_loss_velocity)
#
# loss = loss_note + loss_offset + loss_duration + loss_velocity
#
# loss.backward()
#
# if clip_value is not None: # Gradient clipping
# torch.nn.utils.clip_grad_norm_(model.parameters(), clip_value)
#
# optimizer.step()
#
# if scheduler is not None:
# scheduler.step()
#
# running_loss += loss.item() * inputs.size(0)
#
# # Calculate accuracy
# _, predicted_notes = torch.max(output_note.data, 2)
# _, predicted_offsets = torch.max(output_offset.data, 2)
# _, predicted_durations = torch.max(output_duration.data, 2)
# _, predicted_velocities = torch.max(output_velocity.data, 2)
#
# total_predictions += targets_note.size(0) * targets_note.size(1)
# correct_predictions += (predicted_notes == targets_note).sum().item()
# correct_predictions += (predicted_offsets == targets_offset).sum().item()
# correct_predictions += (predicted_durations == targets_duration).sum().item()
# correct_predictions += (predicted_velocities == targets_velocity).sum().item()
#
# accuracy = correct_predictions / total_predictions
#
# return running_loss / len(train_loader.dataset), accuracy
#
# def train(model, train_loader, criterion, optimizer, device, note_data, scalar, batch_size, scheduler=None, clip_value=2):
# model.train()
# running_loss = 0.0
# total_predictions = 0
# correct_predictions = 0
#
# hidden = model.init_hidden(device, batch_size=batch_size)
# for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(train_loader):
# optimizer.zero_grad()
# hidden = model.detach_hidden(hidden)
#
# inputs = inputs.to(device)
# targets_note = targets_note.to(device)
# targets_offset = targets_offset.to(device)
# targets_duration = targets_duration.to(device)
# targets_velocity = targets_velocity.to(device)
#
# # Forward pass
# output_note, output_offset, output_duration, output_velocity, hidden = model(inputs, hidden)
#
# # Create masks for each target feature
# mask_note = (targets_note != 0).to(device)
# mask_offset = (targets_offset != 0).to(device)
# mask_duration = (targets_duration != 0).to(device)
# mask_velocity = (targets_velocity != 0).to(device)
#
# loss_note = 0
# loss_offset = 0
# loss_duration = 0
# loss_velocity = 0
# for i in range(6):
# # Element-wise loss for each feature
# element_loss_note = criterion(output_note[:, i, :], targets_note[:, i])
# element_loss_offset = criterion(output_offset[:, i, :], targets_offset[:, i])
# element_loss_duration = criterion(output_duration[:, i, :], targets_duration[:, i])
# element_loss_velocity = criterion(output_velocity[:, i, :], targets_velocity[:, i])
#
# # Apply the mask to the loss
# masked_loss_note = element_loss_note * mask_note[:, i]
# masked_loss_offset = element_loss_offset * mask_offset[:, i]
# masked_loss_duration = element_loss_duration * mask_duration[:, i]
# masked_loss_velocity = element_loss_velocity * mask_velocity[:, i]
#
# # Mean of the masked losses
# loss_note += torch.mean(masked_loss_note)
# loss_offset += torch.mean(masked_loss_offset)
# loss_duration += torch.mean(masked_loss_duration)
# loss_velocity += torch.mean(masked_loss_velocity)
# #
# # loss_note += torch.sum(masked_loss_note)
# # loss_offset += torch.sum(masked_loss_offset)
# # loss_duration += torch.sum(masked_loss_duration)
# # loss_velocity += torch.sum(masked_loss_velocity)
#
# _, predicted_notes = torch.max(output_note.data, 2)
# _, predicted_offsets = torch.max(output_offset.data, 2)
# _, predicted_durations = torch.max(output_duration.data, 2)
# _, predicted_velocities = torch.max(output_velocity.data, 2)
# torch.sum
# # Calculate accuracy only on non-padding tokens
# correct_predictions += ((predicted_notes == targets_note) * mask_note).sum().item()
# correct_predictions += ((predicted_offsets == targets_offset) * mask_offset).sum().item()
# correct_predictions += ((predicted_durations == targets_duration) * mask_duration).sum().item()
# correct_predictions += ((predicted_velocities == targets_velocity) * mask_velocity).sum().item()
#
# total_predictions += mask_note.sum().item()
# total_predictions += mask_offset.sum().item()
# total_predictions += mask_duration.sum().item()
# total_predictions += mask_velocity.sum().item()
#
# loss = loss_note + loss_offset + loss_duration + loss_velocity
#
#
# # Backward pass and optimize
# loss.backward()
# if clip_value is not None: # Gradient clipping
# torch.nn.utils.clip_grad_norm_(model.parameters(), clip_value)
# optimizer.step()
#
# if scheduler is not None:
# scheduler.step()
#
# running_loss += loss.item() * inputs.size(0)
#
# accuracy = correct_predictions / total_predictions
#
# return (running_loss / len(train_loader.dataset)) / 4, accuracy
#
#
#
def train(model, train_loader, criterion, optimizer, device, note_data, scalar, batch_size, scheduler=None, clip_value=2):
model.train()
running_loss = 0.0
total_predictions = 0
correct_predictions = 0
hidden = model.init_hidden(device, batch_size=batch_size)
for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(train_loader):
optimizer.zero_grad()
hidden = model.detach_hidden(hidden)
inputs = inputs.to(device)
targets_note = targets_note.to(device)
targets_offset = targets_offset.to(device)
targets_duration = targets_duration.to(device)
targets_velocity = targets_velocity.to(device)
# Forward pass
output_note, output_offset, output_duration, output_velocity, hidden = model(inputs, hidden)
# Create weights for each target feature
weight_note = torch.where(targets_note != 0, 1.0, 0.25).to(device)
weight_offset = torch.where(targets_offset != 0, 1.0, 0.25).to(device)
weight_duration = torch.where(targets_duration != 0, 1.0, 0.25).to(device)
weight_velocity = torch.where(targets_velocity != 0, 1.0, 0.25).to(device)
loss_note = 0
loss_offset = 0
loss_duration = 0
loss_velocity = 0
for i in range(6):
# Element-wise loss for each feature
element_loss_note = criterion(output_note[:, i, :], targets_note[:, i])
element_loss_offset = criterion(output_offset[:, i, :], targets_offset[:, i])
element_loss_duration = criterion(output_duration[:, i, :], targets_duration[:, i])
element_loss_velocity = criterion(output_velocity[:, i, :], targets_velocity[:, i])
# Apply the weights to the loss
weighted_loss_note = element_loss_note * weight_note[:, i]
weighted_loss_offset = element_loss_offset * weight_offset[:, i]
weighted_loss_duration = element_loss_duration * weight_duration[:, i]
weighted_loss_velocity = element_loss_velocity * weight_velocity[:, i]
# Mean of the weighted losses
loss_note += torch.mean(weighted_loss_note)
loss_offset += torch.mean(weighted_loss_offset)
loss_duration += torch.mean(weighted_loss_duration)
loss_velocity += torch.mean(weighted_loss_velocity)
_, predicted_notes = torch.max(output_note.data, 2)
_, predicted_offsets = torch.max(output_offset.data, 2)
_, predicted_durations = torch.max(output_duration.data, 2)
_, predicted_velocities = torch.max(output_velocity.data, 2)
# Calculate accuracy only on non-padding tokens
correct_predictions += ((predicted_notes == targets_note) * weight_note).sum().item()
correct_predictions += ((predicted_offsets == targets_offset) * weight_offset).sum().item()
correct_predictions += ((predicted_durations == targets_duration) * weight_duration).sum().item()
correct_predictions += ((predicted_velocities == targets_velocity) * weight_velocity).sum().item()
total_predictions += weight_note.sum().item()
total_predictions += weight_offset.sum().item()
total_predictions += weight_duration.sum().item()
total_predictions += weight_velocity.sum().item()
loss = loss_note + loss_offset + loss_duration + loss_velocity
# Backward pass and optimize
loss.backward()
if clip_value is not None: # Gradient clipping
torch.nn.utils.clip_grad_norm_(model.parameters(), clip_value)
optimizer.step()
if scheduler is not None:
scheduler.step()
running_loss += loss.item() * inputs.size(0)
accuracy = correct_predictions / total_predictions
return (running_loss / len(train_loader.dataset)) / 4, accuracy
# Evaluation function
def evaluate(model, val_loader, criterion, device, note_data, batch_size):
model.eval()
running_loss = 0.0
correct_predictions = 0
total_predictions = 0
hidden = model.init_hidden(device, batch_size=batch_size)
with torch.no_grad():
for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(val_loader):
hidden = model.init_hidden(device, batch_size=batch_size)
inputs = inputs.to(device)
targets_note = targets_note.to(device)
targets_offset = targets_offset.to(device)
targets_duration = targets_duration.to(device)
targets_velocity = targets_velocity.to(device)
# Forward pass
output_note, output_offset, output_duration, output_velocity, hidden = model(inputs, hidden)
# Calculate loss for each prediction
loss_note = 0
loss_offset = 0
loss_duration = 0
loss_velocity = 0
for i in range(6):
loss_note += criterion(output_note[:, i, :], targets_note[:, i])
loss_offset += criterion(output_offset[:, i, :], targets_offset[:, i])
loss_duration += criterion(output_duration[:, i, :], targets_duration[:, i])
loss_velocity += criterion(output_velocity[:, i, :], targets_velocity[:, i])
loss = loss_note + loss_offset + loss_duration + loss_velocity
running_loss += loss.item() * inputs.size(0)
# Calculate accuracy
_, predicted_notes = torch.max(output_note.data, 2)
_, predicted_offsets = torch.max(output_offset.data, 2)
_, predicted_durations = torch.max(output_duration.data, 2)
_, predicted_velocities = torch.max(output_velocity.data, 2)
total_predictions += targets_note.size(0) * targets_note.size(1)
correct_predictions += (predicted_notes == targets_note).sum().item()
correct_predictions += (predicted_offsets == targets_offset).sum().item()
correct_predictions += (predicted_durations == targets_duration).sum().item()
correct_predictions += (predicted_velocities == targets_velocity).sum().item()
accuracy = correct_predictions / (total_predictions * 6)
return (running_loss / len(val_loader.dataset)) / 4, accuracy
#
# def evaluate(model, val_loader, criterion, device, note_data, batch_size):
# model.eval()
# running_loss = 0.0
# correct_predictions = 0
# total_predictions = 0
#
# hidden = model.init_hidden(device, batch_size=batch_size)
# with torch.no_grad():
# for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(val_loader):
# hidden = model.detach_hidden(hidden)
# inputs = inputs.to(device)
# targets_note = targets_note.to(device)
# targets_offset = targets_offset.to(device)
# targets_duration = targets_duration.to(device)
# targets_velocity = targets_velocity.to(device)
#
# # Forward pass
# output_note, output_offset, output_duration, output_velocity, hidden = model(inputs, hidden)
#
# # Create masks for each target feature
# mask_note = (targets_note != 0).to(device)
# mask_offset = (targets_offset != 0).to(device)
# mask_duration = (targets_duration != 0).to(device)
# mask_velocity = (targets_velocity != 0).to(device)
#
# loss_note = 0
# loss_offset = 0
# loss_duration = 0
# loss_velocity = 0
# for i in range(6):
# # Element-wise loss for each feature
# element_loss_note = criterion(output_note[:, i, :], targets_note[:, i])
# element_loss_offset = criterion(output_offset[:, i, :], targets_offset[:, i])
# element_loss_duration = criterion(output_duration[:, i, :], targets_duration[:, i])
# element_loss_velocity = criterion(output_velocity[:, i, :], targets_velocity[:, i])
#
# # Apply the mask to the loss
# masked_loss_note = element_loss_note * mask_note[:, i]
# masked_loss_offset = element_loss_offset * mask_offset[:, i]
# masked_loss_duration = element_loss_duration * mask_duration[:, i]
# masked_loss_velocity = element_loss_velocity * mask_velocity[:, i]
#
# # Mean of the masked losses
# loss_note += torch.mean(masked_loss_note)
# loss_offset += torch.mean(masked_loss_offset)
# loss_duration += torch.mean(masked_loss_duration)
# loss_velocity += torch.mean(masked_loss_velocity)
#
# loss = loss_note + loss_offset + loss_duration + loss_velocity