Ubuntu上Caffe的安装及配置

1.安装依赖项

1
2
3
4
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 libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

2.安装必要的库

1
2
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev  
sudo apt-get install libatlas-base-dev

3.下载caffe

1
2
cd ~   
git clone https://github.com/BVLC/caffe.git #克隆caffe到本地,并命名为caffe

4.配置caffe

4.1生成Makefile.config文件

1
2
cd caffe/
cp Makefile.config.example Makefile.config

4.2修改Makefile.config文件中的配置

1
2
3
4
5
6
7
8
9
sudo gedit Makefile.config
#第一步
#去掉CPU_ONLY:=1的注释

#第二步
#将下面第一行代码改为第二行代码
#INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include

#INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/

5.编译caffe

1
2
3
4
5
6
7
make all

make pycaffe

make distribute

make test

若出现错误“fatal error: hdf5.h: 没有那个文件或目录”,4.1.2中第二步为解决方法。

6.配置caffe的pyhton接口pycaffe

6.1安装接口依赖库

1
2
3
4
5
6
7
sudo apt-get install python-pip
sudo atp-get install python-dev python-numpy
sudo apt-get install gfortran

sudo pip install –r python/requirements.txt
sudo pip install pydot
numpy scipy matplotlib sklearn skimage h5py protobuf leveldb networkx nose pandas gflags cython ipython gfortran

# 验证

安装结束后,可以执行如下语句验证:

1
sudo pip install -r requirements.txt

如果显示Requirement already satisfied,则安装成功,否则会继续进行安装。

6.2将caffe根目录下的python文件夹加入到环境变量中

1
2
3
4
5
6
7
vi ~/.bashrc
export PYTHONPATH=/usr/caffe/python:$PYTHONPATH
#在打开的文档中加入以下代码
export PYTHONPATH=/home/dlnu/caffe/python:$PYTHONPATH

#执行更新配置
sudo ldconfig

6.3编译Python接口

1
2
cd ~/caffe/
sudo make pycaffe

如果出现找不到numpy/arrayobject.h这种问题,则检查Makefile.config文件中的PYTHON路径(Python.h、
numpy/arrayobject.h的路径):
PYTHON_INCLUDE := /usr/include/python2.7 /usr/lib/python2.7/dist-packages/numpy/core/include

6.4验证python接口 进行python环境,引入caffe包,如果没有报错,则安装成功!

参考链接
https://blog.csdn.net/losteng/article/details/50809753
https://blog.csdn.net/u010167269/article/details/50703923