前言
一、准备环境
1.1基本环境要求
1.2安装库文件,确保每一个都安装成功
1.3 安装Opencv
1.4 安装anaconda
1.5 安装boost
二、安装yolov5_caffe
2.1.准备环境
2.2.修改Makefile.config
2.3.修改Makefile
2.4安装yolov5_caffe环境命令
三、yolov5-4.0安装训练
四、onnx模型转换caffe模型
记录一下安装yolov5_caffe环境以及将pytorch模型->onnx模型->caffe模型
首先感谢以下作者:
https://blog.csdn.net/Yong_Qi2015/article/details/114362223
https://blog.csdn.net/tangshopping/article/details/110470050
nvidia/cuda:10.0
cudnn:7
ubuntu:16.04
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git
sudo apt-get install libprotobuf-dev libleveldb-dev libopencv-dev libsnappy-dev
sudo apt-get install libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install python-protobuf
unzip OpenCV-3.4.5.zip
cd opencvmkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j${proc}
make install
查看是否opencv成功:
pkg-config --modversion opencv,出现版本号即成功
将opencv加入环境变量:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/
tips: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
下载对应的anacoda,我用的是Anaconda3-5.1.0-Linux-x86_64.sh,该版本的python为3.6
./Anaconda3-5.1.0-Linux-x86_64.sh
export PATH=/root/anaconda3/bin/:$PATH
source ~/.bashrc
安装pytorch==1.6.0
pip install torch==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
python3.6版本没有libboost_python-py36.so以及libboost_python-py36.a 需要手动生成
wget -O boost_1_55_0.tar.gz http://sourceforge.net/projects/boost/files/boost/1.65.1/boost_1_65_1.tar.gz/download
tar xzvf boost_1_65_1.tar.gz
cd boost_1_65_1
./bootstrap.sh --with-python=/root/anaconda3/bin/python3 --with-python-version=3.6 --with-python-root=/root/anaconda3/lib/python3.6
./b2 --with-python include="/root/anaconda3/include/python3.6m/"
./b2 install
建立软链接
cd /usr/local/lib
sudo ln -s libboost_python-py36.so libboost_python3.so
sudo ln -s libboost_python-py36.a libboost_python3.a
1.git clone https://github.com/Wulingtian/yolov5_caffe.git
2.cd yolov5_caffe
1.将CPU_ONLY:=1注释:
# CPU_ONLY := 1
2.将ANACONDA_HOME改为:
ANACONDA_HOME := $(HOME)/anaconda3
3.INCLUDE_DIRS和LIBRARY_DIRS如下:
# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
1.将LIBRARIES += glog gflags protobuf boost_system boost_filesystem m 改为
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
2.在ifeq ($(LINUX), 1)下,修改
1.在 CXX ?= /usr/bin/g++下面,添加CXXFLAGS += -std=c++11
2.#LIBRARIES += boost_thread stdc++
LIBRARIES += boost_thread stdc++ boost_regex (修改后)
1.在命令行输入以下语句
export CPLUS_INCLUDE_PATH=/root/anaconda3/include/python3.6m
make all -j16 make pycaffe -j8
2.进行yolov5_caffe环境编译
make all -j16
make pycaffe -j8
3.yolov5_caffe运行
caffe模型推理
定位到yolov5_caffe目录下cd tools
vim caffe_yolov5s.cpp
设置如下参数:
INPUT_W(模型输入宽度)
INPUT_H(模型输入高度)
NUM_CLASS(模型有多少个类别,例如我训练的模型是安全帽检测,只有1类,所以设置为1,不需要加背景类)
NMS_THRESH(做非极大值抑制的阈值)
CONF_THRESH(类别置信度)
prototxt_path(caffe模型的prototxt路径)
caffemodel_path(caffe模型的caffemodel路径)
pic_path(预测图片的路径)
定位到yolov5_caffe目录下
make -j8
cd build
./tools/caffe_yolov5s 输出平均推理时间
https://github.com/ultralytics/yolov5/releases/tag/v4.0
tips: 不需要安装wandb,有小伙伴安装了wandb会出现小问题
train: /home/xxx/yolov5-4.0/trainingData/images/train/
val: /home/xxx/yolov5-4.0/trainingData/images/val/
具体如下图,放置数据:
#[[-1, 1, Focus, [64, 3,2]], # 0-P1/2
[[-1, 1, Conv, [64, 3,2]], # 0-P1/2
pip安装onnx和onnx-simplifier
pip install onnx
pip install onnx-simplifier
cd yolov5
python models/export.py
python -m onnxsim best.onnx yolov5s-simple.onnx
git clone https://github.com/Wulingtian/yolov5_onnx2caffe.git
cd yolov5_onnx2caffe
vim convertCaffe.py
设置onnx_path(上面转换得到的onnx模型),prototxt_path(caffe的prototxt保存路径),caffemodel_path(caffe的caffemodel保存路径)
python convertCaffe.py 得到转换好的caffe模
总结:自己在进行yolov5转caffe时,踩的坑,记录一下。