1. 程式人生 > >Ubuntu16.04 安裝 Caffe(SSD版)

Ubuntu16.04 安裝 Caffe(SSD版)

安裝步驟

首先安裝各種依賴包:

$ sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev $ python-dev libgflags-dev libatlas-base-dev libhdf5-serial-dev protobuf-compiler
$ sudo apt-get install --no-install-recommends libboost-all-dev

github上面拷貝下來caffe專案:

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

安裝caffe版的SSD拷貝步驟為:

$ git clone https://github.com/weiliu89/caffe.git
$ cd caffe
$ git checkout ssd

然後將caffe主目錄下面的Makefile.config.example拷貝更名為Makefile.config,開啟操作:

$ cp Makefile.config.example Makefile.config
$ gedit Makefile.config

將其中的:

#USE_CUDNN := 1
#WITH_PYTHON_LAYER := 1
INCLUDE_DIRS :=
$(PYTHON_INCLUDE) /usr/local/include LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

分別更改為:

USE_CUDNN := 1
WITH_PYTHON_LAYER := 1
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

開啟 Makefile檔案:

$ gedit Makefile

將其中的:

NVCCFLAGS += -ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)

更改為:

NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

開啟/usr/local/cuda/include/crt/host_config.h檔案:

$ sudo gedit /usr/local/cuda/include/crt/host_config.h

將其中的:

#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!

更改為:

//#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!

下面就是編譯caffe並測試:

$ make clean -j8
$ make all -j8 
$ make runtest -j8

最後輸出PASS說明測試成功。
配置環境變數:

$ vim ~/.bashrc

在檔案末尾寫入caffe-pathon的安裝路徑:

export PYTHONPATH=caffe安裝路徑/caffe/python:$PYTHONPATH

上述語句中的~表示caffe所在的根目錄。
是環境變數生效:

$ source ~/.bashrc

然後執行:

$ make pycaffe

常見問題

CUDA9錯誤
NVCC src/caffe/layers/bnll_layer.cu
nvcc fatal   : Unsupported gpu architecture 'compute_20'
Makefile:594: recipe for target '.build_release/cuda/src/caffe/layers/bnll_layer.o' failed
make: *** [.build_release/cuda/src/caffe/layers/bnll_layer.o] Error 1
make: *** Waiting for unfinished jobs....
解決方案

cuda9不支援‘ compute-20 ’,需要修改Makefile.config檔案中CUDA_ARCH設定,將

# 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

中的

-gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \

刪除即可重新編譯。

HDF5錯誤
src/caffe/net.cpp:8:18: fatal error: hdf5.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/net.o' failed
make: *** [.build_release/src/caffe/net.o] Error 1

或者

AR -o .build_release/lib/libcaffe.a
LD -o .build_release/lib/libcaffe.so.1.0.0
/usr/bin/ld: cannot find -lhdf5_hl
/usr/bin/ld: cannot find -lhdf5
collect2: error: ld returned 1 exit status
Makefile:572: recipe for target '.build_release/lib/libcaffe.so.1.0.0' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0] Error 1
解決方案

執行命令安裝libhdf5-dev

$ sudo apt-get install libhdf5-dev

然後再重新編譯。

gflags錯誤
In file included from src/caffe/net.cpp:10:0:
./include/caffe/common.hpp:5:27: fatal error: gflags/gflags.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/net.o' failed
make: *** [.build_release/src/caffe/net.o] Error 1
解決方案

執行命令安裝gflags

$ sudo apt-get install libgflags-dev

然後再重新編譯。

glog錯誤
In file included from src/caffe/net.cpp:10:0:
./include/caffe/common.hpp:6:26: fatal error: glog/logging.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/net.o' failed
make: *** [.build_release/src/caffe/net.o] Error 1
解決方案

執行命令安裝glog

$ sudo apt-get install libgoogle-glog-dev

然後再重新編譯。

LMDB錯誤
In file included from src/caffe/util/db.cpp:3:0:
./include/caffe/util/db_lmdb.hpp:8:18: fatal error: lmdb.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/util/db.o' failed
make: *** [.build_release/src/caffe/util/db.o] Error 1
解決方案

執行命令安裝lmdb

$ sudo apt-get install liblmdb-dev

然後再重新編譯。

opencv_imgcodecs opencv_videoio錯誤
/usr/bin/ld: cannot find -lopencv_imgcodecs
/usr/bin/ld: cannot find -lopencv_videoio
collect2: error: ld returned 1 exit status
Makefile:579: recipe for target '.build_release/lib/libcaffe.so.1.0.0-rc5' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0-rc5] Error 1
解決方案

開啟Makefile檔案,在164行(我的檔案)加上opencv_imgcodecs,如下:

LIBRARIES += glog gflags protobuf leveldb snappy \
  lmdb boost_system hdf5_hl hdf5 m \
  opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

然後再重新編譯。

numpy路徑錯誤
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or
directory
compilation terminated.
make: *** [python/caffe/_caffe.so] Error 1`
解決方案

開啟python編輯器,通過命令得到numpy的安裝路徑:

>>> import numpy
>>> dirs = numpy.get_include()
>>> print(dirs)

然後就能看到numpy的安裝路徑,開啟caffe目錄下的Makefile.config檔案,將65行(我的檔案)的路徑:/usr/lib/python2.7/dist-packages/numpy/core/include換成剛剛得到numpy的安裝路徑,然後重新編譯。

本人部落格:寸山河
本人GitHub:oh,ss