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

[Tools] Support replacing the ${key} with the value of cfg.key #7492

Merged
merged 22 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 15 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
3 changes: 2 additions & 1 deletion mmdet/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from .compat_config import compat_cfg
from .logger import get_caller_name, get_root_logger, log_img_scale
from .misc import find_latest_checkpoint, update_data_root
from .replace_cfg_vals import replace_cfg_vals
from .setup_env import setup_multi_processes

__all__ = [
'get_root_logger', 'collect_env', 'find_latest_checkpoint',
'update_data_root', 'setup_multi_processes', 'get_caller_name',
'log_img_scale', 'compat_cfg'
'log_img_scale', 'compat_cfg', 'replace_cfg_vals'
]
59 changes: 59 additions & 0 deletions mmdet/utils/replace_cfg_vals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) OpenMMLab. All rights reserved.
import re

from mmcv.utils import Config


def replace_cfg_vals(ori_cfg):
Czm369 marked this conversation as resolved.
Show resolved Hide resolved
Czm369 marked this conversation as resolved.
Show resolved Hide resolved
"""Replace the string "${key}" with the corresponding value.

Replace the "${key}" with the value of ori_cfg.key in the config. And
support replacing the chained ${key}. Such as, replace "${key0.key1}"
with the value of cfg.key0.key1. Code is modified from `vars.py
< https://github.com/microsoft/SoftTeacher/blob/main/ssod/utils/vars.py>`_ # noqa: E501

Args:
ori_cfg (mmcv.utils.config.Config):
The origin config with "${key}" generated from a file.

Returns:
updated_cfg [mmcv.utils.config.Config]:
Czm369 marked this conversation as resolved.
Show resolved Hide resolved
The config with "${key}" replaced by the corresponding value.
"""

def get_value(cfg, key):
chained_key = key.split('.')
if len(chained_key) == 1:
return cfg[chained_key[0]]
else:
return get_value(cfg[chained_key[0]], '.'.join(chained_key[1:]))
Czm369 marked this conversation as resolved.
Show resolved Hide resolved

def replace_value(cfg):
if isinstance(cfg, dict):
return {key: replace_value(value) for key, value in cfg.items()}
elif isinstance(cfg, list):
return [replace_value(item) for item in cfg]
elif isinstance(cfg, tuple):
return tuple([replace_value(item) for item in cfg])
elif isinstance(cfg, str):
# replace the "${key}" with cfg.key
keys = pattern_key.findall(cfg)
hhaAndroid marked this conversation as resolved.
Show resolved Hide resolved
values = [get_value(ori_cfg, key[2:-1]) for key in keys]
# only support replacing one "${key}" for dict, list, or tuple
for key, value in zip(keys, values):
cfg = value if len(keys) == 1 and len(
keys[0]) == len(cfg) else cfg.replace(key, str(value))
Czm369 marked this conversation as resolved.
Show resolved Hide resolved
return cfg
else:
return cfg
Czm369 marked this conversation as resolved.
Show resolved Hide resolved

# the pattern of string "${key}"
pattern_key = re.compile(r'\$\{[a-zA-Z\d_.]*\}')
# the type of ori_cfg._cfg_dict is mmcv.utils.config.ConfigDict
updated_cfg = Config(
replace_value(ori_cfg._cfg_dict), filename=ori_cfg.filename)
# replace the model with model_wrapper
if updated_cfg.get('model_wrapper', None) is not None:
updated_cfg.model = updated_cfg.model_wrapper
updated_cfg.pop('model_wrapper')
return updated_cfg
61 changes: 61 additions & 0 deletions tests/test_utils/test_replace_cfg_vals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os.path as osp
import tempfile
from copy import deepcopy

from mmcv.utils import Config

from mmdet.utils import replace_cfg_vals


def test_replace_cfg_vals():
temp_file = tempfile.NamedTemporaryFile()
Czm369 marked this conversation as resolved.
Show resolved Hide resolved
cfg_path = f'{temp_file.name}.py'
with open(cfg_path, 'w') as f:
f.write('configs')

ori_cfg_dict = dict()
ori_cfg_dict['cfg_name'] = osp.basename(temp_file.name)
ori_cfg_dict['work_dir'] = 'work_dirs/${cfg_name}/${percent}/${fold}'
ori_cfg_dict['percent'] = 5
ori_cfg_dict['fold'] = 1
ori_cfg_dict['model_wrapper'] = dict(
type='SoftTeacher', detector='${model}')
ori_cfg_dict['model'] = dict(
type='FasterRCNN',
backbone=dict(type='ResNet'),
neck=dict(type='FPN'),
rpn_head=dict(type='RPNHead'),
roi_head=dict(type='StandardRoIHead'),
train_cfg=dict(
rpn=dict(
assigner=dict(type='MaxIoUAssigner'),
sampler=dict(type='RandomSampler'),
),
rpn_proposal=dict(nms=dict(type='nms', iou_threshold=0.7)),
rcnn=dict(
assigner=dict(type='MaxIoUAssigner'),
sampler=dict(type='RandomSampler'),
),
),
test_cfg=dict(
rpn=dict(nms=dict(type='nms', iou_threshold=0.7)),
rcnn=dict(nms=dict(type='nms', iou_threshold=0.5)),
),
)
ori_cfg_dict['iou_threshold'] = dict(
rpn_proposal_nms='${model.train_cfg.rpn_proposal.nms.iou_threshold}',
test_rpn_nms='${model.test_cfg.rpn.nms.iou_threshold}',
test_rcnn_nms='${model.test_cfg.rcnn.nms.iou_threshold}',
)
ori_cfg_dict['a'] = 'xxxxx${b}xxxxx'
ori_cfg_dict['b'] = 'Hello, world!'

ori_cfg = Config(ori_cfg_dict, filename=cfg_path)
updated_cfg = replace_cfg_vals(deepcopy(ori_cfg))

assert updated_cfg.work_dir \
== f'work_dirs/{osp.basename(temp_file.name)}/5/1'
assert updated_cfg.model.detector == ori_cfg.model
assert updated_cfg.iou_threshold.rpn_proposal_nms \
== ori_cfg.model.train_cfg.rpn_proposal.nms.iou_threshold
assert updated_cfg.a == 'xxxxxHello, world!xxxxx'
8 changes: 6 additions & 2 deletions tools/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from mmdet.apis import init_random_seed, set_random_seed, train_detector
from mmdet.datasets import build_dataset
from mmdet.models import build_detector
from mmdet.utils import (collect_env, get_root_logger, setup_multi_processes,
update_data_root)
from mmdet.utils import (collect_env, get_root_logger, replace_cfg_vals,
setup_multi_processes, update_data_root)


def parse_args():
Expand Down Expand Up @@ -142,6 +142,10 @@ def main():
# use config filename as default work_dir if cfg.work_dir is None
cfg.work_dir = osp.join('./work_dirs',
osp.splitext(osp.basename(args.config))[0])

# replace the ${key} with the value of cfg.key
cfg = replace_cfg_vals(cfg)
Czm369 marked this conversation as resolved.
Show resolved Hide resolved

if args.resume_from is not None:
cfg.resume_from = args.resume_from
cfg.auto_resume = args.auto_resume
Expand Down