1. 程式人生 > >Ubuntu 16.04配置caffe-ssd

Ubuntu 16.04配置caffe-ssd

1. 安裝依賴項

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-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 python-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

2. 獲取ssd版caffe

說明:SSD採用的是在caffe資料夾中內嵌例程的方式,作者改動了原版caffe,所以你需要把原來的caffe資料夾移除,git命令會新建一個帶有SSD程式的caffe資料夾,當然,這個新的caffe要重新編譯一次。

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

這裡需出現分支成功的提示方可繼續:

Branch ssd set up to track remote branch ssd from origin.
Switched to a new branch 'ssd'

3. 編譯caffe準備:

cd caffe
cp Makefile.config.example Makefile.config

Makefile.config修改:

1.若使用cudnn,取消“# USE_CUDNN := 1” 前的註釋即:USE_CUDNN := 1

2.若使用opencv3.x,取消“# OPENCV_VERSION := 3” 前的註釋,即:OPENCV_VERSION := 3

3.取消“# WITH_PYTHON_LAYER := 1” 前的註釋。即 WITH_PYTHON_LAYER := 1 

4.加入hdf5的目錄:

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
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial

Makefile修改:

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5

修改為:

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)

修改為:

NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
LIBRARIES += boost_thread stdc++後加boost_regex

修改為:

LIBRARIES += boost_thread stdc++ boost_regex

4. 編譯caffe

make -j8

make py

make test -j8

make runtest -j8

1)使用GPU版的caffe編譯可能會報錯

nvcc fatal : Unsupported gpu architecture 'compute_20'

這是由於高版本的cuda不支援造成的,只需把Makefile.config中compute20的兩行刪除或者註釋掉即可

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_61,code=sm_61

刪除這兩行即可:
-gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \

2)搜尋路徑時LD -o .build_release/lib/libcaffe.so.1.0.0-rc3時,報錯

Makefile:568: recipe for target '.build_release/lib/libcaffe.so.1.0.0-rc3' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0-rc3] Error 1
原因:缺少openblas

sudo apt-get install libopenblas-dev

3)沒有安裝python-numpy的情況下make py時會報錯:

python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory
compilation terminated.

安裝python-numpy即可:

sudo apt-get install python-numpy

4)make runtest時報錯: recipe for target 'runtest' failed

檢視記錄可以看到:Error in `python': free(): invalid pointer

sudo apt-get install libtcmalloc-minimal4

5. 編譯完成後

① import caffe報錯:No Module named caffe

需將caffe/python目錄新增到Python的路徑:(sudo gedit ~/.bashrc)

export PYTHONPATH=~/caffe-ssd/python:$PYTHONPATH

然後開啟~/.bashrc加入路徑:(並更新)

export LD_PRELOAD="/usr/lib/libtcmalloc_minimal.so.4" 

修改完成後,source ~/.bashrc即可

② ImportError: No module named google.protobuf.internal

pip install protobuf

③ ImportError: No module named 'skimage'

pip install -U scikit-image

6. 使用系統Python3.5

① 首先修改Makefile.config

將Python2.7的目錄註釋掉,然後取消註釋Python3.5的兩行庫目錄和包含目錄,並找到Python3.5對應的numpy包進行修改:

PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := /usr/include/python3.5m \
                 /usr/lib/python3/dist-packages/numpy/core/include

② 編譯會報錯cannot find -lbboost_python3,這是因為 Caffe 預設使用的 Python 是2.7,所以是有 lboost_python2的,這個時候我們就需要使用 Ubuntu 自帶的3版本的 Python 建立一個軟連線就可以了,在命令列中輸入:

sudo ln -s  /usr/lib/x86_64-linux-gnu/libboost_python-py35.so  /usr/lib/x86_64-linux-gnu/libboost_python3.so

make clean之後再重新make

③ 編譯完成後,import caffe時仍有可能報錯 ImportError: No module named 'skimage'

這就不能通過上面pip的方式進行安裝了,經嘗試,可通過如下方式安裝Python3.5下的scikit-image:

sudo apt-get install python3-skimage