前言
2D卷不动了,来卷3D,之后更多地工作会放到3D检测上
本文将简单介绍什么是3D目标检测、KITTI数据集以及MMDetection3D算法库,重点介绍如何在MMDetection3D中,使用PointPillars算法训练KITTI数据集,并对结果进行测试和可视化。
3D目标检测概述
对于一张输入图像,2D目标检测旨在给出物体类别并标出物体位置,而3D目标检测则要给出物体的位置(x, y, z)、尺寸(x_size, y_size, z_size)以及大致方向(框的朝向角),检测结果的示意图如下所示:

与2D目标检测相比,3D检测要处理更加多元的数据类型,得到更复杂的目标检测结果:
| 2D检测 | 3D检测 | |
|---|---|---|
| 输入数据 | 图片 | 点云(LiDAR)、图片(Camera)、多模态(点云+图片) |
| 输出结果 | 4-DoF(矩形框+分类) | 7-DoF(位置、尺寸、朝向角)、9-DoF(位置、尺寸、3个方向角) |
| 评估标准 | IOU、mAP | 3D-IoU-based or center-distance-based mAP |
KITTI数据集简介
KITTI官网:The KITTI Vision Benchmark Suite
相关论文1:Vision meets Robotics: The KITTI Dataset相关论文2:Are we ready for Autonomous Driving? The KITTI Vision Benchmark Suite
KITTI数据集由德国卡尔斯鲁厄理工学院和丰田美国技术研究院联合创办,是目前国际上最大的自动驾驶场景下的计算机视觉算法评测数据集。作者收集了长达6个小时的真实交通环境,数据集由经过校正和同步的图像、雷达扫描、高精度的GPS信息和IMU加速信息等多种模态的信息组成。该数据集用于评测立体图像(stereo),光流(optical flow),视觉测距(visual odometry),3D物体检测(object detection)和3D跟踪(tracking)等计算机视觉技术在车载环境下的性能。
场景:Road、City、Residential、Campus、Person
类别:Car、Van、Truck、Pedestrian、Person_sitting、Cyclist、Tram、Misc、DontCare,其中DontCare标签表示该区域没有被标注
3D目标检测数据集由7481个训练图像和7518个测试图像以及相应的点云数据组成,包括总共80256个标记对象。具体来看,下图蓝色框标记的为我们需要的数据,分别是
- 彩色图像数据(12GB)
- 点云数据(29GB)
- 相机矫正数据(16MB)
- 标签数据(5MB)
其中彩色图像数据、点云数据、相机矫正数据均包含training(7481)和testing(7518)两个部分,标签数据只有training部分。

