-
Notifications
You must be signed in to change notification settings - Fork 23
/
util.py
49 lines (34 loc) · 1.4 KB
/
util.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
from datetime import datetime
import json
import logging
import os
def get_time():
return datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
# noinspection SpellCheckingInspection
def prepare_dirs_and_logger(config):
formatter = logging.Formatter('"%(asctime)s:%(levelname)s::%(message)s')
logger = logging.getLogger()
for handler in logger.handlers:
logger.removeHandler(handler)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
if config.load_path:
if config.load_path.startswith(config.log_dir):
config.model_dir = config.load_path
else:
config.model_name = '{}_{}'.format(config.dataset, config.load_path)
else:
config.model_name = '{}_{}'.format(config.dataset, get_time())
if not hasattr(config, 'model_dir'):
config.model_dir = os.path.join(config.log_dir, config.model_name)
config.data_path = os.path.join(config.data_dir, config.dataset)
for path in [config.log_dir, config.data_dir, config.model_dir]:
if not os.path.exists(path):
os.makedirs(path)
def save_config(config):
param_path = os.path.join(config.model_dir, 'params.json')
print('[*] MODEL dir: %s' % config.model_dir)
print('[*] PARAM path: %s' % param_path)
with open(param_path, 'w') as f:
json.dump(config.__dict__, f, indent=4, sort_keys=True)