-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicCNN2.py
160 lines (108 loc) · 4.47 KB
/
musicCNN2.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
import json
import keras as keras
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from MUSIC.MUSICRNN import predict
DATA_PATH = "data.json"
def load_data(data_path):
with open(data_path, "r") as fp:
data = json.load(fp)
x = np.array(data["mfcc"])
y = np.array(data["labels"])
return x, y
def prepare_datasets(test_size, validation_size):
# load data
x, y = load_data(DATA_PATH)
# create train/test split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=test_size)
# create train/validation split
x_train, x_validation, y_train, y_validation = train_test_split(x_train, y_train, test_size=validation_size)
# 3D array
x_train = x_train[..., np.newaxis]
x_validation = x_validation[..., np.newaxis]
x_test = x_test[..., np.newaxis]
return x_train, x_validation, x_test, y_train, y_validation, y_test
def build_model(input_shape):
# create model
model = keras.Sequential()
# First Layer
model.add(keras.layers.Conv2D(filters=32, kernel_size=3, padding="same", activation="relu", input_shape=input_shape))
# Second Layer
model.add(keras.layers.Conv2D(filters=32, kernel_size=3, padding="same", activation="relu"))
# Max Pooling Layer
model.add(keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))
# Third Layer
model.add(keras.layers.Conv2D(filters=64, kernel_size=3, padding="same", activation="relu"))
# Fourth Layer
model.add(keras.layers.Conv2D(filters=64, kernel_size=3, padding="same", activation="relu"))
# Max Pooling Layer
model.add(keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))
# fifth Layer
model.add(keras.layers.Conv2D(filters=128, kernel_size=3, padding="same", activation="relu"))
# Sixth Layer
model.add(keras.layers.Conv2D(filters=128, kernel_size=3, padding="same", activation="relu"))
# Max Pooling Layer
model.add(keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))
# Flattening Layer
model.add(keras.layers.Flatten())
# Dropout Layer
model.add(keras.layers.Dropout(0.5, noise_shape=None, seed=None))
# Adding the first fully connected layer
model.add(keras.layers.Dense(units=128, activation='relu'))
# Output Layer
model.add(keras.layers.Dense(units=10, activation='softmax'))
return model
x = x[np.newaxis, ...]
prediction = model.predict(x)
# extract index with max value
predicted_index = np.argmax(prediction, axis=1)
print("Expected index: {}, Predicted index: {}".format(y, predicted_index))
def plot_history(history):
fig, axs = plt.subplots(2)
#create accuracy subplot
axs[0].plot(history.history["accuracy"], label = "train accuracy")
axs[0].plot(history.history["val_accuracy"], label = "test accuracy")
axs[0].set_ylabel("Accuracy")
axs[0].legend(loc = "lower right")
axs[0].set_title("Accuracy eval")
# create error subplot
axs[1].plot(history.history["loss"], label="train error")
axs[1].plot(history.history["val_loss"], label="test error")
axs[1].set_ylabel("Error")
axs[1].set_xlabel("Epoch")
axs[1].legend(loc="lower right")
axs[1].set_title("Error eval")
plt.show()
if __name__ == "__main__":
# create train, validation and test set
x_train, x_validation, x_test, y_train, y_validation, y_test = prepare_datasets(0.25, 0.2)
# build the CNN net
input_shape = (x_train.shape[1], x_train.shape[2], 1)
model = build_model(input_shape)
keras.utils.plot_model(
model,
to_file="model.png",
show_shapes=False,
show_layer_names=True,
rankdir="TB",
expand_nested=False,
dpi=96,
)
# Train the CNN
optimizer = keras.optimizers.Adam(learning_rate=0.0001)
model.compile(optimizer=optimizer,
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
model.summary()
keras.utils.plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)
# Train the CNN
history = model.fit(x_train, y_train, validation_data=(x_validation, y_validation), batch_size=36, epochs=30)
# Evaluate the CNN on test set
test_error, test_accuracy = model.evaluate(x_test, y_test, verbose=1)
print("Accuracy on test set is : {}".format(test_accuracy))
# make prediction
x = x_test[100]
y = y_test[100]
predict(model, x, y)
plot_history(history)