-
Notifications
You must be signed in to change notification settings - Fork 0
/
pop_melody_transformer.py
687 lines (464 loc) · 18.2 KB
/
pop_melody_transformer.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
# -*- coding: utf-8 -*-
"""POP_Melody_Transformer.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/github/asigalov61/POP-Melody-Transformer/blob/main/POP_Melody_Transformer.ipynb
# POP Melody Transformer (ver. 2.0)
***
Powered by tegridy-tools: https://github.com/asigalov61/tegridy-tools
***
WARNING: This complete implementation is a functioning model of the Artificial Intelligence. Please excercise great humility, care, and respect. https://www.nscai.gov/
***
#### Project Los Angeles
#### Tegridy Code 2023
***
# (SETUP ENVIRONMENT)
"""
#@title Install all dependencies (run only once per session)
!git clone --depth 1 https://github.com/asigalov61/POP-Melody-Transformer
!pip install einops
!pip install torch-summary
!pip install tqdm
!pip install matplotlib
!pip install gdown
!pip install huggingface_hub
# Commented out IPython magic to ensure Python compatibility.
#@title Import all needed modules
print('=' * 70)
print('Loading needed modules. Please wait...')
import os
import pickle
import secrets
import tqdm
import math
from joblib import Parallel, delayed, parallel_config
import torch
torch.set_float32_matmul_precision('high')
torch.backends.cuda.matmul.allow_tf32 = True # allow tf32 on matmul
torch.backends.cudnn.allow_tf32 = True # allow tf32 on cudnn
import matplotlib.pyplot as plt
from torchsummary import summary
from sklearn import metrics
from huggingface_hub import hf_hub_download
print('=' * 70)
print('Loading TMIDIX module...')
# %cd /content/POP-Melody-Transformer
import TMIDIX
print('=' * 70)
print('Loading X Transformer module...')
from x_transformer_1_23_2 import *
import random
# %cd /content/
print('=' * 70)
print('Creating I/O dirs...')
if not os.path.exists('/content/Dataset'):
os.makedirs('/content/Dataset')
if not os.path.exists('/content/Output'):
os.makedirs('/content/Output')
print('=' * 70)
print('Done!')
print('=' * 70)
print('PyTorch version:', torch.__version__)
print('=' * 70)
print('Enjoy! :)')
print('=' * 70)
"""# (LOAD PRE-TRAINED MODEL)"""
#@title Load POP Melody Transformer Pre-Trained Model
#@markdown Choose model
select_model_to_load = "93M-8L-Very-Fast-Small" # @param ["93M-8L-Very-Fast-Small", "160M-12L-Fast-Medium"]
#@markdown Model precision option
model_precision = "bfloat16" # @param ["bfloat16", "float16"]
#@markdown bfloat16 == Half precision/faster speed (if supported, otherwise the model will default to float16)
#@markdown float16 == Full precision/fast speed
print('=' * 70)
print('Loading POP Melody Transformer', select_model_to_load,'Pre-Trained Model...')
print('Please wait...')
print('=' * 70)
full_path_to_models_dir = "/content/POP-Melody-Transformer/Model"
if select_model_to_load == '160M-12L-Fast-Medium':
model_checkpoint_file_name = 'POP_Melody_Transformer_Medium_Trained_Model_7544_steps_0.223_loss_0.926_acc.pth'
model_path = full_path_to_models_dir+'/Medium/'+model_checkpoint_file_name
num_layers = 12
num_heads = 16
if os.path.isfile(model_path):
print('Model already exists...')
else:
hf_hub_download(repo_id='asigalov61/POP-Melody-Transformer',
filename=model_checkpoint_file_name,
local_dir='/content/POP-Melody-Transformer/Model/Medium',
local_dir_use_symlinks=False)
else:
model_checkpoint_file_name = 'POP_Melody_Transformer_Small_Trained_Model_5395_steps_0.3491_loss_0.8924_acc.pth'
model_path = full_path_to_models_dir+'/Small/'+model_checkpoint_file_name
num_layers = 8
num_heads = 8
if os.path.isfile(model_path):
print('Model already exists...')
else:
hf_hub_download(repo_id='asigalov61/POP-Melody-Transformer',
filename=model_checkpoint_file_name,
local_dir='/content/POP-Melody-Transformer/Model/Small',
local_dir_use_symlinks=False)
print('=' * 70)
print('Instantiating model...')
torch.backends.cuda.matmul.allow_tf32 = True # allow tf32 on matmul
torch.backends.cudnn.allow_tf32 = True # allow tf32 on cudnn
device_type = 'cuda'
if model_precision == 'bfloat16' and torch.cuda.is_bf16_supported():
dtype = 'bfloat16'
else:
dtype = 'float16'
if model_precision == 'float16':
dtype = 'float16'
ptdtype = {'float32': torch.float32, 'bfloat16': torch.bfloat16, 'float16': torch.float16}[dtype]
ctx = torch.amp.autocast(device_type=device_type, dtype=ptdtype)
SEQ_LEN = 8192
# instantiate the model
model = TransformerWrapper(
num_tokens = 643,
max_seq_len = SEQ_LEN,
attn_layers = Decoder(dim = 1024, depth = num_layers, heads = num_heads, attn_flash = True)
)
model = AutoregressiveWrapper(model, ignore_index=642)
model.cuda()
print('=' * 70)
print('Loading model checkpoint...')
model.load_state_dict(torch.load(model_path))
print('=' * 70)
model.eval()
print('Done!')
print('=' * 70)
print('Model will use', dtype, 'precision...')
print('=' * 70)
# Model stats
print('Model summary...')
summary(model)
"""# (DOWNLOAD SAMPLE MIDI DATASET)"""
# Commented out IPython magic to ensure Python compatibility.
# @title Download and unzip POP1k7 Piano MIDI dataset
#@markdown Source GitHub repo https://github.com/YatingMusic/compound-word-transformer
# %cd /content/Dataset
!gdown '1qw_tVUntblIg4lW16vbpjLXVndkVtgDe'
!unzip dataset.zip
!rm dataset.zip
# %cd /content/
"""# (LOAD MIDI PROCESSOR)"""
#@title TMIDIX MIDI Processor
print('=' * 70)
print('Loading TMIDIX MIDI Processor...')
print('=' * 70)
def group_single_elements(lst):
new_lst = []
temp = []
for sublist in lst:
if len(sublist) == 1:
temp.extend(sublist)
else:
if temp:
new_lst.append(temp)
temp = []
new_lst.append(sublist)
if temp:
new_lst.append(temp)
return new_lst
def TMIDIX_MIDI_Processor(midi_file):
melody_chords = []
try:
fn = os.path.basename(midi_file)
fn1 = fn.split('.mid')[0]
# Filtering out GIANT4 MIDIs
file_size = os.path.getsize(midi_file)
if file_size <= 1000000:
#=======================================================
# START PROCESSING
# Convering MIDI to ms score with MIDI.py module
score = TMIDIX.midi2single_track_ms_score(open(midi_file, 'rb').read(), recalculate_channels=False)
# INSTRUMENTS CONVERSION CYCLE
events_matrix = []
itrack = 1
patches = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
while itrack < len(score):
for event in score[itrack]:
if event[0] == 'note' or event[0] == 'patch_change':
events_matrix.append(event)
itrack += 1
events_matrix.sort(key=lambda x: x[1])
events_matrix1 = []
for event in events_matrix:
if event[0] == 'patch_change':
patches[event[2]] = event[3]
if event[0] == 'note':
event.extend([patches[event[3]]])
if events_matrix1:
if (event[1] == events_matrix1[-1][1]):
if ([event[3], event[4]] != events_matrix1[-1][3:5]):
events_matrix1.append(event)
else:
events_matrix1.append(event)
else:
events_matrix1.append(event)
if len(events_matrix1) > 0:
if min([e[1] for e in events_matrix1]) >= 0 and min([e[2] for e in events_matrix1]) >= 0:
#=======================================================
# PRE-PROCESSING
# checking number of instruments in a composition
instruments_list = list(set([y[3] for y in events_matrix1]))
if len(events_matrix1) > 0:
#===================================
# ORIGINAL COMPOSITION
#===================================
# Adjusting timings
for e in events_matrix1:
e[1] = int(e[1] / 16)
e[2] = int(e[2] / 16)
# Sorting by patch, pitch, then by start-time
events_matrix1.sort(key=lambda x: x[6])
events_matrix1.sort(key=lambda x: x[4], reverse=True)
events_matrix1.sort(key=lambda x: x[1])
#=======================================================
# FINAL PROCESSING
#=======================================================
# MAIN PROCESSING CYCLE
#=======================================================
pe = events_matrix1[0]
notes = []
for e in events_matrix1:
time = max(0, min(255, (e[1] - pe[1])))
dur = max(0, min(255, e[2]))
cha = max(0, min(15, e[3]))
ptc = max(1, min(127, e[4]))
vel = max(1, min(127, e[5]))
if cha != 9:
notes.append([time, dur, 0, ptc, vel])
pe = e
return [notes, fn1]
except:
return None
print('Done!')
print('=' * 70)
"""# (FILES LIST)"""
#@title Save file list
source_MIDI_dataset_folder_path = "/content/Dataset" # @param {type:"string"}
###########
print('=' * 70)
print('Loading MIDI files...')
print('This may take a while on a large dataset in particular.')
dataset_addr = source_MIDI_dataset_folder_path
# os.chdir(dataset_addr)
filez = list()
for (dirpath, dirnames, filenames) in os.walk(dataset_addr):
filez += [os.path.join(dirpath, file) for file in filenames]
print('=' * 70)
if not filez:
print('Could not find any MIDI files. Please check Dataset dir...')
print('=' * 70)
else:
print('Randomizing file list...')
random.shuffle(filez)
print('Done!')
print('=' * 70)
print('Total files:', len(filez))
print('=' * 70)
"""# (PROCESS)"""
#@title Process MIDIs with TMIDIX MIDI processor
print('=' * 70)
print('TMIDIX MIDI Processor')
print('=' * 70)
print('Starting up...')
print('=' * 70)
###########
melody_chords_f = []
print('Processing MIDI files. Please wait...')
print('=' * 70)
for i in tqdm.tqdm(range(0, len(filez), 16)):
with parallel_config(backend='threading', n_jobs=4, verbose = 0):
output = Parallel()(delayed(TMIDIX_MIDI_Processor)(f) for f in filez[i:i+16])
for o in output:
if o is not None:
melody_chords_f.append(o)
print('Done!')
print('=' * 70)
"""# (SAVE/LOAD PROCESSED MIDIs)"""
#@title Save processed MIDIs
TMIDIX.Tegridy_Any_Pickle_File_Writer(melody_chords_f, '/content/Processed_MIDIs')
# @title Load processed MIDIs
melody_chords_f = TMIDIX.Tegridy_Any_Pickle_File_Reader('/content/Processed_MIDIs')
print('Done!')
"""# (EXTRACT MELODY)"""
#@title Melody arrangement/extraction
#@markdown You can stop the extraction at any time to render partial results
#@markdown Output folder path
MIDIs_output_folder_path = "/content/Output" # @param {type:"string"}
#@markdown Melody settings
melody_MIDI_patch_number = 40 # @param {type:"slider", min:0, max:127, step:1}
melody_max_velocity = 120 # @param {type:"slider", min:1, max:127, step:1}
#@markdown Accompaniment settings
accompaniment_MIDI_patch_number = 0 # @param {type:"slider", min:0, max:127, step:1}
accompaniment_max_velocity = 85 # @param {type:"slider", min:1, max:127, step:1}
#@markdown Base line settings
add_base_line = True # @param {type:"boolean"}
base_line_threshold_pitch_number = 50 # @param {type:"slider", min:10, max:60, step:1}
base_line_MIDI_patch_number = 35 # @param {type:"slider", min:0, max:127, step:1}
base_line_max_velocity = 100 # @param {type:"slider", min:1, max:127, step:1}
#@markdown Generation settings
number_of_prime_notes = 1 # @param {type:"slider", min:1, max:256, step:1}
number_of_memory_tokens = 4096 # @param {type:"slider", min:3, max:8190, step:3}
number_of_samples_per_note = 1 #@param {type:"slider", min:1, max:16, step:1}
temperature = 1 # @param {type:"slider", min:0.1, max:1, step:0.05}
#@markdown Other settings
verbose = False # @param {type:"boolean"}
#===============================================
# Helper function
#===============================================
def adjust_velocities_in_place(chan, max_velocity):
min_velocity = min([c[5] for c in chan])
max_velocity_all_channels = max([c[5] for c in chan])
min_velocity_ratio = min_velocity / max_velocity_all_channels
max_channel_velocity = max([c[5] for c in chan])
if max_channel_velocity < min_velocity:
factor = max_velocity / min_velocity
else:
factor = max_velocity / max_channel_velocity
for i in range(len(chan)):
chan[i][5] = int(chan[i][5] * factor)
#===============================================
if not os.path.exists(MIDIs_output_folder_path):
os.makedirs(MIDIs_output_folder_path)
print('=' * 70)
print('POP Melody Transformer Model Generator')
for j in range(len(melody_chords_f)):
try:
melody_chords = melody_chords_f[j][0]
fname = melody_chords_f[j][1]
print('=' * 70)
print('Processing MIDI file', j+1, 'out of', len(melody_chords_f))
print('MIDI file name:', fname+'.mid')
print('=' * 70)
out2 = []
out3 = []
abs_time = 0
for m in melody_chords[:number_of_prime_notes]:
out2.extend([m[0], m[1]+256, m[3]+512, 640+0])
out3.extend([m[0], m[1]+256, m[3]+512, 640+0, m[4]+642])
abs_time += m[0]
torch.cuda.empty_cache()
pt = abs_time
pd = m[1]
for i in tqdm.tqdm(range(number_of_prime_notes, len(melody_chords))):
m = melody_chords[i]
out2.extend([m[0], m[1]+256, m[3]+512])
out3.extend([m[0], m[1]+256, m[3]+512])
abs_time += m[0]
if abs_time >= pt+pd:
samples = []
for _ in range(number_of_samples_per_note):
inp = torch.LongTensor(out2[-number_of_memory_tokens:]).cuda()
with ctx:
out1 = model.generate(inp,
1,
temperature=temperature,
return_prime=True,
verbose=False)
with torch.no_grad():
test_loss, test_acc = model(out1)
samples.append([[out1.tolist()[0][-1]], test_acc.tolist()])
accs = [y[1] for y in samples]
max_acc = max(accs)
max_acc_sample = samples[accs.index(max_acc)][0]
out2.extend(max_acc_sample)
out3.extend(max_acc_sample + [m[4]+642])
if max_acc_sample == [641]:
pt = abs_time
pd = m[1]
else:
out2.extend([640])
out3.extend([640, m[4]+642])
if verbose:
print('Done!')
print('=' * 70)
torch.cuda.empty_cache()
#==================================================
train_data1 = out3 # y[0]
#train_data1 = max(melody_chords_f, key = len)
if verbose:
print('Sample INTs', train_data1[:15])
out = train_data1
patches = [0] * 16
patches[0] = accompaniment_MIDI_patch_number
patches[2] = base_line_MIDI_patch_number
patches[3] = melody_MIDI_patch_number
#==================================================
if len(out) != 0:
song = out
song_f = []
time = 0
dur = 0
vel = 90
pitch = 0
channel = 0
for ss in song:
if 0 <= ss < 256:
time += (ss * 16)
if 256 <= ss < 512:
dur = (ss-256) * 16
if 512 <= ss < 640:
pitch = ss-512
if 640 <= ss < 642:
channel = ss-640
if channel == 1:
channel = 3
if 642 <= ss < 770:
vel = ss-642
song_f.append(['note', time, dur, channel, pitch, vel ])
#==================================================
song_f_chords = []
cho = []
pe = song_f[0]
for s in song_f:
if s[1]-pe[1] == 0:
cho.append(s)
else:
if len(cho) > 0:
song_f_chords.append(cho)
cho = []
cho.append(s)
pe = s
if len(cho) > 0:
song_f_chords.append(cho)
song_f_base = []
for s in song_f_chords:
if s[-1][4] <= base_line_threshold_pitch_number:
s[-1][3] = 2
for ss in s:
song_f_base.append(ss)
#==================================================
# Base line stuff
if add_base_line:
song_final = song_f_base
else:
song_final = song_f
#==================================================
# Velocities stuff
melody_notes = [s for s in song_final if s[3] == 3]
accompaniment_notes = [s for s in song_final if s[3] == 0]
base_notes = [s for s in song_final if s[3] == 2]
adjust_velocities_in_place(melody_notes, melody_max_velocity)
adjust_velocities_in_place(accompaniment_notes, accompaniment_max_velocity)
adjust_velocities_in_place(base_notes, base_line_max_velocity)
#==================================================
detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_final,
output_signature = 'POP Melody Transformer',
output_file_name = MIDIs_output_folder_path+'/'+fname,
track_name='Project Los Angeles',
list_of_MIDI_patches=patches,
verbose=verbose)
except KeyboardInterrupt:
print('Stopping extraction...')
break
except Exception as e:
print('Error', e)
continue
print('=' * 70)
print('Done!')
print('=' * 70)
"""# Congrats! You did it! :)"""