Skip to content
New issue

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

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

Already on GitHub? # to your account

[Feature] Support auto import modules from registry. #2314

Merged
merged 3 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 0 additions & 4 deletions demo/mono_det_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from mmdet3d.apis import inference_mono_3d_detector, init_model
from mmdet3d.registry import VISUALIZERS
from mmdet3d.utils import register_all_modules


def parse_args():
Expand Down Expand Up @@ -38,9 +37,6 @@ def parse_args():


def main(args):
# register all modules in mmdet3d into the registries
register_all_modules()

# build the model from a config file and a checkpoint file
model = init_model(args.config, args.checkpoint, device=args.device)

Expand Down
4 changes: 0 additions & 4 deletions demo/multi_modality_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from mmdet3d.apis import inference_multi_modality_detector, init_model
from mmdet3d.registry import VISUALIZERS
from mmdet3d.utils import register_all_modules


def parse_args():
Expand Down Expand Up @@ -39,9 +38,6 @@ def parse_args():


def main(args):
# register all modules in mmdet3d into the registries
register_all_modules()

# build the model from a config file and a checkpoint file
model = init_model(args.config, args.checkpoint, device=args.device)

Expand Down
4 changes: 0 additions & 4 deletions demo/pcd_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from mmdet3d.apis import inference_detector, init_model
from mmdet3d.registry import VISUALIZERS
from mmdet3d.utils import register_all_modules


def parse_args():
Expand All @@ -30,9 +29,6 @@ def parse_args():


def main(args):
# register all modules in mmdet3d into the registries
register_all_modules()

# TODO: Support inference of point cloud numpy file.
# build the model from a config file and a checkpoint file
model = init_model(args.config, args.checkpoint, device=args.device)
Expand Down
4 changes: 0 additions & 4 deletions demo/pcd_seg_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from mmdet3d.apis import inference_segmentor, init_model
from mmdet3d.registry import VISUALIZERS
from mmdet3d.utils import register_all_modules


def parse_args():
Expand All @@ -28,9 +27,6 @@ def parse_args():


def main(args):
# register all modules in mmdet3d into the registries
register_all_modules()

# build the model from a config file and a checkpoint file
model = init_model(args.config, args.checkpoint, device=args.device)

Expand Down
2 changes: 0 additions & 2 deletions docs/en/get_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ Case b: If you install MMDetection3D with MIM, open your python interpreter and

```python
from mmdet3d.apis import init_model, inference_detector
from mmdet3d.utils import register_all_modules

register_all_modules()
config_file = 'pointpillars_hv_secfpn_8xb6-160e_kitti-3d-car.py'
checkpoint_file = 'hv_pointpillars_secfpn_6x8_160e_kitti-3d-car_20220331_134606-d42d15ed.pth'
model = init_model(config_file, checkpoint_file)
Expand Down
2 changes: 1 addition & 1 deletion docs/en/notes/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ We list some potential troubles encountered by users and developers, along with

| MMDetection3D version | MMEngine version | MMCV version | MMDetection version |
| --------------------- | :----------------------: | :---------------------: | :----------------------: |
| dev-1.x | mmengine>=0.1.0, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 | mmdet>=3.0.0rc0, \<3.1.0 |
| dev-1.x | mmengine>=0.4.0, \<1.0.0 | mmcv>=2.0.0rc4, \<2.1.0 | mmdet>=3.0.0rc0, \<3.1.0 |
| v1.1.0rc3 | mmengine>=0.1.0, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 | mmdet>=3.0.0rc0, \<3.1.0 |
| v1.1.0rc2 | mmengine>=0.1.0, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 | mmdet>=3.0.0rc0, \<3.1.0 |
| v1.1.0rc1 | mmengine>=0.1.0, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 | mmdet>=3.0.0rc0, \<3.1.0 |
Expand Down
2 changes: 0 additions & 2 deletions docs/zh_cn/get_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ to_ply('./test.obj', './test.ply', 'obj')

```python
from mmdet3d.apis import init_model, inference_detector
from mmdet3d.utils import register_all_modules

register_all_modules()
config_file = 'pointpillars_hv_secfpn_8xb6-160e_kitti-3d-car.py'
checkpoint_file = 'hv_pointpillars_secfpn_6x8_160e_kitti-3d-car_20220331_134606-d42d15ed.pth'
model = init_model(config_file, checkpoint_file)
Expand Down
3 changes: 2 additions & 1 deletion docs/zh_cn/notes/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

| MMDetection3D 版本 | MMEngine 版本 | MMCV 版本 | MMDetection 版本 |
| ------------------ | :----------------------: | :---------------------: | :----------------------: |
| dev-1.x | mmengine>=0.1.0, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 | mmdet>=3.0.0rc0, \<3.1.0 |
| dev-1.x | mmengine>=0.4.0, \<1.0.0 | mmcv>=2.0.0rc4, \<2.1.0 | mmdet>=3.0.0rc0, \<3.1.0 |
| v1.1.0rc3 | mmengine>=0.1.0, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 | mmdet>=3.0.0rc0, \<3.1.0 |
| v1.1.0rc2 | mmengine>=0.1.0, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 | mmdet>=3.0.0rc0, \<3.1.0 |
| v1.1.0rc1 | mmengine>=0.1.0, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 | mmdet>=3.0.0rc0, \<3.1.0 |
| v1.1.0rc0 | mmengine>=0.1.0, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 | mmdet>=3.0.0rc0, \<3.1.0 |
Expand Down
2 changes: 1 addition & 1 deletion mmdet3d/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
mmcv_maximum_version = '2.1.0'
mmcv_version = digit_version(mmcv.__version__)

mmengine_minimum_version = '0.1.0'
mmengine_minimum_version = '0.4.0'
mmengine_maximum_version = '1.0.0'
mmengine_version = digit_version(mmengine.__version__)

Expand Down
2 changes: 2 additions & 0 deletions mmdet3d/apis/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import torch.nn as nn
from mmengine.config import Config
from mmengine.dataset import Compose, pseudo_collate
from mmengine.registry import init_default_scope
from mmengine.runner import load_checkpoint

from mmdet3d.registry import MODELS
Expand Down Expand Up @@ -63,6 +64,7 @@ def init_model(config: Union[str, Path, Config],

convert_SyncBN(config.model)
config.model.train_cfg = None
init_default_scope(config.get('default_scope', 'mmdet3d'))
model = MODELS.build(config.model)

if checkpoint is not None:
Expand Down
5 changes: 3 additions & 2 deletions mmdet3d/apis/inferencers/base_det3d_inferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
from mmengine.fileio import (get_file_backend, isdir, join_path,
list_dir_or_file)
from mmengine.infer.infer import BaseInferencer, ModelType
from mmengine.registry import init_default_scope
from mmengine.runner import load_checkpoint
from mmengine.structures import InstanceData
from mmengine.visualization import Visualizer

from mmdet3d.registry import MODELS
from mmdet3d.utils import ConfigType, register_all_modules
from mmdet3d.utils import ConfigType

InstanceList = List[InstanceData]
InputType = Union[str, np.ndarray]
Expand Down Expand Up @@ -60,7 +61,7 @@ def __init__(self,
scope: Optional[str] = 'mmdet3d',
palette: str = 'none') -> None:
self.palette = palette
register_all_modules()
init_default_scope(scope)
super().__init__(
model=model, weights=weights, device=device, scope=scope)

Expand Down
3 changes: 1 addition & 2 deletions mmdet3d/apis/inferencers/lidar_det3d_inferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from mmengine.structures import InstanceData

from mmdet3d.registry import INFERENCERS
from mmdet3d.utils import ConfigType, register_all_modules
from mmdet3d.utils import ConfigType
from .base_det3d_inferencer import BaseDet3DInferencer

InstanceList = List[InstanceData]
Expand Down Expand Up @@ -62,7 +62,6 @@ def __init__(self,
# naming of the output results
self.num_visualized_frames = 0
self.palette = palette
register_all_modules()
super().__init__(
model=model, weights=weights, device=device, scope=scope)

Expand Down
85 changes: 64 additions & 21 deletions mmdet3d/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,54 +33,97 @@
from mmengine.registry import Registry

# manage all kinds of runners like `EpochBasedRunner` and `IterBasedRunner`
RUNNERS = Registry('runner', parent=MMENGINE_RUNNERS)
RUNNERS = Registry(
'runner', parent=MMENGINE_RUNNERS, locations=['mmdet3d.engine.runner'])
# manage runner constructors that define how to initialize runners
RUNNER_CONSTRUCTORS = Registry(
'runner constructor', parent=MMENGINE_RUNNER_CONSTRUCTORS)
'runner constructor',
parent=MMENGINE_RUNNER_CONSTRUCTORS,
locations=['mmdet3d.engine.runner'])
# manage all kinds of loops like `EpochBasedTrainLoop`
LOOPS = Registry('loop', parent=MMENGINE_LOOPS)
LOOPS = Registry(
'loop', parent=MMENGINE_LOOPS, locations=['mmdet3d.engine.runner'])
# manage all kinds of hooks like `CheckpointHook`
HOOKS = Registry('hook', parent=MMENGINE_HOOKS)
HOOKS = Registry(
'hook', parent=MMENGINE_HOOKS, locations=['mmdet3d.engine.hooks'])

# manage data-related modules
DATASETS = Registry('dataset', parent=MMENGINE_DATASETS)
DATA_SAMPLERS = Registry('data sampler', parent=MMENGINE_DATA_SAMPLERS)
TRANSFORMS = Registry('transform', parent=MMENGINE_TRANSFORMS)
DATASETS = Registry(
'dataset', parent=MMENGINE_DATASETS, locations=['mmdet3d.datasets'])
DATA_SAMPLERS = Registry(
'data sampler',
parent=MMENGINE_DATA_SAMPLERS,
locations=['mmdet3d.datasets.samplers'])
TRANSFORMS = Registry(
'transform',
parent=MMENGINE_TRANSFORMS,
locations=['mmdet3d.datasets.transforms'])

# mangage all kinds of modules inheriting `nn.Module`
MODELS = Registry('model', parent=MMENGINE_MODELS)
MODELS = Registry(
'model', parent=MMENGINE_MODELS, locations=['mmdet3d.models'])
# mangage all kinds of model wrappers like 'MMDistributedDataParallel'
MODEL_WRAPPERS = Registry('model_wrapper', parent=MMENGINE_MODEL_WRAPPERS)
MODEL_WRAPPERS = Registry(
'model_wrapper',
parent=MMENGINE_MODEL_WRAPPERS,
locations=['mmdet3d.models'])
# mangage all kinds of weight initialization modules like `Uniform`
WEIGHT_INITIALIZERS = Registry(
'weight initializer', parent=MMENGINE_WEIGHT_INITIALIZERS)
'weight initializer',
parent=MMENGINE_WEIGHT_INITIALIZERS,
locations=['mmdet3d.models'])

