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
本文使用的makefile和makefile.config附件见https://download.csdn.net/download/lukaslong/10586127
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