先赞后看,养成好习惯。有帮助的话,点波关注!我会坚持更新,感谢谢您的支持!
需求: 学习并复现PointPillars,解决部署时遇到的各类问题。
参考工程:
1. PointPillars_MultiHead_40FPS
2. OpenPCDet
3. onnx2trt
4. onnx2trt安装过程可能遇到的问题
5. Spconv
1. 下载工程
PointPillars_MultiHead_40FPS
:点云目标检测网络主工程OpenPCDet工具
:用于将pth的模型,转换为onnx模型onnx-tensorrt工具
:用于onnx到trt模型的转换。git clone https://github.com/hova88/PointPillars_MultiHead_40FPS.git --recursive
git clone https://github.com/hova88/OpenPCDet.git
git clone https://github.com/onnx/onnx-tensorrt.git
2. 模型转换:
注意
:
官方提供的两个onnx两个链接文件地址失效,故只能采用OpenPCDet工具进行onnx的生成。
1) cbgs_pp_multihead_pfe.onnx. 2) cbgs_pp_multihead_backbone.onnx
2.1)pth转化为onnx
在OpenPCDet工具工程下,执行下面的操作:
## 1. trans_pfe.py, 修改main文件中三处路径, 用### 标记
if __name__ == "__main__":
from pcdet.config import cfg, cfg_from_yaml_file
cfg_file = '/home/cui/workspace/deepLearning/OpenPCDet/tools/cfgs/nuscenes_models/cbgs_pp_multihead.yaml' ###
filename_mh = "/home/cui/workspace/deepLearning/OpenPCDet/pp_multihead_nds5823_updated.pth" ###
cfg_from_yaml_file(cfg_file, cfg)
model_cfg=cfg.MODEL
pfe , dummy_input = build_pfe( filename_mh, cfg)
pfe.eval().cuda()
export_onnx_file = "/home/cui/workspace/deepLearning/OpenPCDet/cbgs_pp_multihead_pfe.onnx" ###
torch.onnx.export(pfe,
dummy_input,
export_onnx_file,
opset_version=12,
verbose=True,
do_constant_folding=True) # 输出名
## 2. trans_backbone_multihead.py,同样修改三处路径,形式如下:
if __name__ == "__main__":
from pcdet.config import cfg, cfg_from_yaml_file
cfg_file = '/home/cui/workspace/deepLearning/OpenPCDet/tools/cfgs/nuscenes_models/cbgs_pp_multihead.yaml'
filename_mh = "/home/cui/workspace/deepLearning/OpenPCDet/pp_multihead_nds5823_updated.pth"
cfg_from_yaml_file(cfg_file, cfg)
model_cfg=cfg.MODEL
pfe , dummy_input = build_pfe( filename_mh, cfg)
pfe.eval().cuda()
export_onnx_file = "/home/cui/workspace/deepLearning/OpenPCDet/cbgs_pp_multihead_pfe.onnx"
torch.onnx.export(pfe,
dummy_input,
export_onnx_file,
opset_version=12,
verbose=True,
do_constant_folding=True) # 输出名
pip install spconv-cu102 # 本人使用cuda10.2版本
conda create -n pointpillars python=3.6 #创建pointpillars
conda activate pointpillars #进入pointpillars虚拟环境
pip install -r requirements.txt # 安装依赖,在OpenPCDet工程下
python setup.py develop
cd tools/onnx_utils
python trans_pfe.py # 在OpenPCDet目录下,生成cbgs_pp_multihead_pfe.onnx
python trans_backbone_multihead.py # 在OpenPCDet目录下,生成cbgs_pp_multihead_backbone.onnx
注意:执行脚本过程中有任何问题,参考第五部分问题答疑
。
2.2)onnx转化为trt模型
拷贝上一步中两个onnx文件到workspace/tools/onnx-tensorrt/build目录下,然后执行转换命令:
cd workspace/tools/onnx-tensorrt/build # 进入onnx-tensorrt工具的目录
onnx2trt cbgs_pp_multihead_pfe.onnx -o cbgs_pp_multihead_pfe.trt -b 1 -d 16
onnx2trt cbgs_pp_multihead_backbone.onnx -o cbgs_pp_multihead_backbone.trt -b 1 -d 16
生成类似如下结果,即生成成功!
----------------------------------------------------------------
Input filename: cbgs_pp_multihead_backbone.onnx
ONNX IR version: 0.0.7
Opset version: 10
Producer name: pytorch
Producer version: 1.10
Domain:
Model version: 0
Doc string:
----------------------------------------------------------------
Parsing model
[2022-07-29 02:47:54 WARNING] [TRT]/home/cui/workspace/tools/onnx-tensorrt/onnx2trt_utils.cpp:220: Your ONNX model has been generated with INT64 weights, while TensorRT does not natively support INT64. Attempting to cast down to INT32.
Building TensorRT engine, FP16 available:1
Max batch size: 1
Max workspace size: 1024 MiB
Writing TensorRT engine to cbgs_pp_multihead_backbone.trt
All done
3. 模型路径修改
拷贝4个模型文件到PointPillars/model下。
如果有模型文件变动,则需要修改bootstrap.yaml文件中,*.onnx 和 *.trt 模型路径。
## bootstrap.yaml文件内容
BoxFeature: 7
ScoreThreshold: 0.1
NmsOverlapThreshold: 0.2
UseOnnx: false
PfeOnnx: ../model/cbgs_pp_multihead_pfe.onnx
BackboneOnnx: ../model/cbgs_pp_multihead_backbone.onnx
PfeTrt: ../model/cbgs_pp_multihead_pfe.trt
BackboneTrt: ../model/cbgs_pp_multihead_backbone.trt
ModelConfig: ../pointpillars/cfgs/cbgs_pp_multihead.yaml
InputFile: ../test/testdata/nuscenes_10sweeps_points.txt
OutputFile: ../test/testdata/demo_boxes.txt
4. 下载测试点云数据
nuscenes_10sweeps_points.txt
cd PointPillars_MultiHead_40FPS
mkdir build && cd build
cmake .. && make -j8
注意:编译过程中有任何问题,参考第五部分问题答疑
。
./test/test_model
注意:运行过程中有任何问题,参考第五部分问题答疑
。
python viewer.py
Q1. CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
CMake 3.17 or higher is required. You are running version 3.16.6
A1:Cmakelists.txt中cmake版本过高,降低即可。
cmake_minimum_required(VERSION 3.16) # 本机是3.16版本
Q2. test中的Cmakelists报错,CMake Error at test/CMakeLists.txt:1 (add_subdirectory): The source directory
A2:工程中test/gtest是属于子工程 submodule,原因在于工程未下载完全。
git clone https://github.com/hova88/PointPillars_MultiHead_40FPS.git --recursive
Q3. NVINFER NOT FOUND
A3:添加TensorRT的地址
set(CMAKE_PREFIX_PATH "/usr/local/TensorRT/TensorRT-7.1.3.4/lib") # 设置前缀路径
Q4. #error – unsupported GNU version! gcc versions later than 6 are not supported!
A4: 查看CUDA环境,发现版本不对,切换到CUDA10.2的conda环境,重新编译执行即可。
Q5. fatal error: NvInfer.h: No such file or directory
A5: 未添加包含目录,根据本机TensorRT情况添加include路径
include_directories("/usr/local/TensorRT/TensorRT-7.1.3.4/include")
Q6. nuscenes_10sweeps_points.txt 样例数据下载地址不对
A6. 更换下载地址为: nuscenes_10sweeps_points.txt 新下载地址
Q7. 使用可视化脚本python viewer.py提示找不到数据
A7. 需要修改数据路径,bootstrap.yaml文件中, 修改InputFile和OutputFile,例如
InputFile: ../test/testdata/nuscenes_10sweeps_points.txt # 将Q6中下载好的数据放入此文件夹中
OutputFile: ../test/testdata/demo_boxes.txt
Q8. 执行trans_pfe.py 脚本时,报错 SyntaxError: Non-ASCII character ‘\xe8’ in file trans_pfe.py on line 113, but no encoding declared;
A8. 在trans_pfe.py文件开头位置,添加如下代码,支持中文字符
# -*- coding:utf-8 -*-
Q9. 执行trans_pfe.py 脚本时, 报错 No module named ‘pcdet’
A9. 需要根据安装教程,先安装pcdet工程,也可以参考本文 2.1)C.步骤。
Q10. 执行trans_pfe.py 脚本时,报错 AttributeError: module ‘spconv’ has no attribute ‘SparseModule’
A10. 参考:
1. github spconv issue
2. openpcdet项目代码(调试及问答记录)
修改pcdet/models/backbones_3d/spconv_backbone.py文件和pcdet/models/backbones_3d/spconv_unet.py文件
## 文件初始位置引入spconv
from spconv.pytorch import ops
from spconv.pytorch.conv import (SparseConv2d, SparseConv3d, SparseConvTranspose2d,
SparseConvTranspose3d, SparseInverseConv2d,
SparseInverseConv3d, SubMConv2d, SubMConv3d)
from spconv.pytorch.core import SparseConvTensor
from spconv.pytorch.identity import Identity
from spconv.pytorch.modules import SparseModule, SparseSequential
from spconv.pytorch.ops import ConvAlgo
from spconv.pytorch.pool import SparseMaxPool2d, SparseMaxPool3d
from spconv.pytorch.tables import AddTable, ConcatTable
class SparseBasicBlock(SparseModule) ## 去掉spconv.
Q11. 执行trans_pfe.py 脚本时,报错No such file or directory: ‘cfgs/dataset_configs/nuscenes_dataset.yaml’
A11. 修改tools/cfgs/nuscenes_models/cbgs_pp_multihead.yaml文件中第5行,_BASE_CONFIG_为绝对路径,修改为如下效果:
_BASE_CONFIG_: /home/cui/workspace/deepLearning/OpenPCDet/tools/cfgs/dataset_configs/nuscenes_dataset.yaml
Q12. 执行可视化脚本python viewer.py 时,报错 ModuleNotFoundError: No module named ‘open3d’
A12. 安装open3d
pip install open3d
Q13. 将gtest文件修改为普通文件,方便单步调试查看文件中变量,编译报错 unsupported GNU version! gcc versions later than 6 are not supported
A13. 编译时候,cmake指令指定gcc g++6.0版本
cmake -DCMAKE_C_COMPILER=/usr/bin/gcc-6 -DCMAKE_CXX_COMPILER=/usr/bin/g++-6 ..