# mangage all kinds of optimizers like `SGD` and `Adam`
OPTIMIZERS = Registry('optimizer', parent=MMENGINE_OPTIMIZERS)
OPTIMIZERS = Registry(
'optimizer',
parent=MMENGINE_OPTIMIZERS,
locations=['mmdet3d.engine.optimizers'])
# manage optimizer wrapper
OPTIM_WRAPPERS = Registry('optim wrapper', parent=MMENGINE_OPTIM_WRAPPERS)
OPTIM_WRAPPERS = Registry(
'optim wrapper',
parent=MMENGINE_OPTIM_WRAPPERS,
locations=['mmdet3d.engine.optimizers'])
# manage constructors that customize the optimization hyperparameters.
OPTIM_WRAPPER_CONSTRUCTORS = Registry(
'optimizer wrapper constructor',
parent=MMENGINE_OPTIM_WRAPPER_CONSTRUCTORS)
parent=MMENGINE_OPTIM_WRAPPER_CONSTRUCTORS,
locations=['mmdet3d.engine.optimizers'])
# mangage all kinds of parameter schedulers like `MultiStepLR`
PARAM_SCHEDULERS = Registry(
'parameter scheduler', parent=MMENGINE_PARAM_SCHEDULERS)
'parameter scheduler',
parent=MMENGINE_PARAM_SCHEDULERS,
locations=['mmdet3d.engine.schedulers'])
# manage all kinds of metrics
METRICS = Registry('metric', parent=MMENGINE_METRICS)
METRICS = Registry(
'metric', parent=MMENGINE_METRICS, locations=['mmdet3d.evaluation'])
# manage evaluator
EVALUATOR = Registry('evaluator', parent=MMENGINE_EVALUATOR)
EVALUATOR = Registry(
'evaluator', parent=MMENGINE_EVALUATOR, locations=['mmdet3d.evaluation'])

# manage task-specific modules like anchor generators and box coders
TASK_UTILS = Registry('task util', parent=MMENGINE_TASK_UTILS)
TASK_UTILS = Registry(
'task util', parent=MMENGINE_TASK_UTILS, locations=['mmdet3d.models'])

