-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
76 lines (58 loc) · 2.88 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
import os
import sys
from pathlib import Path
import argparse
import importlib
import yaml
def construct_python_tuple(loader, node):
return tuple(loader.construct_sequence(node))
yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:python/tuple', construct_python_tuple)
class DotDict(dict):
"""dot.notation access to dictionary attributes
Reference:
https://stackoverflow.com/questions/2352181/how-to-use-a-dot-to-access-members-of-dictionary/23689767#23689767
"""
__getattr__ = dict.get # returns None if missing key, don't use getattr() with default!
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def save_yaml(filepath, content, width=120):
with open(filepath, "w") as f:
yaml.dump(content, f, width=width)
def load_yaml(filepath):
with open(filepath, "r") as f:
content = yaml.safe_load(f)
return content
class Config(DotDict):
def __init__(self, config_file):
if config_file.endswith('.yaml'):
cfg = load_yaml(config_file)
else:
sys.path.append(str(Path(config_file).parent))
cfg = importlib.import_module(Path(config_file).stem).cfg
super().__init__(cfg)
def update(self, cfg):
if isinstance(cfg, str) or isinstance(cfg, Path):
cfg = self.__class__(cfg)
super().update(cfg)
def save_yaml(self, filename=None, width=120):
if filename is None:
os.makedirs(self.out_dir, exist_ok=True)
filename = Path(self.out_dir) / "config.yaml"
save_yaml(filename, dict(self), width=width)
def __repr__(self):
s = [f"{self.__class__.__name__}(\n"]
for k, v in self.items():
s.append(f" {k} = '{v}',\n" if isinstance(v, str) else f" {k} = {v},\n")
return "".join(s) + ")"
parser = argparse.ArgumentParser(description="Command line arguments supersede config file")
parser.add_argument("-c", "--config_file", help="config file path")
parser.add_argument("-m", "--mode", default='train', help="mode")
parser.add_argument("-f", "--use_folds", nargs="+", type=int, help="cfg.use_folds")
parser.add_argument("-v", "--batch_verbose", help="mbatch frequency of progress outputs")
parser.add_argument("--epochs", type=int)
parser.add_argument("--size", nargs="+", type=int, help="model input size (int) or (height, width)")
parser.add_argument("--metrics", nargs="+", type=str, help="metrics (str) keywords or aliases")
parser.add_argument("--dropout_ps", nargs="+", type=float, help="Dropout probabilities for head Dropout layers")
parser.add_argument("--lin_ftrs", nargs="+", type=int, help="None|output dims of linear (FC) head layers before output layer")
parser.add_argument("--betas", nargs=2, type=float, help="Adam (β1, β2) or SGD (μ, 1 - τ) parameters")
parser.add_argument("--set", nargs=2, action='append', help="(key, value) pair for setting (non-iterable) cfg attributes")