mirror of
https://github.com/open-mmlab/mmengine.git
synced 2025-06-03 21:54:44 +08:00
[Enhance] Support multiple --cfg-options
. (#759)
This commit is contained in:
parent
bced7d6ef0
commit
fe26c6559e
@ -477,6 +477,19 @@ Config (path: ./example.py): {'model': {'type': 'CustomModel', 'in_channels': [1
|
||||
The standard procedure only supports modifying String, Integer, Floating Point, Boolean, None, List, and Tuple fields from the command line. For the elements of list and tuple instance, each of them must be one of the above seven types.
|
||||
```
|
||||
|
||||
:::{note}
|
||||
The behavior of `DictAction` is similar with `"extend"`. It stores a list, and extends each argument value to the list, like:
|
||||
|
||||
```bash
|
||||
python demo_train.py ./example.py --cfg-options optimizer.type="Adam" --cfg-options model.in_channels="[1, 1, 1]"
|
||||
```
|
||||
|
||||
```
|
||||
Config (path: ./example.py): {'model': {'type': 'CustomModel', 'in_channels': [1, 1, 1]}, 'optimizer': {'type': 'Adam', 'lr': 0.01}}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
### import the custom module
|
||||
|
||||
If we customize a module and register it into the corresponding registry, could we directly build it from the configuration file as the previous [section](#how-to-use-config) does? The answer is "I don't know" since I'm not sure the registration process has been triggered. To solve this "unknown" case, `Config` provides the `custom_imports` function, to make sure your module could be registered as expected.
|
||||
|
@ -477,6 +477,19 @@ Config (path: ./example.py): {'model': {'type': 'CustomModel', 'in_channels': [1
|
||||
上述流程只支持在命令行里修改字符串、整型、浮点型、布尔型、None、列表、元组类型的配置项。对于列表、元组类型的配置,里面每个元素的类型也必须为上述七种类型之一。
|
||||
```
|
||||
|
||||
:::{note}
|
||||
`DictAction` 的行为与 `"extend"` 相似,支持多次传递,并保存在同一个列表中。如
|
||||
|
||||
```bash
|
||||
python demo_train.py ./example.py --cfg-options optimizer.type="Adam" --cfg-options model.in_channels="[1, 1, 1]"
|
||||
```
|
||||
|
||||
```
|
||||
Config (path: ./example.py): {'model': {'type': 'CustomModel', 'in_channels': [1, 1, 1]}, 'optimizer': {'type': 'Adam', 'lr': 0.01}}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
### 导入自定义 Python 模块
|
||||
|
||||
将配置与注册器结合起来使用时,如果我们往注册器中注册了一些自定义的类,就可能会遇到一些问题。因为读取配置文件的时候,这部分代码可能还没有被执行到,所以并未完成注册过程,从而导致构建自定义类的时候报错。
|
||||
|
@ -1029,7 +1029,8 @@ class DictAction(Action):
|
||||
option_string (list[str], optional): Option string.
|
||||
Defaults to None.
|
||||
"""
|
||||
options = {}
|
||||
# Copied behavior from `argparse._ExtendAction`.
|
||||
options = copy.copy(getattr(namespace, self.dest, None) or {})
|
||||
if values is not None:
|
||||
for kv in values:
|
||||
key, val = kv.split('=', maxsplit=1)
|
||||
|
@ -322,6 +322,14 @@ class TestConfig:
|
||||
assert cfg.item2 == dict(a=1, b=0.1, c='x')
|
||||
assert cfg.item3 is False
|
||||
|
||||
# test multiple options
|
||||
args = parser.parse_args([
|
||||
'--options', 'item1.a=1', 'item2.a=2', '--options', 'item2.a=1',
|
||||
'item3=false'
|
||||
])
|
||||
out_dict = {'item1.a': 1, 'item2.a': 1, 'item3': False}
|
||||
assert args.options == out_dict
|
||||
|
||||
def test_validate_py_syntax(self, tmp_path):
|
||||
tmp_cfg = tmp_path / 'tmp_config.py'
|
||||
with open(tmp_cfg, 'w') as f:
|
||||
|
Loading…
x
Reference in New Issue
Block a user