Registry#

class Registry(name, build_func=None, parent=None, scope=None)[source]#

Bases: object

A registry to map strings to classes.

Registered object could be built from registry. .. rubric:: Example

>>> MODELS = Registry('models')
>>> @MODELS.register_module()
>>> class ResNet:
>>>     pass
>>> resnet = MODELS.build(dict(type='ResNet'))

Please refer to https://mmcv.readthedocs.io/en/latest/understand_mmcv/registry.html for advanced usage.

Parameters:
  • name (str) – Registry name.

  • build_func (func, optional) – Build function to construct instance from Registry, func:build_from_cfg is used if neither parent or build_func is specified. If parent is specified and build_func is not given, build_func will be inherited from parent. Default: None.

  • parent (Registry, optional) – Parent registry. The class registered in children registry could be built from parent. Default: None.

  • scope (str, optional) – The scope of registry. It is the key to search for children registry. If not specified, scope will be the name of the package where class is defined, e.g. mmdet, mmcls, mmseg. Default: None.

build(*args, **kwargs)[source]#

Build an object with this registry injected into the build function.

deprecated_register_module(cls=None, force=False)[source]#

Backward-compatible registration API kept for old decorators.

get(key)[source]#

Get the registry record.

Parameters:

key (str) – The class name in string format.

Returns:

class – The corresponding class.

static infer_scope()[source]#

Infer the scope of registry.

The name of the package where registry is defined will be returned.

Example

# in mmdet/models/backbone/resnet.py >>> MODELS = Registry(‘models’) >>> @MODELS.register_module() >>> class ResNet: >>> pass The scope of ResNet will be mmdet.

Returns:

scope (str) – The inferred scope name.

register_module(name=None, force=False, module=None)[source]#

Register a module.

A record will be added to self._module_dict, whose key is the class name or the specified name, and value is the class itself. It can be used as a decorator or a normal function.

Example

>>> backbones = Registry('backbone')
>>> @backbones.register_module()
>>> class ResNet:
>>>     pass
>>> backbones = Registry('backbone')
>>> @backbones.register_module(name='mnet')
>>> class MobileNet:
>>>     pass
>>> backbones = Registry('backbone')
>>> class ResNet:
>>>     pass
>>> backbones.register_module(ResNet)
Parameters:
  • name (str | None) – The module name to be registered. If not specified, the class name will be used.

  • force (bool, optional) – Whether to override an existing class with the same name. Default: False.

  • module (type) – Module class to be registered.

static split_scope_key(key)[source]#

Split scope and key.

The first scope will be split from key.

Examples

>>> Registry.split_scope_key('mmdet.ResNet')
'mmdet', 'ResNet'
>>> Registry.split_scope_key('ResNet')
None, 'ResNet'
Returns:

scope (str, None) – The first scope. key (str): The remaining key.

property children#

Return child registries keyed by scope.

property module_dict#

Return the registered name-to-class mapping.

property name#

Return this registry’s display name.

property scope#

Return the scope used to qualify this registry’s keys.