Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Support config override with argparse #228

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions paddle3d/apis/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import codecs
import os
import six
from ast import literal_eval
from collections.abc import Iterable, Mapping
from typing import Any, Dict, Generic, Optional

Expand Down Expand Up @@ -78,10 +80,11 @@ def __init__(self,
else:
raise RuntimeError('Config file should in yaml format!')

self.update(learning_rate=learning_rate,
batch_size=batch_size,
iters=iters,
epochs=epochs)
self.update(
learning_rate=learning_rate,
batch_size=batch_size,
iters=iters,
epochs=epochs)

def _update_dic(self, dic: Dict, base_dic: Dict):
'''Update config from dic based base_dic
Expand Down Expand Up @@ -120,7 +123,8 @@ def update(self,
learning_rate: Optional[float] = None,
batch_size: Optional[int] = None,
iters: Optional[int] = None,
epochs: Optional[int] = None):
epochs: Optional[int] = None,
opts: Optional[list] = None):
'''Update config'''

if learning_rate is not None:
Expand All @@ -135,6 +139,26 @@ def update(self,
if epochs is not None:
self.dic['epochs'] = epochs

if opts is not None:
if len(opts) % 2 != 0 or len(opts) == 0:
raise ValueError(
"Command line options config `--opts` format error! It should be even length like: k1 v1 k2 v2 ... Please check it: {}"
.format(opts))
for key, value in zip(opts[0::2], opts[1::2]):
if isinstance(value, six.string_types):
try:
value = literal_eval(value)
except ValueError:
pass
except SyntaxError:
pass
key_list = key.split('.')
dic = self.dic
for subkey in key_list[:-1]:
dic.setdefault(subkey, dict())
dic = dic[subkey]
dic[key_list[-1]] = value

@property
def batch_size(self) -> int:
return self.dic.get('batch_size', 1)
Expand Down Expand Up @@ -282,8 +306,8 @@ def _load_object(self, obj: Generic, recursive: bool = True) -> Any:
if recursive:
params = {}
for key, val in dic.items():
params[key] = self._load_object(obj=val,
recursive=recursive)
params[key] = self._load_object(
obj=val, recursive=recursive)
else:
params = dic
try:
Expand Down
5 changes: 4 additions & 1 deletion tools/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def parse_args():
help='epochs for training',
type=int,
default=None)
parser.add_argument(
'--opts', help='override config options.', default=None, nargs='+')
parser.add_argument(
'--keep_checkpoint_max',
dest='keep_checkpoint_max',
Expand Down Expand Up @@ -154,7 +156,8 @@ def main(args):
learning_rate=args.learning_rate,
batch_size=args.batch_size,
iters=args.iters,
epochs=args.epochs)
epochs=args.epochs,
opts=args.opts)

if cfg.train_dataset is None:
raise RuntimeError(
Expand Down