环境:Ubuntu20.04,cudnn8.1.1.33-1+cuda11.2,opencv4.2
caffe编译安装
注:caffe只支持到cudnn7.x,我鼓捣了一阵(直接把cudnn相关文件换掉,解决了一部分问题),放弃使用cudnn。https://github.com/BVLC/caffe/issues/6970
(1)安装依赖包
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt install --no-install-recommends libboost-all-dev
sudo apt install libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt install git cmake build-essential
(2)修改 Makefile.config
cd caffe
cp Makefile.config.example Makefile.config
vim Makefile.config
详情
修改代码:
#USE_OPENCV := 0
#OPENCV_VERSION := 3
#WITH_PYTHON_LAYER := 1
为:
USE_OPENCV := 1
OPENCV_VERSION := 4
WITH_PYTHON_LAYER := 1
修改代码:
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
为:
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial /usr/local/include/opencv4
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial /usr/local/bin
# 最后是自己的opencv的include和bin路径,解决fatal error: opencv2/core/core.hpp问题
修改代码:
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/lib/python/dist-packages/numpy/core/include
为:
PYTHON_INCLUDE := /usr/include/python3.8 \
/usr/lib/python3/dist-packages/numpy/core/include
# 根据自己的路径修改
修改代码:
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61
为:
CUDA_ARCH := -gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61
# 根据自己的架构和版本修改,可参考https://arnon.dk/matching-sm-architectures-arch-and-gencode-for-various-nvidia-cards/
(3)修改Makefile
vim Makefile
修改代码:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
修改代码:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
为:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem hdf5_hl hdf5 m opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
# 解决undefined reference to cv::imread(cv::String const&, int)...,可参考https://github.com/BVLC/caffe/issues/2348
修改代码:
PYTHON_LIBRARIES ?= boost_python python2.7
为:
PYTHON_LIBRARIES ?= boost_python38 python3.8
# 根据自己的版本修改
(4)支持opencv4
修改src/caffe/util/io.cpp,解决error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope
vim src/caffe/util/io.cpp
修改代码:
cv_img = cv::imread(image.first, CV_LOAD_IMAGE_COLOR);
为:
cv_img = cv::imread(image.first,cv::IMREAD_COLOR);
# 需要修改两处
修改src/caffe/layers/window_data_layer.cpp,解决error: ‘CV_LOAD_IMAGE_GRAYSCALE’ was not declared in this scope…
vim src/caffe/layers/window_data_layer.cpp
修改代码:
int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
CV_LOAD_IMAGE_GRAYSCALE);
为:
int cv_read_flag = (is_color ? cv::IMREAD_COLOR :
cv::IMREAD_GRAYSCALE);
在src/caffe/data_transformer.cpp中添加opencv头文件,解决.build失败
vim src/caffe/data_transformer.cpp
修改代码:
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#endif // USE_OPENCV
为:
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/opencv.hpp>
#endif // USE_OPENCV
在 include/caffe/common.hpp
中增加opencv4支持(第70行后)
vim include/caffe/common.hpp
新增代码:
// Supporting OpenCV4
#if (CV_MAJOR_VERSION == 4)
#define CV_LOAD_IMAGE_COLOR cv::IMREAD_COLOR
#define CV_LOAD_IMAGE_GRAYSCALE cv::IMREAD_GRAYSCALE
#endif
以下为原代码(用于定位):
// See PR #1236
namespace cv { class Mat; }
(5)编译
make all -j8
make pycaffe -j8
若import caffe 报错 Traceback (most recent call last): File “<stdin>”, line 1, in <module> ModuleNotFoundError: No module named ‘caffe’
sudo echo export PYTHONPATH="~/caffe/python" >> ~/.bashrc
source ~/.bashrc
版权声明:本文为ynzzxc原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。