forked from hyyh1314/BGShadowNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
135 lines (117 loc) · 3.46 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
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
from logging import getLogger
import pandas as pd
logger = getLogger(__name__)
class TrainLogger(object):
def __init__(self, log_path: str, resume: bool) -> None:
self.log_path = log_path
self.columns = [
"epoch",
"lr",
"train_time[sec]",
"train_loss",
"val_time[sec]",
"val_loss",
]
if resume:
self.df = self._load_log()
else:
self.df = pd.DataFrame(columns=self.columns)
def _load_log(self) -> pd.DataFrame:
try:
df = pd.read_csv(self.log_path)
logger.info("successfully loaded log csv file.")
return df
except FileNotFoundError as err:
logger.exception(f"{err}")
raise err
def _save_log(self) -> None:
self.df.to_csv(self.log_path, index=False)
logger.debug("training logs are saved.")
def update(
self,
epoch: int,
lr: float,
train_time: int,
train_loss: float,
val_time: int,
val_loss: float,
) -> None:
tmp = pd.Series(
[
epoch,
lr,
train_time,
train_loss,
val_time,
val_loss,
],
index=self.columns,
)
self.df = pd.concat([tmp, self.df], ignore_index=True)
self._save_log()
logger.info(
f"epoch: {epoch}\tepoch time[sec]: {train_time + val_time}\tlr: {lr}\t"
f"train loss: {train_loss:.4f}\tval loss: {val_loss:.4f}\t"
)
class TrainLoggerBEDSRNet(object):
def __init__(self, log_path: str, resume: bool) -> None:
self.log_path = log_path
self.columns = [
"epoch",
"lrG",
"lrD",
"train_time[sec]",
"train_g_loss",
"train_d_loss",
"val_time[sec]",
"val_g_loss",
"val_d_loss"
]
if resume:
self.df = self._load_log()
else:
self.df = pd.DataFrame(columns=self.columns)
def _load_log(self) -> pd.DataFrame:
try:
df = pd.read_csv(self.log_path)
logger.info("successfully loaded log csv file.")
return df
except FileNotFoundError as err:
logger.exception(f"{err}")
raise err
def _save_log(self) -> None:
self.df.to_csv(self.log_path, index=False)
logger.debug("training logs are saved.")
def update(
self,
epoch: int,
lrG: float,
lrD: float,
train_time: int,
train_g_loss: float,
train_d_loss: float,
val_time: int,
val_g_loss: float,
val_d_loss: float,
) -> None:
tmp = pd.Series(
[
epoch,
lrG,
lrD,
train_time,
train_g_loss,
train_d_loss,
val_time,
val_g_loss,
val_d_loss,
],
index=self.columns,
)
self.df = pd.concat([tmp, self.df], ignore_index=True)
self._save_log()
logger.info(
f"epoch: {epoch}\tepoch time[sec]: {train_time + val_time}\tlr: {lrG}\t"
f"train g loss: {train_g_loss:.4f}\tval g loss: {val_g_loss:.4f}\t"
f"train d loss: {train_d_loss:.4f}\tval d loss: {val_d_loss:.4f}\t"
)