From e45cf1b74027f0ef876296934dc9c1163e00ed57 Mon Sep 17 00:00:00 2001 From: JingweiZhang12 Date: Tue, 15 Nov 2022 14:09:12 +0800 Subject: [PATCH] simplify the warning and error when using open3d and MinkowskiEngine --- mmdet3d/models/backbones/mink_resnet.py | 9 +++++---- mmdet3d/models/dense_heads/fcaf3d_head.py | 6 +++++- mmdet3d/models/detectors/mink_single_stage.py | 7 +++++-- mmdet3d/visualization/local_visualizer.py | 9 ++++++--- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/mmdet3d/models/backbones/mink_resnet.py b/mmdet3d/models/backbones/mink_resnet.py index e4bae72a87..f295714e6a 100644 --- a/mmdet3d/models/backbones/mink_resnet.py +++ b/mmdet3d/models/backbones/mink_resnet.py @@ -5,11 +5,8 @@ import MinkowskiEngine as ME from MinkowskiEngine.modules.resnet_block import BasicBlock, Bottleneck except ImportError: - import warnings - warnings.warn( - 'Please follow `getting_started.md` to install MinkowskiEngine.`') # blocks are used in the static part of MinkResNet - BasicBlock, Bottleneck = None, None + ME = BasicBlock = Bottleneck = None import torch.nn as nn @@ -38,6 +35,10 @@ class MinkResNet(nn.Module): def __init__(self, depth, in_channels, num_stages=4, pool=True): super(MinkResNet, self).__init__() + if ME is None: + raise ImportError( + 'Please follow `getting_started.md` to install MinkowskiEngine.`' # noqa: E501 + ) if depth not in self.arch_settings: raise KeyError(f'invalid depth {depth} for resnet') assert 4 >= num_stages >= 1 diff --git a/mmdet3d/models/dense_heads/fcaf3d_head.py b/mmdet3d/models/dense_heads/fcaf3d_head.py index f43cf698a1..22652e30c6 100644 --- a/mmdet3d/models/dense_heads/fcaf3d_head.py +++ b/mmdet3d/models/dense_heads/fcaf3d_head.py @@ -7,7 +7,7 @@ from MinkowskiEngine import SparseTensor except ImportError: # Please follow getting_started.md to install MinkowskiEngine. - SparseTensor = None + ME = SparseTensor = None pass import torch @@ -69,6 +69,10 @@ def __init__(self, test_cfg: Optional[dict] = None, init_cfg: Optional[dict] = None): super(FCAF3DHead, self).__init__(init_cfg) + if ME is None: + raise ImportError( + 'Please follow `getting_started.md` to install MinkowskiEngine.`' # noqa: E501 + ) self.voxel_size = voxel_size self.pts_prune_threshold = pts_prune_threshold self.pts_assign_threshold = pts_assign_threshold diff --git a/mmdet3d/models/detectors/mink_single_stage.py b/mmdet3d/models/detectors/mink_single_stage.py index 120743349b..12394fcbc0 100644 --- a/mmdet3d/models/detectors/mink_single_stage.py +++ b/mmdet3d/models/detectors/mink_single_stage.py @@ -9,6 +9,7 @@ import MinkowskiEngine as ME except ImportError: # Please follow getting_started.md to install MinkowskiEngine. + ME = None pass from mmdet3d.registry import MODELS @@ -56,9 +57,11 @@ def __init__(self, test_cfg=test_cfg, data_preprocessor=data_preprocessor, init_cfg=init_cfg) + if ME is None: + raise ImportError( + 'Please follow `getting_started.md` to install MinkowskiEngine.`' # noqa: E501 + ) self.voxel_size = bbox_head['voxel_size'] - # # TODO: unify the keys - # self.head = self.bbox_head def extract_feat( self, batch_inputs_dict: Dict[str, Tensor] diff --git a/mmdet3d/visualization/local_visualizer.py b/mmdet3d/visualization/local_visualizer.py index 065683c2dc..5f9f4188ee 100644 --- a/mmdet3d/visualization/local_visualizer.py +++ b/mmdet3d/visualization/local_visualizer.py @@ -26,9 +26,9 @@ try: import open3d as o3d from open3d import geometry + from open3d.visualization import Visualizer except ImportError: - raise ImportError( - 'Please run "pip install open3d" to install open3d first.') + o3d = geometry = Visualizer = None @VISUALIZERS.register_module() @@ -126,7 +126,7 @@ def _clear_o3d_vis(self) -> None: del self.pcd del self.points_colors - def _initialize_o3d_vis(self, frame_cfg) -> o3d.visualization.Visualizer: + def _initialize_o3d_vis(self, frame_cfg) -> Visualizer: """Initialize open3d vis according to frame_cfg. Args: @@ -136,6 +136,9 @@ def _initialize_o3d_vis(self, frame_cfg) -> o3d.visualization.Visualizer: Returns: :obj:`o3d.visualization.Visualizer`: Created open3d vis. """ + if o3d is None or geometry is None: + raise ImportError( + 'Please run "pip install open3d" to install open3d first.') o3d_vis = o3d.visualization.Visualizer() o3d_vis.create_window() # create coordinate frame