-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathvisual_callbacks.py
executable file
·174 lines (129 loc) · 5.76 KB
/
visual_callbacks.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
from keras.callbacks import Callback
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from sklearn.metrics import confusion_matrix
import itertools
import numpy as np
class AccLossPlotter(Callback):
"""Plot training Accuracy and Loss values on a Matplotlib graph.
The graph is updated by the 'on_epoch_end' event of the Keras Callback class
# Arguments
graphs: list with some or all of ('acc', 'loss')
save_graph: Save graph as an image on Keras Callback 'on_train_end' event
"""
def __init__(self, graphs=['acc', 'loss'], save_graph=False):
self.graphs = graphs
self.num_subplots = len(graphs)
self.save_graph = save_graph
def on_train_begin(self, logs={}):
self.acc = []
self.val_acc = []
self.loss = []
self.val_loss = []
self.epoch_count = 0
plt.ion()
plt.show()
def on_epoch_end(self, epoch, logs={}):
self.epoch_count += 1
self.val_acc.append(logs.get('val_acc'))
self.acc.append(logs.get('acc'))
self.loss.append(logs.get('loss'))
self.val_loss.append(logs.get('val_loss'))
epochs = [x for x in range(self.epoch_count)]
count_subplots = 0
if 'acc' in self.graphs:
count_subplots += 1
plt.subplot(self.num_subplots, 1, count_subplots)
plt.title('Accuracy')
#plt.axis([0,100,0,1])
plt.plot(epochs, self.val_acc, color='r')
plt.plot(epochs, self.acc, color='b')
plt.ylabel('accuracy')
red_patch = mpatches.Patch(color='red', label='Test')
blue_patch = mpatches.Patch(color='blue', label='Train')
plt.legend(handles=[red_patch, blue_patch], loc=4)
if 'loss' in self.graphs:
count_subplots += 1
plt.subplot(self.num_subplots, 1, count_subplots)
plt.title('Loss')
#plt.axis([0,100,0,5])
plt.plot(epochs, self.val_loss, color='r')
plt.plot(epochs, self.loss, color='b')
plt.ylabel('loss')
red_patch = mpatches.Patch(color='red', label='Test')
blue_patch = mpatches.Patch(color='blue', label='Train')
plt.legend(handles=[red_patch, blue_patch], loc=4)
plt.draw()
plt.pause(0.001)
def on_train_end(self, logs={}):
if self.save_graph:
plt.savefig('training_acc_loss.png')
class ConfusionMatrix(Callback):
def __init__(self, X_val, Y_val, classes, normalize=False, cmap=plt.cm.Blues, title='Confusion Matrix'):
self.X_val = X_val
self.Y_val = Y_val
self.title = title
self.classes = classes
self.normalize = normalize
self.cmap = cmap
plt.ion()
plt.show()
def on_train_begin(self, logs={}):
pass
# def on_epoch_end(self, epoch, logs={}):
def on_train_end(self, logs={}):
# print('epoch end')
pred = self.model.predict(self.X_val)
max_pred = np.argmax(pred, axis=1)
max_y = np.argmax(self.Y_val, axis=1)
cnf_mat = confusion_matrix(max_y, max_pred)
plt.imshow(cnf_mat, interpolation='nearest', cmap=self.cmap)
plt.title(self.title)
plt.colorbar()
tick_marks = np.arange(len(self.classes))
plt.xticks(tick_marks, self.classes, rotation=45)
plt.yticks(tick_marks, self.classes)
if self.normalize:
cnf_mat = cnf_mat.astype('float') / cnf_mat.sum(axis=1)[:, np.newaxis]
thresh = cnf_mat.max() / 2.
for i, j in itertools.product(range(cnf_mat.shape[0]), range(cnf_mat.shape[1])):
plt.text(j, i, cnf_mat[i, j],
horizontalalignment="center",
color="white" if cnf_mat[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.draw()
plt.pause(0.001)
# def on_train_end(self, logs={}):
# pass
class ConfusionMatrixPlotter():
"""
# Arguments
"""
def __init__(self, cmap=plt.cm.Blues, title='Confusion Matrix'):
plt.ion()
plt.show()
self.title = title
self.cmap = cmap
def update(self, conf_mat, classes, normalize=False):
"""This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(conf_mat, interpolation='nearest', cmap=self.cmap)
plt.title(self.title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
conf_mat = conf_mat.astype('float') / conf_mat.sum(axis=1)[:, np.newaxis]
thresh = conf_mat.max() / 2.
for i, j in itertools.product(range(conf_mat.shape[0]), range(conf_mat.shape[1])):
plt.text(j, i, conf_mat[i, j],
horizontalalignment="center",
color="white" if conf_mat[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.draw()