-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
102 lines (85 loc) · 3.08 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
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
import os
import json
import matplotlib.pyplot as plt
from cycler import cycler
import numpy as np
from collections import OrderedDict
# x axis of plot
LOG_KEYS = {
"train":"epoch",
"valid":"epoch",
"test": "fname"
}
# y axis of plot
# save datas like loss, f1-score, PSNR, SSIM ..
# can multiple datas
LOG_VALUES = {
"train":["loss", "acc"],
"valid":["acc","valid_acc"],
"test": ["train_acc", "valid_acc"]
}
class Logger:
def __init__(self, log_dir):
if not os.path.exists(log_dir):
os.mkdir(log_dir)
self.log_file = os.path.join(log_dir, 'log.txt')
self.buffers = []
def will_write(self, line):
print(line)
self.buffers.append(line)
def flush(self):
with open(self.log_file, "a", encoding="utf-8") as f:
f.write("\n".join(self.buffers))
f.write("\n")
self.buffers = []
def write(self, line):
self.will_write(line)
self.flush()
def log_write(self, learn_type, **values):
"""log write in buffers
ex ) log_write("train", epoch=1, loss=0.3)
Parmeters:
learn_type : it must be train, valid or test
values : values keys in LOG_VALUES
"""
assert learn_type in ["train", "valid", "test"]
for k in values.keys():
if k not in LOG_VALUES[learn_type] and k != LOG_KEYS[learn_type]:
raise KeyError("%s Log %s keys not in log" % (learn_type, k))
log = "[%s] %s" % (learn_type, json.dumps(values))
self.will_write(log)
if learn_type != "train":
self.flush()
def log_parse(self, log_key):
log_dict = OrderedDict()
with open(self.log_file, "r", encoding="utf-8") as f:
for line in f.readlines():
if len(line) == 1 or not line.startswith("[%s]" % (log_key)):
continue
# line : ~~
line = line[line.find("] ") + 2:] # ~~
line_log = json.loads(line)
train_log_key = line_log[LOG_KEYS[log_key]]
line_log.pop(LOG_KEYS[log_key], None)
log_dict[train_log_key] = line_log
return log_dict
def log_plot(self, log_key,
figsize=(12, 12), title="plot", colors=["C1", "C2"]):
fig = plt.figure(figsize=figsize)
plt.title(title)
plt.legend(LOG_VALUES[log_key], loc="best")
ax = plt.subplot(111)
colors = plt.cm.nipy_spectral(np.linspace(0.1, 0.9, len(LOG_VALUES[log_key])))
ax.set_prop_cycle(cycler('color', colors))
log_dict = self.log_parse(log_key)
x = log_dict.keys()
for keys in LOG_VALUES[log_key]:
if keys not in list(log_dict.values())[0]:
continue
y = [v[keys] for v in log_dict.values()]
label = keys + ", max : %f" % (max(y))
ax.plot(x, y, marker="o", linestyle="solid", label=label)
if max(y) > 1:
ax.set_ylim([min(y) - 1, y[0] + 1])
ax.legend(fontsize=30)
plt.show()