本教程是在 ubuntu16.04下安装的caffe cpu版本,使用的是python3.5版本。具体安装步骤如下:
1.安装caffe相关依赖包:
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 libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install libatlas-base-dev
2.安装caffe python3接口
sudo apt install python3-pip #安装pip工具。
sudo apt install python3-numpy #解决后面编译无法找到numpy错误。
sudo apt-get install gfortran #安装依赖文件
sudo pip3 install matplotlib #解决后面import caffe报错
3.安装caffe
(1)下载caffe源码
git clone http://github.com/BVLC/caffe.git
(2)修改Makefile.config编译文件
先复制编译文件 Makefile.config.example 并命名为 Makefile.config,编辑Makefile.config
(a)
#CPU_ONLY := 1
去掉前面的#号:
CPU_ONLY := 1
(b)
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/hdf5/serial
(c)屏蔽python2版本,使用python3版本
#python2
#PYTHON_INCLUDE := /usr/include/python2.7 \
# /usr/lib/python2.7/dist-packages/numpy/core/include
#python3
PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/lib/python3/dist-packages/numpy/core/include
编辑完成后,保存并退出。
4.编译caffe
执行编译命令:
sudo make all -j2
sudo make test -j2
sudo make runtest -j2
要是没有错误就表示编译成功了。
5.设置python接口
(1)先要安装一些依赖库:
cd ~/caffe
sudo pip3 install -r python/requirements.txt(2)将caffe根目录下的python文件夹加入到环境变量:
vim ~/.bashrc
在文件的最后一行添加:
export PYTHONPATH=/home/liguiyuan/deep_learning/caffe/python:$PYTHONPATH记得根据自己的路径做下修改哦!
(3)更新下配置:
sudo ldconfig
(4)编译python接口:
cd ~/caffe/
sudo make pycaffe -j2
6.验证安装是否成功
$ python
>>> import caffe
>>>
表示安装成功。
下面是安装过程中遇到的一些错误:
错误1:
touch python/caffe/proto/__init__.py
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
PROTOC (python) src/caffe/proto/caffe.proto
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: 没有那个文件或目录
compilation terminated.
Makefile:517: recipe for target 'python/caffe/_caffe.so' failed
make: *** [python/caffe/_caffe.so] Error 1
解决办法:安装python-numpy库
sudo apt-get install python-numpy
错误2:
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
/usr/bin/ld: 找不到 -lboost_python3
collect2: error: ld returned 1 exit status
Makefile:517: recipe for target 'python/caffe/_caffe.so' failed
make: *** [python/caffe/_caffe.so] Error 1
原因是找不到库,创建一个链接就好:
sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so /usr/lib/x86_64-linux-gnu/libboost_python3.so
参考: