/
config.py
151 lines (120 loc) · 3.42 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import argparse
from pathlib import Path
import yaml
class Config:
pass
global_config = Config()
_config_argparse_fields = []
def setup_arg_parser(parser: argparse.ArgumentParser):
sample_config = open('cfg_sample.yaml', 'w+t')
sample_config.write('## Uncomment options to override command line values.\n\n\n')
def add_config_argument(
argname: str,
default_value,
help: str,
type,
parser: argparse.ArgumentParser,
**kwargs):
_config_argparse_fields.append(argname)
setattr(global_config, argname, default_value)
parser.add_argument(
f'--{argname}',
type=type,
help=help,
**kwargs)
sample_config.write(
f'\
# {help}\n\
# {argname}: {default_value}\n\n'
)
add_config_argument(
'expand_per_width',
2,
help='How many blocks to concatenate per width.',
type=int,
parser=parser)
add_config_argument(
'expand_per_height',
1,
help='How many blocks to concatenate per height.',
type=int,
parser=parser)
add_config_argument(
'latent_dim',
128,
help='Size of the latent dimension.',
type=int,
parser=parser)
add_config_argument(
'beta',
2.0,
help='KL loss weight.',
type=float,
parser=parser)
add_config_argument(
'gamma',
0.05,
help='Entropy loss weight.',
type=float,
parser=parser)
add_config_argument(
'nlayers',
4,
help='How many CNN layers the model should have.',
type=int,
parser=parser)
add_config_argument(
'nvaes',
2,
help='How many VAEs the module should include.',
type=int,
parser=parser)
add_config_argument(
'epoch_length',
500,
help='How many parameter updates an epoch should contain.',
type=int,
parser=parser)
add_config_argument(
'stage_length',
20,
help='How many epochs a single stage should last for.',
type=int,
parser=parser)
add_config_argument(
'nstages',
100,
help='How many stages to execute in total.',
type=int,
parser=parser)
add_config_argument(
'clevr',
None,
help='Use the clevr dataset, and find it at this path.',
type=str,
parser=parser)
sample_config.close()
def update_config_from_parsed_args(args):
for field_name in _config_argparse_fields:
val = getattr(args, field_name)
if val:
setattr(global_config, field_name, val)
def update_config_from_yaml(cfg: Path):
doc = yaml.load(cfg.read_text())
for (k, v) in doc.items():
assert hasattr(global_config, k)
setattr(global_config, k, v)
def dump_config_to_yaml(cfg: Path):
with cfg.open('w+t') as f:
for field_name in _config_argparse_fields:
val = getattr(global_config, field_name)
if not val:
continue
f.write(f'{field_name}: {val}\n')
# These options probably don't need to be set often.
global_config.num_examples = 16
global_config.batch_size = 32
global_config.checkpoint_dir = Path('checkpoints')
global_config.img_width = 28
global_config.img_height = 28
global_config.img_channels = 1