-
Notifications
You must be signed in to change notification settings - Fork 1
/
kombMfccSpektarMrezaNata.py
117 lines (82 loc) · 3.27 KB
/
kombMfccSpektarMrezaNata.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
import numpy as np
from numpy.core.records import array
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping
import random
from matplotlib import pyplot as plt
import pandas as pd
from tensorflow.keras import initializers
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from mlxtend.plotting import plot_confusion_matrix
import os
from tensorflow_core import optimizers
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
m = {'classical' : [1] + [0]*5, 'folk' : [0] + [1] + [0]*4, 'house' : [0]*2 + [1] + [0]*3,
'jazz' : [0]*3 + [1] + [0]*2, 'rnb' : [0]*4 + [1] + [0]*1, 'rock' : [0]*5 + [1]}
t = {'classical' : 0, 'folk' : 1, 'house' : 2, 'jazz' : 3, 'rnb' : 4,
'rock' : 5}
p = pd.read_csv('mfccIspektar.csv')
brojKoef = 76
brojIzlaza = 6
labels = p['genre']
labels = np.array(labels)
features = np.array([])
readData = np.zeros((1,len(labels)))
for i in range(brojKoef):
colName = 'feature'+str(i)
column = np.array(p[colName])
column = np.reshape(column, (1,len(column)) )
readData = np.concatenate((readData,column),axis = 0)
readData = readData[1:]
features = readData.T
temp = np.zeros((len(labels),brojIzlaza))
i = 0
for x in labels:
temp[i] = m[x]
i+=1
labels = np.copy(temp)
features = (features - np.min(features))
features = features/np.max(features)
features = features * (25+brojIzlaza)
x_trainVal, x_test, y_trainVal, y_test = train_test_split( features, labels, test_size=0.15, random_state=42)
n = len(x_trainVal)
x_train = x_trainVal[:int(n*(1 - 0.1764))]
x_val = x_trainVal[int(n*(1 - 0.1764)):]
y_train = y_trainVal[:int(n*(1 - 0.1764))]
y_val = y_trainVal[int(n*(1 - 0.1764)):]
x_train = np.array(x_train)
y_train = np.array(y_train)
x_test = np.array(x_test)
y_test = np.array(y_test)
x_val = np.array(x_val)
y_val = np.array(y_val)
def getModel():
model = Sequential()
model.add(Input(shape = (brojKoef,)))
model.add(Dense(50 ,activation = 'sigmoid', use_bias = True, kernel_initializer=initializers.GlorotNormal))
model.add(Dense(brojIzlaza, activation = 'softmax'))
opt = tf.optimizers.Adam(learning_rate = 0.001)
model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=["accuracy"])
return model
model = getModel()
es = EarlyStopping(monitor = 'val_accuracy', mode = 'max', patience = 50, restore_best_weights = True)
#history = model.fit(x_train, y_train, epochs = 500, callbacks = [es], validation_data = ( x_val, y_val), shuffle = True)
#model.save_weights("mfccIspektar50.h5")
model.load_weights("mfccIspektar50.h5")
rez = model.predict(x_train)
y_pred = np.array([])
y_actu = np.array([])
for i in range(0, len(y_train)):
x = np.argmax(rez[i])
y_pred = np.append(y_pred, x)
y = np.argmax(y_train[i])
y_actu = np.append(y_actu, y)
print(accuracy_score(y_actu, y_pred))
cm = confusion_matrix(y_actu,y_pred)
plot_confusion_matrix(conf_mat=cm, figsize = (10,10),class_names=['Klasika','Folk','House','Jazz','RnB','Rock'])
plt.show()