-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
83 lines (71 loc) · 2.62 KB
/
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
from pprint import pprint
from collections import OrderedDict
class Config:
def __init__(self):
return
def load_config(self, config_path):
print('Config:',config_path)
with open(config_path, 'r') as f:
config_list = f.readlines()
# all, spec, class, seg
self.config_type_dict = {}
for each in config_list:
if len(each) > 4 and each[4] == '#':
tmp_flag = each.split(' ')[1]
self.config_type_dict[tmp_flag] = {}
if each[0] in ['#','*']:
continue
elif each == '\n':
continue
else:
kv = each.strip().split('=')
k = kv[0].strip()
v = kv[1].strip()
if v[0] == "'":
v = str(v[1:-1])
elif v[0] == 'T':
v = True
elif v[0] == 'F':
v = False
elif v[0] == 'N':
v = None
elif v[0] == '[':
v_split = v[1:-1].split(',')
v = [float(each.strip()) for each in v_split]
elif v.isdigit():
v = int(v)
else:
v = float(v)
self.config_type_dict[tmp_flag][k] = v
config_type_dict_keys = list(self.config_type_dict.keys())
for config_type_dict_key in config_type_dict_keys:
self.__dict__.update(self.config_type_dict[config_type_dict_key])
# spec config for task
def _spec(self, kwargs):
print_dict = OrderedDict()
state_dict = self._state_dict()
for k, v in kwargs.items():
if k not in state_dict:
print('%s is not in config dict, plz check'%k)
setattr(self, k, v)
if isinstance(v, list) and len(v) > 10:
continue
else:
print_dict[k] = v
state_dict = self._state_dict()
state_dict.pop('config_type_dict')
state_dict_keys = sorted(list(state_dict.keys()))
save_config_save = OrderedDict()
for key in state_dict_keys:
save_config_save[key] = state_dict[key]
if state_dict['mode'] == 'train':
print('======user config========')
pprint(print_dict)
print('==========end============')
return save_config_save
else:
return
def _state_dict(self):
return {k: getattr(self, k) for k, _ in self.__dict__.items() \
if not k.startswith('_')}
opt = Config()