forked from Worldie-com/Self-Driving-Car
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myhistory.py
80 lines (63 loc) · 2.27 KB
/
myhistory.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
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
from tensorflow.keras.callbacks import Callback
style.use('ggplot')
'''
Usage:
custom_history = MyHistory(model_name='directory\\name', win_size=64)
model.fit(x_train, y_train, epochs=20, batch_size=32,
validation_split=0.2, ..., callbacks=[custom_history])
'''
class MyHistory(Callback):
def __init__(self, model_name='', win_size=32):
super(MyHistory, self).__init__()
self.model_name = model_name
self.win_size = win_size
def on_train_begin(self, logs={}):
self.batch_acc = []
self.batch_loss = []
self.epoch_acc = []
self.epoch_loss = []
self.val_acc = []
self.val_loss = []
def on_batch_end(self, batch, logs={}):
self.batch_acc.append(logs.get('accuracy'))
self.batch_loss.append(logs.get('loss'))
def on_epoch_end(self, epoch, logs={}):
self.epoch_acc.append(logs.get('accuracy'))
self.epoch_loss.append(logs.get('loss'))
self.val_acc.append(logs.get('val_accuracy'))
self.val_loss.append(logs.get('val_loss'))
def on_train_end(self, logs={}):
self.model.save(f'{self.model_name}model_from_callback.h5')
self.plot_training_history()
def plot_training_history(self):
plt.figure(figsize=(12, 7))
plt.subplot(221)
x = pd.DataFrame(self.batch_acc)
x = x.rolling(window=self.win_size).mean().dropna()
plt.plot(x, label='batch_acc')
plt.xlabel('Batch')
plt.ylabel('Accuracy')
plt.legend()
plt.subplot(222)
plt.plot(self.epoch_acc, label='epoch_acc')
plt.plot(self.val_acc, label='val_acc')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.subplot(223)
x = pd.DataFrame(self.batch_loss)
x = x.rolling(window=self.win_size).mean().dropna()
plt.plot(x, label='batch_loss')
plt.xlabel('Batch')
plt.ylabel('Loss')
plt.legend()
plt.subplot(224)
plt.plot(self.epoch_loss, label='epoch_loss')
plt.plot(self.val_loss, label='val_loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.savefig(f'{self.model_name}history.png')