caffe+opencv4
- 记录一个长时间的安装记录
- 因为项目需要安装caffe,但是需要高版本的cuda opencv4兼容。
- 经过长时间的摸索终于成功安装
- 注:caffe只支持到cudnn7.x,,放弃使用cudnn。https://github.com/BVLC/caffe/issues/6970
- Xavier 的安装与x86安张大同小异,主要是文件的位置关系。通过SDK安装cuda Opencv的路径可能与电脑安装的有点区别。可以灵活应用软连接‘’
sudo ln -s xxx xxx
下载模型
cd
git clone [https://github.com/BVLC/caffe.git](https://github.com/BVLC/caffe.git)
before install , install the dependencies:
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
修改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问题
#Xavier **/usr/lib/aarch64-linux-gnu**
修改代码:
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
##Xavier is 72
# 根据自己的架构和版本修改,可参考https://arnon.dk/matching-sm-architectures-arch-and-gencode-for-various-nvidia-cards/
修改Makefle
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
# 根据自己的版本修改
生成软连接
- 针对找不到lhdf5_hl和lhdf5的错误
cd /usr/lib/x86_64-linux-gnu
**//Xavier 是arm 架构#####
cd /usr/lib/aarch64-linux-gnu**
\\然后根据情况执行下面两句:
sudo ln -s libhdf5_serial.so.10.1.0 libhdf5.so
sudo ln -s libhdf5_serial_hl.so.10.0.2 libhdf5_hl.so
修改源程序
- 支持OPENCV
- 修改
Caffe/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; }
编译
方案1:
make all -j8
make pycaffe -j8
方案2:
sudo make -j8
sudo make distribute
- 若import caffe 报错 Traceback (most recent call last): File “”, line 1, in ModuleNotFoundError: No module named 'caffe’
sudo echo export PYTHONPATH="~/caffe/python" >> ~/.bashrc
source ~/.bashrc