forked from brunnergino/JamBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chord_lstm_training.py
executable file
·166 lines (147 loc) · 6.07 KB
/
chord_lstm_training.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
# Author: Jonas Wiesendanger, Andres Konrad, Gino Brunner ([email protected])
from settings import *
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense, Activation
from keras.layers import Embedding
from keras.optimizers import RMSprop, Adam
import keras.utils
from keras.utils import np_utils
from keras.layers.wrappers import Bidirectional
from random import shuffle
import progressbar
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import _pickle as pickle
import os
import data_class
import time
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
# Uncomment next block if you only want to use a fraction of the GPU memory:
# config = tf.ConfigProto()
# config.gpu_options.visible_device_list = "2"
# config.gpu_options.per_process_gpu_memory_fraction = 0.9
# set_session(tf.Session(config=config))
#Path where the models are saved:
model_path = 'models/chords/'
model_filetype = '.pickle'
epochs = 25 #20
train_set_prop = 1
test_set_prop = 0
test_step = 300 # Calculate error for test set every this many songs
verbose = False
show_plot = False
save_plot = True
lstm_size = 512
batch_size = 1
learning_rate = 0.00005
step_size = 1
save_step = 2
shuffle_train_set = True
bidirectional = False
optimizer = 'Adam'
print('loading data...')
train_set, test_set = data_class.get_chord_train_and_test_set(train_set_prop, test_set_prop)
train_set_size = len(train_set)
print('Training set size: ',train_set_size)
test_set_size = len(test_set)
print('Test set size: ',test_set_size)
fd = {'shifted': shifted, 'lr': learning_rate, 'emdim': chord_embedding_dim, 'opt': optimizer,
'bi': bidirectional, 'lstms': lstm_size, 'trainsize': train_set_size, 'testsize': test_set_size, 'samples_per_bar': samples_per_bar}
t = str(int(round(time.time())))
#model_name = t+ '-Shifted_%(shifted)s_Lr_%(lr)s_EmDim_%(emdim)s_opt_%(opt)s_bi_%(bi)s_lstmsize_%(lstms)s_trainsize_%(trainsize)s_testsize_%(testsize)s_samples_per_bar%(samples_per_bar)s' % fd
model_name = 'overfitted_chords'
model_path = model_path + model_name + '/'
if not os.path.exists(model_path):
os.makedirs(model_path)
print('creating model...')
model = Sequential()
model.add(Embedding(num_chords, chord_embedding_dim, input_length=step_size, name="embedding", batch_input_shape=(batch_size,step_size)))
# model.add(Embedding(num_chords, chord_embedding_dim, input_length=step_size))
# if bidirectional: model.add(Bidirectional(LSTM(lstm_size, stateful=True)))
# else: model.add(LSTM(lstm_size, stateful=True))
model.add(LSTM(lstm_size, stateful=True))
model.add(Dense(num_chords))
model.add(Activation('softmax'))
if optimizer == 'Adam':
optimizer = Adam(lr=learning_rate)
elif optimizer == 'RMS':
optimizer = RMSprop(lr=learning_rate)
loss = 'categorical_crossentropy'
print("compiling model")
model.compile(optimizer, loss)
total_test_loss_array = []
total_train_loss_array = []
total_test_loss = 0
def test():
print('\nTesting:')
total_test_loss = 0
bar = progressbar.ProgressBar(maxval=len(test_set), redirect_stdout=False)
for i, test_song in enumerate(test_set):
X_test = test_song[:-1]
Y_test = np_utils.to_categorical(test_song[1:], num_classes=num_chords)
loss = model.evaluate(X_test, Y_test, batch_size=batch_size, verbose=verbose)
model.reset_states()
total_test_loss += loss
bar.update(i+1)
total_test_loss_array.append(total_test_loss/len(test_set))
print('\nTotal test loss: ', total_test_loss/len(test_set))
print('-'*50)
plt.plot(total_test_loss_array, 'b-', label='test loss')
plt.plot(total_train_loss_array, 'r-', label='train loss')
# plt.legend()
plt.ylabel(model_path)
# plt.axis([0, 50, 3, 5])
plt.grid()
if show_plot: plt.show()
if save_plot: plt.savefig(model_path+'plot.png')
pickle.dump(total_test_loss_array,open(model_path+'total_test_loss_array.pickle', 'wb'))
pickle.dump(total_train_loss_array,open(model_path+'total_train_loss_array.pickle', 'wb'))
def train():
print('training model...')
total_train_loss = 0
for e in range(1, epochs+1):
print('Epoch ', e, 'of ', epochs, 'Epochs\nTraining:')
if shuffle_train_set:
shuffle(train_set)
bar = progressbar.ProgressBar(maxval=len(train_set))
for i, song in enumerate(train_set):
# bar.start()
X = song[:-1]
Y = np_utils.to_categorical(song[1:], num_classes=num_chords)
hist = model.fit(X, Y, batch_size=batch_size, shuffle=False, verbose=verbose)
model.reset_states()
bar.update(i+1)
# print(hist.history)
total_train_loss += hist.history['loss'][0]
if (i+1)%test_step is 0:
total_train_loss = total_train_loss/test_step
total_train_loss_array.append(total_train_loss)
print('\nTotal training loss: ',total_train_loss)
test()
total_train_loss = 0
if e%save_step is 0:
print('saving model')
model_save_path = model_path + 'model_' + 'Epoch' + str(e) + model_filetype
model.save(model_save_path)
def save_params():
with open(model_path + 'params.txt', "w") as text_file:
text_file.write("epochs: %s" % epochs + '\n')
text_file.write("train_set_size: %s" % train_set_size + '\n')
text_file.write("test_set_size: %s" % test_set_size + '\n')
text_file.write("lstm_size: %s" % lstm_size + '\n')
text_file.write("embedding_dim: %s" % chord_embedding_dim + '\n')
text_file.write("learning_rate: %s" % learning_rate + '\n')
#text_file.write("save_step: %s" % save_step + '\n')
text_file.write("shuffle_train_set: %s" % shuffle_train_set + '\n')
text_file.write("test_step: %s" % test_step + '\n')
text_file.write("bidirectional: %s" % bidirectional + '\n')
text_file.write("num_chords: %s" % num_chords + '\n')
text_file.write("chord_n: %s" % chord_n + '\n')
print("saving params")
save_params()
print("starting training..")
train()