-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
74 lines (62 loc) · 2.3 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
import json
import os.path as osp
import numpy as np
from collections import defaultdict, OrderedDict
from tensorboardX import SummaryWriter
import os
import sys
class Mylog(object):
def __init__(self, save_path='', filename="log.txt"):
self.terminal = sys.stdout
save_path = save_path + '/log.txt'
print(save_path)
self.log = open(save_path, "w")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
_log_path = None
def set_log_path(path, filename='log.txt'):
global _log_path
_log_path = path
# fobj = open(os.path.join(_log_path, filename), 'w')
# fobj.close()
def log(obj, filename='log.txt'):
print(obj)
if _log_path is not None:
with open(os.path.join(_log_path, filename), 'a') as f:
print(obj, file=f)
class ConfigEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, type):
return {'$class': o.__module__ + "." + o.__name__}
elif isinstance(o, Enum):
return {
'$enum': o.__module__ + "." + o.__class__.__name__ + '.' + o.name
}
elif callable(o):
return {
'$function': o.__module__ + "." + o.__name__
}
return json.JSONEncoder.default(self, o)
class Logger(object):
def __init__(self, args, log_dir, **kwargs):
self.logger_path = osp.join(log_dir, 'scalars.json')
self.tb_logger = SummaryWriter(
logdir=osp.join(log_dir, 'tflogger'),
**kwargs,
)
self.log_config(vars(args))
self.scalars = defaultdict(OrderedDict)
def add_scalar(self, key, value, counter):
assert self.scalars[key].get(counter, None) is None, 'counter should be distinct'
self.scalars[key][counter] = value
self.tb_logger.add_scalar(key, value, counter)
def log_config(self, variant_data):
config_filepath = osp.join(osp.dirname(self.logger_path), 'configs.json')
with open(config_filepath, "w") as fd:
json.dump(variant_data, fd, indent=2, sort_keys=True, cls=ConfigEncoder)
def dump(self):
with open(self.logger_path, 'w') as fd:
json.dump(self.scalars, fd, indent=2)