ubuntu18.04+opencv4.1.0安装caffe-gpu

前言

本人安装caffe-gpu时的基础:
已经安装了显卡驱动 【超简单方法】ubantu18.04.2安装最新显卡驱动.
安装了cuda和cudnn cuda与cudnn安装.
安装了python3.6
安装了opencv4.1.0 IPPICV: Download: ippicv_2019_lnx_intel64_general_20180723.tgz手动安装解决.
这些是安装caffe-gpu的基础,我已经事先安装了,当然安装opencv3.X的版本更好。

1.标准开局

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 liblapack-dev libatlas-base-dev
 
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
 
sudo apt-get install git cmake build-essential

执行以上命令,有一定几率安装失败而导致后续步骤出现问题,所以要确保以上依赖包都已安装成功,验证方法就是重新运行安装命令,如验证 git cmake build-essential是否安装成功共则再次运行以下命令:

sudo apt-get install git cmake build-essential 

安装有点久,可以一边喝茶。

2.安装caffe

2.1 下载caffe

git clone https://github.com/BVLC/caffe.git

2.2 复制caffe

进入 caffe ,将 Makefile.config.example 文件复制一份并更名为 Makefile.config ,也可以在 caffe 目录下直接调用以下命令完成复制操作 :

sudo cp Makefile.config.example Makefile.config

复制一份的原因是编译 caffe 时需要的是 Makefile.config 文件,而Makefile.config.example 只是caffe 给出的配置文件例子,不能用来编译 caffe。

2.3 修改 Makefile.config 文件

修改 Makefile.config 文件,在 caffe 目录下打开该文件:

sudo gedit Makefile.config

1.应用 cudnn

将
#USE_CUDNN := 1
修改成: 
USE_CUDNN := 1

2.应用 opencv 版本

将
#OPENCV_VERSION := 3 
修改为: 
OPENCV_VERSION := 3

3.使用 python 接口

将
#WITH_PYTHON_LAYER := 1 
修改为 
WITH_PYTHON_LAYER := 1

4.更改为 python3.6 接口

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
# PYTHON_INCLUDE := /usr/include/python2.7 \
#		/usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
# ANACONDA_HOME := $(HOME)/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		# $(ANACONDA_HOME)/include/python2.7 \
		# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.6m
PYTHON_INCLUDE := /usr/include/python3.6m \
                 /usr/lib/python3.5/dist-packages/numpy/core/include

原python2接口注释,python3改成自己的版本
6.cuda 9.0及以上版本注释掉_20和_21

# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
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

6.继续更改注释

# uncomment to disable IO dependencies and corresponding data layers
USE_OPENCV := 1
USE_LEVELDB := 1
USE_LMDB := 1
# This code is taken from https://github.com/sh1r0/caffe-android-lib
USE_HDF5 := 1

6.添加路径

# 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

2.4 修改makefile

makefile文件的LIBRARIES修改为以下:

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

2.5 caffe编译

OK ,可以开始编译了。
在 caffe 目录下执行 :

sudo make all

然后报错:

.build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
.build_release/lib/libcaffe.so: undefined reference to `cv::String::allocate(unsigned long)'
.build_release/lib/libcaffe.so: undefined reference to `cv::String::deallocate()'
.build_release/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector >&, std::vector > const&)'
collect2: error: ld returned 1 exit status
Makefile:636: recipe for target '.build_release/tools/extract_features.bin' failed
make: *** [.build_release/tools/extract_features.bin] Error 1

可能的解决方法包括:

  1. Makefile.config中pkg-config --modversion opencv取消注释
  2. Makefile.config中OPENCV_VERSION := 3取消注释
  3. Makefile.config中找到LIBRARIES(在PYTHON_LIBRARIES := boost_python python2.7 前一行)并修改为:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial  opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

本文使用方法3,即继续修改makefile文件
继续报错:

src/caffe/util/io.cpp: In function ‘cv::Mat caffe::ReadImageToCVMat(const string&, int, int, bool)’:
src/caffe/util/io.cpp:76:34: error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope
   int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
                                  ^~~~~~~~~~~~~~~~~~~
src/caffe/util/io.cpp:76:34: note: suggested alternative: ‘CV_IS_IMAGE_HDR’
   int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
                                  ^~~~~~~~~~~~~~~~~~~
                                  CV_IS_IMAGE_HDR
src/caffe/util/io.cpp:77:5: error: ‘CV_LOAD_IMAGE_GRAYSCALE’ was not declared in this scope
     CV_LOAD_IMAGE_GRAYSCALE);
     ^~~~~~~~~~~~~~~~~~~~~~~
