Skip to content
Snippets Groups Projects
Unverified Commit acf21607 authored by Zaida Zhou's avatar Zaida Zhou Committed by GitHub
Browse files

[Enhancement] Improve registry infer_scope (#334)

* Improve registry infer_scope

* add warning info

* set scope as mmengine when failed to infer it

* refine message
parent 498080b1
No related branches found
No related tags found
No related merge requests found
...@@ -68,7 +68,7 @@ class Registry: ...@@ -68,7 +68,7 @@ class Registry:
>>> fasterrcnn = DETECTORS.build(dict(type='det.MaskRCNN')) >>> fasterrcnn = DETECTORS.build(dict(type='det.MaskRCNN'))
More advanced usages can be found at 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, def __init__(self,
...@@ -142,15 +142,31 @@ class Registry: ...@@ -142,15 +142,31 @@ class Registry:
>>> pass >>> pass
>>> # The scope of ``ResNet`` will be ``mmdet``. >>> # The scope of ``ResNet`` will be ``mmdet``.
""" """
from ..logging import print_log
# `sys._getframe` returns the frame object that many calls below the # `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 # top of the stack. The call stack for `infer_scope` can be listed as
# follow: # follow:
# frame-0: `infer_scope` itself # frame-0: `infer_scope` itself
# frame-1: `__init__` of `Registry` which calls the `infer_scope` # frame-1: `__init__` of `Registry` which calls the `infer_scope`
# frame-2: Where the `Registry(...)` is called # frame-2: Where the `Registry(...)` is called
filename = inspect.getmodule(sys._getframe(2)).__name__ # type: ignore module = inspect.getmodule(sys._getframe(2))
split_filename = filename.split('.') if module is not None:
return split_filename[0] 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 @staticmethod
def split_scope_key(key: str) -> Tuple[Optional[str], str]: def split_scope_key(key: str) -> Tuple[Optional[str], str]:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment