docs/应用实践/开发调试教程/Apollo感知实践/感知模型部署训练/点云模型二次开发.md
目前,Apollo 中提供了以下几个 lidar 检测模型:centerpoint,pointpillars,maskpillars,cnnseg。
为了支持用户将自己训练的 lidar 检测模型部署到 apollo 中,本文将以 centerpoint 模型和 paddle3d 开源代码仓库为基础,介绍用户如何在 apollo 中新增模型,包括:paddle3d 的安装、训练配置介绍、centerpoint 的训练评测和导出,以及如何部署 centerpoint 到 apollo 中查看效果。
centerpoint 的完整训练流程可参考 AI Studio 项目:
接下来将以 centerpoint_pillars_016voxel_kitti.yml 为例,介绍训练配置。
| 配置名称 | 默认值 | 含义 |
|---|---|---|
| batch_size | 4 | 训练时一个batch包含的点云帧数 |
| val_batch_size | 1 | 评测时一个batch包含的点云帧数 |
| epochs | 160 | epoch个数 |
自动混合精度(Automatic Mixed Precision,以下简称为 AMP)指的是在训练时使用fp16精度,提升计算速度并降低存储空间。详细介绍参见 自动混合精度训练(AMP)。
| 配置名称 | 默认值 | 含义 |
|---|---|---|
| use_amp | False | 是否开启amp训练 |
| enable | False | - |
| level | O1 | 混合精度训练的策略,可选项:O1,O2 |
| scalerinit_loss_scaling | 32.0 | 控制 loss 缩放比例,规避浮点数下溢问题 |
SamplingDatabase
| 数据增强之前 | 数据增强之后 |
|---|---|
接下来以 KITTI 数据集为例,训练 CenterPoint 模型。
首先请在 官网 进行下载相关数据
Download Velodyne point clouds, if you want to use laser information (29 GB)
training labels of object data set (5 MB)
camera calibration matrices of object data set (16 MB)
下载数据集的划分文件列表:
wget https://bj.bcebos.com/paddle3d/datasets/KITTI/ImageSets.tar.gz
将数据解压后按照下方的目录结构进行组织:
kitti_dataset_root
|—— training
| |—— label_2
| | |—— 000001.txt
| | |—— ...
| |—— calib
| | |—— 000001.txt
| | |—— ...
| |—— velodyne
| | |—— 000001.bin
| | |—— ...
|—— ImageSets
│ |—— test.txt
│ |—— train.txt
│ |—— trainval.txt
│ |—— val.txt
在 Paddle3D 的目录下创建软链接datasets/KITTI,指向到上面的数据集目录:
mkdir datasets
ln -s /path/to/kitti_dataset_root ./datasets
mv ./datasets/kitti_dataset_root ./datasets/KITTI
生成训练时数据增强所需的真值库:
python tools/create_det_gt_database.py --dataset_name kitti --dataset_root ./datasets/KITTI --save_dir ./datasets/KITTI
-dataset_root指定 KITTI 数据集所在路径,--save_dir指定用于保存所生成的真值库的路径。该命令执行后,save_dir生成的目录如下:
kitti_train_gt_database
|—— anno_info_train.pkl
|—— Car
| |—— 4371_Car_7.bin
| |—— ...
|—— Cyclist
注意:如果想用自己的数据集进行训练,推荐将自己的数据集组织为 KITTI 标准格式。
KITTI 数据集上的训练使用 8 张 GPU:
python -m paddle.distributed.launch --gpus 0,1,2,3,4,5,6,7 tools/train.py --config configs/centerpoint/centerpoint_pillars_016voxel_kitti.yml --save_dir ./output_kitti --num_workers 4 --save_interval 5
训练启动参数介绍:
| 参数名 | 用途 | 是否必选项 | 默认值 |
|---|---|---|---|
| iters | 训练迭代步数 | 否 | 配置文件中指定值 |
| epochs | 训练迭代次数 | 否 | 配置文件中指定值 |
| batch_size | 单卡batch size | 否 | 配置文件中指定值 |
| learning_rate | 初始学习率 | 否 | 配置文件中指定值 |
| config | 配置文件路径 | 是 | - |
| save_dir | 检查点(模型和visualdl日志文件)的保存根路径 | 否 | output |
| num_workers | 用于异步读取数据的进程数量, 大于等于1时开启子进程读取数据 | 否 | 2 |
| save_interval | 模型保存的间隔步数 | 否 | 1000 |
| do_eval | 是否在保存模型时启动评估 | 否 | 否 |
| log_interval | 打印日志的间隔步数 | 否 | 10 |
| resume | 是否从检查点中恢复训练状态 | 否 | None |
| keep_checkpoint_max | 最多保存模型的数量 | 否 | 5 |
| quant_config | 量化配置文件,一般放在configs/quant目录下 | 否 | None |
| seed | Paddle/numpy/random的全局随机种子值 | 否 | None |
| model | 基于预训练模型进行finetune | 否 | .pdparam模型文件 |
python tools/evaluate.py --config configs/centerpoint/centerpoint_pillars_016voxel_kitti.yml --model ./output_kitti/epoch_160/model.pdparams --batch_size 1 --num_workers 4
注意:CenterPoint 的评估目前只支持 batch_size 为 1。评估启动参数介绍如下所示:
| 参数名 | 用途 | 是否必选项 | 默认值 |
|---|---|---|---|
| batch_size | 单卡batch size | 否 | 配置文件中指定值 |
| config | 配置文件路径 | 是 | - |
| model | 模型参数文件的路径 | 是 | - |
| num_workers | 用于异步读取数据的进程数量, 大于等于1时开启子进程读取数据 | 否 | 2 |
| quant_config | 量化配置文件,一般放在configs/quant目录下 | 否 | None |
Paddle3D 使用 VisualDL 来记录训练过程中的指标和数据,我们可以在训练过程中,在命令行使用 VisualDL 启动一个 server,并在浏览器查看相应的数据
# logdir需要和训练脚本中指定的save_dir保持一致
visualdl --logdir output
运行以下命令,将训练时保存的动态图模型文件导出成推理引擎能够加载的静态图模型文件。
python tools/export.py --config configs/centerpoint/centerpoint_pillars_016voxel_kitti.yml --model /path/to/model.pdparams --save_dir /path/to/output
导出脚本参数介绍
| 参数名 | 用途 | 是否必选项 | 默认值 |
|---|---|---|---|
| config | 配置文件路径 | 是 | - |
| model | 模型参数路径 | 否 | - |
| export_for_apollo | 是否用于Apollo部署,当打开该开关时,会同步生成用于Apollo部署的meta文件 | 否 | False |
| save_dir | 推理模型文件的保存路径 | 否 | exported_model |
| save_name | 推理模型文件的保存名字 | 否 | None(由各模型自定决定) |
| quant_config | 量化配置文件,如果模型使用量化训练,则在模型导出时同样需要指定量化配置文件 | 否 |
运行完成之后,会输出如下的文件:
|-- centerpoint_pillars_016voxel_kitti
|-- apollo_deploy.yaml
|-- centerpoint.pdiparams
|-- centerpoint.pdiparams.info
|-- centerpoint.pdmodel
接下来介绍如何将导出的 centerpoint 模型部署到 apollo 中,使用 centerpoint 模型进行 3d 目标检测:
首先介绍一下 centerpoint 部署的配置,配置文件路径:/apollo/modules/perception/lidar_detection/data/center_point_param.pb.txt。
# center point params
info {
name: "center_point"
version: ""
dataset: "apollo"
task_type: Detection3D
sensor_type: Lidar
framework: PaddlePaddle
proto_file {
file: "centerpoint.pdmodel"
}
weight_file {
file: "centerpoint.pdiparams"
}
inputs {name: "data"}
outputs {name: "_generated_var_9"}
outputs {name: "_generated_var_10"}
outputs {name: "_generated_var_11"}
}
preprocess {
gpu_id: 0
normalizing_factor: 255
num_point_feature: 4
enable_ground_removal: false
ground_removal_height: -1.5
enable_downsample_beams: false
downsample_beams_factor: 1
enable_downsample_pointcloud: true
downsample_voxel_size_x: 0.09
downsample_voxel_size_y: 0.09
downsample_voxel_size_z: 0.09
enable_fuse_frames: false
num_fuse_frames: 5
fuse_time_interval: 0.5
enable_shuffle_points: false
max_num_points: 2147483647
reproduce_result_mode: false
enable_roi_outside_removal: false
}
postprocess {
score_threshold: 0.25
num_output_box_feature: 7
bottom_enlarge_height: 0.25
top_enlarge_height: 0.25
width_enlarge_value: 0
length_enlarge_value: 0
}
paddle_settings {
use_trt: false
trt_precision: 1
trt_use_static: true
use_calibration: true
collect_shape_info: false
use_dynamicshape: true
dynamic_shape_file: "modules/perception/lidar_detection/data/center_point_paddle/collect_shape_info_3lidar_20.pbtxt"
}
point2box_max_num: 5
quantize: 0.2
ModelInfo 配置,文件路径:modules/perception/common/proto/model_info.proto。
| 参数类型 | 参数名 | 默认值 | 含义 |
|---|---|---|---|
| string | name | / | 模型名称,同models/下文件夹名 |
| string | framework | / | 模型推理框架 |
| string | ModelFile.proto_file | / | 模型网络结构 |
| string | ModelFile.weight_file | / | 模型权重文件 |
| string | ModelFile.anchors_file | / | anchor size |
| string | ModelBlob.inputs | / | 模型输入数据名称及维度 |
| int32 | ModelBlob.outputs | / | 模型输出数据名称及维度 |
PointCloudPreProcess:
| 参数类型 | 参数名 | 默认值 | 含义 |
|---|---|---|---|
| int32 | gpu_id | 0 | GPU的id |
| double | normalizing_factor | 255 | 强度归一化的缩放因子 |
| int32 | num_point_feature | 4 | 每个点的特征数量 |
| bool | enable_ground_removal | false | 是否过滤掉地面点 |
| double | ground_removal_height | -1.5 | 过滤掉z值小于阈值的点 |
| bool | enable_downsample_beams | false | 是否根据beam id对点云进行过滤 |
| int32 | downsample_beams_factor | 4 | 保留beam id为downsample_beams_factor的倍数的点云 |
| bool | enable_downsample_pointcloud | false | 是否根据voxel过滤点云 |
| double | downsample_voxel_size_x | 0.01 | 过滤时voxel的x方向长度 |
| double | downsample_voxel_size_y | 0.01 | 过滤时voxel的y方向长度 |
| double | downsample_voxel_size_z | 0.01 | 过滤时voxel的z方向长度 |
| bool | enable_fuse_frames | false | 是否融合多帧点云 |
| int32 | num_fuse_frames | 5 | 融合点云的帧数 |
| double | fuse_time_interval | 0.5 | 融合点云的时间间隔 |
| bool | enable_shuffle_points | false | 是否打乱点云索引 |
| int32 | max_num_points | 2147483647 | 允许的最大点云数量 |
| bool | reproduce_result_mode | false | 是否开启复现结果模式 |
| bool | enable_roi_outside_removal | false | 是否在输入模型之前将roi外的点云进行过滤 |
PointCloudPostProcess
| 参数类型 | 参数名 | 默认值 | 含义 |
|---|---|---|---|
| float | score_threshold | 0.5 | 置信度阈值 |
| float | nms_overlap_threshold | 0.5 | NMS的iou阈值 |
| int32 | num_output_box_feature | 7 | 输出障碍物的属性个数 |
| float | bottom_enlarge_height | 0.25 | 获取目标真实点云时向上扩充的范围 |
| float | top_enlarge_height | 0.25 | 获取目标真实点云时向下扩充的范围 |
| float | width_enlarge_value | 0 | 获取目标真实点云时宽度扩充的范围 |
| float | length_enlarge_value | 0 | 获取目标真实点云时长度扩充的范围 |
centerpoint 独有的配置
| 参数类型 | 参数名 | 默认值 | 含义 |
|---|---|---|---|
| ModelInfo | info | / | 模型通用配置 |
| PointCloudPreProcess | preprocess | / | 预处理 |
| PointCloudPostProcess | postprocess | / | 后处理 |
| int32 | point2box_max_num | 5 | 每个点最多可以属于多少个box |
| float | quantize | 0.2 | 将尺寸量化为quantize的倍数 |
apollo 的环境搭建参见 perception 2.0 综述。
# 进入容器
aem enter
# 下载安装依赖包: 会拉取安装core目录下的cyberfile.xml里面所有的依赖包
# 同时会自动下载centerpoint模型并放置到指定位置
buildtool build --gpu
上述步骤部署的是 apollo 官方的 centerpoint 模型,如果要部署自己的 centerpoint 模型:需要将上一步中导出的文件放置到/apollo/modules/perception/data/models/center_point_paddle下面即可:
|-- /apollo/modules/perception/data/models/center_point_paddle
|-- apollo_deploy.yaml
|-- centerpoint.pdiparams
|-- centerpoint.pdiparams.info
|-- centerpoint.pdmodel
aem bootstrap start --plus
选择相应车型配置:
启动 transform 模块:
启动 lidar 感知模块:
方式一:在 dreamview+ 左下角点击 Resource Manger,下载 sensor_rgb 数据包,下载完成后选择 sensor_rgb 数据包点击播放。
方式二:终端启动下载好的 sensor_rgb 数据包,下载链接参考开放资源record下载。
cyber_recorder play -f sensor_rgb.record
dreamview+ 查看 lidar 检测结果: