最近入了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-dev2.安装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.config2.配置Makefile.config 1)CPU_ONLY := 1
2)配置一些引用文件(增加部分主要是解决新版本下,HDF5的路径问题)
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/serialBLAS := atlas3.在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_serial4.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-pip2)新建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-numpy2.运行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.sh2、修改配置
修改该目录下的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
这个模型就可以用来直接使用了