-
Notifications
You must be signed in to change notification settings - Fork 1
/
mfcc_model.py
executable file
·151 lines (126 loc) · 4.62 KB
/
mfcc_model.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
#!/usr/local/bin/python3
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.layers import Convolution1D, MaxPooling1D
from keras.layers import LSTM, GRU, Flatten
import matplotlib.pyplot as plt
import pickle
import json
numGenres=3
def mfcc_model(input_shape):
nb_filter = 100
filter_length = 4
hidden_dims = 250
pool_length = 4
# LSTM
lstm_output_size = 300
# create model
model = Sequential()
model.add(Convolution1D(
input_shape=input_shape,
nb_filter=nb_filter,
filter_length=filter_length,
border_mode='valid',
subsample_length=1))
model.add(Activation('relu'))
model.add(MaxPooling1D(pool_length=pool_length))
model.add(Dropout(0.4))
# #
# # #
model.add(Convolution1D(
nb_filter=int(nb_filter/5),
filter_length=int(filter_length),
border_mode='valid',
subsample_length=1))
model.add(Activation('relu'))
model.add(MaxPooling1D(pool_length=pool_length))
model.add(Dropout(0.4))
# model.add(Flatten())
# # #
# # #
#
# model.add(Convolution1D(
# nb_filter=int(nb_filter/10),
# filter_length=int(filter_length/20),
# border_mode='valid',
# subsample_length=2))
# model.add(Activation('relu'))
# model.add(MaxPooling1D(pool_length=pool_length))
# model.add(Dropout(0.2))
#
model.add(LSTM(lstm_output_size,
# input_shape=input_shape,
activation='sigmoid',
inner_activation='hard_sigmoid'))
#
# model.add(Dropout(0.2))
# model.add(Flatten())
# model.add(LSTM(lstm_output_size))
model.add(Dropout(0.4))
model.add(Dense(numGenres))
model.add(Dropout(0.2))
#
# model.add(Convolution1D(
# nb_filter=int(nb_filter/10),
# filter_length=int(filter_length/5),
# border_mode='valid',
# subsample_length=1))
# model.add(Activation('relu'))
# model.add(MaxPooling1D(pool_length=pool_length))
# model.add(Dropout(0.4))
# model.add(Lambda(max_1d, output_shape=(nb_filter)))
# model.add(LSTM(lstm_output_size))
# model.add(Dropout(0.2))
# # We add a vanilla hidden layer:
# model.add(Activation('relu'))
# model.add(Dense(hidden_dims))
# model.add(Flatten())
# model.add(Dense(200))
# model.add(Activation("sigmoid"))
# model.add(Dropout(0.2))
return model
if __name__=="__main__":
# print(X)
# load vectorized song features
#
batch_size = 20
nb_epoch = 50
X = pickle.load(open("pickled_vectors/mfcc_coefficients_training_vector.pickle","rb"))
y = pickle.load(open("pickled_vectors/mfcc_coefficients_label.pickle","rb"))
X_test = pickle.load(open("pickled_vectors/mfcc_coefficients_evaluation_training_vector.pickle","rb"))
y_test = pickle.load(open("pickled_vectors/mfcc_coefficients_evaluation_label.pickle","rb"))
model = mfcc_model((X.shape[1],X.shape[2]))
model.add(Dense(numGenres))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
print("X shape",X.shape)
print("y shape",y.shape)
print("X_test", X_test.shape)
print("y_test", y_test.shape)
print("Fitting")
history = model.fit(X, y,
batch_size=batch_size,
nb_epoch=nb_epoch,
validation_data=(X_test, y_test),
shuffle="batch"
)
with open("experimental_results.json","w") as f:
f.write(json.dumps(history.history, sort_keys=True, indent=4, separators=(',', ': ')))
for k,v in history.history.items():
# print(k,v)
_keys = list(history.history.keys())
_keys.sort()
plt.subplot(411+_keys.index(k))
# x_space = np.linspace(0,1,len(v))
plt.title(k)
plt.plot(range(0,len(v)),v,marker="8",linewidth=1.5)
if not os.path.exists("model_weights"):
os.makedirs("model_weights")
model.save_weights("model_weights/mfcc_model_weights.hdf5",overwrite=True)
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
plt.show()