Config — mmengine 0.10.7 documentation (original) (raw)

class mmengine.config.Config(cfg_dict=None, cfg_text=None, filename=None, env_variables=None, format_python_code=True)[source]

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.

Parameters:

Here is a simple example:

Examples

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'}"

You can find more advance usage in the config tutorial.

static auto_argparser(description=None)[source]

Generate argparser from config file automatically (experimental)

dump(file=None)[source]

Dump config to file or return config text.

Parameters:

Returns:

Config text.

Return type:

str or None

property env_variables_: dict_

Get used environment variables.

property filename_: str_

Get file name of config.

static fromfile(filename, use_predefined_variables=True, import_custom_modules=True, use_environment_variables=True, lazy_import=None, format_python_code=True)[source]

Build a Config instance from config file.

Parameters:

Returns:

Config instance built from config file.

Return type:

Config

static fromstring(cfg_str, file_format)[source]

Build a Config instance from config text.

Parameters:

Returns:

Config object generated from cfg_str.

Return type:

Config

merge_from_dict(options, allow_list_keys=True)[source]

Merge list into cfg_dict.

Merge the dict parsed by MultipleKVAction into this cfg.

Parameters:

Return type:

None

Examples

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.

to_dict(keep_imported=False)[source]

Convert all data in the config to a builtin dict.

Parameters:

keep_imported (bool) – Whether to keep the imported field. Defaults to False

If you import third-party objects in the config file, all imported objects will be converted to a string like torch.optim.SGD