forked from MaheshaKulatunga/thirdeye
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
190 lines (171 loc) · 5.89 KB
/
evaluate.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
import matplotlib.pyplot as plt
import pickle
import constants
import time
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_curve, auc
from sklearn.metrics import auc
from scipy import interp
import numpy as np
from itertools import cycle
"""
The evaluate class performs all necessary evaluation procedures for a given network.
"""
class Evaluator:
"""
Initialize class
-----------------------------------------------------------
model: Keras object for the network to be evaluated
show: boolean to choose if the evaluated graphs are displayed
on screen in addition being saved as pngs
"""
def __init__(self, model, show=True):
self.model = model
self.show = show
print(model.summary())
"""
Plot epoch loss graph
-----------------------------------------------------------
histroy: the history object of the model saved during Training
name: Name of the model being evaluated
"""
def plot_accloss_graph(self, histroy, name):
plt.plot(histroy.history['acc'])
plt.plot(histroy.history['val_acc'])
plt.title('{} Model Accuracy'.format(name))
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Training', 'Validation'], loc='upper left')
plt.savefig('{}{}_aegraph.png'.format(constants.FIGURES, name))
if self.show:
plt.show()
plt.clf()
"""
Plot Confusion matrix
-----------------------------------------------------------
y_true: true labels
y_pred: predicted labels
name: Name of the model being evaluated
"""
def plot_cm(self, y_true, y_pred, name):
labels = ['Deepfake', 'Real']
cm = confusion_matrix(y_true, y_pred, labels)
print(cm)
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(cm)
fig.colorbar(cax)
plt.title('Confusion matrix')
ax.set_xticklabels([''] + labels)
ax.set_yticklabels([''] + labels)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.savefig('{}{}_cm.png'.format(constants.FIGURES, name))
if self.show:
plt.show()
plt.clf()
"""
Plot ROC Curve
-----------------------------------------------------------
y_true: true labels
y_score: predicted probabilities
name: Name of the model being evaluated
"""
def plot_roc(self, y_true, y_score, name):
lw = 2
n_classes = 2
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_true[:, i], y_score[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# Combine false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))
# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
mean_tpr += interp(all_fpr, fpr[i], tpr[i])
# Get average and calculate AUC
mean_tpr /= n_classes
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])
# Plot all ROC curves
plt.figure()
colors = cycle(['red', 'blue'])
for i, color in zip(range(n_classes), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=lw,
label='ROC curve of class {0} (area = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=lw)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.title('ROC Curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.legend(loc="lower right")
plt.savefig('{}{}_roc.png'.format(constants.FIGURES, name))
if self.show:
plt.show()
plt.clf()
"""
Predict test data
-----------------------------------------------------------
x, y: independent and dependent variables
name: Name of the model being evaluated
"""
def predict_test_data(self, x, y, name):
# TODO Check size is correct and resize?
y_true = []
y_true_multi = []
y_score = []
y_pred = []
start = time.time()
predictions = self.model.predict(x)
end = time.time()
print('{} completed the predictions in {}s ({}ps)'.format(name, round((end - start),2), round(len(predictions)/(end - start),2)))
count = 0
for pred in range(len(predictions)):
if predictions[pred][0] > predictions[pred][1]:
label = 'Real'
else:
label = 'Deepfake'
y_score.append([predictions[pred][0],predictions[pred][1]])
if y[pred][0] > y[pred][1]:
true = 'Real'
y_true_multi.append([1,0])
else:
true = 'Deepfake'
y_true_multi.append([0,1])
y_true.append(true)
y_pred.append(label)
if label is true:
count += 1
accuracy = count/len(predictions)
self.plot_cm(y_true, y_pred, name)
self.plot_roc(np.array(y_true_multi), np.array(y_score), name)
print('Accuracy: {}%'.format(round((accuracy*100), 2)))
"""
Set model
-----------------------------------------------------------
Sets the model to be evaluated
model: New model Keras object
"""
def set_model(self, model):
self.model = model
"""
Set show
-----------------------------------------------------------
Sets the figures to display in the GUI
show: boolean for showing figures
"""
def set_show(self, show):
self.show = show
"""
Get model
-----------------------------------------------------------
Returns model being evaluated
"""
def get_model(self):
return self.model