Spaces:
Build error
Build error
Create ser.py
Browse files
ser.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sklearn.neural_network import MLPClassifier
|
2 |
+
|
3 |
+
from sklearn.metrics import accuracy_score
|
4 |
+
from utils import load_data
|
5 |
+
|
6 |
+
import os
|
7 |
+
import pickle
|
8 |
+
|
9 |
+
# load RAVDESS dataset
|
10 |
+
X_train, X_test, y_train, y_test = load_data(test_size=0.25)
|
11 |
+
# print some details
|
12 |
+
# number of samples in training data
|
13 |
+
print("[+] Number of training samples:", X_train.shape[0])
|
14 |
+
# number of samples in testing data
|
15 |
+
print("[+] Number of testing samples:", X_test.shape[0])
|
16 |
+
# number of features used
|
17 |
+
# this is a vector of features extracted
|
18 |
+
# using utils.extract_features() method
|
19 |
+
print("[+] Number of features:", X_train.shape[1])
|
20 |
+
# best model, determined by a grid search
|
21 |
+
model_params = {
|
22 |
+
'alpha': 0.01,
|
23 |
+
'batch_size': 256,
|
24 |
+
'epsilon': 1e-08,
|
25 |
+
'hidden_layer_sizes': (300,),
|
26 |
+
'learning_rate': 'adaptive',
|
27 |
+
'max_iter': 500,
|
28 |
+
}
|
29 |
+
# initialize Multi Layer Perceptron classifier
|
30 |
+
# with best parameters ( so far )
|
31 |
+
model = MLPClassifier(**model_params)
|
32 |
+
|
33 |
+
# train the model
|
34 |
+
print("[*] Training the model...")
|
35 |
+
model.fit(X_train, y_train)
|
36 |
+
|
37 |
+
# predict 25% of data to measure how good we are
|
38 |
+
y_pred = model.predict(X_test)
|
39 |
+
|
40 |
+
# calculate the accuracy
|
41 |
+
accuracy = accuracy_score(y_true=y_test, y_pred=y_pred)
|
42 |
+
|
43 |
+
print("Accuracy: {:.2f}%".format(accuracy*100))
|
44 |
+
|
45 |
+
# now we save the model
|
46 |
+
# make result directory if doesn't exist yet
|
47 |
+
if not os.path.isdir("result"):
|
48 |
+
os.mkdir("result")
|
49 |
+
|
50 |
+
pickle.dump(model, open("result/mlp_classifier.model", "wb"))
|