Skip to content

Commit

Permalink
Refine debug message
Browse files Browse the repository at this point in the history
  • Loading branch information
HAOCHENYE committed Apr 6, 2023
1 parent b8db0e2 commit aae512e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
23 changes: 12 additions & 11 deletions mmengine/registry/build_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,18 @@ def build_from_cfg(
else:
obj = obj_cls(**args) # type: ignore

# For some rare cases (e.g. obj_cls is a partial function), obj_cls
# doesn't have the following attributes. Use default value to
# prevent error
cls_name = getattr(obj_cls, '__name__', str(obj_cls))
cls_module = getattr(obj_cls, '__module__', 'unknown')
print_log(
f'An `{cls_name}` instance is built from ' # type: ignore # noqa: E501
'registry, its implementation can be found in '
f'{cls_module}', # type: ignore
logger='current',
level=logging.DEBUG)
if (inspect.isclass(obj_cls) or inspect.isfunction(obj_cls)
or inspect.ismethod(obj_cls)):
print_log(
f'An `{obj_cls.__name__}` instance is built from ' # type: ignore # noqa: E501
'registry, and its implementation can be found in '
f'{obj_cls.__module__}', # type: ignore
logger='current',
level=logging.DEBUG)
else:
print_log(
'An instance is built from registry, and its constructor '
f'is {obj_cls}')
return obj

except Exception as e:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_registry/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,11 @@ def forward(self, x):
assert isinstance(model[1], ResNeXt)
assert model[1].depth == 50 and model[1].stages == 3

NEW_FUNCTIONS = Registry('functions', scope='new')
NEW_FUNCTIONS.register_module(module=lambda x: x, name='lambda_function')
cfg = dict(type='lambda_function', x=1)
assert NEW_FUNCTIONS.build(cfg) == 1

# test inherit `build_func` from parent
NEW_MODELS = Registry('models', parent=BACKBONES, scope='new')
assert NEW_MODELS.build_func is build_model_from_cfg
Expand Down

0 comments on commit aae512e

Please sign in to comment.