-
Notifications
You must be signed in to change notification settings - Fork 38
/
convnet_chord_classification_training.py
242 lines (189 loc) · 7.48 KB
/
convnet_chord_classification_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
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
# Chord classification
#
# The task is to classify chords (or more precisely pitch class sets) based on chromagram features.
#
# We use a the whole Beatles dataset (ie. many songs).
#
# The task is in fact multilabel classification, since each pitch class is generally independent.
import numpy as np
import pandas as pd
import matplotlib as mpl
# do not use Qt/X that require $DISPLAY
mpl.use('Agg')
import matplotlib.pyplot as plt
import arrow
import os
import scipy.signal
import scipy.misc
from sklearn.metrics import hamming_loss, accuracy_score, roc_auc_score
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Flatten, Dropout
from keras.layers.convolutional import Convolution1D, MaxPooling1D
from keras.layers.normalization import BatchNormalization
from keras.callbacks import ModelCheckpoint
## Data loading
dataset_file = '../data/beatles/ml_dataset/block=4096_hop=2048_bins=-48,67_div=1/dataset_2016-05-15.npz'
dataset = np.load(dataset_file)
X_train, Y_train, X_valid, Y_valid, X_test, Y_test = \
dataset['X_train'], dataset['Y_train'], \
dataset['X_valid'], dataset['Y_valid'], \
dataset['X_test'], dataset['Y_test']
## Data preprocessing
### Features
# scaler = MinMaxScaler()
# X = scaler.fit_transform(features).astype('float32')
# let's rescale the features manually so that the're the same in all songs
# the range (in dB) is -120 to X.shape[1] (= 115)
# TODO: there's a bug: should be + 120 on both places!!!
def normalize(X):
return (X.astype('float32') - 120) / (X.shape[1] - 120)
X_train = normalize(X_train)
X_valid = normalize(X_valid)
X_test = normalize(X_test)
for d in [X_train, X_valid, X_test, Y_train, Y_valid, Y_test]:
print(d.shape)
# reshape for 1D convolution
def conv_reshape(X):
return X.reshape(X.shape[0], X.shape[1], 1)
X_conv_train = conv_reshape(X_train)
X_conv_valid = conv_reshape(X_valid)
X_conv_test = conv_reshape(X_test)
## Model training and evaluation
def new_model_id():
return 'model_%s' % arrow.get().format('YYYY-MM-DD-HH-mm-ss')
def save_model_arch(model_id, model):
arch_file = '%s/%s_arch.yaml' % (model_dir, model_id)
print('architecture:', arch_file)
open(arch_file, 'w').write(model.to_yaml())
def weights_file(model_id, suffix=''):
return '%s/%s_weights%s.h5' % (model_dir, model_id, suffix)
def report_model_parameters(model):
print('number of parameters:', model.count_params())
print('weights:', [w.shape for w in model.get_weights()])
# #### Notes
#
# - the last layer has to be sigmoid, not softmax
# - since each output label should be independent a multiple can be active at the same time
# - very sparse inputs can easily saturate sigmoid activation if it's near the first layer
# - class_mode='binary' for multi-label classification
# - predict_classes() then returns a binary vector
# - loss: MAE or binary_crossentropy?
# - why binary_crossentropy gives worse accuracy than MAE?
# - binary_crossentropy works ok
# - problems with loss going to NAN after the first training iteration
# - optimizer clipnorm doesn't help
# - BatchNormalization doesn't help
# - BatchNormalization between convolution and activation works
# - BatchNormalization might be useful
# - be aware to use scaled inputs, not raw ones
model_id = new_model_id()
print('model id:', model_id)
model_dir = '../data/beatles/models/' + model_id
os.makedirs(model_dir, exist_ok=True)
model = Sequential()
model.add(Convolution1D(32, 3, input_shape=(X_train.shape[1], 1)))
# model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Convolution1D(32, 3))
model.add(Activation('relu'))
model.add(MaxPooling1D(2, 2))
model.add(Dropout(0.25))
model.add(Convolution1D(64, 3))
# model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Convolution1D(64, 3))
model.add(Activation('relu'))
model.add(MaxPooling1D(2, 2))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(12))
model.add(Activation('sigmoid'))
report_model_parameters(model)
print('compiling the model')
model.compile(class_mode='binary', loss='binary_crossentropy', optimizer='adam')
save_model_arch(model_id, model)
print('training the model')
checkpointer = ModelCheckpoint(filepath=weights_file(model_id, '_checkpoint'), verbose=1, save_best_only=True)
epoch_count = 10
batch_size = 512
training_hist = model.fit(
X_conv_train, Y_train,
validation_data=(X_conv_valid, Y_valid),
nb_epoch=epoch_count,
batch_size=batch_size,
callbacks=[checkpointer],
verbose=1)
# There's a problem with checkpointer that it produces weight with one more layer
# and the weights cannot be easily imported.
model.save_weights(weights_file(model_id, ''))
def report_training_curve(training_hist):
history = training_hist.history
pd.DataFrame(history).to_csv(model_dir+'/'+model_id+'_training_history.tsv', header=True)
plt.figure()
for label in history:
plt.plot(history[label], label=label)
plt.xlabel('epochs')
plt.title('%s - learning curves' % model_id)
plt.suptitle('validation loss: %s' % history['val_loss'][-1])
plt.legend()
plt.savefig(model_dir+'/'+model_id+'_learning_curves.png')
report_training_curve(training_hist)
def model_report_multilabel(model, X_train, Y_train, X_valid, Y_valid):
def report_dataset(X, y_true, title):
y_proba = model.predict_proba(X, batch_size=batch_size)
# multi-label classes with default threshold
y_pred = y_proba >= 0.5
print(title + ' accuracy (exatch match):', accuracy_score(y_true, y_pred))
print(title + ' hamming score (non-exatch match):', 1 - hamming_loss(y_true, y_pred))
print(title + 'AUC:', roc_auc_score(y_true.flatten(), y_proba.flatten()))
report_dataset(X_train, Y_train, 'training')
report_dataset(X_valid, Y_valid, 'validation')
model_report_multilabel(model, X_conv_train, Y_train, X_conv_valid, Y_valid)
# visualization
def plot_labels(l, title, fifths=False, resample=True, exact=False):
if fifths:
l = l[:,np.arange(12)*7 % 12]
l = l.T
file = model_dir+'/'+model_id+'_'+title+'.png'
if exact:
scipy.misc.imsave(file, l)
else:
if resample:
l = scipy.signal.resample(l, 200, axis=1)
plt.figure(figsize=(20, 2))
plt.imshow(l, cmap='gray', interpolation='none')
plt.tight_layout()
plt.savefig(file)
# # true labels
# plot_labels(labels_pcs, 'true')
# plot_labels(labels_pcs, 'exact_true', exact=True)
#
# # predicted labels
# labels_pred_full = model.predict_classes(conv_reshape(X))
# plot_labels(labels_pred_full, 'pred')
# plot_labels(labels_pred_full, 'exact_pred', exact=True)
#
# # difference
# plot_labels(labels_pcs - labels_pred_full, 'diff')
# plot_labels(labels_pcs - labels_pred_full, 'exact_diff', exact=True)
# plot_labels(labels_pred_full[:100], resample=False)
# plot_labels(labels_pcs[:100] - labels_pred_full[:100], resample=False)
# in case of input features with original time order we can apply median filter:
# medfilt(labels_pred_full, (15, 1))
def plot_labels_true_pred_diff():
def plot2d(x):
plt.imshow(scipy.signal.resample(x.T, 200, axis=1), cmap='gray', interpolation='none')
plt.figure(figsize=(20, 6))
ax = plt.subplot(3,1,1)
plot2d(labels_pcs)
ax.set_title('true')
ax = plt.subplot(3,1,2)
plot2d(labels_pred_full)
ax.set_title('predicted')
ax = plt.subplot(3,1,3)
plot2d(labels_pred_full - labels_pcs)
ax.set_title('difference')
plt.tight_layout()