mmcv icon indicating copy to clipboard operation
mmcv copied to clipboard

Support using variables in base config directly as normal variables.

Open mzr1996 opened this issue 3 years ago • 7 comments

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.

mzr1996 avatar Jan 10 '22 03:01 mzr1996

Need to add test to show case the new features

innerlee avatar Jan 24 '22 08:01 innerlee

Need to add test to show case the new features

Done

mzr1996 avatar Jan 28 '22 03:01 mzr1996

_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)
)

zhouzaida avatar Jan 28 '22 15:01 zhouzaida

Sounds good, I will try it.

mzr1996 avatar Jan 29 '22 03:01 mzr1996

_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.

mzr1996 avatar Jan 29 '22 04:01 mzr1996

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

zhouzaida avatar Jan 29 '22 06:01 zhouzaida

In the doc, don't forget to mark it as python-config-only feature.

innerlee avatar Jan 30 '22 13:01 innerlee