Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Improve registry infer_scope #334

Merged
merged 5 commits into from
Jan 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions mmengine/registry/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -142,15 +142,31 @@ def infer_scope() -> str:
>>> 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]:
Expand Down