# manage visualizer
VISUALIZERS = Registry('visualizer', parent=MMENGINE_VISUALIZERS)
VISUALIZERS = Registry(
'visualizer',
parent=MMENGINE_VISUALIZERS,
locations=['mmdet3d.visualization'])
# manage visualizer backend
VISBACKENDS = Registry('vis_backend', parent=MMENGINE_VISBACKENDS)
VISBACKENDS = Registry(
'vis_backend',
parent=MMENGINE_VISBACKENDS,
locations=['mmdet3d.visualization'])

# manage logprocessor
LOG_PROCESSORS = Registry('log_processor', parent=MMENGINE_LOG_PROCESSORS)
LOG_PROCESSORS = Registry(
'log_processor',
parent=MMENGINE_LOG_PROCESSORS,
# TODO: update the location when mmdet3d has its own log processor
locations=['mmdet3d.engine'])

# manage inferencer
INFERENCERS = Registry('inferencer', parent=MMENGINE_INFERENCERS)
INFERENCERS = Registry(
'inferencer',
parent=MMENGINE_INFERENCERS,
locations=['mmdet3d.api.inferencers'])
4 changes: 2 additions & 2 deletions requirements/mminstall.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mmcv>=2.0.0rc0,<2.1.0
mmcv>=2.0.0rc4,<2.1.0
mmdet>=3.0.0rc0,<3.1.0
mmengine>=0.1.0,<1.0.0
mmengine>=0.4.0,<1.0.0
2 changes: 1 addition & 1 deletion tests/test_datasets/test_transforms/test_transforms_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def test_transform(self):
transform = PolarMix(instance_classes=[1.0, 2.0])

transform = PolarMix(
instance_classes=[1, 2],
instance_classes=[15, 16, 17],
swap_ratio=1.0,
pre_transform=self.pre_transform)
results = transform.transform(copy.deepcopy(self.results))
Expand Down
4 changes: 2 additions & 2 deletions tools/analysis_tools/get_flops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import torch
from mmengine import Config, DictAction
from mmengine.registry import init_default_scope

from mmdet3d.registry import MODELS
from mmdet3d.utils import register_all_modules

try:
from mmcv.cnn import get_model_complexity_info
Expand Down Expand Up @@ -43,7 +43,6 @@ def parse_args():


def main():
register_all_modules()
args = parse_args()

if args.modality == 'point':
Expand All @@ -64,6 +63,7 @@ def main():
cfg = Config.fromfile(args.config)
if args.cfg_options is not None:
cfg.merge_from_dict(args.cfg_options)
init_default_scope(cfg.get('default_scope', 'mmdet3d'))

model = MODELS.build(cfg.model)
if torch.cuda.is_available():
Expand Down
6 changes: 3 additions & 3 deletions tools/misc/browse_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from os import path as osp

from mmengine.config import Config, DictAction
from mmengine.registry import init_default_scope
from mmengine.utils import ProgressBar, mkdir_or_exist

from mmdet3d.registry import DATASETS, VISUALIZERS
from mmdet3d.utils import register_all_modules, replace_ceph_backend
from mmdet3d.utils import replace_ceph_backend


def parse_args():
Expand Down Expand Up @@ -99,8 +100,7 @@ def main():
if args.ceph:
cfg = replace_ceph_backend(cfg)

# register all modules in mmdet3d into the registries
register_all_modules()
init_default_scope(cfg.get('default_scope', 'mmdet3d'))

try:
dataset = DATASETS.build(
Expand Down
6 changes: 1 addition & 5 deletions tools/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from mmengine.registry import RUNNERS
from mmengine.runner import Runner

from mmdet3d.utils import register_all_modules, replace_ceph_backend
from mmdet3d.utils import replace_ceph_backend


# TODO: support fuse_conv_bn and format_only
Expand Down Expand Up @@ -84,10 +84,6 @@ def trigger_visualization_hook(cfg, args):
def main():
args = parse_args()

# register all modules in mmdet3d into the registries
# do not init the default scope here because it will be init in the runner
register_all_modules(init_default_scope=False)

# load config
cfg = Config.fromfile(args.config)

Expand Down
5 changes: 1 addition & 4 deletions tools/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from mmengine.registry import RUNNERS
from mmengine.runner import Runner

from mmdet3d.utils import register_all_modules, replace_ceph_backend
from mmdet3d.utils import replace_ceph_backend


def parse_args():
Expand Down Expand Up @@ -59,9 +59,6 @@ def parse_args():

def main():
args = parse_args()
# register all modules in mmdet3d into the registries
# do not init the default scope here because it will be init in the runner
register_all_modules(init_default_scope=False)

# load config
cfg = Config.fromfile(args.config)
Expand Down