-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogger.py
More file actions
209 lines (171 loc) · 6.76 KB
/
Logger.py
File metadata and controls
209 lines (171 loc) · 6.76 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""Logging utilities for experiment tracking."""
import json
import logging
import math
import os
from functools import lru_cache
from time import time
from typing import Optional
import wandb
from torch.utils.tensorboard import SummaryWriter
logger_types = {'wandb', 'std', 'tensorboard'}
class BaseLogger:
"""Base class for experiment loggers."""
def __init__(self, run_dir: Optional[str] = None, history_flush_interval: float = 2.0):
self.history = {} # Store history: {metric: [(step, value, timestamp)]}
self.start_time = time()
self.run_dir = None
self.history_path = None
self.history_flush_interval = history_flush_interval
self._last_flush_time = 0.0
if run_dir is not None:
self.set_run_dir(run_dir)
print("Logger enabled at", run_dir)
def log_hparams(self, hparam_dict):
"""Log hyperparameters."""
raise NotImplementedError()
def log_history(self, param, value, step):
"""Log history metric."""
raise NotImplementedError()
def set_run_dir(self, run_dir: str) -> None:
"""Set the run directory and load existing history."""
self.run_dir = run_dir
os.makedirs(run_dir, exist_ok=True)
self.history_path = os.path.join(run_dir, "history.json")
if os.path.exists(self.history_path):
try:
with open(self.history_path, "r", encoding="utf-8") as f:
data = json.load(f)
for metric, entries in data.items():
self.history[metric] = [
(int(step), value, float(ts)) for step, value, ts in entries
]
except (json.JSONDecodeError, ValueError):
pass
def _store_history(self, param, value, step):
"""Store history locally for dashboard access."""
# Sanitize value to prevent NaN/Infinity in JSON
if isinstance(value, float):
if math.isnan(value) or math.isinf(value):
value = None
if param not in self.history:
self.history[param] = []
current_time = time() - self.start_time # Time since training start
self.history[param].append((step, value, current_time))
self._maybe_flush_history()
def _maybe_flush_history(self):
"""Flush history to disk if interval elapsed."""
if not self.history_path:
return
now = time()
if now - self._last_flush_time < self.history_flush_interval:
return
self._last_flush_time = now
try:
serializable = {
metric: [[step, value, ts] for step, value, ts in entries]
for metric, entries in self.history.items()
}
with open(self.history_path, "w", encoding="utf-8") as f:
json.dump(serializable, f)
except (IOError, TypeError):
pass
def log_video(self, video_path):
"""Log video."""
raise NotImplementedError()
def log_image(self, image_path):
"""Log image."""
raise NotImplementedError()
def close(self):
"""Close the logger."""
pass
class WandBLogger(BaseLogger):
"""Weights & Biases logger."""
def __init__(self, entity, project, run_dir: Optional[str] = None):
super().__init__(run_dir=run_dir)
wandb.init(entity=entity, project=project)
def log_hparams(self, hparam_dict):
"""Log hyperparameters."""
for param, value in hparam_dict.items():
try:
wandb.log({param: value})
except Exception:
print(f"Could not log {param}: {value}")
def log_history(self, param, value, step):
"""Log history metric."""
self._store_history(param, value, step)
wandb.log({param: value}, step=step)
def log_video(self, video_path, name="video"):
"""Log video."""
wandb.log({name: wandb.Video(video_path)})
def log_image(self, image_path, name="image"):
"""Log image."""
wandb.log({name: wandb.Image(image_path)})
def close(self):
"""Close the WandB run."""
try:
wandb.finish()
except Exception:
pass
class StdLogger(BaseLogger):
"""Standard output logger."""
def __init__(self, logger=None, run_dir: Optional[str] = None):
super().__init__(run_dir=run_dir)
if logger is not None:
self.log = logger
else:
self.log = logging.getLogger("Barl")
self.log.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s:%(levelname)s: %(message)s')
handler.setFormatter(formatter)
self.log.addHandler(handler)
def log_hparams(self, hparam_dict):
"""Log hyperparameters."""
for param, value in hparam_dict.items():
self.log.info("%s: %s", param, value)
def log_history(self, param, value, step):
"""Log history metric."""
self._store_history(param, value, step)
self.log.info("%s: %s", param, value)
def log_video(self, video_path, name="video"):
"""Log video."""
self.log.warning("videos are not logged by std logger")
def log_image(self, image_path, name="image"):
"""Log image."""
pass
def close(self):
"""Close the logger."""
pass
class TensorboardLogger(BaseLogger):
"""TensorBoard logger."""
def __init__(self, log_dir):
folder_name = log_dir
i = 1
while os.path.exists(folder_name):
folder_name = f"{log_dir}_{i}"
i += 1
self.writer = SummaryWriter(folder_name)
super().__init__(run_dir=self.writer.log_dir)
def log_hparams(self, hparam_dict):
"""Log hyperparameters."""
for param, value in hparam_dict.items():
self.writer.add_text(param, str(value), global_step=0)
with open(os.path.join(self.writer.log_dir, "hparams.txt"), "w", encoding="utf-8") as f:
f.write(f"Timestamp: {time()}\nHyperparameters:\n")
for param, value in hparam_dict.items():
f.write(f"{param}: {value}\n")
def log_history(self, param, value, step):
"""Log history metric."""
self._store_history(param, value, step)
self.writer.add_scalar(param, value, global_step=step)
def log_video(self, video_path, name="video"):
"""Log video."""
self.writer.add_video(name, video_path)
def log_image(self, image_path, name="image"):
"""Log image."""
self.writer.add_image(name, image_path)
def close(self):
"""Close the TensorBoard writer."""
self.writer.close()