Ubuntu16.04 安装caffe(纯CPU版)+ MNIST集实例测试
原创
©著作权归作者所有:来自51CTO博客作者luteresa的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.安装caffe依赖包,apt-get大法搞定:
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
2.下载caffe源码
仓库:git clone https://github.com/BVLC/caffe.git my_caffe
3.修改Makefile.config配置文件
cd my_caffe/
ps:配置为CPU版本,赞不支持GPU
cp Makefile.config.example Makefile.config
修改:
--- a/Makefile.config
+++ b/Makefile.config
@@ -5,7 +5,7 @@
# USE_CUDNN := 1
# CPU-only switch (uncomment to build without GPU support).
-# CPU_ONLY := 1
+CPU_ONLY := 1
# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
@@ -92,8 +92,8 @@ PYTHON_LIB := /usr/lib
# WITH_PYTHON_LAYER := 1
# 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/hdf5/serial
编译caffe:
make all
make test
make runtest
测试全部通过,说明caffe编译成功(默认支持C++接口)。
caffe编译成功(默认支持C++接口)。
4.配置支持python接口:(默认python2.7)
安装python相关依赖库
cd my_caffe
cat python/requirements.txt |xargs -l sudo pip2 install
可能会报scikit-image错误
执行make pycaffe命令的时候,如果报错:fatalerror: numpy/arrayobject.h没有那个文件或目录
解决方法:
配置python路径;
在.bashrc末尾添加:
export PYTHONPATH=/home/leon/work/ai/my_caffe/python:$PYTHONPATH
<strong>import caffe没报错,说明路径配置成功;</strong>
5、训练MNIST集
(1)获取MNIST数据
./data/mnist/get_mnist.sh
(2)将其转换成Lmdb数据库格式
./examples/mnist/create_mnist.sh
(3)训练网络
./examples/mnist/train_lenet.sh
训练完成
测试:
./build/tools/caffe.bin test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -iterations 100
参数解释:
test:只做测试(前向传播计算),不更新参数(后向传播计算)
-model examples/mnist/lenet_train_test.prototxt:指定模型描述文件
-weights examples/mnist/lenet_iter_10000.caffemodel :指定模型预先训练好的权值文件
-iterations 100:测试迭代次数。测试数目为(iterations*batch_size),batch_size在model prototxt中设定。
PS:本流程为caffe默认配置,支持了C++和Python 2.7接口,接下来想配置支持python3.6。caffe默认配置,支持了C++和Python 2.7接口,接下来想配置支持python3.6。