-
Notifications
You must be signed in to change notification settings - Fork 1
/
spectral_contrast_peaks_model.py
180 lines (156 loc) · 5.34 KB
/
spectral_contrast_peaks_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
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
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
# load vectorized song features
#
def model(input_shape):
nb_filter = 100
filter_length = 3
hidden_dims = 250
pool_length = 1
# LSTM
lstm_output_size = 100
# print("creating model")
# create model
model = Sequential()
#
model.add(Convolution1D(
input_shape=input_shape,
nb_filter=nb_filter,
filter_length=filter_length,
border_mode='valid',
subsample_length=4))
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',
# return_sequences=True
))
model.add(Dropout(0.2))
#
#
# # #
# # # #
# model.add(Convolution1D(
# nb_filter=int(nb_filter/5),
# filter_length=int(filter_length/10),
# border_mode='valid',
# subsample_length=1))
# model.add(Activation('relu'))
# model.add(MaxPooling1D(pool_length=pool_length))
# model.add(Dropout(0.2))
# 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=(X.shape[1],X.shape[2]),
# activation='sigmoid',
# inner_activation='hard_sigmoid',
# return_sequences=True
# ))
#
#
# #
# # model.add(Dropout(0.2))
#
# model.add(Dropout(0.2))
# model.add(Flatten())
model.add(Dense(numGenres))
model.add(Dropout(0.2))
# model.add(Flatten())
# model.add(LSTM(lstm_output_size))
return model
# model.add(LSTM(lstm_output_size,
# # input_shape=(X.shape[1],X.shape[2]),
# activation='sigmoid',
# inner_activation='hard_sigmoid',
# # return_sequences=True
# ))
#
# 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))
if __name__=="__main__":
X = pickle.load(open("pickled_vectors/spectral-contrast_peaks_training_vector.pickle","rb"))
y = pickle.load(open("pickled_vectors/spectral-contrast_peaks_label.pickle","rb"))
X_test = pickle.load(open("pickled_vectors/spectral-contrast_peaks_evaluation_training_vector.pickle","rb"))
y_test = pickle.load(open("pickled_vectors/spectral-contrast_peaks_evaluation_label.pickle","rb"))
batch_size = 20
nb_epoch = 50
model = 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)
print("X shape",X.shape)
print("y shape",y.shape)
print("X_test", X_test.shape)
print("y_test", y_test.shape)
print("Fitting")
#
# X = np.random.random(X.shape)
# y = np.random.random(y.shape)
#
history = model.fit(X, y,
batch_size=batch_size,
nb_epoch=nb_epoch,
validation_data=(X_test, y_test),
shuffle=True
)
#
# # with open("experimental_results.json","w") as f:
# # f.write(json.dumps(history.history, sort_keys=True,indent=4, separators=(',', ': ')))
#
if not os.path.exists("model_weights"):
os.makedirs("model_weights")
model.save_weights("model_weights/spectral_contrast_peaks_model_weights.hdf5",overwrite=True)
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)
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
plt.show()