mmengine.registry.build_from_cfg¶
- mmengine.registry.build_from_cfg(cfg, registry, default_args=None)[source]¶
Build a module from config dict when it is a class configuration, or call a function from config dict when it is a function configuration.
If the global variable default scope (
DefaultScope
) exists,build()
will firstly get the responding registry and then call its ownbuild()
.At least one of the
cfg
anddefault_args
contains the key “type”, which should be either str or class. If they all contain it, the key incfg
will be used becausecfg
has a high priority thandefault_args
that means if a key exists in both of them, the value of the key will becfg[key]
. They will be merged first and the key “type” will be popped up and the remaining keys will be used as initialization arguments.Examples
>>> from mmengine import Registry, build_from_cfg >>> MODELS = Registry('models') >>> @MODELS.register_module() >>> class ResNet: >>> def __init__(self, depth, stages=4): >>> self.depth = depth >>> self.stages = stages >>> cfg = dict(type='ResNet', depth=50) >>> model = build_from_cfg(cfg, MODELS) >>> # Returns an instantiated object >>> @MODELS.register_module() >>> def resnet50(): >>> pass >>> resnet = build_from_cfg(dict(type='resnet50'), MODELS) >>> # Return a result of the calling function
- Parameters:
cfg (dict or ConfigDict or Config) – Config dict. It should at least contain the key “type”.
registry (
Registry
) – The registry to search the type from.default_args (dict or ConfigDict or Config, optional) – Default initialization arguments. Defaults to None.
- Returns:
The constructed object.
- Return type: