基于 mmdetection3D 的点云目标检测

mmdetection3d 是基于 mmdetection 框架的扩展,专注于提供用于 3D 目标检测的工具和模型。它是一个通用的三维检测框架,具有高度的灵活性和模块化设计,能够适应多种三维检测任务,并提供多种现代的 3D 检测算法。
PointPillars 是一种针对点云数据的三维目标检测方法。它利用了特定的点云编码和特征提取策略,通过将点云数据转换为二维的特征图表示,然后应用常规的二维目标检测算法来实现三维目标检测。它是 mmdetection3d 中的一种实现或模型之一,但 mmdetection3d 并不仅限于 PointPillars 这一种方法。
本文参考了:https://blog.youkuaiyun.com/m0_46556474/article/details/130778016

(一)MMDetection3D环境配置

项目地址: https://github.com/open-mmlab/mmdetection3d

第一步:Anaconda+Pytorch环境搭建

Anaconda环境配置,下载地址:https://www.anaconda.com/download

1.1 pytorch环境配置

打开Anaconda Prompt,创建虚拟环境pytorch-mmdet3d并激活,该环境在Anaconda/envs中。

conda create -n pytorch-mmdet3d python=3.8
conda activate pytorch-mmdet3d
conda install pytorch==1.10.1 torchvision==0.11.2 torchaudio==0.10.1 cudatoolkit=11.3 -c pytorch -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

1.2 验证pytorch环境

python                     # 3.8.18
import torch
torch.__version__          # 1.10.1
torch.version.cuda         # 11.3
torch.cuda.is_available()  # True
exit()

验证成功,如下:

第二步:安装MMDetection3D

2.1 预先安装包

pip install openmim -i https://pypi.tuna.tsinghua.edu.cn/simple
mim install mmcv-full -i https://pypi.tuna.tsinghua.edu.cn/simple
mim install mmdet -i https://pypi.tuna.tsinghua.edu.cn/simple
mim install mmsegmentation -i https://pypi.tuna.tsinghua.edu.cn/simple

2.2 克隆MMDetection3D

git clone https://github.com/open-mmlab/mmdetection3d.git
cd mmdetection3d
pip install -e .

2.3 验证MMDetection3D环境

python                     # 3.8.18
import open3d
import mmcv
import mmdet
import mmdet3d
mmdet3d.__version__        # 1.3.0
exit()

验证成功如下:

第三步:运行Demo进行测试

3.1 使用second模型测试demo

/mmdetection3d/configs/second中下载预训练模型及对应权重文件。
选择预训练模型second_hv_secfpn_8xb6-80e_kitti-3d-3class.py,打开metafile.yml,下载预训练模型对应的权重文件second_hv_secfpn_8xb6-80e_kitti-3d-3class-b086d0a3.pth,并保存在/mmdetection3d/checkpoint文件夹里。
运行下面代码,结果保存在outputs中。

set DISPLAY=:0 #显示图像需要指定设备
pip install open3d
python demo/pcd_demo.py demo/data/kitti/000008.bin configs/second/second_hv_secfpn_8xb6-80e_kitti-3d-3class.py checkpoints/second_hv_secfpn_8xb6-80e_kitti-3d-3class-b086d0a3.pth --show
#利用python直接进行测试,但是由于函数被更改,无法显示图片
python
#from mmdet3d.apis import init_model, inference_detector, show_result_meshlab show_result_meshlab被取消
from mmdet3d.apis import init_model, inference_detector
config_file = 'configs/second/second_hv_secfpn_8xb6-80e_kitti-3d-3class.py'
checkpoint_file = 'checkpoints/second_hv_secfpn_8xb6-80e_kitti-3d-3class-b086d0a3.pth'
model = init_model(config_file, checkpoint_file, device = 'cuda:0')

pcd = 'demo/data/kitti/000008.bin'
result, data = inference_detector(model, pcd)

原图如下:

可视化的结果展示如下(对应权重文件存在问题):

3.2 使用pointpillars模型测试demo

下载配置文件和模型权重文件:

mim download mmdet3d --config pointpillars_hv_secfpn_8xb6-160e_kitti-3d-car --dest .

运行验证:

set DISPLAY=:0 #显示图像需要指定设备
python demo/pcd_demo.py demo/data/kitti/000008.bin pointpillars_hv_secfpn_8xb6-160e_kitti-3d-car.py hv_pointpillars_secfpn_6x8_160e_kitti-3d-car_20220331_134606-d42d15ed.pth --show

运行结果:

3.3 使用smoke图像3D检测测试demo

检查点中对应权重文件(.pth文件)需要自己提前下载,可以mim或者直接网址下载。

python demo/mono_det_demo.py demo/data/nuscenes/n015-2018-07-24-11-22-45+0800__CAM_BACK__1532402927637525.jpg configs/smoke/smoke_dla34_dlaneck_gn-all_4xb8-6x_kitti-mono3d.py checkpoints/smoke_dla34_pytorch_dlaneck_gn-all_8x4_6x_kitti-mono3d_20210929_015553-d46d9bb0.pth --show

存在权重问题,代码有bug。

第四步:Kitty数据集下载与处理

4.1 Kitty数据集下载

官网网址:KITTI 3D object detection dataset
道路平面信息:https://download.openmmlab.com/mmdetection3d/data/train_planes.zip
其中,道路平面信息是由 AVOD 生成的,其在训练过程中作为一个可选项,用来提高模型的性能。

4.2 官网下载KITTI数据集后的组织结构

在官网下载KITTI数据集后,在mmdetection3d文件夹下组织成以下所示的结构。

mmdetection3d
├── configs
├── mmdet3d
├── tools
├── data
│   ├── kitti
│   │   ├── testing
│   │   │   ├── calib
│   │   │   ├── image_2
│   │   │   ├── velodyne
│   │   ├── training
│   │   │   ├── calib
│   │   │   ├── image_2
│   │   │   ├── label_2
│   │   │   ├── velodyne
│   │   │   ├── planes

4.3 数据预处理

项目的数据预处理需要使用tools/create_data.py重新处理一次。
创建 KITTI 点云数据,首先需要加载原始的点云数据并生成相关的包含目标标签和标注框的数据标注文件,同时还需要为 KITTI 数据集生成每个单独的训练目标的点云数据,并将其存储在 data/kitti/kitti_gt_database.bin 格式的文件中。此外,需要为训练数据或者验证数据生成 .pkl 格式的包含数据信息的文件。

cd mmdetection3d
mkdir ./data/kitti/ImageSets

# Download data split
#wegt未安装或者版本较低时,需要手动下载安装并设置环境变量
wget -c  https://raw.githubusercontent.com/traveller59/second.pytorch/master/second/data/ImageSets/test.txt --no-check-certificate --content-disposition -O ./data/kitti/ImageSets/test.txt
wget -c  https://raw.githubusercontent.com/traveller59/second.pytorch/master/second/data/ImageSets/train.txt --no-check-certificate --content-disposition -O ./data/kitti/ImageSets/train.txt
wget -c  https://raw.githubusercontent.com/traveller59/second.pytorch/master/second/data/ImageSets/val.txt --no-check-certificate --content-disposition -O ./data/kitti/ImageSets/val.txt
wget -c  https://raw.githubusercontent.com/traveller59/second.pytorch/master/second/data/ImageSets/trainval.txt --no-check-certificate --content-disposition -O ./data/kitti/ImageSets/trainval.txt

# Data preprocessing
python tools/create_data.py kitti --root-path ./data/kitti --out-dir ./data/kitti --extra-tag kitti --with-plane

