[Enhancement] Improve registry infer_scope (#334)

* Improve registry infer_scope

* add warning info

* set scope as mmengine when failed to infer it

* refine message
pull/897/head
Zaida Zhou 2023-01-31 19:39:37 +08:00 committed by GitHub
parent 498080b1d3
commit acf21607be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 4 deletions

View File

@ -68,7 +68,7 @@ class Registry:
>>> fasterrcnn = DETECTORS.build(dict(type='det.MaskRCNN'))
More advanced usages can be found at
https://mmengine.readthedocs.io/en/latest/tutorials/registry.html.
https://mmengine.readthedocs.io/en/latest/advanced_tutorials/registry.html.
"""
def __init__(self,
@ -142,15 +142,31 @@ class Registry:
>>> pass
>>> # The scope of ``ResNet`` will be ``mmdet``.
"""
from ..logging import print_log
# `sys._getframe` returns the frame object that many calls below the
# top of the stack. The call stack for `infer_scope` can be listed as
# follow:
# frame-0: `infer_scope` itself
# frame-1: `__init__` of `Registry` which calls the `infer_scope`
# frame-2: Where the `Registry(...)` is called
filename = inspect.getmodule(sys._getframe(2)).__name__ # type: ignore
split_filename = filename.split('.')
return split_filename[0]
module = inspect.getmodule(sys._getframe(2))
if module is not None:
filename = module.__name__
split_filename = filename.split('.')
scope = split_filename[0]
else:
# use "mmengine" to handle some cases which can not infer the scope
# like initializing Registry in interactive mode
scope = 'mmengine'
print_log(
'set scope as "mmengine" when scope can not be inferred. You '
'can silence this warning by passing a "scope" argument to '
'Registry like `Registry(name, scope="toy")`',
logger='current',
level=logging.WARNING)
return scope
@staticmethod
def split_scope_key(key: str) -> Tuple[Optional[str], str]: