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

[Docs] Refine registry docs (#443)

* [Docs] Refine registry docs

* explain how to use _scope_

* refine
parent dcab0f50
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
...@@ -190,45 +190,45 @@ class Registry: ...@@ -190,45 +190,45 @@ class Registry:
scope (str): The target scope. scope (str): The target scope.
Examples: Examples:
>>> from mmengine.registry import Registry, DefaultScope, MODELS >>> from mmengine.registry import Registry, DefaultScope, MODELS
>>> import time >>> import time
>>> # External Registry >>> # External Registry
>>> MMDET_MODELS = Registry('mmdet_model', scope='mmdet', >>> MMDET_MODELS = Registry('mmdet_model', scope='mmdet',
>>> parent=MODELS) >>> parent=MODELS)
>>> MMCLS_MODELS = Registry('mmcls_model', scope='mmcls', >>> MMCLS_MODELS = Registry('mmcls_model', scope='mmcls',
>>> parent=MODELS) >>> parent=MODELS)
>>> # Local Registry >>> # Local Registry
>>> CUSTOM_MODELS = Registry('custom_model', scope='custom', >>> CUSTOM_MODELS = Registry('custom_model', scope='custom',
>>> parent=MODELS) >>> parent=MODELS)
>>> >>>
>>> # Initiate DefaultScope >>> # Initiate DefaultScope
>>> DefaultScope.get_instance(f'scope_{time.time()}', >>> DefaultScope.get_instance(f'scope_{time.time()}',
>>> scope_name='custom') >>> scope_name='custom')
>>> # Check default scope >>> # Check default scope
>>> DefaultScope.get_current_instance().scope_name >>> DefaultScope.get_current_instance().scope_name
custom custom
>>> # Switch to mmcls scope and get `MMCLS_MODELS` registry. >>> # Switch to mmcls scope and get `MMCLS_MODELS` registry.
>>> with CUSTOM_MODELS.switch_scope_and_registry(scope='mmcls') as registry: # noqa: E501 >>> with CUSTOM_MODELS.switch_scope_and_registry(scope='mmcls') as registry:
>>> DefaultScope.get_current_instance().scope_name >>> DefaultScope.get_current_instance().scope_name
mmcls mmcls
>>> registry.scope >>> registry.scope
mmcls mmcls
>>> # Nested switch scope >>> # Nested switch scope
>>> with CUSTOM_MODELS.switch_scope_and_registry(scope='mmdet') as mmdet_registry: # noqa: E501 >>> with CUSTOM_MODELS.switch_scope_and_registry(scope='mmdet') as mmdet_registry:
>>> DefaultScope.get_current_instance().scope_name >>> DefaultScope.get_current_instance().scope_name
mmdet mmdet
>>> mmdet_registry.scope >>> mmdet_registry.scope
mmdet mmdet
>>> with CUSTOM_MODELS.switch_scope_and_registry(scope='mmcls') as mmcls_registry: # noqa: E501 >>> with CUSTOM_MODELS.switch_scope_and_registry(scope='mmcls') as mmcls_registry:
>>> DefaultScope.get_current_instance().scope_name >>> DefaultScope.get_current_instance().scope_name
mmcls mmcls
>>> mmcls_registry.scope >>> mmcls_registry.scope
mmcls mmcls
>>> >>>
>>> # Check switch back to original scope. >>> # Check switch back to original scope.
>>> DefaultScope.get_current_instance().scope_name >>> DefaultScope.get_current_instance().scope_name
custom custom
""" """ # noqa: E501
from ..logging import print_log from ..logging import print_log
# Switch to the given scope temporarily. If the corresponding registry # Switch to the given scope temporarily. If the corresponding registry
......
...@@ -54,10 +54,20 @@ def count_registered_modules(save_path: Optional[str] = None, ...@@ -54,10 +54,20 @@ def count_registered_modules(save_path: Optional[str] = None,
Args: Args:
save_path (str, optional): Path to save the json file. save_path (str, optional): Path to save the json file.
verbose (bool): Whether to print log. Default: True verbose (bool): Whether to print log. Defaults to True.
Returns: Returns:
dict: Statistic results of all registered modules. dict: Statistic results of all registered modules.
""" """
# import modules to trigger registering
import mmengine.dataset
import mmengine.evaluator
import mmengine.hooks
import mmengine.model
import mmengine.optim
import mmengine.runner
import mmengine.visualization # noqa: F401
registries_info = {} registries_info = {}
# traverse all registries in MMEngine # traverse all registries in MMEngine
for item in dir(root): for item in dir(root):
......
# Copyright (c) OpenMMLab. All rights reserved. # Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp import os.path as osp
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from unittest import TestCase from unittest import TestCase, skipIf
from mmengine.registry import (Registry, count_registered_modules, root, from mmengine.registry import (Registry, count_registered_modules, root,
traverse_registry_tree) traverse_registry_tree)
from mmengine.utils import is_installed
class TestUtils(TestCase): class TestUtils(TestCase):
...@@ -42,6 +43,7 @@ class TestUtils(TestCase): ...@@ -42,6 +43,7 @@ class TestUtils(TestCase):
# result from any node should be the same # result from any node should be the same
self.assertEqual(result, result_leaf) self.assertEqual(result, result_leaf)
@skipIf(not is_installed('torch'), 'tests requires torch')
def test_count_all_registered_modules(self): def test_count_all_registered_modules(self):
temp_dir = TemporaryDirectory() temp_dir = TemporaryDirectory()
results = count_registered_modules(temp_dir.name, verbose=True) results = count_registered_modules(temp_dir.name, verbose=True)
......
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