-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_affective_prosody_model.py
120 lines (100 loc) · 3.73 KB
/
train_affective_prosody_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
import pickle
from keras.models import model_from_json
import pandas as pd
import os
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten, Dropout, Activation
from keras.layers import Conv1D, MaxPooling1D
from tensorflow.keras import optimizers
from sklearn.preprocessing import LabelEncoder
import matplotlib.pyplot as plt
from keras.utils import np_utils
# load a trained model from a json file
def loadModel(weightFile, modelFile):
try:
jsonFile = open(modelFile, 'r')
loadedModel = jsonFile.read()
jsonFile.close()
loadedModel = model_from_json(loadedModel)
# load weights
loadedModel.load_weights(weightFile)
print("Loaded Model From Disk")
return loadedModel
except FileNotFoundError:
print("ERROR - The imported model files were not found")
exit(1)
# Save the model and weights
def saveModel(modelName, model):
save_dir = os.path.join(os.getcwd(), 'my_models')
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
model_path = os.path.join(save_dir, modelName+".h5")
model.save(model_path)
print('Saved trained model at %s ' % model_path)
model_json = model.to_json()
with open(save_dir + "/" + modelName+".json", "w") as json_file:
json_file.write(model_json)
# Save the model and weights
def savePickleModel(modelName, model):
save_dir = os.path.join(os.getcwd(), 'my_models')
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
model_path = os.path.join(save_dir, modelName+".sav")
pickle.dump(model, open(model_path, 'wb'))
print('Saved trained model at %s ' % model_path)
# Load the dataset
dataset = pd.read_csv("../Datasets/RAVDESS/speechActorDataset2.csv")
# Split features and labels
datasetCopy = dataset
labels = datasetCopy['label']
features = datasetCopy.drop(columns='label')
# Train Test Split
trainIndex = int(len(features) * 0.7)
train_features = features[:trainIndex]
train_labels = labels[:trainIndex]
test_features = features[trainIndex+1:-1]
test_labels = labels[trainIndex+1:-1]
lb = LabelEncoder()
y_train = np_utils.to_categorical(lb.fit_transform(train_labels))
savePickleModel("baseline_AP_labelEncoder", lb)
y_test = np_utils.to_categorical(lb.fit_transform(test_labels))
# Changing Dimension for CNN model
x_traincnn =np.expand_dims(train_features, axis=2)
x_testcnn= np.expand_dims(test_features, axis=2)
model = Sequential()
model.add(Conv1D(256, 5,padding='same',
input_shape=(216,1)))
model.add(Activation('relu'))
model.add(Conv1D(128, 5,padding='same'))
model.add(Activation('relu'))
model.add(Dropout(0.1))
model.add(MaxPooling1D(pool_size=(8)))
model.add(Conv1D(128, 5,padding='same',))
model.add(Activation('relu'))
#model.add(Conv1D(128, 5,padding='same',))
#model.add(Activation('relu'))
#model.add(Conv1D(128, 5,padding='same',))
#model.add(Activation('relu'))
#model.add(Dropout(0.2))
model.add(Conv1D(128, 5,padding='same',))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(5))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',optimizer="SGD",metrics=['accuracy'])
cnnhistory=model.fit(x_traincnn, y_train, batch_size=16, epochs=300, validation_data=(x_testcnn, y_test))
preds = model.predict(x_testcnn, batch_size=16, verbose=1)
preds1=preds.argmax(axis=1)
abc = preds1.astype(int).flatten()
predictions = (lb.inverse_transform((abc)))
preddf = pd.DataFrame({'predictedvalues': predictions})
plt.plot(cnnhistory.history['accuracy'])
plt.plot(cnnhistory.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
saveModel("affectiveProsody_model", model)