-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_callback.py
143 lines (118 loc) · 4.75 KB
/
email_callback.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
import os
from matplotlib import pyplot as plt
import numpy as np
import yagmail
import tensorflow as tf
from tensorflow import keras
from typing import Optional
class EmailCallback(keras.callbacks.Callback):
message_contents = ""
yag: yagmail.SMTP
def __init__(self, to: Optional[str]=None, train_size=None, val_size=None, wait_for_train_end=False) -> None:
super().__init__()
print("setting up yagmail...")
import credentials
self.yag = yagmail.SMTP(credentials.username, credentials.app_password)
self.to = to if to is not None else credentials.username
if train_size is not None:
self.message_contents += f"size of train dataset: {train_size}\n"
if val_size is not None:
self.message_contents += f"size of validation dataset: {val_size}\n"
self.wait_for_train_end = wait_for_train_end
# Initialize the lists for holding the logs, losses and accuracies
self.losses = []
self.acc = []
self.val_losses = []
self.val_acc = []
self.logs = []
def on_train_begin(self, logs=None):
msg = f"Start Training with Model {self.model.name}\n"
msg += str([f"{k}: {v}" for k,v in logs.items()])
self.message_contents += (msg + "\n---\n")
print(msg)
def on_train_end(self, logs=None):
msg = f"Stop training.\n"
msg += str([f"{k}: {v}" for k,v in logs.items()])
self.message_contents += (msg + "\n---\n")
print(msg)
N = np.arange(0, len(self.losses))
# You can chose the style of your preference
# print(plt.style.available) to see the available options
plt.style.use("seaborn")
# Plot train loss, train acc, val loss and val acc against epochs passed
plt.figure()
plt.plot(N, self.losses, label = "train_loss")
plt.plot(N, self.acc, label = "train_acc")
plt.plot(N, self.val_losses, label = "val_loss")
plt.plot(N, self.val_acc, label = "val_acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend()
temp = self.get_temp_dir()
plt.savefig(os.path.join(temp, "histplot.png"))
plt.close()
if not self.wait_for_train_end:
print("sendming e-mail...")
self.send_message()
def on_test_begin(self, logs=None):
msg = f"Starting testing with model {self.model.name}.\n"
msg += str([f"{k}: {v}" for k,v in logs.items()])
self.message_contents += (msg + "\n---\n")
print(msg)
def on_test_end(self, logs=None):
msg = f"stop testing.\n"
msg += str([f"{k}: {v}" for k,v in logs.items()])
self.message_contents += (msg + "\n---\n")
print(msg)
if self.wait_for_train_end:
print("sending e-mail..")
self.send_message()
def on_epoch_end(self, epoch, logs={}):
log_keys = [f"{k}: {v:7.2f}" for k,v in logs.items() if k != 'loss']
msg = f"The average loss for epoch {epoch} is {logs['loss']:7.2f} with the following metrics: {log_keys}."
self.message_contents += (msg + "\n")
# Append the logs, losses and accuracies to the lists
self.logs.append(logs)
self.losses.append(logs.get('loss'))
self.acc.append(logs.get('acc'))
self.val_losses.append(logs.get('val_loss'))
self.val_acc.append(logs.get('val_acc'))
@staticmethod
def get_temp_dir():
import os
temp_dir = "./temp/"
if not os.path.exists(temp_dir):
os.mkdir(temp_dir)
return temp_dir
def send_message(self):
import datetime
date = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
import os
import shutil
res = ""
temp_dir = self.get_temp_dir()
modelplot_fn = os.path.join(temp_dir, "plot.png")
histplot_fn = os.path.join(temp_dir, "histplot.png")
attachments = [histplot_fn, modelplot_fn] if os.path.exists(histplot_fn) else [modelplot_fn]
keras.utils.plot_model(
self.model,
to_file=modelplot_fn,
show_shapes=True,
rankdir="LR")
res = self.yag.send(
to=self.to,
subject=f"TensorFlow Training Callback {self.model.name} {date}",
contents=self.message_contents,
attachments=attachments)
shutil.rmtree(temp_dir)
print("e-mail sent.")
print(res)
@staticmethod
def get_model_summary(model):
import io
stream = io.StringIO()
model.summary(print_fn=lambda x: stream.write(x + '\n'))
summary_string = stream.getvalue()
stream.close()
return summary_string