Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chopin and Ragtime model training and output with loss of 0.0049 #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added data/notes_chopin_ragtime
Binary file not shown.
Binary file added generated/nocturne_entertainer.midi
Binary file not shown.
Binary file added generated/nocturne_entertainer.mp3
Binary file not shown.
28 changes: 20 additions & 8 deletions lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
#from keras.layers import CuDNNLSTM
from keras.layers import LSTM
from keras.layers import BatchNormalization
from keras.layers import Activation
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint

def train_network():
""" Train a Neural Network to generate music """
notes = get_notes()
#print("Loading notes")
#with open('data/notes', 'rb') as filepath:
# notes = pickle.load(filepath)

# get amount of pitch names
n_vocab = len(set(notes))
print("Size of vocab: %s" % n_vocab)

print("Preparing sequences")
network_input, network_output = prepare_sequences(notes, n_vocab)

print("Creating metwork")
model = create_network(network_input, n_vocab)

train(model, network_input, network_output)
Expand All @@ -29,10 +37,12 @@ def get_notes():
""" Get all the notes and chords from the midi files in the ./midi_songs directory """
notes = []

count = 0
for file in glob.glob("midi_songs/*.mid"):
midi = converter.parse(file)

print("Parsing %s" % file)
count += 1
print("[%3d] Parsing %s" % (count, file))

notes_to_parse = None

Expand Down Expand Up @@ -86,21 +96,23 @@ def prepare_sequences(notes, n_vocab):

def create_network(network_input, n_vocab):
""" create the structure of the neural network """
size = 3
model = Sequential()
model.add(LSTM(
512,
512 * size,
input_shape=(network_input.shape[1], network_input.shape[2]),
return_sequences=True
))
model.add(Dropout(0.3))
model.add(LSTM(512, return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(512))
model.add(Dense(256))
model.add(LSTM(512 * size, return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(512 * size))
#model.add(Dense(256))
#model.add(Dropout(0.3))
model.add(BatchNormalization())
model.add(Dense(n_vocab))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
model.compile(loss='categorical_crossentropy', optimizer='adagrad')

return model

Expand All @@ -116,7 +128,7 @@ def train(model, network_input, network_output):
)
callbacks_list = [checkpoint]

model.fit(network_input, network_output, epochs=200, batch_size=64, callbacks=callbacks_list)
model.fit(network_input, network_output, epochs=1000, batch_size=64, callbacks=callbacks_list)

if __name__ == '__main__':
train_network()
22 changes: 13 additions & 9 deletions predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
trained neural network """
import pickle
import numpy
import time

from music21 import instrument, note, stream, chord
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.layers import BatchNormalization
from keras.layers import Activation

def generate():
Expand Down Expand Up @@ -50,22 +53,23 @@ def prepare_sequences(notes, pitchnames, n_vocab):

def create_network(network_input, n_vocab):
""" create the structure of the neural network """
size = 3
model = Sequential()
model.add(LSTM(
512,
512 * size,
input_shape=(network_input.shape[1], network_input.shape[2]),
return_sequences=True
))
model.add(Dropout(0.3))
model.add(LSTM(512, return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(512))
model.add(Dense(256))
model.add(LSTM(512 * size, return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(512 * size))
#model.add(Dense(256))
#model.add(Dropout(0.3))
model.add(BatchNormalization())
model.add(Dense(n_vocab))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

model.compile(loss='categorical_crossentropy', optimizer='adagrad')
# Load the weights to each node
model.load_weights('weights.hdf5')

Expand Down Expand Up @@ -124,11 +128,11 @@ def create_midi(prediction_output):
output_notes.append(new_note)

# increase offset each iteration so that notes do not stack
offset += 0.5
offset += 0.37

midi_stream = stream.Stream(output_notes)

midi_stream.write('midi', fp='test_output.mid')
midi_stream.write('midi', fp='%s.mid' % str(time.time()))

if __name__ == '__main__':
generate()