Repository: open-mmlab/mmdetection3d

Issue:
[Bug] _draw_instances_3d crashes when no detection
### Prerequisite

- [X] I have searched [Issues](https://github.com/open-mmlab/mmdetection3d/issues) and [Discussions](https://github.com/open-mmlab/mmdetection3d/discussions) but cannot get the expected help.
- [X] I have read the [FAQ documentation](https://github.com/open-mmlab/mmdetection3d/blob/1.1/docs/en/notes/faq.md) but cannot get the expected help.
- [X] The bug has not been fixed in the [latest version (dev)](https://github.com/open-mmlab/mmdetection3d/tree/dev) or [latest version (1.x)](https://github.com/open-mmlab/mmdetection3d/tree/dev-1.x).

### Task

I'm using the official example scripts/configs for the officially supported tasks/models/datasets.

### Branch

1.1x branch https://github.com/open-mmlab/mmdetection3d/tree/1.1

### Environment

sys.platform: linux
Python: 3.8.15 | packaged by conda-forge | (default, Nov 22 2022, 08:49:35) [GCC 10.4.0]
CUDA available: True
numpy_random_seed: 2147483648
GPU 0: NVIDIA T1200 Laptop GPU
CUDA_HOME: /home/mambaforge/envs/openmmlab
NVCC: Cuda compilation tools, release 11.7, V11.7.99
GCC: gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
PyTorch: 1.13.0
PyTorch compiling details: PyTorch built with:
  - GCC 9.3
  - C++ Version: 201402
  - Intel(R) oneAPI Math Kernel Library Version 2022.1-Product Build 20220311 for Intel(R) 64 architecture applications
  - Intel(R) MKL-DNN v2.6.0 (Git Hash 52b5f107dd9cf10910aaa19cb47f3abf9b349815)
  - OpenMP 201511 (a.k.a. OpenMP 4.5)
  - LAPACK is enabled (usually provided by MKL)
  - NNPACK is enabled
  - CPU capability usage: AVX2
  - CUDA Runtime 11.7
  - NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;arch=compute_86,code=sm_86;-gencode;arch=compute_37,code=compute_37
  - CuDNN 8.5
  - Magma 2.6.1
  - Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=11.7, CUDNN_VERSION=8.5.0, CXX_COMPILER=/opt/rh/devtoolset-9/root/usr/bin/c++, CXX_FLAGS= -fabi-version=11 -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -DNDEBUG -DUSE_KINETO -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE -DEDGE_PROFILER_USE_KINETO -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -Werror=non-virtual-dtor -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wunused-local-typedefs -Wno-unused-parameter -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Werror=cast-function-type -Wno-stringop-overflow, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, TORCH_VERSION=1.13.0, USE_CUDA=ON, USE_CUDNN=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON, USE_ROCM=OFF, 

TorchVision: 0.14.0
OpenCV: 4.6.0
MMEngine: 0.3.2
MMDetection: 3.0.0rc4
MMDetection3D: 1.1.0rc2+7fd57b1
spconv2.0: False

### Reproduces the problem - code sample

NA

### Reproduces the problem - command or script

```shell
python ./tools/test.py ./configs/pointpillars/debug_train_config.py ./work_dirs/debug_train_config/epoch_80.pth --show
```

### Reproduces the problem - error message

```
  File "/home/workspace/mmdetection3d-1.1/mmdet3d/visualization/local_visualizer.py", line 731, in add_datasample
    gt_data_3d = self._draw_instances_3d(
  File "/home/workspace/mmdetection3d-1.1/mmdet3d/visualization/local_visualizer.py", line 564, in _draw_instances_3d
    bboxes_3d = instances.bboxes_3d  # BaseInstance3DBoxes
AttributeError: 'InstanceData' object has no attribute 'bboxes_3d'
```

### Additional information

Code below fails when there is no detection 

https://github.com/open-mmlab/mmdetection3d/blob/d7067e4430f4e99697b382b0e3ea5597af737a2c/mmdet3d/visualization/local_visualizer.py#L543-L560


Discussion:
Thanks for reminder, I'll fix it ASAP.
Hi, I also found the same problem, is it fixed yet or any quick workaround.
> Hi, I also found the same problem, is it fixed yet or any quick workaround.

A quick workaround is to use try except for this line `bboxes_3d = instances.bboxes_3d`

Resolving patch:
diff --git a/mmdet3d/visualization/local_visualizer.py b/mmdet3d/visualization/local_visualizer.py
--- a/mmdet3d/visualization/local_visualizer.py
+++ b/mmdet3d/visualization/local_visualizer.py
@@ -557,6 +557,10 @@ def _draw_instances_3d(self, data_input: dict, instances: InstanceData,
             dict: the drawn point cloud and image which channel is RGB.
         """
 
+        # Only visualize when there is at least one instance
+        if not len(instances) > 0:
+            return None
+
         bboxes_3d = instances.bboxes_3d  # BaseInstance3DBoxes
 
         data_3d = dict()
@@ -786,6 +790,8 @@ def add_datasample(self,
                 drawn_img_3d = gt_data_3d['img']
             elif pred_data_3d is not None:
                 drawn_img_3d = pred_data_3d['img']
+            else:  # both instances of gt and pred are empty
+                drawn_img_3d = None
         else:
             drawn_img_3d = None
 


Test patch: