bevfusion代码复现环境部署
- mit版本bevfusion代码github地址
- 基于mmdet3d版本框架的bevfusion复现
- Fast-BEV代码复现实践
- BEVFustion-TensorRT部署
- BEV各算法环境部署实战汇总
- 如果觉得本文章可以,一键三连支持一波,
^_^
- 部署有问题的小伙伴欢迎留言和加
Q
裙-472648720
1 环境安装
-
ubuntu20.04,python-3.8, torch-1.10.0, cuda-11.3, cudnn-8.6
-
不要问其他版本能不能通,小白和不想折腾环境的童鞋直接抄作业, 完全抄,不要只抄半路!
-
默认显卡驱动
cuda,cudnn,conda
已装好,未安装请参考cuda, cudnn,conda
安装 -
安装环境时报错,可以先参考本文第2小节报错汇总
- 虚拟环境
- conda(含显卡驱动,cuda安装)构建虚拟环境参考链接
# 1 创建虚拟环境
conda create -n bevfusion python=3.8
# 2 激活虚拟环境
conda activate bevfusion
- 后面python包都是安装在虚拟环境中
- torch安装
cuda与torch版本查询 我用的torch-1.10.0
pip install torch==1.10.0+cu113 torchvision==0.11.0+cu113 torchaudio==0.10.0 -f https://download.pytorch.org/whl/torch_stable.html
- 其他依赖安装
libgl
与libopenmpi-dev
# 安装mpi4py时依赖openmpi,不然会报错fatal error: mpi.h
sudo apt-get install wget libgl1-mesa-glx libglib2.0-0 openmpi-bin openmpi-common libopenmpi-dev libgtk2.0-dev git -y
openlib
相关包
pip install Pillow==8.4.0 tqdm torchpack nuscenes-devkit mpi4py==3.0.3 numba==0.48.0 setuptools==56.1.0 ninja==1.11.1 numpy==1.23.4 opencv-python==4.8.0.74 opencv-python-headless==4.8.0.74 yapf==0.40.1
- mmcv-full、mmdet
# 1 安装mmcv-full==1.4.0
pip install mmcv-full==1.4.0
## 上面命令安装失败,可以使用下面命令安装, 注意cuda和torch版本号
pip install mmcv-full==1.4.0 -f https://download.openmmlab.com/mmcv/dist/cu113/torch1.10.0/index.html
# 2 安装mmdet==2.20.0
pip install mmdet==2.20.0
安装mmcv-full
时,可能需要时间比较长,只要电脑没卡住,都是正常的
- 根据
setup.py
进行配置, 这里会安装mmdet3d
包
下载源码运行setup.py
,强烈建议直接拉取本人的仓库代码,原作者代码仓库已更新,此帖不适配。
- 拉取源码
# 本人仓库拉取源码
git clone https://gitee.com/linClubs/bevfusion.git
# 1 拉取官方源码 本帖不适配
git clone https://github.com/mit-han-lab/bevfusion.git
- 运行
setup.py
pip install -v -e .
# -v 调试信息
# -e 可编辑模型
# . 寻找当前目录下的setup.py
- 运行完,显示如下:
mmdet3d
版本0.0.0
,不影响代码的运行
Successfully installed mmdet3d-0.0.0
- 查看环境
- 查看
torch,cuda
相关包版本号
pip list | grep torch
- 查看mmopenlab相关包版本号
pip list | grep mm
2 报错修改汇总
-
将
mmdet3d/ops/spconv/src/indice_cuda.cu
文件里面所有的4096
改为256
-
算力更改:setup.py文件中第22行左右,只保留一行
-gencode=arch=compute_86,code=sm_86"
- 参数86就是自己显卡的算力根据实际修改, 显卡算力查询
- 运行报错
- 1 错误1 运行
tools/visualize.py
报错No module named 'torchpack.utils.tqdm
修改:把tools/visualize.py
文件中from torchpack.utils.tqdm import tqdm
改成from tqdm import tqdm
,如下:
# from torchpack.utils.tqdm import tqdm
from tqdm import tqdm
- 2 错误2
mmdet3d/models/vtransforms/base.py
中2个forward
函数的参数都加上metas
变量,加到**kwargs
前即可,如下所示
def forward(
...
metas,
**kwargs,
):
# 错误1
...
from numba.np.ufunc import _internal
SystemError: initialization of _internal failed without raising an exception
# 修改方法: 降低numpy版本即可
pip install numpy==1.23.4
# 错误2
ModuleNotFoundError: No module named 'spconv'
# 原因是未安装spconv,安装版本要和自己的cuda对应,我cuda-11.3,就安装spconv-cu113
# 修改方法
pip install spconv-cu113
# 错误3
ModuleNotFoundError: No module named 'IPython'
# 修改方法
pip install IPython
# 错误4
# 情况1:'No module named 'projects.mmdet3d_plugin'
# 情况2:ModuleNotFoundError: No module named 'tools'
# 情况3: ModuleNotFoundError: No module named 'tools.data_converter'
# 因为tools和projects.mmdet3d_plugin都是从本地导入模块,
# 导入失败要么是python环境变量没生效, 要么是模块的路径不对
# 修改办法: 更新python-path环境即可, 当前python虚拟环境的终端执行下面语句
export PYTHONPATH=$PYTHONPATH:"./"
# 如果还报错检查这句代码的路径是否正确, 可是使用绝对路径代替
# 错误5
TypeError: FormatCode() got an unexpected keyword argument 'verify'
# 修改办法: 降低yapf版本
pip install yapf==0.40.1
# 错误6
ImportError: libcudart.so.11.0: cannot open shared object file: No such file or directory
# 原因: 安装的mmcv与cuda版本没对用上,建议去whl官方下载离线安装
# 修改参考1.4.1安装mmcv-full教程
# 错误7
# AttributeError: module 'distutils' has no attribute 'version'
修改:更新setuptools版本
pip install setuptools==58.4.0
# 错误8
# docker里面提示libGL.so.1不存在
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
# 修改方法:安装ffmpeg即可
apt-get install ffmpeg -y
# 错误9 pip安装mmcv-full时报错
subprocess.CalledProcessError: Command '['which', 'g++']' returned non-zero exit status 1.
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for mmcv-full
# 修改方法:由于g++,gcc工具没安装,安装build-essential即可
sudo apt-get install build-essential
# 错误10 训练时显存爆炸 RuntimeError: CUDA out of memory
# 修改:先将配置文件中samples_per_gpu改为1即可workers_per_gpu改0测试环境,
# 后期正式训练时逐渐增加这2个参数的数字, 直到显存占满
# 如果设置成1和0都显存不够, 可以更换显卡了
samples_per_gpu=1, workers_per_gpu=0
# 错误11
master_host = 'tcp://' + os.environ['MASTER_HOST']
File "/home/XXX/anaconda3/envs/BEVFusionEnv/lib/python3.8/os.py", line 673, in __getitem__
raise KeyError(key) from None
KeyError: 'MASTER_HOST'
# github上issue上给的解决方法可以参考 地址如下:
https://github.com/mit-han-lab/bevfusion/issues/172
# 错误12
ModuleNotFoundError: No module named 'mmcv._ext'
#错误原因是安装了mmcv=1.4.0不是安装mmcv-full==1.4.0
解决方法是安装mmcv-full==1.4.0即可
# 错误13
ERROR: Failed building wheel for mpi4py
Running setup.py clean for mpi4py
Failed to build mpi4py
ERROR: Could not build wheels for mpi4py, which is required to install pyproject.toml-based projects
# 先安装libopenmpi-dev解决办法安装mpi4py
sudo apt install libopenmpi-dev
pip install mpi4py==3.0.3
# 错误14
ype(e)(f'{obj_cls.__name__}: {e}')
TypeError: CyclicLrUpdaterHook: __init__() got an unexpected keyword argument 'min_lr_ratio',
# 修改方法,找到配置文件中min_lr_ratio, 如果不知道哪个配置文件就全局搜索min_lr_ratio 然后直接删除就好了,或者注释掉min_lr_ratio即可
# 错误15 如果是官网最新代码,运行时会出现下面错误, 本人代码可忽略
RuntimeError: Given groups=1, weight of size [8, 1, 1, 1], expected input[6, 6, 256, 704] to have 1 channels, but got 6 channels instead
# 修改详情参考 https://github.com/mit-han-lab/bevfusion/issues/478
# 错误16 找不到THC.h文件
/mmdet3d/ops/ball_query/src/ball_query.cpp:4:10: fatal error: THC/THC.h: 没有那个文件或目录
#include <THC/THC.h>
## 方法1修改:
THC.h在pytorch 1.11版本中被移除了,需要修改代码。
将set.py中提到的cpp文件的所有关于THC.h的代码修改为下面2句:
#include <ATen/cuda/CUDAContext.h>
#include <ATen/cuda/CUDAEvent.h>
## 方法2修改:
pytorch安装1.10.0版本,本人已经验证成功
- distributed分布式训练
- 如果需要多卡训练,请把
distributed
参数设置为True
,直接vs-code全局搜索找到distributed
3 运行
3.1 准备数据集
nuscenes-mini
数据集得加上--version v1.0-mini
参数,下以nuscenes-mini
作代码验证
具体下载细节参考Fast-BEV代码复现实践的第2小节数据集准备内容
nuscenes-mini百度网盘下载地址 提取码: v3cv
下载后数据集后运行create_data.py
生成训练数据,
create_data.py
只运行下面代码第一行nuscenes-mini
即可
# nuscenes-mini
python tools/create_data.py nuscenes --root-path ./data/nuscenes --out-dir ./data/nuscenes --extra-tag nuscenes --version v1.0-mini
# nuscenes
python tools/create_data.py nuscenes --root-path ./data/nuscenes --out-dir ./data/nuscenes --extra-tag nuscenes
原始总共4个文件夹 maps,samples,sweeps,v1.0-mini
,运行create_data.py
后生成3
个pkl
文件和一个nuscenes_gt_database
文件夹,目录结构如下:
data
└──nuscenes
├── maps
├── nuscenes_dbinfos_train.pkl
├── nuscenes_gt_database
├── nuscenes_infos_train.pkl
├── nuscenes_infos_val.pkl
├── samples
├── sweeps
└── v1.0-mini
3.2 预训练权重
./tools/download_pretrained.sh
- 运行后会在cd pretrained中生成7个权重文件,具体信息可以阅读
./tools/download_pretrained.sh
文件内容 - 使用
swint-nuimages-pretrained.pth
这个预训练权重进行训练 - 如果运行上
./tools/download_pretrained.sh
下载失败,可以直接Google网盘上下载 - 加文章末尾群号, 群公告也有
swint-nuimages-pretrained.pth
下载地址, swint-nuimages-pretrained.pth
百度网盘下载地址
3.3 训练
根据显卡性能修改参数:
- 只有一张显卡:
-np 1
configs/default.yaml
中修改epoch:max_epochs: 2
(本人只跑2个周期测试)configs/nuscenes/det/centerhead/lssfpn/camera/256x704/swint/default.yaml
文件中,测试环境是否正确时,建议设置samples_per_gpu
:1
,,后期训练根据硬件配置修改,如果使用其他配置文件,修改同理。- 测试环境时,
configs/nuscenes/default.yaml
中workers_per_gpu
参数修改为0:workers_per_gpu
:0
,samples_per_gpu: 1
torchpack dist-run -np 1 python tools/train.py configs/nuscenes/det/centerhead/lssfpn/camera/256x704/swint/default.yaml --model.encoders.camera.backbone.init_cfg.checkpoint pretrained/swint-nuimages-pretrained.pth --run-dir train_result
- 训练完成后会在
train_result目录下生成下面文件
结构如下:
└── train_result
├── 20230809_203249.log
├── 20230809_203249.log.json
├── configs.yaml
├── epoch_2.pth
├── latest.pth -> epoch_2.pth
├── logging
└── tf_logs
# pretrained
torchpack dist-run -np 1 python tools/test.py configs/nuscenes/det/centerhead/lssfpn/camera/256x704/swint/default.yaml pretrained/swint-nuimages-pretrained.pth --eval bbox --out box.pkl
# Custom
torchpack dist-run -np 1 python tools/test.py train_result/configs.yaml train_result/latest.pth --eval bbox --out box.pkl
运行后会生成box.pkl文档
3.5 visualize
- 首先改错误
1.2
小节3.
的2个错误
# Custom
torchpack dist-run -np 1 python tools/visualize.py train_result/configs.yaml --mode pred --checkpoint train_result/latest.pth --bbox-score 0.2 --out-dir vis_result
# gt
torchpack dist-run -np 1 python tools/visualize.py train_result/configs.yaml --mode gt --checkpoint train_result/latest.pth --bbox-score 0.5 --out-dir vis_result
# pretrained
torchpack dist-run -np 1 python tools/visualize.py train_result/configs.yaml --mode pred --checkpoint pretrained/swint-nuimages-pretrained.pth --bbox-score 0.2 --out-dir vis_result
- 运行后会在
vis_result
下生成可视化结果,如下:
└── vis_result
├── camera-0
├── camera-1
├── camera-2
├── camera-3
├── camera-4
├── camera-5
└── lidar
运行--mode gt
模式,也就是标签数据可视化,自己训练权重效果比较差(原因:钞能力有限),可视化结果如下:
小伙伴们部署时遇到问题,欢迎各位小伙伴留言,欢迎进入bev交流抠抠裙472648720,大家一起学bev!
如果觉得文章可以,一键三连支持一波,瑞思拜^-^