Shortcuts

Config

class mmengine.config.Config(cfg_dict=None, cfg_text=None, filename=None)[源代码]

A facility for config and config files.

It supports common file formats as configs: python/json/yaml. Config.fromfile can parse a dictionary from a config file, then build a Config instance with the dictionary. The interface is the same as a dict object and also allows access config values as attributes.

参数
  • cfg_dict (dict, optional) – A config dictionary. Defaults to None.

  • cfg_text (str, optional) – Text of config. Defaults to None.

  • filename (str or Path, optional) – Name of config file. Defaults to None.

实际案例

>>> cfg = Config(dict(a=1, b=dict(b1=[0, 1])))
>>> cfg.a
1
>>> cfg.b
{'b1': [0, 1]}
>>> cfg.b.b1
[0, 1]
>>> cfg = Config.fromfile('tests/data/config/a.py')
>>> cfg.filename
"/home/username/projects/mmengine/tests/data/config/a.py"
>>> cfg.item4
'test'
>>> cfg
"Config [path: /home/username/projects/mmengine/tests/data/config/a.py]
:"
"{'item1': [1, 2], 'item2': {'a': 0}, 'item3': True, 'item4': 'test'}"
static auto_argparser(description=None)[源代码]

Generate argparser from config file automatically (experimental)

dump(file=None)[源代码]

Dump config to file or return config text.

参数
  • file (str or Path, optional) – If not specified, then the object

  • dumped to a str (is) –

  • to a file specified by the filename. (otherwise) –

  • to None. (Defaults) –

返回

Config text.

返回类型

str or None

property filename: str

get file name of config.

static fromfile(filename, use_predefined_variables=True, import_custom_modules=True)[源代码]

Build a Config instance from config file.

参数
  • filename (str or Path) – Name of config file.

  • use_predefined_variables (bool, optional) – Whether to use predefined variables. Defaults to True.

  • import_custom_modules (bool, optional) – Whether to support importing custom modules in config. Defaults to True.

返回

Config instance built from config file.

返回类型

Config

static fromstring(cfg_str, file_format)[源代码]

Build a Config instance from config text.

参数
  • cfg_str (str) – Config text.

  • file_format (str) – Config file format corresponding to the config str. Only py/yml/yaml/json type are supported now!

返回

Config object generated from cfg_str.

返回类型

Config

merge_from_dict(options, allow_list_keys=True)[源代码]

Merge list into cfg_dict.

Merge the dict parsed by MultipleKVAction into this cfg.

参数
  • options (dict) – dict of configs to merge from.

  • allow_list_keys (bool) – If True, int string keys (e.g. ‘0’, ‘1’) are allowed in options and will replace the element of the corresponding index in the config if the config is a list. Defaults to True.

返回类型

None

实际案例

>>> from mmengine import Config
>>> #  Merge dictionary element
>>> options = {'model.backbone.depth': 50, 'model.backbone.with_cp': True}
>>> cfg = Config(dict(model=dict(backbone=dict(type='ResNet'))))
>>> cfg.merge_from_dict(options)
>>> cfg._cfg_dict
{'model': {'backbone': {'type': 'ResNet', 'depth': 50, 'with_cp': True}}}
>>> # Merge list element
>>> cfg = Config(
>>>     dict(pipeline=[dict(type='LoadImage'),
>>>                    dict(type='LoadAnnotations')]))
>>> options = dict(pipeline={'0': dict(type='SelfLoadImage')})
>>> cfg.merge_from_dict(options, allow_list_keys=True)
>>> cfg._cfg_dict
{'pipeline': [{'type': 'SelfLoadImage'}, {'type': 'LoadAnnotations'}]}
property pretty_text: str

get formatted python config text.

property text: str

get config text.

Read the Docs v: latest
Versions
latest
stable
Downloads
html
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.