-
Notifications
You must be signed in to change notification settings - Fork 6
/
mini_muse.py
465 lines (311 loc) · 11.7 KB
/
mini_muse.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
# -*- coding: utf-8 -*-
"""Mini_Muse.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Tyq-n-kH0iKl9Zp1Kbtp6FQKJUw9DOnW
# Mini Muse (ver. 1.0)
***
Powered by tegridy-tools: https://github.com/asigalov61/tegridy-tools
***
Credit for GPT2-RGA code used in this colab goes out @ Sashmark97 https://github.com/Sashmark97/midigen and @ Damon Gwinn https://github.com/gwinndr/MusicTransformer-Pytorch
***
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 2022
***
# (Setup Environment)
"""
#@title nvidia-smi gpu check
!nvidia-smi
#@title Install all dependencies (run only once per session)
!git clone https://github.com/asigalov61/Mini-Muse
!pip install torch
!pip install tqdm
!pip install matplotlib
!pip install torch-summary
!apt install fluidsynth #Pip does not work for some reason. Only apt works
!pip install midi2audio
!pip install pretty_midi
#@title Import all needed modules
print('Loading needed modules. Please wait...')
import os
import random
import copy
from collections import OrderedDict
from tqdm import tqdm
import matplotlib.pyplot as plt
from torchsummary import summary
print('Loading TMIDIX module...')
os.chdir('/content/Mini-Muse')
import TMIDIX
from GPT2RGAX import *
from midi2audio import FluidSynth
import pretty_midi
import librosa.display
from IPython.display import Audio
os.chdir('/content/')
"""# (UNZIP)"""
# Commented out IPython magic to ensure Python compatibility.
#@title Unzip pre-trained Mini Muse Model
# %cd /content/Mini-Muse/Model
print('=' * 70)
print('Unzipping pre-trained model...Please wait...')
print('=' * 70)
!cat /content/Mini-Muse/Model/Mini-Muse-Trained-Model.zip* > Mini-Muse-Trained-Model.zip
print('=' * 70)
!unzip -j Mini-Muse-Trained-Model.zip
print('=' * 70)
print('Done! Enjoy! :)')
print('=' * 70)
# %cd /content/
# Commented out IPython magic to ensure Python compatibility.
#@title Unzip Mini Muse Training Data
# %cd /content/Mini-Muse/Training-Data
print('=' * 70)
print('Unzipping pre-trained model...Please wait...')
print('=' * 70)
!cat /content/Mini-Muse/Training-Data/Mini-Muse-Training-Data.zip* > Mini-Muse-Training-Data.zip
print('=' * 70)
!unzip -j Mini-Muse-Training-Data.zip
print('=' * 70)
print('Done! Enjoy! :)')
print('=' * 70)
# %cd /content/
"""# (LOAD)"""
#@title Load/Reload the model
full_path_to_model_checkpoint = "/content/Mini-Muse/Model/Mini_Muse_Trained_Model_88000_steps_0.6129_loss.pth" #@param {type:"string"}
print('Loading the model...')
config = GPTConfig(512,
1024,
dim_feedforward=1024,
n_layer=16,
n_head=16,
n_embd=1024,
enable_rpr=True,
er_len=1024)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = GPT(config)
state_dict = torch.load(full_path_to_model_checkpoint, map_location=device)
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] #remove 'module'
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
model.to(device)
model.eval()
print('Done!')
summary(model)
#@title Load/Reload training data
print('Loading training data...')
train_data = TMIDIX.Tegridy_Any_Pickle_File_Reader('/content/Mini-Muse/Training-Data/Mini-Muse-Training-Data')
print('Done!')
"""# (GENERATE)"""
#@title Generate Prime
number_of_prime_tokens = 256 #@param {type:"slider", min:16, max:512, step:16}
#@markdown Select desired combination of instruments (less is better)
Piano = True #@param {type:"boolean"}
Guitar = False #@param {type:"boolean"}
Bass = False #@param {type:"boolean"}
Violin = False #@param {type:"boolean"}
Cello = False #@param {type:"boolean"}
Harp = False #@param {type:"boolean"}
Trumpet = False #@param {type:"boolean"}
Clarinet = False #@param {type:"boolean"}
Flute = False #@param {type:"boolean"}
Drums = False #@param {type:"boolean"}
Choir = False #@param {type:"boolean"}
instruments = []
if Piano:
instruments += [0]
if Guitar:
instruments += [1]
if Bass:
instruments += [2]
if Violin:
instruments += [3]
if Cello:
instruments += [4]
if Harp:
instruments += [5]
if Trumpet:
instruments += [6]
if Clarinet:
instruments += [7]
if Flute:
instruments += [8]
if Drums:
instruments += [9]
if Choir:
instruments += [10]
iidx = []
print('Looking for matching primes...')
for i in tqdm(range(0, len(train_data), 1025)):
instr = sorted(list(set([y // 10 for y in train_data[i+1:i+1025:4][:number_of_prime_tokens]])))
if instr == sorted(instruments):
iidx.append(i)
print('Found', len(iidx), 'matching primes...')
iindex = random.choice(iidx)
print('Selected prime #', iindex // 1025)
print('Prime index:', iindex)
out1 = train_data[iindex:iindex+number_of_prime_tokens+1]
if len(out1) != 0:
song = out1
song_f = []
time = 0
dur = 0
vel = 0
pitch = 0
channel = 0
son = []
song1 = []
for s in song:
if s > 127:
son.append(s)
else:
if len(son) == 4:
song1.append(son)
son = []
son.append(s)
for s in song1:
channel = s[0] // 10
vel = (s[0] % 10) * 16
time += (s[1]-128) * 16
dur = (s[2] - 256) * 32
pitch = (s[3] - 384)
song_f.append(['note', time, dur, channel, pitch, vel ])
detailed_stats = TMIDIX.Tegridy_SONG_to_MIDI_Converter(song_f,
output_signature = 'Mini Muse',
output_file_name = '/content/Mini-Muse-Music-Composition',
track_name='Project Los Angeles',
list_of_MIDI_patches=[0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 0, 0, 0, 0, 0],
number_of_ticks_per_quarter=500)
print('Done!')
print('Displaying resulting composition...')
fname = '/content/Mini-Muse-Music-Composition'
pm = pretty_midi.PrettyMIDI(fname + '.mid')
# Retrieve piano roll of the MIDI file
piano_roll = pm.get_piano_roll()
plt.figure(figsize=(14, 5))
librosa.display.specshow(piano_roll, x_axis='time', y_axis='cqt_note', fmin=1, hop_length=160, sr=16000, cmap=plt.cm.hot)
plt.title(fname)
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))
Audio(str(fname + '.wav'), rate=16000)
#@title Single Continuation Block Generator
#@markdown NOTE: Play with the settings to get different results
temperature = 0.8 #@param {type:"slider", min:0.1, max:1, step:0.1}
show_stats = True #@param {type:"boolean"}
#===================================================================
print('=' * 70)
print('Mini Muse Music Model Continuation Generator')
print('=' * 70)
print('Generation settings:')
print('=' * 70)
print('Number of prime tokens:', number_of_prime_tokens)
print('Model temperature:', temperature)
print('=' * 70)
print('Generating...')
inputs = train_data[iindex:iindex+number_of_prime_tokens+1]
rand_seq = model.generate(torch.Tensor(inputs),
target_seq_length=1024,
temperature=temperature,
stop_token=512,
verbose=show_stats)
out1 = rand_seq[0].cpu().numpy().tolist()
if len(out1) != 0:
song = out1
song_f = []
time = 0
dur = 0
vel = 0
pitch = 0
channel = 0
son = []
song1 = []
for s in song:
if s > 127:
son.append(s)
else:
if len(son) == 4:
song1.append(son)
son = []
son.append(s)
for s in song1:
channel = s[0] // 10
vel = (s[0] % 10) * 16
time += (s[1]-128) * 16
dur = (s[2] - 256) * 32
pitch = (s[3] - 384)
song_f.append(['note', time, dur, channel, pitch, vel ])
detailed_stats = TMIDIX.Tegridy_SONG_to_MIDI_Converter(song_f,
output_signature = 'Mini Muse',
output_file_name = '/content/Mini-Muse-Music-Composition',
track_name='Project Los Angeles',
list_of_MIDI_patches=[0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 0, 0, 0, 0, 0],
number_of_ticks_per_quarter=500)
print('Done!')
print('Displaying resulting composition...')
fname = '/content/Mini-Muse-Music-Composition'
pm = pretty_midi.PrettyMIDI(fname + '.mid')
# Retrieve piano roll of the MIDI file
piano_roll = pm.get_piano_roll()
plt.figure(figsize=(14, 5))
librosa.display.specshow(piano_roll, x_axis='time', y_axis='cqt_note', fmin=1, hop_length=160, sr=16000, cmap=plt.cm.hot)
plt.title(fname)
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))
Audio(str(fname + '.wav'), rate=16000)
#@title Auto-continue resulting composition
#@markdown NOTE: This may or may not work well due to long-term structure decay
number_of_continuation_blocks = 1 #@param {type:"slider", min:1, max:5, step:1}
out2 = copy.deepcopy(out1)
for i in range(number_of_continuation_blocks):
rand_seq = model.generate(torch.Tensor(out2[-64:]),
target_seq_length=1024,
temperature=temperature,
stop_token=512,
verbose=show_stats)
out = rand_seq[0].cpu().numpy().tolist()
out2.extend(out[64:])
if len(out2) != 0:
song = out2
song_f = []
time = 0
dur = 0
vel = 0
pitch = 0
channel = 0
son = []
song1 = []
for s in song:
if s > 127:
son.append(s)
else:
if len(son) == 4:
song1.append(son)
son = []
son.append(s)
for s in song1:
channel = s[0] // 10
vel = (s[0] % 10) * 16
time += (s[1]-128) * 16
dur = (s[2] - 256) * 32
pitch = (s[3] - 384)
song_f.append(['note', time, dur, channel, pitch, vel ])
detailed_stats = TMIDIX.Tegridy_SONG_to_MIDI_Converter(song_f,
output_signature = 'Mini Muse',
output_file_name = '/content/Mini-Muse-Music-Composition',
track_name='Project Los Angeles',
list_of_MIDI_patches=[0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 0, 0, 0, 0, 0],
number_of_ticks_per_quarter=500)
print('Done!')
print('Displaying resulting composition...')
fname = '/content/Mini-Muse-Music-Composition'
pm = pretty_midi.PrettyMIDI(fname + '.mid')
# Retrieve piano roll of the MIDI file
piano_roll = pm.get_piano_roll()
plt.figure(figsize=(14, 5))
librosa.display.specshow(piano_roll, x_axis='time', y_axis='cqt_note', fmin=1, hop_length=160, sr=16000, cmap=plt.cm.hot)
plt.title(fname)
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))
Audio(str(fname + '.wav'), rate=16000)
"""# Congrats! You did it! :)"""