-
Notifications
You must be signed in to change notification settings - Fork 18
/
parse_config.py
150 lines (122 loc) · 5.57 KB
/
parse_config.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import logging
from pathlib import Path
from functools import reduce
from operator import getitem
from datetime import datetime
from everything_at_once.logger import setup_logging
from everything_at_once.utils import write_yaml, read_config
import time
import inspect
class ConfigParser:
def __init__(self, args, options='', timestamp=True, test=False, parse_from_string=None):
# parse default and custom cli options
for opt in options:
args.add_argument(*opt.flags, default=None, type=opt.type)
if parse_from_string is not None:
import shlex
args = args.parse_args(shlex.split(parse_from_string))
else:
args = args.parse_args()
if args.device:
os.environ["CUDA_VISIBLE_DEVICES"] = args.device
if args.resume is None:
msg_no_cfg = "Configuration file need to be specified. Add '-c config.json', for example."
assert args.config is not None, msg_no_cfg
self.cfg_fname = Path(args.config)
config = read_config(self.cfg_fname)
self.resume = None
else:
self.resume = Path(args.resume)
resume_cfg_fname = self.resume.parent / 'config.yaml'
config = read_config(resume_cfg_fname)
if args.config is not None:
config.update(read_config(Path(args.config)))
# update neptune
if not test and args.neptune:
config['trainer']['neptune'] = True
if args.config is not None:
config['name'] = os.path.splitext(os.path.basename(args.config))[0]
# load config file and apply custom cli options
self.config = _update_config(config, options, args)
# set save_dir where trained model and log will be saved.
save_dir = Path(self.config['trainer']['save_dir'])
timestamp = datetime.now().strftime(r'%m%d_%H%M%S') if timestamp else ''
exper_name = self.config['name']
self._save_dir = save_dir / 'models' / exper_name / timestamp
self._web_log_dir = save_dir / 'web' / exper_name / timestamp
self._log_dir = save_dir / 'log' / exper_name / timestamp
if not test:
self.save_dir.mkdir(parents=True, exist_ok=True)
self.log_dir.mkdir(parents=True, exist_ok=True)
# if set, remove all previous experiments with the current config
if vars(args).get("purge_exp_dir", False):
for dirpath in (self._save_dir, self._log_dir, self._web_log_dir):
config_dir = dirpath.parent
existing = list(config_dir.glob("*"))
print(f"purging {len(existing)} directories from config_dir...")
tic = time.time()
os.system(f"rm -rf {config_dir}")
print(f"Finished purge in {time.time() - tic:.3f}s")
# save updated config file to the checkpoint dir
if not test:
write_yaml(self.config, self.save_dir / 'config.yaml')
# configure logging module
setup_logging(self.log_dir)
self.log_levels = {
0: logging.WARNING,
1: logging.INFO,
2: logging.DEBUG
}
def initialize(self, name, module, *args, index=None, **kwargs):
"""
finds a function handle with the name given as 'type' in config, and returns the
instance initialized with corresponding keyword args given as 'args'.
"""
if index is None:
module_name = self[name]['type']
module_args = dict(self[name]['args'])
assert all([k not in module_args for k in kwargs]), 'Overwriting kwargs given in config file is not allowed'
module_args.update(kwargs)
else:
module_name = self[name][index]['type']
module_args = dict(self[name][index]['args'])
# if parameter not in config subdict, then check if it's in global config.
signature = inspect.signature(getattr(module, module_name).__init__)
for param in signature.parameters.keys():
if param not in module_args and param in self.config:
module_args[param] = self[param]
return getattr(module, module_name)(*args, **module_args)
def __getitem__(self, name):
return self.config[name]
def get_logger(self, name, verbosity=2):
msg_verbosity = 'verbosity option {} is invalid. Valid options are {}.'.format(verbosity,
self.log_levels.keys())
assert verbosity in self.log_levels, msg_verbosity
logger = logging.getLogger(name)
logger.setLevel(self.log_levels[verbosity])
return logger
@property
def save_dir(self):
return self._save_dir
@property
def log_dir(self):
return self._log_dir
# helper functions used to update config dict with custom cli options
def _update_config(config, options, args):
for opt in options:
value = getattr(args, _get_opt_name(opt.flags))
if value is not None:
_set_by_path(config, opt.target, value)
return config
def _get_opt_name(flags):
for flg in flags:
if flg.startswith('--'):
return flg.replace('--', '')
return flags[0].replace('--', '')
def _set_by_path(tree, keys, value):
"""Set a value in a nested object in tree by sequence of keys."""
_get_by_path(tree, keys[:-1])[keys[-1]] = value
def _get_by_path(tree, keys):
"""Access a nested object in tree by sequence of keys."""
return reduce(getitem, keys, tree)