Config#

class Config(cfg_dict=None, cfg_text=None, filename=None)[source]#

Bases: object

A facility for config and config files.

It supports common file formats as configs: python/json/yaml. The interface is the same as a dict object and also allows access config values as attributes.

Example

>>> 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/kchen/projects/mmcv/tests/data/config/a.py"
>>> cfg.item4
'test'
>>> cfg
"Config [path: /home/kchen/projects/mmcv/tests/data/config/a.py]: "
"{'item1': [1, 2], 'item2': {'a': 0}, 'item3': True, 'item4': 'test'}"
static auto_argparser(description=None)[source]#

Generate argparser from config file automatically (experimental)

dump(file=None)[source]#

Dump the config to a string or file, preserving Python configs.

static fromfile(filename, use_predefined_variables=True, import_custom_modules=True)[source]#

Build a Config from a config file path.

static fromstring(cfg_str, file_format)[source]#

Generate config from config str.

Parameters:
  • cfg_str (str) – Config str.

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

Returns:

objConfig: Config obj.

merge_from_dict(options, allow_list_keys=True)[source]#

Merge list into cfg_dict.

Merge the dict parsed by MultipleKVAction into this cfg.

Examples

>>> options = {'models.backbone.depth': 50,
...            'models.backbone.with_cp':True}
>>> cfg = Config(dict(models=dict(backbone=dict(type='ResNet'))))
>>> cfg.merge_from_dict(options)
>>> cfg_dict = super(Config, self).__getattribute__('_cfg_dict')
>>> assert cfg_dict == dict(
...     models=dict(backbone=dict(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_dict = super(Config, self).__getattribute__(‘_cfg_dict’) >>> assert cfg_dict == dict(pipeline=[ … dict(type=’SelfLoadImage’), dict(type=’LoadAnnotations’)])

Parameters:
  • 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. Default: True.

property filename#

Return the source filename for this config, if available.

property pretty_text#

Return the config formatted as executable Python-style text.

property text#

Return the raw source text captured when loading this config.