- 安装必要的依赖库
- 由于之前装过opencv,所以这里没有安装libopencv
sudo apt-get install build-essential
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 libatlas-base-dev
sudo apt-get install python-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
- 下载caffe
- sudo git clone https://github.com/BVLC/caffe.git
- 进入文件夹,执行
- cp Makefile.config.example Makefile.config
- 重命名 Makefile.config.sample 为 Makefile.config
- 并更改如下内容
- gedit Makefile.config
a.使用 cudnn
USE_CUDNN := 1
b.使用的 opencv 版本是 3
OPENCV_VERSION := 3
c.使用 python 来编写 layer
WITH_PYTHON_LAYER := 1
d.重要的一项 :
将# Whatever else you find you need goes here.下面的
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
这是因为ubuntu16.04的文件包含位置发生了变化,尤其是需要用到的hdf5的位置,所以需要更改这一路径
e.删除 CUDA_ARCH := 的前两行,避免 CUDA 报错
f. 修改 PYTHON_INCLUDE 路径
# 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
g. 修改 Anaconda 路径
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := $(HOME)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.5m \
$(ANACONDA_HOME)/lib/python3.5/site-packages/numpy/core/include
h. 使用 Python3
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python-py35 python3.6m
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/lib/python3.5/dist-packages/numpy/core/include \
/home/zy/anaconda3/include/python3.5m
i. 修改 PYTHON_LIB 路径
# We need to be able to find libpythonX.X.so or .dylib.
#PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib \
$(ANACONDA_HOME)/pkgs/python-3.5.2.0/lib
j.更改matlab路径
# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
MATLAB_DIR := /usr/local
MATLAB_DIR := /usr/local/MATLAB/R2018a
以上内容一定要仔细查看自己相应的目录一一对应,若没有相关目录一定是少安装了某些依赖,这一步配置好了后面就不会有什么错误
- 修改 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
- 编辑 /usr/local/cuda/include/host_config.h ,将其中的第115行注释掉
//#error -- unsupported GNU version! gcc versions later than 5 are not supported!
- 编译 caffe
mkdir build
cd build
cmake ..
sudo make all
sudo make install
sudo make runtest
所有的 RUN 都为 OK 的话就编译成功了
- 编译 python 接口
cd .. #到caffe-master目录
sudo make pycaffe -j4
若出现 cannot find -lboost_python3 错误(参考 http://blog.csdn.net/u012675539/article/details/51351553),首先检查是否有文件存在 ls /usr/lib/x86_64-linux-gnu/libboost_python-py35.so
,若存在则建立软链接
cd /usr/lib/x86_64-linux-gnu/
sudo ln -s libboost_python-py35.so libboost_python3.so
sudo ln -s libboost_python-py35.a libboost_python3.a
sudo ln -s libboost_python-py35.so.1.58.0 libboost_python3.so.1.58.0
然后 sudo gedit /etc/ld.so.conf
添加 /usr/lib/x86_64-linux-gnu
sudo ldconfig
然后重新编译
sudo make clean
sudo make pycaffe -j4
- 添加环境变量
sudo gedit ~/.bashrc
最后一行添加export PYTHONPATH=/home/senius/Software/caffe-master/python:$PYTHONPATH
source ~/.bashrc
- 测试 python 接口
进入终端import caffe
不报错说明安装成功