-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
71 lines (56 loc) · 1.7 KB
/
run.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
import argparse
import os
import sys
import torch
from mmcv import Config
import mmskeleton
from mmskeleton.utils import call_obj, set_attr, get_attr
""" Configuration Structure
argparse_cfg:
<shortcut_name 1>:
bind_to: <full variable path>
help: <help message>
<shortcut_name 2>:
...
processor_cfg:
name: <full processor path>
...
"""
def parse_cfg():
parser = argparse.ArgumentParser(description='Run a processor.')
parser.add_argument('config', help='configuration file path')
if len(sys.argv) <= 1:
args = parser.parse_args()
return
if sys.argv[1] == '-h' or sys.argv[1] == '--help':
args = parser.parse_args()
return
# load argument setting from configuration file
cfg = Config.fromfile(sys.argv[1])
if 'description' in cfg:
parser.description = cfg.description
if 'argparse_cfg' not in cfg:
cfg.argparse_cfg = dict()
for key, info in cfg.argparse_cfg.items():
if 'bind_to' not in info:
continue
default = get_attr(cfg, info['bind_to'])
if 'type' not in info:
if default is not None:
info['type'] = type(default)
kwargs = dict(default=default)
kwargs.update({k: v for k, v in info.items() if k != 'bind_to'})
parser.add_argument('--' + key, **kwargs)
args = parser.parse_args()
# update config from command line
for key, info in cfg.argparse_cfg.items():
if 'bind_to' not in info:
continue
value = getattr(args, key)
set_attr(cfg, info['bind_to'], value)
return cfg
def main():
cfg = parse_cfg()
call_obj(**cfg.processor_cfg)
if __name__ == "__main__":
main()