-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.py
59 lines (43 loc) · 1.67 KB
/
logger.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
import json
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib as mpl
from torch.utils.tensorboard import SummaryWriter
mpl.font_manager._rebuild()
plt.rc('font', family='Raleway')
def truncate_colormap(cmapIn='jet', minval=0.0, maxval=1.0, n=100):
cmapIn = plt.get_cmap(cmapIn)
new_cmap = colors.LinearSegmentedColormap.from_list(
'trunc({n},{a:.2f},{b:.2f})'.format(n=cmapIn.name, a=minval, b=maxval),
cmapIn(np.linspace(minval, maxval, n)))
return new_cmap
class Logger:
def __init__(self, model_fname, cfg):
self.cfg = cfg
self.model_fname = model_fname
self.writer = SummaryWriter(log_dir='runs/' + model_fname)
def log_config(self):
self.writer.add_text('Info/Config', json.dumps(self.cfg), 0)
def log_train(self, data, n_iter):
self.writer.add_scalar('Loss/Train', data['loss_train'], n_iter)
def log_eval(self, data, n_iter):
self.writer.add_scalar('Loss/Eval', data['loss_eval'], n_iter)
for k, v in data['Problem_Misclassifications'].items():
self.writer.add_scalar(
'Problem_Misclassifications/' + k,
v,
n_iter
)
self.writer.add_scalar(
'Total_Misclassifications',
data['Total_Misclassifications'],
n_iter
)
def log_eval_reverse(self, data, n_iter):
self.writer.add_scalar('Loss/Eval', data['loss_eval'], n_iter)
def log_custom_reverse_kpi(self, kpi, data, n_iter):
self.writer.add_scalar('Custom/' + kpi, data, n_iter)
def close(self):
self.writer.close()