-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_evaluation_audio.py
154 lines (122 loc) · 5.21 KB
/
model_evaluation_audio.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
# External Imports
from itertools import cycle
import matplotlib.pyplot as plt
import os
import time
import numpy as np
from sklearn import metrics
import pickle
from keras.models import model_from_json
import pandas as pd
# Project Level Imports
from sklearn.metrics import roc_curve, auc
from sklearn.preprocessing import label_binarize
import config
# Plot the accuracy vs val accuracy and loss vs val loss
def plot_cnn_history(history, titleDetail):
plt.style.use('ggplot')
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
x = range(1, len(acc) + 1)
plt.figure(figsize=(10, 5))
plt.plot(x, acc, 'royalblue', label='Training acc')
plt.plot(x, val_acc, 'r', label='Validation acc')
# plt.title('Training and validation accuracy' + " " + titleDetail)
plt.legend()
plt.plot(x, loss, 'lightsteelblue', label='Training loss')
plt.plot(x, val_loss, 'rosybrown', label='Validation loss')
plt.ylim(bottom=0, top=1.5)
plt.title('CNN Train/Test History - ' + " " + titleDetail)
plt.xlabel("Epoch")
plt.ylabel("Accuracy/Loss")
plt.legend()
saveLoc = os.path.join(config.runtimeCfg["model_result_path"], titleDetail + "-cnnHistory.png")
print(saveLoc)
plt.savefig(saveLoc)
plt.show()
def plot_roc_curve(labels, predictions, titleDetail):
saveLoc = os.path.join(config.runtimeCfg["model_result_path"], titleDetail + "-ROC.png")
print(saveLoc)
plt.savefig(saveLoc)
plt.show()
# Store confusion matrix
def storeConfMatrix(labels, predictions, titleDetail):
saveLoc = os.path.join(config.runtimeCfg["model_result_path"], titleDetail + "-confMatrix"+".txt")
# Write as plain text
with open(saveLoc, "w") as f:
f.writelines(np.array2string(metrics.confusion_matrix(labels, predictions)))
f.close()
# Write as labelled Pandas DataFrame
saveLoc = os.path.join(config.runtimeCfg["model_result_path"], titleDetail + "-confMatrix" + ".csv")
uniqueLabels = labels.unique()
confDF = pd.DataFrame(metrics.confusion_matrix(labels, predictions), columns=uniqueLabels, index=uniqueLabels)
confDF.to_csv(saveLoc, sep=",")
# Store classification report
def storeClassReport(labels, predictions, titleDetail):
saveLoc = os.path.join(config.runtimeCfg["model_result_path"], titleDetail + "-classReport" + ".txt")
with open(saveLoc, "w") as f:
f.writelines(metrics.classification_report(labels, predictions))
f.close()
# Save the model and weights
def saveModel(modelName, model):
save_dir = os.path.join(config.runtimeCfg["model_result_path"], modelName)
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('IO Log - 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(config.runtimeCfg["model_result_path"], modelName)
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('IO Log - Saved trained model at %s ' % model_path)
# Load the models from pickled files
def loadPickledModel(filepath):
try:
loaded_model = pickle.load(open(filepath, 'rb'))
return loaded_model
except FileNotFoundError:
print("ERROR - The imported model files were not found")
exit(1)
# Load model and weights from Json
def loadJsonModel(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)
# Persist our model and associated evaluation data
def storeCnnResults(iteration, origin, cnnHistory, predictions, labels, model):
# Create a new directory
if iteration == 0:
named_tuple = time.localtime() # get struct_time
time_string = time.strftime("%m-%d-%Y-%H-%M-%S", named_tuple)
config.runtimeCfg["model_result_path"] = os.path.join(config.cfg["results_save_loc"], origin+"_"+time_string)
os.mkdir(config.runtimeCfg["model_result_path"])
print("IO LOG - Storing Model Results in:", config.runtimeCfg["model_result_path"])
# Store the plotted CNN History, confusion matrix, and classification report
if os.path.exists(config.runtimeCfg["model_result_path"]):
modelTitle = origin+"-iter-"+str(iteration)
# Save the results
if cnnHistory != "":
plot_cnn_history(cnnHistory, modelTitle)
# plot_roc_curve(labels, predictions, modelTitle)
storeConfMatrix(labels, predictions, modelTitle)
storeClassReport(labels, predictions, modelTitle)
# Save the model
saveModel(modelTitle, model)