From 59827cfe67cbbf69e23484006a2550b4364504da Mon Sep 17 00:00:00 2001 From: lupeng Date: Fri, 10 Mar 2023 15:47:51 +0800 Subject: [PATCH 1/3] remove init_default_scope --- demo/topdown_demo_with_mmdet.py | 4 ++-- .../webcam/nodes/model_nodes/detector_node.py | 4 ++-- mmpose/utils/__init__.py | 4 +++- mmpose/utils/config_utils.py | 16 ++++++++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 mmpose/utils/config_utils.py diff --git a/demo/topdown_demo_with_mmdet.py b/demo/topdown_demo_with_mmdet.py index 7ed3107b3a..fddcc71388 100644 --- a/demo/topdown_demo_with_mmdet.py +++ b/demo/topdown_demo_with_mmdet.py @@ -8,13 +8,13 @@ import mmcv import mmengine import numpy as np -from mmengine.registry import init_default_scope from mmpose.apis import inference_topdown from mmpose.apis import init_model as init_pose_estimator from mmpose.evaluation.functional import nms from mmpose.registry import VISUALIZERS from mmpose.structures import merge_data_samples, split_instances +from mmpose.utils import convert_mmdet_test_pipeline try: from mmdet.apis import inference_detector, init_detector @@ -28,7 +28,6 @@ def process_one_image(args, img_path, detector, pose_estimator, visualizer, """Visualize predicted keypoints (and heatmaps) of one image.""" # predict bbox - init_default_scope(detector.cfg.get('default_scope', 'mmdet')) det_result = inference_detector(detector, img_path) pred_instance = det_result.pred_instances.cpu().numpy() bboxes = np.concatenate( @@ -147,6 +146,7 @@ def main(): # build detector detector = init_detector( args.det_config, args.det_checkpoint, device=args.device) + detector.cfg = convert_mmdet_test_pipeline(detector.cfg) # build pose estimator pose_estimator = init_pose_estimator( diff --git a/mmpose/apis/webcam/nodes/model_nodes/detector_node.py b/mmpose/apis/webcam/nodes/model_nodes/detector_node.py index fa925a2d25..0f8bbc9aa1 100644 --- a/mmpose/apis/webcam/nodes/model_nodes/detector_node.py +++ b/mmpose/apis/webcam/nodes/model_nodes/detector_node.py @@ -2,8 +2,8 @@ from typing import Dict, List, Optional, Union import numpy as np -from mmengine.registry import init_default_scope +from mmpose.utils import convert_mmdet_test_pipeline from ...utils import get_config_path from ..node import Node from ..registry import NODES @@ -92,6 +92,7 @@ def __init__(self, # Init model self.model = init_detector( self.model_config, self.model_checkpoint, device=self.device) + self.model.cfg = convert_mmdet_test_pipeline(self.model.cfg) # Register buffers self.register_input_buffer(input_buffer, 'input', trigger=True) @@ -109,7 +110,6 @@ def process(self, input_msgs): img = input_msg.get_image() - init_default_scope(self.model.cfg.get('default_scope', 'mmdet')) preds = inference_detector(self.model, img) objects = self._post_process(preds) input_msg.update_objects(objects) diff --git a/mmpose/utils/__init__.py b/mmpose/utils/__init__.py index 044bb286c4..14f6c8816b 100644 --- a/mmpose/utils/__init__.py +++ b/mmpose/utils/__init__.py @@ -1,11 +1,13 @@ # Copyright (c) OpenMMLab. All rights reserved. from .camera import SimpleCamera, SimpleCameraTorch from .collect_env import collect_env +from .config_utils import convert_mmdet_test_pipeline from .logger import get_root_logger from .setup_env import register_all_modules, setup_multi_processes from .timer import StopWatch __all__ = [ 'get_root_logger', 'collect_env', 'StopWatch', 'setup_multi_processes', - 'register_all_modules', 'SimpleCamera', 'SimpleCameraTorch' + 'register_all_modules', 'SimpleCamera', 'SimpleCameraTorch', + 'convert_mmdet_test_pipeline' ] diff --git a/mmpose/utils/config_utils.py b/mmpose/utils/config_utils.py new file mode 100644 index 0000000000..18cf41a8f9 --- /dev/null +++ b/mmpose/utils/config_utils.py @@ -0,0 +1,16 @@ +# Copyright (c) OpenMMLab. All rights reserved. +from mmpose.utils.typing import ConfigDict + + +def convert_mmdet_test_pipeline(cfg: ConfigDict): + from mmdet.datasets import transforms + + if 'test_dataloader' not in cfg: + return cfg + + pipeline = cfg.test_dataloader.dataset.pipeline + for trans in pipeline: + if trans['type'] in dir(transforms): + trans['type'] = 'mmdet.' + trans['type'] + + return cfg From 74b44c5e10c494392410ac449016e766bb841cbd Mon Sep 17 00:00:00 2001 From: lupeng Date: Fri, 10 Mar 2023 15:59:00 +0800 Subject: [PATCH 2/3] add docstring --- mmpose/utils/config_utils.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mmpose/utils/config_utils.py b/mmpose/utils/config_utils.py index 18cf41a8f9..10181a3de5 100644 --- a/mmpose/utils/config_utils.py +++ b/mmpose/utils/config_utils.py @@ -2,7 +2,17 @@ from mmpose.utils.typing import ConfigDict -def convert_mmdet_test_pipeline(cfg: ConfigDict): +def convert_mmdet_test_pipeline(cfg: ConfigDict) -> ConfigDict: + """Converts pipeline types in MMDetection's test dataloader to use the + 'mmdet' namespace. + + Args: + cfg (ConfigDict): Configuration dictionary for MMDetection. + + Returns: + ConfigDict: Configuration dictionary with updated pipeline types. + """ + # use lazy import to avoid hard dependence on mmdet from mmdet.datasets import transforms if 'test_dataloader' not in cfg: From 102c5f2b5f9c62eff027d447ae6e6267198d5ce9 Mon Sep 17 00:00:00 2001 From: lupeng Date: Sun, 12 Mar 2023 12:57:38 +0800 Subject: [PATCH 3/3] rename convert_mmdet_test_pipeline to adapt_mmdet_pipeline --- demo/topdown_demo_with_mmdet.py | 4 ++-- mmpose/apis/webcam/nodes/model_nodes/detector_node.py | 4 ++-- mmpose/utils/__init__.py | 4 ++-- mmpose/utils/config_utils.py | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/demo/topdown_demo_with_mmdet.py b/demo/topdown_demo_with_mmdet.py index fddcc71388..f1a4e42b4d 100644 --- a/demo/topdown_demo_with_mmdet.py +++ b/demo/topdown_demo_with_mmdet.py @@ -14,7 +14,7 @@ from mmpose.evaluation.functional import nms from mmpose.registry import VISUALIZERS from mmpose.structures import merge_data_samples, split_instances -from mmpose.utils import convert_mmdet_test_pipeline +from mmpose.utils import adapt_mmdet_pipeline try: from mmdet.apis import inference_detector, init_detector @@ -146,7 +146,7 @@ def main(): # build detector detector = init_detector( args.det_config, args.det_checkpoint, device=args.device) - detector.cfg = convert_mmdet_test_pipeline(detector.cfg) + detector.cfg = adapt_mmdet_pipeline(detector.cfg) # build pose estimator pose_estimator = init_pose_estimator( diff --git a/mmpose/apis/webcam/nodes/model_nodes/detector_node.py b/mmpose/apis/webcam/nodes/model_nodes/detector_node.py index 0f8bbc9aa1..350831fe62 100644 --- a/mmpose/apis/webcam/nodes/model_nodes/detector_node.py +++ b/mmpose/apis/webcam/nodes/model_nodes/detector_node.py @@ -3,7 +3,7 @@ import numpy as np -from mmpose.utils import convert_mmdet_test_pipeline +from mmpose.utils import adapt_mmdet_pipeline from ...utils import get_config_path from ..node import Node from ..registry import NODES @@ -92,7 +92,7 @@ def __init__(self, # Init model self.model = init_detector( self.model_config, self.model_checkpoint, device=self.device) - self.model.cfg = convert_mmdet_test_pipeline(self.model.cfg) + self.model.cfg = adapt_mmdet_pipeline(self.model.cfg) # Register buffers self.register_input_buffer(input_buffer, 'input', trigger=True) diff --git a/mmpose/utils/__init__.py b/mmpose/utils/__init__.py index 14f6c8816b..c48ca01cea 100644 --- a/mmpose/utils/__init__.py +++ b/mmpose/utils/__init__.py @@ -1,7 +1,7 @@ # Copyright (c) OpenMMLab. All rights reserved. from .camera import SimpleCamera, SimpleCameraTorch from .collect_env import collect_env -from .config_utils import convert_mmdet_test_pipeline +from .config_utils import adapt_mmdet_pipeline from .logger import get_root_logger from .setup_env import register_all_modules, setup_multi_processes from .timer import StopWatch @@ -9,5 +9,5 @@ __all__ = [ 'get_root_logger', 'collect_env', 'StopWatch', 'setup_multi_processes', 'register_all_modules', 'SimpleCamera', 'SimpleCameraTorch', - 'convert_mmdet_test_pipeline' + 'adapt_mmdet_pipeline' ] diff --git a/mmpose/utils/config_utils.py b/mmpose/utils/config_utils.py index 10181a3de5..2f54d2ef24 100644 --- a/mmpose/utils/config_utils.py +++ b/mmpose/utils/config_utils.py @@ -2,7 +2,7 @@ from mmpose.utils.typing import ConfigDict -def convert_mmdet_test_pipeline(cfg: ConfigDict) -> ConfigDict: +def adapt_mmdet_pipeline(cfg: ConfigDict) -> ConfigDict: """Converts pipeline types in MMDetection's test dataloader to use the 'mmdet' namespace.