mmcv
mmcv copied to clipboard
Support using variables in base config directly as normal variables.
Motivation
A config feature improvement draft. You can directly use the variables in the base config as normal variables virtually in the Python format config file.
Modification
Parse the base config before loading the entire config file. And use eval
instead of import_module
to load configs.
BC-breaking (Optional)
Should be no BC-breaking. It can pass the original unit tests. But please check it carefully.
Use cases (Optional)
base.py
:
list_variable = [0, 1, 2]
tuple_variable = (0, 1)
dict_variable = dict(
a=dict(first=1, second=2),
b=dict(first=1, second=2)
)
final.py
:
_base_ = './base.py'
_base_.list_variable[1] = 5
x, y = _base_.tuple_variable
dict_variable = dict(
a=dict(third=3, _delete_=True),
b=dict(third=3)
)
>>> from mmcv import Config
>>> cfg = Config.fromfile("final.py")
>>> print(cfg.pretty_text)
list_variable = [0, 5, 2]
tuple_variable = (0, 1)
dict_variable = dict(
a=dict(third=3),
b=dict(first=1, second=2, third=3)
)
x = 0
y = 1
Checklist
Before PR:
- [x] I have read and followed the workflow indicated in the CONTRIBUTING.md to create this PR.
- [x] Pre-commit or linting tools indicated in CONTRIBUTING.md are used to fix the potential lint issues.
- [x] Bug fixes are covered by unit tests, the case that causes the bug should be added in the unit tests.
- [x] New functionalities are covered by complete unit tests. If not, please add more unit test to ensure the correctness.
- [ ] The documentation has been modified accordingly, including docstring or example tutorials.
After PR:
- [x] If the modification has potential influence on downstream or other related projects, this PR should be tested with some of those projects, like MMDet or MMCls.
- [x] CLA has been signed and all committers have signed the CLA in this PR.
Need to add test to show case the new features
Need to add test to show case the new features
Done
_base_ = './base.py'
list_variable[1] = 5
x, y = tuple_variable
dict_variable = dict(
a=dict(third=3, _delete_=True),
b=dict(third=3)
)
Does the usage of list_variable[1] = 5
confuse the users?
Would the follow design be more appropriate?
_base_ = './base.py'
_base_.list_variable[1] = 5
x, y = tuple_variable
dict_variable = dict(
a=dict(third=3, _delete_=True),
b=dict(third=3)
)
Sounds good, I will try it.
_base_ = './base.py' list_variable[1] = 5 x, y = tuple_variable dict_variable = dict( a=dict(third=3, _delete_=True), b=dict(third=3) )
Does the usage of
list_variable[1] = 5
confuse the users?Would the follow design be more appropriate?
_base_ = './base.py' _base_.list_variable[1] = 5 x, y = tuple_variable dict_variable = dict( a=dict(third=3, _delete_=True), b=dict(third=3) )
Done, and the usage and the unit tests are updated.
We also need to add the example in the following documentation.
- https://github.com/open-mmlab/mmcv/blob/master/docs/en/understand_mmcv/config.md
- https://github.com/open-mmlab/mmcv/blob/master/docs/zh_cn/understand_mmcv/config.md
In the doc, don't forget to mark it as python-config-only feature.