-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeras_predict.py
106 lines (86 loc) · 3.05 KB
/
keras_predict.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
import numpy as np
from music21 import *
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.layers import Activation
import utildata as ud
num_timesteps = 16
output_size = 79 #FIX THIS HACK
def generate():
model = create_network(output_size)
prediction_output = generate_notes(model)
print(prediction_output)
create_midi(prediction_output)
def create_network(output_size):
model = Sequential()
model.add(LSTM(
512,
input_shape=(network_input.shape[1],network_input.shape[2]),
return_sequences=True
))
model.add(Dropout(0.5))
model.add(LSTM(1024,return_sequences=True))
model.add(Dropout(0.5))
model.add(LSTM(1024,return_sequences=True))
model.add(Dropout(0.5))
model.add(LSTM(512))
model.add(Dense(512))
model.add(Dropout(0.5))
model.add(Dense(output_size))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',optimizer='rmsprop')
#Load the weights
model.load_weights('TrainingData\KERAS-LSTM\weights-199-0.9446.hdf5')
return model
'''
Generate melody from scratch (sort of)
Initial input is num_timesteps of random notes
'''
def generate_notes(model):
#Generate Initial (random) input
pattern = []
for i in range(num_timesteps):
pattern.append(np.random.randint(0,output_size))
prediction_output = []
for i in range(500):
prediction_input = np.reshape(pattern,(1,len(pattern),1))
prediction_input = prediction_input/float(output_size)
prediction = model.predict(prediction_input,verbose=0)
index = np.argmax(prediction)
prediction_output.append(index)
pattern.append(index)
pattern = pattern[1:len(pattern)]
return prediction_output
'''
Create a midi file from a list of integers
integers represent a certain pitch
'''
def create_midi(prediction_output):
pitch_dict = ud.loadobj('./Files/BachPitch')
duration_dict = ud.loadobj('./Files/BachDuration')
encoding_dict = ud.loadobj('./Files/BachEncoding')
inv_encoding = {val: key for key,val in encoding_dict}
inv_pitch = {pitch: pnum for pnum,pitch in pitch_dict}
inv_duration = {duration: dnum for dnum,duration in duration_dict}
import datetime
fmt = '%Y%m%d%H%M%S'
now_str = datetime.datetime.now().strftime(fmt)
dirstr ="./GeneratedMusic/KERAS_LSTM_SONG"+now_str+".midi"
song = stream.Stream()
for element in prediction_output:
pd = inv_encoding[element].split(';')
p = inv_pitch[int(pd[0])]
d = inv_duration[int(pd[1])]
a_note = note.Note(p)
try:
a_note.duration.quarterLength = float(d)
except:
tmp = d.split('/')
a_note.duration.quarterLength = float(float(tmp[0])/float(tmp[1]))
pass
song.append(a_note)
song.write('midi',fp=dirstr)
if __name__ == '__main__':
generate()