-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_midi.py
376 lines (327 loc) · 13.1 KB
/
generate_midi.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
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 16 21:25:48 2020
@author: incog
"""
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential,Model,load_model
from tensorflow.keras.layers import Input, Dense, Dropout, LSTM, Activation, Bidirectional, Flatten, AdditiveAttention
from tensorflow.keras import utils
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from tensorflow.keras.utils import Sequence
from music21 import *
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
import datetime
import pytz
from IPython.display import clear_output, Audio
from collections import Counter
import glob
import pickle
import sys
from util import midi_to_onehot_dict, midi_to_onehot,load_doc,add_piece_start_stop
from extract_notes import note_length_event
def sample(preds, temperature=1.0):
#print('sampling one')
# helper function to sample an index from a probability array (from Keras library)
preds = np.asarray(preds).astype('float64')
#print (preds)
preds = np.log(preds + 1e-8) / temperature # Taking the log should be optional? add fudge factor to avoid log(0)
exp_preds = np.exp(preds)
preds = exp_preds / np.sum(exp_preds)
#print (preds)
probas = np.random.multinomial(1, preds, 1)
#print(np.argmax(probas))
return np.argmax(probas)
def generate_notes_hot(model,network_input,smpl=True,temperature=1.0,output_length=200):
""" Generate notes from the neural network based on a sequence of notes """
# pick a random sequence from the input as a starting point for the prediction
#start = np.random.randint(0, len(network_input)-1)
network_input=np.array(network_input)
pattern = network_input
prediction_output = []
# generate 500 notes
for note_index in range(output_length):
prediction_input = np.reshape(pattern, (1, pattern.shape[0]))
#prediction_input = prediction_input / float(n_vocab)
prediction = model(prediction_input, training=False)[0][-1]
if smpl:
index=sample(prediction,temperature=temperature)
else:
index = np.argmax(prediction)
#result = int_to_note[index]
prediction_output.append(index)
pattern=np.append(pattern,index)
#pattern = pattern[1:len(pattern)]
return prediction_output
def create_midi(encoding,resolution,prediction_output,save_path='',name='test_output',return_stream=False):
if encoding==1:
s=create_midi1(prediction_output,save_path=save_path,name=name,resolution=resolution,return_stream=return_stream)
if encoding==2:
s=create_midi2(prediction_output,save_path=save_path,name=name,resolution=resolution,return_stream=return_stream)
if encoding==4:
s=create_midi4(prediction_output,save_path=save_path,name=name,resolution=resolution,return_stream=return_stream)
if return_stream:
return s
def create_midi1(prediction_output,resolution=8,save_path='',name='test_output',return_stream=False):
""" convert the output from the prediction to notes and create a midi file
from the notes """
if save_path=='':
save_path=os.getcwd()
offset = 0
output_notes = []
timestep=1/resolution #timestep quarterLength
# create note and rest objects based on the values generated by the model
for pattern in prediction_output:
if pattern==128:
new_rest=note.Rest()
new_rest.offset=offset
new_rest.duration.quarterLength=timestep
offset+=timestep
output_notes.append(new_rest)
elif pattern==129:
if len(output_notes)!=0:
output_notes[-1].duration.quarterLength+=timestep
offset+=timestep
else:
# pattern is a note
new_note = note.Note(pattern)
new_note.offset = offset
new_note.duration.quarterLength= timestep
new_note.storedInstrument = instrument.Piano()
output_notes.append(new_note)
# increase offset each iteration so that notes do not stack
offset += timestep
midi_stream = stream.Stream(output_notes)
if return_stream:
return midi_stream
else:
midi_stream.write('midi', fp=save_path+'/'+name+'.mid')
def create_midi2(prediction_output,save_path='',resolution=8,name='test_output',return_stream=False):
""" convert the output from the prediction to notes and create a midi file
from the notes """
if save_path=='':
save_path=os.getcwd()
offset = 0
output_notes = []
timestep=1/resolution #one-sixteenth
# create note and rest objects based on the values generated by the model
old_pattern=132
for pattern in prediction_output:
if pattern==130 or pattern==131:
continue
if pattern==128:
new_rest=note.Rest()
new_rest.offset=offset
new_rest.duration.quarterLength=timestep
offset+=timestep
output_notes.append(new_rest)
elif pattern==129:
if (old_pattern==128 or old_pattern==129):
new_rest=note.Rest()
new_rest.offset=offset
new_rest.duration.quarterLength=timestep
output_notes.append(new_rest)
else:
if len(output_notes)>0:
old_note=output_notes[-1]
old_note.duration.quarterLength+=timestep
offset+=timestep
else:
# pattern is a note
if (old_pattern==pattern):
if len(output_notes)>0:
old_note=output_notes[-1]
old_note.duration.quarterLength+=timestep
else:
new_note = note.Note(pattern)
new_note.offset = offset
new_note.duration.quarterLength= timestep
new_note.storedInstrument = instrument.Piano()
output_notes.append(new_note)
# increase offset each iteration so that notes do not stack
offset += timestep
old_pattern=pattern
midi_stream = stream.Stream(output_notes)
if return_stream:
return midi_stream
else:
midi_stream.write('midi', fp=save_path+'/'+name+'.mid')
def create_midi4(prediction_output,resolution=8,save_path='',name='test_output',return_stream=False):
""" convert the output from the prediction to notes and create a midi file
from the notes """
if save_path=='':
save_path=os.getcwd()
offset = 0
dur=0
old={}
output_notes = []
timestep=1/4 #one-sixteenth
# create note and rest objects based on the values generated by the model
for pattern in prediction_output:
if pattern<=127:
new_note = note.Note(pattern)
new_note.offset = offset
new_note.duration.quarterLength=dur
new_note.storedInstrument = instrument.Piano()
output_notes.append(new_note)
dur=0
elif pattern>=256:
if len(output_notes)>0:
dur+=(pattern-255)/resolution
offset+=(pattern-255)/resolution
else:
if len(output_notes)>0:
if output_notes[-1].pitch.midi==pattern-128:
output_notes[-1].duration.quarterLength=dur
dur=0
midi_stream = stream.Stream(output_notes)
if return_stream:
return midi_stream
else:
midi_stream.write('midi', fp=save_path+'/'+name+'.mid')
import sim_functions
def generate(encoding,resolution,seed,seed_name,model_path='',generate_pieces=True, keep_seed=False,temperature=1,output_length=200):
""" Generate a piano midi file """
experiment_path=os.path.dirname(os.path.dirname(model_path))
output_path=experiment_path+'/output'
if generate_pieces:
output_path+='/pieces'
filename=os.path.basename(model_path)
filename=filename[6:23]+f'-temp_{temperature}_sl_{len(seed)}'
dictionary=np.load(experiment_path+'/dictionary',allow_pickle=True)
seed_dict=[]
for i in seed:
seed_dict.append(dictionary[i])
network_input=np.array(seed_dict)
#load the model
model=load_model(model_path)
model.layers[0].trainable=False
#model.summary()
#prediction_output = generate_notes_hot(model, seed_dict,temperature=temperature,output_length=output_length)
rev_dict={value:key for (key,value) in dictionary.items()}
#make the predictions
prediction_output = []
if generate_pieces:
stop=False
count=0
while stop==False and count<output_length:
prediction_input = np.reshape(network_input, (1, network_input.shape[0]))
prediction = model(prediction_input, training=False)[0][-1]
index=sample(prediction,temperature=temperature)
if rev_dict[index]==501:
stop=True
prediction_output.append(index)
network_input=np.append(network_input,index)
count+=1
else:
for note_index in range(output_length):
prediction_input = np.reshape(network_input, (1, network_input.shape[0]))
prediction = model(prediction_input, training=False)[0][-1]
index=sample(prediction,temperature=temperature)
prediction_output.append(index)
network_input=np.append(network_input,index)
#convert the dictionary outputs back to the original
dict_output=[]
for i in prediction_output:
dict_output.append(rev_dict[i])
if keep_seed:
dict_output=np.append(seed,dict_output)
filename+='+seed'
#return prediction_output
os.makedirs(output_path,exist_ok=True)
create_midi(encoding,resolution,dict_output,save_path=output_path+'/'+seed_name,name=filename)
return (dict_output)
def seed_to_midi(encoding,resolution,seed,seed_name,model_path='',generate_pieces=False):
""" Generate a piano midi file """
#loader= Data_Gen_Midi(batch_folder='npz/validate')
#network_input = loader.__getitem__(0)[0][0]
#seed=notes[0][0:64]
experiment_path=os.path.dirname(os.path.dirname(model_path))
output_path=experiment_path+'/output'
if generate_pieces:
output_path+='/pieces'
#experiment_path=os.path.dirname(os.path.dirname(model_path))
output_path+='/'+seed_name
filename='seed_'+seed_name
#resolution=int(experiment_path[-1])
os.makedirs(f'{output_path}',exist_ok=True)
create_midi(encoding,resolution,seed,save_path=output_path,name=filename)
def get_stats_dataset(notes_path,encoding,resolution,no_pieces,piece_length,output_path='stats'):
notes=pd.read_pickle(notes_path)
nnotes=[]
for n in notes:
if len(n)>=piece_length:
nnotes.append(n)
piece_ind=np.random.choice(len(nnotes), size=no_pieces, replace=False)
for i in range(no_pieces):
if len(nnotes[piece_ind[i]])<piece_length:
print('fuuuuuuu')
extra=np.random.randint(0,len(notes),1)
piece_ind=np.append(piece_ind,0)
else:
piece_name='piece'+str(piece_ind[i])
piece=nnotes[piece_ind[i]][0:piece_length]
create_midi(encoding,resolution,piece,save_path=output_path,name=piece_name)
#%%
'''
if __name__=='__main__':
notes_path='notes16/notes_event1_res8'
encoding=4
resolution=8
no_pieces=100
piece_length=200
output_path='stats'
get_stats_dataset(notes_path,encoding,resolution,no_pieces,piece_length,output_path='stats')
'''
if __name__=='__main__':
model_path='models/notes_event1_res8_c44_model_n1_s32_d0.2_sl100_bs256run_0/models/model-028-0.7721-0.7641'
notes_path='notes/notes_event1_res8_c44'
encoding=4
#resolution=int(notes_path[-1])
resolution=8
notes=pd.read_pickle(notes_path)
#notes=notes[0:int(len(notes)/4)]
notes=add_piece_start_stop(notes)
#notes=list(notes)
#notes=notes[0:12117]
#notes.sort(key=lambda x: len(x), reverse=True)
val_split=0.1
notes_validate=notes[len(notes)-int(val_split*len(notes)):len(notes)]
temperatures=[0.1,0.3,0.5]
#seed_ind=[10,11,13]
#seed_ind=[4,5,6]
seed_ind=range(200)
seq_length=32
for i,seed in enumerate ([notes_validate[i][0:seq_length] for i in seed_ind]):
if len(seed)<seq_length:
seed_ind.append(seed_ind[-1]+1)
continue
#seed=seed_list[i]
seed_name='seed'+str(seed_ind[i])
print('Generating with seed: ',seed)
seed_to_midi(encoding,resolution,seed,seed_name,model_path=model_path,generate_pieces=True)
for t in temperatures:
if t==0.01:
generate(encoding,resolution,seed,seed_name,model_path,keep_seed=True, temperature=t,generate_pieces=True)
continue
generate(encoding,resolution,seed,seed_name,model_path,keep_seed=True, temperature=t,generate_pieces=True)
'''
if __name__=='__main__':
model_path='model-016-0.8498-0.8190'
notes_path='notes/notes_tstep1_res8'
encoding=1
#resolution=int(notes_path[-1])
resolution=8
seq_length=64
notes=pd.read_pickle(notes_path)
notes=notes[0:int(len(notes)/4)]
notes=add_piece_start_stop(notes)
seed=notes[0][0:32]
seed_name='seed0'
generate(encoding,resolution,seed,seed_name,model_path,keep_seed=True, temperature=1.,dict=True)
'''