-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
78 lines (65 loc) · 2.42 KB
/
utils.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
import numpy as np
import matplotlib.pyplot as plt
import itertools
from sklearn.metrics import confusion_matrix
def PlotConfusionMatrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues,
showAcc=True):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
#print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
verticalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
if showAcc:
acc = 100*(np.trace(cm) / np.sum(cm))
title = title + " | Acc=%.2f%%" % acc
plt.title(title)
def PlotModelEval(Model, History, y_test, y_preds, Labels):
# Scores for each class (can be interpreted as probabilities since we use softmax output)
S = y_preds
# Prediction (class number) for each test image
P = y_test
# Calculate confusion matrix
CM = confusion_matrix(Y,P)
# Plot training history
plt.figure(figsize=(16,6))
plt.subplot(2,2,1)
plt.semilogy(History.history['loss'], label="Training")
if 'val_loss' in History.history:
plt.semilogy(History.history['val_loss'], label="Validation")
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(loc='upper right')
plt.grid(True, which="both")
plt.subplot(2,2,3)
plt.plot(100 * np.array(History.history['accuracy']), label="Training")
if 'val_accuracy' in History.history:
plt.plot(100 * np.array(History.history['val_accuracy']), label="Validation")
plt.title('Model accuracy')
plt.ylabel('Acc [%]')
plt.xlabel('Epoch')
plt.legend(loc='lower right')
plt.grid(True, which="both")
# Plot confusion matrix
plt.subplot(2,2,(2,4))
PlotConfusionMatrix(CM, classes=Labels, title="Confusion matrix (test)")
plt.show()