-
Notifications
You must be signed in to change notification settings - Fork 0
/
song_cnn.py
383 lines (302 loc) · 14.8 KB
/
song_cnn.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras import layers
from tensorflow.keras.utils import to_categorical
from tensorflow import keras
import tensorflow as tf
import numpy as np
import csv
import math
import pydub
import librosa
import json
"""
Converting to categorical data- helper functions.
"""
def map_values_5(arr):
arr = np.where(arr < 100000, 0, arr)
arr = np.where((arr >= 100000) & (arr < 1000000), 1, arr)
arr = np.where((arr >= 1000000) & (arr < 10000000), 2, arr)
arr = np.where((arr >= 10000000) & (arr < 100000000), 3, arr)
arr = np.where(arr >= 100000000, 4, arr)
return arr
def map_values_3(arr):
arr = np.where(arr < 1000000, 0, arr)
arr = np.where((arr >= 1000000) & (arr < 15000000), 1, arr)
arr = np.where((arr >= 15000000), 2, arr)
return arr
#Load up the data
loaded_data_train = np.load('total_train_np.npz')
loaded_data_validation = np.load('total_validation_np.npz')
loaded_data_test = np.load('total_test_np.npz')
total_train_label = loaded_data_train['labels']
total_train_data = loaded_data_train['data']
total_test_label = loaded_data_test['labels']
total_test_data = loaded_data_test['data']
total_validation_label = loaded_data_validation['labels']
total_validation_data = loaded_data_validation['data']
#Convert data to 5 categories
total_train_label_c = map_values_5(total_train_label)
total_validation_label_c = map_values_5(total_validation_label)
total_test_label_c = map_values_5(total_test_label)
total_train_label_c = to_categorical(total_train_label_c, num_classes=5)
total_validation_label_c = to_categorical(total_validation_label_c, num_classes=5)
total_test_label_c = to_categorical(total_test_label_c, num_classes=5)
#############################################################CLASSIFICATION MODELS########################################################
def classification_cnn():
s_cnn = Sequential(name="Song_CNN")
s_cnn.add(Conv2D(16, kernel_size=(3, 3), strides=(1, 1), padding="same",
activation="relu", input_shape=(46, 128, 196)))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Flatten())
s_cnn.add(Dense(5, activation='softmax'))
return s_cnn
def class_highfilters_cnn():
s_cnn = Sequential(name="Song_CNN")
s_cnn.add(Conv2D(128, kernel_size=(3, 3), strides=(1, 1), padding="same",
activation="relu", input_shape=(46, 128, 196)))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Flatten())
s_cnn.add(Dense(5, activation='softmax'))
return s_cnn
def class_highfilters_dense_cnn():
s_cnn = Sequential(name="Song_CNN")
s_cnn.add(Conv2D(128, kernel_size=(3, 3), strides=(1, 1), padding="same",
activation="relu", input_shape=(46, 128, 196)))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Flatten())
s_cnn.add(Dense(128, activation='relu'))
s_cnn.add(Dense(5, activation='softmax'))
return s_cnn
def class_highfilters_dense_2layers_cnn():
s_cnn = Sequential(name="Song_CNN")
s_cnn.add(Conv2D(32, kernel_size=(3, 3), strides=(1, 1), padding="same",
activation="relu", input_shape=(46, 128, 196)))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Conv2D(64, kernel_size=(3, 3), strides=(1, 1), padding="same", activation="relu"))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Flatten())
s_cnn.add(Dense(128, activation='relu'))
s_cnn.add(Dense(5, activation='softmax'))
return s_cnn
def class_highfilters_dense_3layers_cnn():
s_cnn = Sequential(name="Song_CNN")
s_cnn.add(Conv2D(16, kernel_size=(3, 3), strides=(1, 1), padding="same",
activation="relu", input_shape=(46, 128, 196)))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Conv2D(32, kernel_size=(3, 3), strides=(1, 1), padding="same", activation="relu"))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Conv2D(64, kernel_size=(3, 3), strides=(1, 1), padding="same", activation="relu"))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Flatten())
s_cnn.add(Dense(128, activation='relu'))
s_cnn.add(Dense(64, activation='relu'))
s_cnn.add(Dense(5, activation='softmax'))
return s_cnn
#########################################################MODEL GENERATION FOR CLASSIFICATION ###########################################
#THIS SECTION IS FOR CALIBRATING CLASSIFCATION MODELS. COMMENT IF
#YOU WANT TO TEST A SINGULAR SONG
create_class_graph = True
model_c = class_highfilters_dense_3layers_cnn()
model_c.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics='accuracy')
history_c = model_c.fit(total_train_data, total_train_label_c, epochs=5,
validation_data=(total_validation_data, total_validation_label_c))
test_predictions = model_c.predict(total_test_data)
pred = np.apply_along_axis(np.argmax, axis = 1, arr = test_predictions)
test = np.apply_along_axis(np.argmax, axis = 1, arr = total_test_label_c)
mse_error = np.mean(np.square(pred - test))
# mse_error = np.argmax(test_predictions) - total_test_label_c
print("The mean squared error for categorical outputs is: ", mse_error)
#Output loss values to a csv for graph generation
if create_class_graph:
epo = len(history_c.history['loss']) + 1
epochs = [i for i in range(1, epo)]
loss_list = history_c.history['loss']
val_loss_list = history_c.history['val_loss']
accuracy_list = history_c.history['accuracy']
val_accuracy_list = history_c.history['val_accuracy']
with open('Loss_Class_Graph.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Epoch', 'loss', 'val_loss', 'accuracy', 'val_accuracy']) # Write the headers
for i in range(epo-1):
writer.writerow([epochs[i], loss_list[i], val_loss_list[i], accuracy_list[i], val_accuracy_list[i]])
##########################################################################################################################################
#############################################################REGRESSION MODELS########################################################
#1 layer, only 16 filters
def original_cnn():
s_cnn = Sequential(name="Song_CNN")
s_cnn.add(Conv2D(16, kernel_size=(3, 3), strides=(1, 1), padding="same",
activation="relu", input_shape=(46, 128, 196)))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Flatten())
s_cnn.add(Dense(1, activation='relu'))
return s_cnn
def highfilters_cnn():
s_cnn = Sequential(name="Song_CNN")
s_cnn.add(Conv2D(128, kernel_size=(3, 3), strides=(1, 1), padding="same",
activation="relu", input_shape=(46, 128, 196)))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Flatten())
s_cnn.add(Dense(1, activation='relu'))
return s_cnn
def highfilters_dense_cnn():
s_cnn = Sequential(name="Song_CNN")
s_cnn.add(Conv2D(128, kernel_size=(3, 3), strides=(1, 1), padding="same",
activation="relu", input_shape=(46, 128, 196)))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Flatten())
s_cnn.add(Dense(128, activation='relu'))
s_cnn.add(Dense(1, activation='relu'))
return s_cnn
def highfilters_dense_2layers_cnn():
s_cnn = Sequential(name="Song_CNN")
s_cnn.add(Conv2D(32, kernel_size=(3, 3), strides=(1, 1), padding="same",
activation="relu", input_shape=(46, 128, 196)))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Conv2D(64, kernel_size=(3, 3), strides=(1, 1), padding="same", activation="relu"))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Flatten())
s_cnn.add(Dense(128, activation='relu'))
s_cnn.add(Dense(1, activation='relu'))
return s_cnn
def highfilters_dense_3layers_cnn():
s_cnn = Sequential(name="Song_CNN")
s_cnn.add(Conv2D(16, kernel_size=(3, 3), strides=(1, 1), padding="same",
activation="relu", input_shape=(46, 128, 196)))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Conv2D(32, kernel_size=(3, 3), strides=(1, 1), padding="same", activation="relu"))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Conv2D(64, kernel_size=(3, 3), strides=(1, 1), padding="same", activation="relu"))
s_cnn.add(MaxPooling2D(padding="same"))
s_cnn.add(Flatten())
s_cnn.add(Dense(128, activation='relu'))
s_cnn.add(Dense(64, activation='relu'))
s_cnn.add(Dense(1, activation='relu'))
return s_cnn
#########################################################MODEL GENERATION FOR "REGRESSION" ###############################################
#THIS SECTION IS FOR CALIBRATING REGRESSION MODELS. COMMENT IF
#YOU WANT TO TEST A SINGULAR SONG
#Bool to create graph or not
create_reg_graph = True
model = highfilters_dense_3layers_cnn()
log_err = "mean_squared_logarithmic_error"
abs_perc_err = "mean_absolute_percentage_error"
model.compile(loss=log_err,
optimizer='adam')
history = model.fit(total_train_data, total_train_label, epochs=5,
validation_data=(total_validation_data, total_validation_label))
test_predictions = model.predict(total_test_data)
absolute_error = np.mean(np.abs(test_predictions - total_test_label))
print("The absolute mean error is: ", absolute_error)
if create_reg_graph:
#Output loss values to a csv for graph generation
epo = len(history.history['loss']) + 1
epochs = [i for i in range(1, epo)]
loss_list = history.history['loss']
val_loss_list = history.history['val_loss']
with open('Loss_Views_Graph.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Epoch', 'loss', 'val_loss']) # Write the headers
for i in range(epo - 1):
writer.writerow([epochs[i], loss_list[i], val_loss_list[i]])
##################################################################################################################################
#Spectrogram generation functions
def cut_song(song):
start = 0
end = len(song)
song_pieces = []
while start + 100000 < end:
song_pieces.append(song[start:start+100000])
start += 100000
return song_pieces
def prepare_song(song_path):
list_matrices = []
dur = pydub.utils.mediainfo(song_path)["duration"]
y, sr = librosa.load(song_path, duration = math.floor(float(dur)))
song_pieces = cut_song(y)
for song_piece in song_pieces:
melspect = librosa.feature.melspectrogram(y = song_piece)
list_matrices.append(melspect)
return list_matrices
if __name__ == '__main__':
#Load up the data
loaded_data_train = np.load('total_train_np.npz')
loaded_data_validation = np.load('total_validation_np.npz')
loaded_data_test = np.load('total_test_np.npz')
total_train_label = loaded_data_train['labels']
total_train_data = loaded_data_train['data']
total_test_label = loaded_data_test['labels']
total_test_data = loaded_data_test['data']
total_validation_label = loaded_data_validation['labels']
total_validation_data = loaded_data_validation['data']
#Convert data to 5 categories
total_train_label_c = map_values_5(total_train_label)
total_validation_label_c = map_values_5(total_validation_label)
total_test_label_c = map_values_5(total_test_label)
total_train_label_c = to_categorical(total_train_label_c, num_classes=5)
total_validation_label_c = to_categorical(total_validation_label_c, num_classes=5)
total_test_label_c = to_categorical(total_test_label_c, num_classes=5)
##get the spectrogram data from the mp3
##ToDo for rahul
demo_song = np.array(prepare_song("Ariana Grande, The Weeknd - off the table.mp3"))
demo_song.resize(1, 46, 128, 196)
#Want to create a csv with class accuracy?
create_class_graph = True
model_c = class_highfilters_dense_3layers_cnn()
model_c.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics='accuracy')
history_c = model_c.fit(total_train_data, total_train_label_c, epochs=3,
validation_data=(total_validation_data, total_validation_label_c))
test_predictions = model_c.predict(demo_song)
pred = np.argmax(test_predictions)
actual = 3
#pred = np.apply_along_axis(np.argmax, axis = 1, arr = test_predictions)
#test = np.apply_along_axis(np.argmax, axis = 1, arr = total_test_label_c)
mse_error = np.mean(np.square(pred - actual))
# mse_error = np.argmax(test_predictions) - total_test_label_c
print("The actual popularity of your song (on a scale of 0 to 4) is: ", actual)
print("The popularity of your song (on a scale of 0 to 4) is predicted to be: ", pred)
print("The mean squared error for your prediction is: ", mse_error)
#Output loss values to a csv for graph generation
if create_class_graph:
epo = len(history_c.history['loss']) + 1
epochs = [i for i in range(1, epo)]
loss_list = history_c.history['loss']
val_loss_list = history_c.history['val_loss']
accuracy_list = history_c.history['accuracy']
val_accuracy_list = history_c.history['val_accuracy']
with open('Loss_Class_Graph_Demo.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Epoch', 'loss', 'val_loss', 'accuracy', 'val_accuracy']) # Write the headers
for i in range(epo-1):
writer.writerow([epochs[i], loss_list[i], val_loss_list[i], accuracy_list[i], val_accuracy_list[i]])
#################REGRESSION DEMO###########################################################################################
create_reg_graph = True
model = highfilters_dense_3layers_cnn()
log_err = "mean_squared_logarithmic_error"
abs_perc_err = "mean_absolute_percentage_error"
model.compile(loss=log_err,
optimizer='adam')
history = model.fit(total_train_data, total_train_label, epochs=3,
validation_data=(total_validation_data, total_validation_label))
test_predictions = model.predict(demo_song)
total_test_label = 23418985
absolute_error = np.mean(np.abs(test_predictions - total_test_label))
print("The actual popularity of the song (in terms of views) is: ", total_test_label)
print("The popularity of your song (in terms of views) is predicted to be: ", int(test_predictions[0]))
print("The absolute mean error is: ", absolute_error)
if create_reg_graph:
#Output loss values to a csv for graph generation
epo = len(history.history['loss']) + 1
epochs = [i for i in range(1, epo)]
loss_list = history.history['loss']
val_loss_list = history.history['val_loss']
with open('Loss_Views_Graph_Demo.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Epoch', 'loss', 'val_loss']) # Write the headers
for i in range(epo - 1):
writer.writerow([epochs[i], loss_list[i], val_loss_list[i]])