src/caffe/util/io.cpp: In function ‘cv::Mat caffe::DecodeDatumToCVMat(const caffe::Datum&, bool)’:
src/caffe/util/io.cpp:182:34: error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope
   int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
                                  ^~~~~~~~~~~~~~~~~~~
src/caffe/util/io.cpp:182:34: note: suggested alternative: ‘CV_IS_IMAGE_HDR’
   int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
                                  ^~~~~~~~~~~~~~~~~~~
                                  CV_IS_IMAGE_HDR
src/caffe/util/io.cpp:183:5: error: ‘CV_LOAD_IMAGE_GRAYSCALE’ was not declared in this scope
     CV_LOAD_IMAGE_GRAYSCALE);
     ^~~~~~~~~~~~~~~~~~~~~~~
Makefile:592: recipe for target '.build_release/src/caffe/util/io.o' failed
make: *** [.build_release/src/caffe/util/io.o] Error 1

在caffe安装目录执行以下:

sed -i 's/CV_LOAD_IMAGE_COLOR/cv::IMREAD_COLOR/g' src/caffe/layers/window_data_layer.cpp
sed -i 's/CV_LOAD_IMAGE_COLOR/cv::IMREAD_COLOR/g' src/caffe/util/io.cpp
sed -i 's/CV_LOAD_IMAGE_GRAYSCALE/cv::ImreadModes::IMREAD_GRAYSCALE/g' src/caffe/util/io.cpp

再次编译成功:

sudo make all

2.6 caffe测试

sudo make test

然后报错:

src/caffe/test/test_io.cpp: In function ‘bool caffe::ReadImageToDatumReference(const string&, int, int, int, bool, caffe::Datum*)’:
src/caffe/test/test_io.cpp:23:34: error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope
   int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
                                  ^~~~~~~~~~~~~~~~~~~
src/caffe/test/test_io.cpp:23:34: note: suggested alternative: ‘CV_IS_IMAGE_HDR’
   int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
                                  ^~~~~~~~~~~~~~~~~~~
                                  CV_IS_IMAGE_HDR
src/caffe/test/test_io.cpp:24:5: error: ‘CV_LOAD_IMAGE_GRAYSCALE’ was not declared in this scope
     CV_LOAD_IMAGE_GRAYSCALE);
     ^~~~~~~~~~~~~~~~~~~~~~~
Makefile:592: recipe for target '.build_release/src/caffe/test/test_io.o' failed
make: *** [.build_release/src/caffe/test/test_io.o] Error 1

在caffe安装目录执行以下:

sed -i 's/CV_LOAD_IMAGE_COLOR/cv::IMREAD_COLOR/g' src/caffe/test/test_io.cpp
 sed -i 's/CV_LOAD_IMAGE_GRAYSCALE/cv::ImreadModes::IMREAD_GRAYSCALE/g' src/caffe/test/test_io.cpp

继续执行成功:

sudo make test

2.6.1 runtest测试

在caffe安装目录执行成功:(不要sudo)

 make runtest

2.7 编译 pycaffe

 make pycaffe -j8

报错:

touch python/caffe/proto/__init__.py
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
touch: cannot touch 'python/caffe/proto/__init__.py': Permission denied
Makefile:658: recipe for target 'python/caffe/proto/__init__.py' failed
make: *** [python/caffe/proto/__init__.py] Error 1
make: *** Waiting for unfinished jobs....
python/caffe/_caffe.cpp:10:10: fatal error: numpy/arrayobject.h: No such file or directory
 #include 
          ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Makefile:518: recipe for target 'python/caffe/_caffe.so' failed
make: *** [python/caffe/_caffe.so] Error 1

安装:

sudo apt-get install python-numpy

继续:

 make pycaffe -j8

报错:

touch python/caffe/proto/__init__.py
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
touch: cannot touch 'python/caffe/proto/__init__.py': Permission denied
Makefile:658: recipe for target 'python/caffe/proto/__init__.py' failed
make: *** [python/caffe/proto/__init__.py] Error 1
make: *** Waiting for unfinished jobs....

执行成功

sudo make pycaffe -j8

进入python环境import caffe

python3
import caffe

报错:

ModuleNotFoundError: No module named 'caffe'

执行:

sudo echo export PYTHONPATH="<>/caffe-master/python" >> ~/.bashrc
source ~/.bashrc

再次进入python环境import caffe

python3
>>> import caffe
>>> 

安装成功!

你可能感兴趣的:(环境安装)