config.md 4.38 KB
Newer Older
Zaida Zhou's avatar
Zaida Zhou committed
1
## Config
Kai Chen's avatar
Kai Chen committed
2
3

`Config` class is used for manipulating config and config files. It supports
Kai Chen's avatar
Kai Chen committed
4
loading configs from multiple file formats including **python**, **json** and **yaml**.
Kai Chen's avatar
Kai Chen committed
5
6
7
8
9
10
It provides dict-like apis to get and set values.

Here is an example of the config file `test.py`.

```python
a = 1
11
b = dict(b1=[0, 1, 2], b2=None)
Kai Chen's avatar
Kai Chen committed
12
13
14
15
16
17
18
c = (1, 2)
d = 'string'
```

To load and use configs

```python
19
20
21
22
23
24
25
26
>>> cfg = Config.fromfile('test.py')
>>> print(cfg)
>>> dict(a=1,
...      b=dict(b1=[0, 1, 2], b2=None),
...      c=(1, 2),
...      d='string')
```

27
28
29
30
31
32
33
34
35
36
37
38
For all format configs, some predefined variables are supported. It will convert the variable in `{{ var }}` with its real value.

Currently, it supports four predefined variables:

`{{ fileDirname }}` - the current opened file's dirname, e.g. /home/your-username/your-project/folder

`{{ fileBasename }}` - the current opened file's basename, e.g. file.ext

`{{ fileBasenameNoExtension }}` - the current opened file's basename with no file extension, e.g. file

`{{ fileExtname }}` - the current opened file's extension, e.g. .ext

Kai Chen's avatar
Kai Chen committed
39
These variable names are referred from [VS Code](https://code.visualstudio.com/docs/editor/variables-reference).
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

Here is one examples of config with predefined variables.

`config_a.py`

```python
a = 1
b = './work_dir/{{ fileBasenameNoExtension }}'
c = '{{ fileExtname }}'
```

```python
>>> cfg = Config.fromfile('./config_a.py')
>>> print(cfg)
>>> dict(a=1,
...      b='./work_dir/config_a',
...      c='.py')
```

59
60
61
62
63
64
65
66
67
68
69
For all format configs, inheritance is supported. To reuse fields in other config files,
specify `_base_='./config_a.py'` or a list of configs `_base_=['./config_a.py', './config_b.py']`.
Here are 4 examples of config inheritance.

`config_a.py`

```python
a = 1
b = dict(b1=[0, 1, 2], b2=None)
```

Zaida Zhou's avatar
Zaida Zhou committed
70
### Inherit from base config without overlapped keys
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

`config_b.py`

```python
_base_ = './config_a.py'
c = (1, 2)
d = 'string'
```

```python
>>> cfg = Config.fromfile('./config_b.py')
>>> print(cfg)
>>> dict(a=1,
...      b=dict(b1=[0, 1, 2], b2=None),
...      c=(1, 2),
...      d='string')
```

New fields in `config_b.py` are combined with old fields in `config_a.py`

Zaida Zhou's avatar
Zaida Zhou committed
91
### Inherit from base config with overlapped keys
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

`config_c.py`

```python
_base_ = './config_a.py'
b = dict(b2=1)
c = (1, 2)
```

```python
>>> cfg = Config.fromfile('./config_c.py')
>>> print(cfg)
>>> dict(a=1,
...      b=dict(b1=[0, 1, 2], b2=1),
...      c=(1, 2))
```

`b.b2=None` in `config_a` is replaced with `b.b2=1` in `config_c.py`.

Zaida Zhou's avatar
Zaida Zhou committed
111
### Inherit from base config with ignored fields
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

`config_d.py`

```python
_base_ = './config_a.py'
b = dict(_delete_=True, b2=None, b3=0.1)
c = (1, 2)
```

```python
>>> cfg = Config.fromfile('./config_d.py')
>>> print(cfg)
>>> dict(a=1,
...      b=dict(b2=None, b3=0.1),
...      c=(1, 2))
```

You may also set `_delete_=True` to ignore some fields in base configs. All old keys `b1, b2, b3` in `b` are replaced with new keys `b2, b3`.

Zaida Zhou's avatar
Zaida Zhou committed
131
### Inherit from multiple base configs (the base configs should not contain the same keys)
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

`config_e.py`

```python
c = (1, 2)
d = 'string'
```

`config_f.py`

```python
_base_ = ['./config_a.py', './config_e.py']
```

```python
>>> cfg = Config.fromfile('./config_f.py')
>>> print(cfg)
>>> dict(a=1,
...      b=dict(b1=[0, 1, 2], b2=None),
...      c=(1, 2),
...      d='string')
Kai Chen's avatar
Kai Chen committed
153
154
```

Zaida Zhou's avatar
Zaida Zhou committed
155
### Reference variables from base
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

You can reference variables defined in base using the following grammar.

`base.py`

```python
item1 = 'a'
item2 = dict(item3 = 'b')
```

`config_g.py`

```python
_base_ = ['./base.py']
item = dict(a = {{ _base_.item1 }}, b = {{ _base_.item2.item3 }})
```

```python
>>> cfg = Config.fromfile('./config_g.py')
>>> print(cfg.pretty_text)
item1 = 'a'
item2 = dict(item3='b')
item = dict(a='a', b='b')
```
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

### Add deprecation information in configs

Deprecation information can be added in a config file, which will trigger a `UserWarning` when this config file is loaded.

`deprecated_cfg.py`

```python
_base_ = 'expected_cfg.py'

_deprecation_ = dict(
    expected = 'expected_cfg.py',  # optional to show expected config path in the warning information
    reference = 'url to related PR'  # optional to show reference link in the warning information
)
```

```python
>>> cfg = Config.fromfile('./deprecated_cfg.py')

UserWarning: The config file deprecated.py will be deprecated in the future. Please use expected_cfg.py instead. More information can be found at https://github.com/open-mmlab/mmcv/pull/1275
```