小吐槽:我的wegt需要重新安装(https://eternallybored.org/misc/wget/,下载.exe程序放入C:\Windows\System32)。此外,我下载过程一直出现主机地址无法解析的问题,解决方法是重新手动配置DNS,找到正在使用的网络连接,右键点击并选择 “属性”,选中 “Internet 协议版本 4(TCP/IPv4)” ,手动设置 DNS:我选用了Google 的 DNS 服务器:首选 DNS 为 8.8.8.8,备用 DNS 为 8.8.4.4,更改后重连网络即可。

运行完成后,如图:

目录结构如下:

kitti
├── ImageSets
│   ├── test.txt
│   ├── train.txt
│   ├── trainval.txt
│   ├── val.txt
├── testing
│   ├── calib
│   ├── image_2
│   ├── velodyne
│   ├── velodyne_reduced
├── training
│   ├── calib
│   ├── image_2
│   ├── label_2
│   ├── velodyne
│   ├── velodyne_reduced
│   ├── planes 
├── kitti_gt_database
│   ├── xxxxx.bin
├── kitti_infos_train.pkl
├── kitti_infos_val.pkl
├── kitti_dbinfos_train.pkl
├── kitti_infos_test.pkl
├── kitti_infos_trainval.pkl

第五步:训练与测试

5.1 在KITTI数据集上训练pointpillars

1.修改配置文件

  • 修改epoch:打开/mmdetection3d/configs/pointpillars/pointpillars_hv_secfpn_8xb6-160e_kitti-3d-3class.py文件,修改文件中的epoch_num = 80中的epoch_num参数。(我修改为6)
  • 修改batch_size:打开/mmdetection3d/configs/_base_/datasets/kitti-3d-3class.py文件,修改文件中的batch_size=6中的batch_size参数,根据自己显卡的显存进行修改。(我修改为4)

2.执行命令,开始训练

## 单卡训练(我采用了这个)
python tools/train.py configs/pointpillars/pointpillars_hv_secfpn_8xb6-160e_kitti-3d-3class.py

## 多卡训练
CUDA_VISIBLE_DEVICES=0,1,2,3 tools/dist_train.sh configs/pointpillars/pointpillars_hv_secfpn_8xb6-160e_kitti-3d-3class.py 4

训练结束后,可以在/mmdetection3d/work-dirs/pointpillars_hv_secfpn_8xb6-160e_kitti-3d-3class文件夹中看到训练结果,包括日志文件(.log)、权重文件(.pth)以及模型配置文件(.py)等,结果如下:

训练过程运行截图如下:

3.可能出现的错误及解决方案:

  • AttributeError: module ‘numpy’ has no attribute ‘long’错误:重装numpy, pip install numpy==1.23.0

5.2 在KITTI数据集上测试pointpillars

测试文件tools/test.py有两个必选参数config和checkpoint,分别为模型配置文件和训练生成的权重文件,其他几个比较重要的参数:

  • eval:使用的评价指标,取决于数据集(“bbox”, “segm”, “proposal” for COCO, and “mAP”, “recall” for PASCAL VOC),这里直接沿用了2D检测中常用的几个评价标准。
  • show:是否对测试结果进行可视化,需要安装open3d库(没有的话,直接pip install open3d安装一下即可)。
  • show_dir:测试结果的保存目录。

根据自己训练后得到的权重文件进行测试:

## 单卡测试
python tools/test.py configs/pointpillars/pointpillars_hv_secfpn_8xb6-160e_kitti-3d-3class.py work_dirs/pointpillars_hv_secfpn_8xb6-160e_kitti-3d-3class/epoch_6.pth

测试结果如下:


点云的可视化要比图像复杂,需要借助3D显示的工具和库,例如Mayavi、Open3D等。MMDetection 3D基于Open3D构建了若干可视化点云的工具,这里我们采用Open3D来进行点云可视化,使用命令pip install open3d安装Open3D库。

set display=0  #否则报错AttributeError: 'Det3DLocalVisualizer' object has no attribute 'view_control'
# 点云可视化
python tools/misc/browse_dataset.py configs/_base_/datasets/kitti-3d-3class.py --task lidar_det --output-dir work_dirs/lidar

运行上面的命令后将依次展现出KITTI数据集所有点云的可视化结果,如下图所示(部分):
在这里插入图片描述
在这里插入图片描述
图片结果保存在D:\mmdetection3d\work_dirs\lidar,如下:
在这里插入图片描述
选取第一张图片的原图与点云目标检测进行对比如下:

在这里插入图片描述
在这里插入图片描述

MMDetection3D是一个基于PyTorch的目标检测开源工具箱,是OpenMMLab项目的一部分。该项目由香港中文大学多媒体实验室和商汤科技联合发起,旨在提供一个面向3D检测的平台。MMDetection3D涵盖了多个3D深度学习任务,包括3D目标检测、单目3D目标检测、多模态3D目标检测3D语义分割等。它还复现了最新的一些论文和成果,特别是包括了大量CVPR论文的复现。你可以在GitHub上找到MMDetection3D的项目地址:\[1\]。如果你想安装MMDetection3D,可以参考官方提供的安装指南:\[2\]。 #### 引用[.reference_title] - *1* *3* [【mmdetection3dmmdetection3d安装详细步骤](https://blog.youkuaiyun.com/suiyingy/article/details/123477237)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【MMDetection3D】环境搭建,使用PointPillers训练&测试&可视化KITTI数据集](https://blog.youkuaiyun.com/weixin_43799388/article/details/127040587)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值