/
config.py
84 lines (64 loc) · 2.49 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
84
import os.path as osp
import numpy as np
from easydict import EasyDict
from seg.utils import project_root
from seg.utils.serialization import yaml_load
cfg = EasyDict()
cfg.SOURCE = 'GTA'
cfg.TARGET = 'Cityscapes'
cfg.NUM_WORKERS = 4
cfg.DATA_LIST_SOURCE = str(project_root + 'seg/dataset/data/gta5/{}.txt')
cfg.DATA_LIST_TARGET = str(project_root + 'seg/dataset/data/cityscapes/{}.txt')
cfg.DATA_DIRECTORY_SOURCE = str(project_root + 'data/GTA5')
cfg.DATA_DIRECTORY_TARGET = str(project_root + 'data/Cityscapes')
cfg.NUM_CLASSES = 19
cfg.GPU_ID = 0
cfg.TEST = EasyDict()
cfg.TEST.MODE = 'best' # {'single', 'best'}
cfg.TEST.MODEL = ('DeepLabv2',)
cfg.TEST.MODEL_WEIGHT = (1.0,)
cfg.TEST.MULTI_LEVEL = (True,)
cfg.TEST.IMG_MEAN = np.array((104.00698793, 116.66876762, 122.67891434), dtype=np.float32)
cfg.TEST.RESTORE_FROM = ('',)
cfg.TEST.SNAPSHOT_DIR = ('',) # used in 'best' mode
cfg.TEST.SNAPSHOT_STEP = 2000 # used in 'best' mode
cfg.TEST.SNAPSHOT_MAXITER = 120000 # used in 'best' mode
cfg.TEST.SET_TARGET = 'val'
cfg.TEST.BATCH_SIZE_TARGET = 1
cfg.TEST.INPUT_SIZE_TARGET = (1024, 512)
cfg.TEST.OUTPUT_SIZE_TARGET = (2048, 1024)
cfg.TEST.INFO_TARGET = str(project_root + 'seg/dataset/data/cityscapes/data.json')
cfg.TEST.WAIT_MODEL = True
def _merge_a_into_b(a, b):
"""Merge config dictionary a into config dictionary b, clobbering the
options in b whenever they are also specified in a.
"""
if type(a) is not EasyDict:
return
for k, v in a.items():
# a must specify keys that are in b
# if not b.has_key(k):
if k not in b:
raise KeyError(f'{k} is not a valid config key')
# the types must match, too
old_type = type(b[k])
if old_type is not type(v):
if isinstance(b[k], np.ndarray):
v = np.array(v, dtype=b[k].dtype)
else:
raise ValueError(f'Type mismatch ({type(b[k])} vs. {type(v)}) '
f'for config key: {k}')
# recursively merge dicts
if type(v) is EasyDict:
try:
_merge_a_into_b(a[k], b[k])
except Exception:
print(f'Error under config key: {k}')
raise
else:
b[k] = v
def cfg_from_file(filename):
"""Load a config file and merge it into the default options.
"""
yaml_cfg = EasyDict(yaml_load(filename))
_merge_a_into_b(yaml_cfg, cfg)