ubuntu16.04上配置与测试caffe(CPU)

最近入了caffe的坑,在windows下各种配置不成功,不得不转战ubuntu.由于电脑渣,只能用CPU版的caffe.默认使用python2.7.
本文主要参考以下文章,根据自身安装过程中遇到的各种错误进行汇总,自身亲测有效,在此记录下配置过程

1.http://blog.csdn.net/u010402483/article/details/51506616

2.http://blog.csdn.net/xue_wenyuan/article/details/52037121

3.http://blog.csdn.net/striker_v/article/details/51596628

4.http://blog.csdn.net/hongye000000/article/details/51043913

5.http://www.cnblogs.com/yushuo1990/p/5909680.html

安装依赖包
1.安装protobuf,leveldb,snappy,OpenCV,hdf5, protobuf compiler andboost:

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

2.安装gflags,glogs ,lmdb andatlas.

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

下载Caffe
使用Git直接下载Caffe非常简单,或者去https://github.com/BVLC/caffe下载

git clone git://github.com/BVLC/caffe.git

编译Caffe
1.切换到Caffe所在目录

cd caffe

cp Makefile.config.example Makefile.config

2.配置Makefile.config
1)CPU_ONLY := 1

2)配置一些引用文件(增加部分主要是解决新版本下,HDF5的路径问题)

  1. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
  2. LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial
  3. BLAS := atlas

3.在Makefile文件的第173行,把 hdf5_hl 和hdf5修改为hdf5_serial_hl 和 hdf5_serial,也就是把下面第一行代码改为第二行代码。

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

4.Make Caffe

make all -j8
make test -j8
make runtest -j8

编译Python接口
1.Caffe拥有python\C++\shell接口,在Caffe使用python特别方便,在实例中都有接口的说明。

1)确保pip已经安装

sudo apt-get install python-pip

2)新建shell文件并执行安装依赖

cd caffe/python

for req in $(cat requirements.txt); do pip install $req; done

运行图如下,花费15min左右

3)编译python接口

make pycaffe

当出现下面错误的时候修改

fatal error: numpy/arrayobject.h: No such file or directory.
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/lib/python2.7/dist-packages/numpy/core/include 

This is where our error is. So by changing this line to:

PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/local/lib/python2.7/dist-packages/numpy/core/include   

重新编译,如果依然出错,可能python-numpy没有安装到位
解决方法
输入命令

sudo apt-get install python-numpy

2.运行python结构
在Ubuntu中,按住ctrl+alt+t打开终端,输入“python”打开python解释器,输入:

import sys
sys.path.append("/(你的caffe路径)/caffe/python")
sys.path.append("/(你的caffe路径)/caffe/python/caffe")

此时应该就安装成功

$ python2.7
Python 2.7.12 (default, Jul 1 2017, 13:22:29)
[GCC 5.4.020160609] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>>import caffe
>>>

PS:永久性添加python路径:

cd /usr/lib/python2.7/dist-packages 
sudo echo mycaffe.pth    #建立一个mycaffe.pth文件
sudo gedit mycaffe.pth   #编辑文件

在文件里添加caffe模块路径,例如 /home/skuggi/caffe/Python/,然后保存退出,以后就可以直接使用import caffe了。

在Mnist运行Lenet 进行测试
1、准备数据(在caffe路径下)

sudo sh ./data/mnist/get_mnist.sh
sudo sh ./examples/mnist/create_mnist.sh

2、修改配置

修改该目录下的prototxt扩展名配置文件
修改./examples/mnist/lenet_solver.prototxt
定位到最后一行:solver_mode: GPU,将GPU改为CPU。 使用CPU进行测试

3、运行
执行文件命令:

sudo sh ./examples/mnist/train_lenet.sh

最终训练完的模型存储为一个二进制的protobuf文件:
    ./examples/mnist/lenet_iter_10000.caffemodel

这个模型就可以用来直接使用了

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值