caffe的安装与使用

1 caffe安装

1.1 安装依赖

虚拟机为ubuntu 16.04,仅cpu。
按照官网安装相应依赖(https://caffe.berkeleyvision.org/install_apt.html),执行了一下几条命令:

1
2
3
4
5
6
7
8
9
10
11
12
通用依赖:
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

blas依赖:
sudo apt-get install libatlas-base-dev

python依赖:
sudo apt-get install python-dev

以下依赖不知是否必须安装:
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

1.2 源码安装

源码地址:https://github.com/BVLC/caffe
官网方法:https://caffe.berkeleyvision.org/installation.html#compilation
参考中文链接:https://www.jianshu.com/p/8878d2bdaf99

1.2.1 编译源码

1
2
3
unzip caffe-master.zip
cd caffe-master
cp Makefile.config.example Makefile.config

更改Makefile.config文件中,如下行:

1
2
3
4
5
6
7
`CPU_ONLY := 1`前的注释符“#”去掉
将:
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

执行如下三条命令完成安装:

1
2
3
sudo make all
sudo make test
sudo make runtest

1.2.2 python环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
安装pip:
sudo apt-get install python-pip

安装python借口依赖库:
sudo apt-get install gfortran
cd /home/bling/caffe-master/python
for req in $(cat requirements.txt); do pip install $req; done

验证:
sudo pip install -r requirements.txt

设置环境变量:
sudo vim ~/.bashrc
export PYTHONPATH=/home/bling/caffe-master/python:$PYTHONPATH
source ~/.bashrc

安装pycaffe

1
2
3
4
5
cd /home/bling/caffe-master/
sudo make pycaffe

验证使用,进python环境,输入
import caffe

使用示例

MNIST tutorial:
https://caffe.berkeleyvision.org/gathered/examples/mnist.html

reference ImageNet model tutorial:
https://caffe.berkeleyvision.org/gathered/examples/imagenet.html