-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
58 lines (42 loc) · 1.91 KB
/
monitor.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
import time
import smtplib
import yaml
from keras.callbacks import Callback
from email.mime.text import MIMEText
from pprint import pformat
class EmailMonitor(Callback):
def __init__(self, recipient_email):
super(EmailMonitor, self).__init__()
self.recipient_email = recipient_email
sender_info = yaml.load(open('secrets.yaml'))
self.sender_email = sender_info['sender']['email_address']
self.sender_pwd = sender_info['sender']['password']
self.previous_stats_email = None
self.current_epoch = 0
def _send_email(self, body):
msg = MIMEText(body)
msg['Subject'] = self.email_header
msg['From'] = self.sender_email
msg['To'] = self.recipient_email
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(self.sender_email, self.sender_pwd)
server.sendmail(self.sender_email, self.recipient_email, msg.as_string())
server.quit()
def on_train_begin(self, logs=None):
self.train_start_time = time.strftime("%Y-%m-%d %H:%M")
self.email_header = "Monitoring for model that started training at {0}\n".format(self.train_start_time)
def on_epoch_end(self, epoch, logs=None):
self.current_epoch += 1
if logs is not None:
current_stats_email = pformat(logs)
email_body = "Current stats (epoch {0}):\n".format(self.current_epoch) + current_stats_email
if self.previous_stats_email:
email_body += "\n\nPrevious stats (epoch {0}):\n".format(self.current_epoch-1) + self.previous_stats_email
self._send_email(email_body)
self.previous_stats_email = current_stats_email
def on_train_end(self, logs=None):
email_body = "Model finished training at {0}".format(time.strftime("%Y-%m-%d %H:%M"))
self._send_email(email_body)