MMDetection3D
MMDetection3D 是一个基于 PyTorch 的目标检测开源工具箱,面向3D检测的平台,是 OpenMMlab 项目的一部分(由香港中文大学多媒体实验室和商汤科技联合发起)。对MMDet3D的配置文件不熟悉的小伙伴可以移步至:教程 1: 学习配置文件,本文使用版本为: Release V1.1.0rc0,下面,让我们玩耍起来!!!
环境搭建
这里直接放上官网文档中的安装脚本,言简意赅,适合无脑follow~
# 在Anaconda中新建虚拟环境
conda create -n mmdet3d python=3.7 -y
conda activate mmdet3d
# 安装最新的PyTorch版本
conda install -c pytorch pytorch torchvision -y
# install mmcv
pip install mmcv-full
# install mmdetection
pip install git+https://github.com/open-mmlab/mmdetection.git
# install mmsegmentation
pip install git+https://github.com/open-mmlab/mmsegmentation.git
# install mmdetection3d
git clone https://github.com/open-mmlab/mmdetection3d.git
cd mmdetection3d
pip install -v -e . # or "python setup.py develop"
# -v:verbose, or more output
# -e:editable,修改本地文件,调用的模块以最新文件为准
数据集准备
直接参考官网提供的下载-预处理-训练-测试一条龙服务:3D 目标检测 KITTI 数据集
在官网下载 KITTI 3D 检测数据并解压缩所有 zip 文件,并将数据集根目录链接到 ./mmdetection3d/data/,在预处理数据之前,数据集结构组织方式如下:
mmdetection3d
├── mmdet3d
├── tools
├── configs
├── data
│ ├── kitti
│ │ ├── ImageSets
│ │ ├── testing
│ │ │ ├── calib
│ │ │ ├── image_2
│ │ │ ├── velodyne
│ │ ├── training
│ │ │ ├── calib
│ │ │ ├── image_2
│ │ │ ├── label_2
│ │ │ ├── velodyne
│ │ │ ├── planes (optional)
下面开始创建 KITTI 点云数据,首先需要加载原始的点云数据并生成相关的包含目标标签和标注框的数据标注文件,同时还需要为 KITTI 数据集生成每个单独的训练目标的点云数据,并将其存储在 data/kitti/kitti_gt_database 的 .bin 格式的文件中,此外,需要为训练数据或者验证数据生成 .pkl 格式的包含数据信息的文件。
通过运行下面的命令来创建最终的 KITTI 数据:
# 进入mmdetection3d主目录
cd mmdetection3d
# 创建文件夹
mkdir ./data/kitti/ && mkdir ./data/kitti/ImageSets
# Download data split
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
python tools/create_data.py kitti --root-path ./data/kitti --out-dir ./data/kitti --extra-tag kitti --with-plane
处理完成之后的目录如下:
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 (optional)
├── 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
├── kitti_infos_train_mono3d.coco.json
├── kitti_infos_trainval_mono3d.coco.json
├── kitti_infos_test_mono3d.coco.json
├── kitti_infos_val_mono3d.coco.json
训练
数据集和环境都配置好后,就可以开始训练,使用命令python tools/train.py -h查看有哪些训练参数:
(mmdet3d) xxx@xxx:~/det3d/mmdetection3d$ python tools/train.py -h
usage: train.py [-h] [--work-dir WORK_DIR] [--resume-from RESUME_FROM]
[--auto-resume] [--no-validate]
[--gpus GPUS | --gpu-ids GPU_IDS [GPU_IDS ...] | --gpu-id
GPU_ID] [--seed SEED] [--diff-seed] [--deterministic]
[--options OPTIONS [OPTIONS ...]]
[--cfg-options CFG_OPTIONS [CFG_OPTIONS ...]]
[--launcher {
none,pytorch,slurm,mpi}]
[--local_rank LOCAL_RANK] [--autoscale-lr]
config
Train a detector
positional arguments:
config train config file path
optional arguments:
-h, --help show this help message and exit
--work-dir WORK_DIR the dir to save logs and models
--resume-from RESUME_FROM
the checkpoint file to resume from
--auto-resume resume from the latest checkpoint automatically
--no-validate whether not to evaluate the checkpoint during training
--gpus GPUS (Deprecated, please use --gpu-id) number of gpus to
use (only applicable to non-distributed training)
--gpu-ids GPU_IDS [GPU_IDS ...]
(Deprecated, please use --gpu-id) ids of gpus to use
(only applicable to non-distributed training)
--gpu-id GPU_ID number of gpus to use (only applicable to non-
distributed training)
--seed SEED random seed
--diff-seed Whether or not set different seeds for different ranks
--deterministic whether to set deterministic options for CUDNN
backend.
--options OPTIONS [OPTIONS ...]
override some settings in the used config, the key-
value pair in xxx=yyy format will be merged into
config file (deprecate), change to --cfg-options
instead.
--cfg-options CFG_OPTIONS [CFG_OPTIONS ...]
override some settings in the used config, the key-
value pair in xxx=yyy format will be merged into
config file. If the value to be overwritten is a list,
it should be like key="[a,b]" or key=a,b It also
allows nested list/tuple values, e.g.
key="[(a,b),(c,d)]" Note that the quotation marks are
necessary and that no white space is allowed.
--launcher {
none,pytorch,slurm,mpi}
job launcher
--local_rank LOCAL_RANK
--autoscale-lr automatically scale lr with the number of gpus
几个关键参数:
- configs:必选参数,训练模型的参数配置文件
- work-dir:可选参数,训练日志及权重文件保存文件夹,默认会新建
work-dirs文件夹,并保存在以configs文件名命名的文件夹中 - gpu-id:使用的GPU个数
上述参数我们可以在命令行执行训练命令时,直接进行修改,但有些参数需要我们进入模型配置文件进行修改,这里以hv_pointpillars_secfpn_6x8_160e_kitti-3d-3class配置文件为例:
- 修改训练Epoch数:打开
/mmdetection3d/configs/pointpillars/hv_pointpillars_secfpn_6x8_160e_kitti-3d-3class.py文件,修改runner = dict(max_epochs=160)中的max_epochs参数
- 修改batch-size:打开
/mmdetection3d/configs/_base_/datasets/kitti-3d-3class.py文件,修改samples_per_gpu参数
设置好参数后,我们就可以直接执行命令进行训练了:
单GPU训练:
python tools/train.py configs/pointpillars/hv_pointpillars_secfpn_6x8_160e_kitti-3d-3class.py
多GPU训练:
CUDA_VISIBLE_DEVICES=0,1,2,3 tools/dist_train.sh configs/pointpillars/hv_pointpillars_secfpn_6x8_160e_kitti-3d-3class.py 4
训练结束后,我们可以在/mmdetection3d/work-dirs/hv_pointpillars_secfpn_6x8_160e_kitti-3d-3class文件夹中看到训练结果,包括日志文件(.log)、权重文件(.pth)以及模型配置文件(.py)等。
测试及可视化
首先使用命令查看测试函数有哪些可传入参数:python tools/test.py -h:
(mmdet3d) xxx@xxx:~/det3d/mmdetection3d$ python tools/test.py -h
usage: test.py [-h] [--out OUT] [--fuse-conv-bn]
[--gpu-ids GPU_IDS [GPU_IDS ...]] [--gpu-id GPU_ID]
9436



被折叠的 条评论
为什么被折叠?



