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. Example: >>> models = Registry('models') >>> @models.register_module('image-classification', 'SwinT') >>> class SwinTransformer:
(cfg,
registry: Registry,
group_key: str = default_group,
default_args: dict = None)
| 132 | |
| 133 | |
| 134 | def build_from_cfg(cfg, |
| 135 | registry: Registry, |
| 136 | group_key: str = default_group, |
| 137 | default_args: dict = None) -> object: |
| 138 | """Build a module from config dict when it is a class configuration, or |
| 139 | call a function from config dict when it is a function configuration. |
| 140 | |
| 141 | Example: |
| 142 | >>> models = Registry('models') |
| 143 | >>> @models.register_module('image-classification', 'SwinT') |
| 144 | >>> class SwinTransformer: |
| 145 | >>> pass |
| 146 | >>> swint = build_from_cfg(dict(type='SwinT'), MODELS, |
| 147 | >>> 'image-classification') |
| 148 | >>> # Returns an instantiated object |
| 149 | >>> |
| 150 | >>> @MODELS.register_module() |
| 151 | >>> def swin_transformer(): |
| 152 | >>> pass |
| 153 | >>> = build_from_cfg(dict(type='swin_transformer'), MODELS) |
| 154 | >>> # Return a result of the calling function |
| 155 | |
| 156 | Args: |
| 157 | cfg (dict): Config dict. It should at least contain the key "type". |
| 158 | registry (:obj:`Registry`): The registry to search the type from. |
| 159 | group_key (str, optional): The name of registry group from which |
| 160 | module should be searched. |
| 161 | default_args (dict, optional): Default initialization arguments. |
| 162 | type_name (str, optional): The name of the type in the config. |
| 163 | Returns: |
| 164 | object: The constructed object. |
| 165 | """ |
| 166 | if not isinstance(cfg, dict): |
| 167 | raise TypeError(f'cfg must be a dict, but got {type(cfg)}') |
| 168 | if TYPE_NAME not in cfg: |
| 169 | if default_args is None or TYPE_NAME not in default_args: |
| 170 | raise KeyError( |
| 171 | f'`cfg` or `default_args` must contain the key "{TYPE_NAME}", ' |
| 172 | f'but got {cfg}\n{default_args}') |
| 173 | if not isinstance(registry, Registry): |
| 174 | raise TypeError('registry must be an modelscope.Registry object, ' |
| 175 | f'but got {type(registry)}') |
| 176 | if not (isinstance(default_args, dict) or default_args is None): |
| 177 | raise TypeError('default_args must be a dict or None, ' |
| 178 | f'but got {type(default_args)}') |
| 179 | |
| 180 | # dynamic load installation requirements for this module |
| 181 | from modelscope.utils.import_utils import LazyImportModule |
| 182 | sig = (registry.name.upper(), group_key, cfg['type']) |
| 183 | LazyImportModule.import_module(sig) |
| 184 | |
| 185 | args = cfg.copy() |
| 186 | if default_args is not None: |
| 187 | for name, value in default_args.items(): |
| 188 | args.setdefault(name, value) |
| 189 | |
| 190 | if group_key is None: |
| 191 | group_key = default_group |
searching dependent graphs…