-
Notifications
You must be signed in to change notification settings - Fork 31
/
trainingmonitor.py
69 lines (62 loc) · 2.35 KB
/
trainingmonitor.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
# encoding:utf-8
import numpy as np
from pathlib import Path
import matplotlib.pyplot as plt
from tools import load_json
from tools import save_json
plt.switch_backend('agg') # 防止ssh上绘图问题
class TrainingMonitor():
def __init__(self, file_dir, arch, add_test=False):
'''
:param startAt: 重新开始训练的epoch点
'''
if isinstance(file_dir, Path):
pass
else:
file_dir = Path(file_dir)
file_dir.mkdir(parents=True, exist_ok=True)
self.arch = arch
self.file_dir = file_dir
self.H = {}
self.add_test = add_test
self.json_path = file_dir / (arch + "_training_monitor.json")
def reset(self,start_at):
if start_at > 0:
if self.json_path is not None:
if self.json_path.exists():
self.H = load_json(self.json_path)
for k in self.H.keys():
self.H[k] = self.H[k][:start_at]
def epoch_step(self, logs={}):
for (k, v) in logs.items():
l = self.H.get(k, [])
# np.float32会报错
if not isinstance(v, np.float):
v = round(float(v), 4)
l.append(v)
self.H[k] = l
# 写入文件
if self.json_path is not None:
save_json(data = self.H,file_path=self.json_path)
# 保存train图像
if len(self.H["loss"]) == 1:
self.paths = {key: self.file_dir / (self.arch + f'_{key.upper()}') for key in self.H.keys()}
if len(self.H["loss"]) > 1:
# 指标变化
# 曲线
# 需要成对出现
keys = [key for key, _ in self.H.items() if '_' not in key]
for key in keys:
N = np.arange(0, len(self.H[key]))
plt.style.use("ggplot")
plt.figure()
plt.plot(N, self.H[key], label=f"train_{key}")
plt.plot(N, self.H[f"valid_{key}"], label=f"valid_{key}")
if self.add_test:
plt.plot(N, self.H[f"test_{key}"], label=f"test_{key}")
plt.legend()
plt.xlabel("Epoch #")
plt.ylabel(key)
plt.title(f"Training {key} [Epoch {len(self.H[key])}]")
plt.savefig(str(self.paths[key]))
plt.close()