-
Notifications
You must be signed in to change notification settings - Fork 2
/
aim_wrapper.py
98 lines (76 loc) · 2.79 KB
/
aim_wrapper.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
import copy
import os
import types
from aim import Session
import numpy as np
_g_session = None
_g_context = {}
def init(exp_parent_dir, run_group=None):
return
global _g_session
assert _g_session is None, 'aim_wrapper.init() should be called only once.'
_g_session = Session(repo=os.path.realpath(os.path.abspath(exp_parent_dir)),
experiment=run_group or 'default',
flush_frequency=64)
def close():
return
global _g_session
if _g_session is not None:
_g_session.close()
_g_session = None
def track(*args, **kwargs):
return
global _g_session
global _g_context
assert _g_session is not None, 'aim_wrapper.init() should be called before calling aim_wrapper.track().'
return _g_session.track(*args, **kwargs, **_g_context)
def set_params(params, name, *, excluded_keys=[]):
return
global _g_session
assert _g_session is not None, 'aim_wrapper.init() should be called before calling aim_wrapper.set_params().'
params = make_aim_compatible(params, excluded_keys=excluded_keys)
return _g_session.set_params(params=params, name=name)
# Makes given object compatible with aim for logging.
def make_aim_compatible(item, excluded_keys=[]):
if isinstance(item, (np.ndarray, np.generic)):
return make_aim_compatible(item.tolist())
# E.g. torch.relu, functions, ...
if isinstance(item, (types.BuiltinFunctionType, types.FunctionType)):
return item.__name__
if not isinstance(item, (dict, list, tuple, set,)):
if isinstance(item, (str, int, float, bool,)):
return item
return str(item)
if isinstance(item, (list, tuple, set)):
return type(item)([make_aim_compatible(i) for i in item])
if isinstance(item, dict):
return dict([
(k if isinstance(k, (str, int, tuple)) else str(k), make_aim_compatible(v))
for k, v in item.items()
if k not in excluded_keys
])
assert False
class AimContext:
def __init__(self, context):
self.context = context
def __enter__(self):
global _g_context
self.prev_g_context = _g_context
_g_context = self.context
def __exit__(self, exc_type, exc_val, exc_tb):
global _g_context
_g_context = self.prev_g_context
def get_metric_prefixes():
global _g_context
prefix = ''
if 'phase' in _g_context:
prefix += _g_context['phase'].capitalize()
if 'policy' in _g_context:
prefix += {'sampling': 'Sp', 'option': 'Op'}.get(
_g_context['policy'].lower(), _g_context['policy'].lower()).capitalize()
if len(prefix) == 0:
return '', ''
return prefix + '/', prefix + '__'
def get_context():
global _g_context
return copy.copy(_g_context)