Ubuntu14.04搭建Caffe(仅CPU)详解教程

这篇文章主要介绍了Ubuntu14.04在系统平台(未使用anaconda环境管理)搭建CPU版Caffe,无GPU,详解教程,操作系统是Ubuntu 14.04,本文分步骤给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下.
操作系统:  Ubuntu  14.04
PYTHON版本:2.7,使用python3会有各种包依赖问题
是否使用PYTHON API: 是, 目标是安装后CAFFE能作为PYTHON MODULE来使用
硬件: 云服务器, 只使用CPU模式

本文主要参考大牛的博客https://www.cnblogs.com/xuanxufeng/p/6016945.html 然后填了一些自己安装过程中遇到的坑。

1.安装依赖

 
  
1 sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf- compiler
2 sudo apt-get install --no- install -recommends libboost-all- dev
3 sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb- dev
4 sudo apt-get install libatlas-base-dev
5 sudo apt-get install libopencv-dev
PYTHON需要2.7版本,这是操作系统本身已经安装好的. 输入python2.7 --version 会显示具体的版本号说明安装了.
但是还需要sudo apt-get install python-dev

2.下载Caffe 使用Git直接下载Caffe非常简单,或者去https://github.com/BVLC/caffe下载。由于我习惯去github上找代码,所以就直接去下载的源码。

使用git下载caffe到家目录,下载完成后,会在家目录下的下载里找到caffe文件夹,切换到caffe文件夹中。

$ cd ~

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

3.编译Caffe (1)创建Makefile.comfig文件

cp Makefile.config.example Makefile.config

(2)修改配置文件Makefile.config

[plain]  view plain  copy
  1.  style="font-size:14px;"># CPU-only switch (uncomment to build without GPU support).  
  2.  CPU_ONLY := 1        #一定需要打开。  
  3. # Uncomment if you're using OpenCV 3  
  4. # OPENCV_VERSION := 3                    #用的是2.4版本不需要打开。  
  5.   
  6. # To customize your choice of compiler, uncomment and set the following.  
  7. # N.B. the default for Linux is g++ and the default for OSX is clang++  
  8. # CUSTOM_CXX := g++                                               #已经更新到至少4.8,选择默认的就好。  
  9.   
  10. # CUDA directory contains bin/ and lib/ directories that we need.    #这是GPU用到的,注释掉就可以。  
  11. #CUDA_DIR := /usr/local/cuda  
  12. # On Ubuntu 14.04, if cuda tools are installed via  
  13. # "sudo apt-get install nvidia-cuda-toolkit" then use this instead:  
  14. # CUDA_DIR := /usr  
  15.   
  16. # CUDA architecture setting: going with all of them.  
  17. # For CUDA < 6.0, comment the *_50 lines for compatibility.        #这些部分全部注释,和我们cpu家族没有关系。  
  18. #CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \  
  19. ##      -gencode arch=compute_20,code=sm_21 \  
  20. #       -gencode arch=compute_30,code=sm_30 \  
  21. #       -gencode arch=compute_35,code=sm_35 \  
  22. #       -gencode arch=compute_50,code=sm_50 \  
  23. #       -gencode arch=compute_50,code=compute_50  
  24.   
  25. # BLAS choice:  
  26. # atlas for ATLAS (default)  
  27. # mkl for MKL  
  28. # open for OpenBlas  
  29. BLAS := atlas                      #如果安装mkl并且设置好路径的可以试一下mkl,不过一般会出错,找不到lmkl文件,建议采用atlas,简单易行。  
  30. # Custom (MKL/ATLAS/OpenBLAS) include and lib directories.  
  31. # Leave commented to accept the defaults for your choice of BLAS  
  32. # (which should work)!  
  33. # BLAS_INCLUDE := /path/to/your/blas  
  34. # BLAS_LIB := /path/to/your/blas  
  35.   
  36. # Homebrew puts openblas in a directory that is not on the standard search path  
  37. # BLAS_INCLUDE := $(shell brew --prefix openblas)/include  
  38. # BLAS_LIB := $(shell brew --prefix openblas)/lib  
  39.   
  40. # This is required only if you will compile the matlab interface.     #python很流行,matlab暂时没安装,也就用不到了。  
  41. # MATLAB directory should contain the mex binary in /bin.  
  42. # MATLAB_DIR := /usr/local  
  43. # MATLAB_DIR := /Applications/MATLAB_R2012b.app  
  44.   
  45. # NOTE: this is required only if you will compile the python interface.
  46. # We need to be able to find Python.h and numpy/arrayobject.h.  
  47. PYTHON_INCLUDE := /usr/include/python2.7 \  #因为下安装的时候采用的时2.7版本的python,所以这里打开2.7版本的,但是要确保python安装在系统默认路径下。
  48.        /usr/lib/python2.7/dist-packages/numpy/core/include  
  49. # Anaconda Python distribution is quite popular. Include path:  
  50. # Verify anaconda location, sometimes it's in root.  
  51. # ANACONDA_HOME := $(HOME)/anaconda2                               
  52. # PYTHON_INCLUDE := $(ANACONDA_HOME)/include \  
  53.          $(ANACONDA_HOME)/include/python2.7 \              
  54.          $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \  
  55.   
  56. # Uncomment to use Python 3 (default is Python 2)  
  57. # PYTHON_LIBRARIES := boost_python3 python3.5m  
  58. # PYTHON_INCLUDE := /usr/include/python3.5m \  
  59. #                 /usr/lib/python3.5/dist-packages/numpy/core/include  
  60.   
  61. # We need to be able to find libpythonX.X.so or .dylib.  
  62.   PYTHON_LIB := /usr/lib  
  63. # PYTHON_LIB := $(ANACONDA_HOME)/lib                                 
  64.   
  65. # Homebrew installs numpy in a non standard path (keg only)  
  66. # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include  
  67. # PYTHON_LIB += $(shell brew --prefix numpy)/lib  
  68.   
  69. # Uncomment to support layers written in Python (will link against Python libs)  
  70. # WITH_PYTHON_LAYER := 1  
  71.   
  72. # Whatever else you find you need goes here.  
  73. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include  /usr/include/hdf5/serial    #增加hdf5路径
  74. LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib  /usr/lib/x86_64-linux-gnu/hdf5/serial  
  75. # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies  
  76. # INCLUDE_DIRS += $(shell brew --prefix)/include  
  77. # LIBRARY_DIRS += $(shell brew --prefix)/lib  
  78.   
  79. # Uncomment to use `pkg-config` to specify OpenCV library paths.  
  80. # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)  
  81. # USE_PKG_CONFIG := 1  
  82.   
  83. BUILD_DIR := build                            #编译分配用到的路径,打开即可。  
  84. DISTRIBUTE_DIR := distribute  
  85.   
  86. # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171  
  87. # DEBUG := 1                       #挑错模式,喜欢debug的朋友不妨试试。

(3)编译 Caffe


[plain]
  view plain  copy
  1. make pycaffe -jX                  #为了提高编译速度,这里的x设成自己的cpu核数  
  2. make all -jX  
  3. make test -jX   
[plain]  view plain  copy
  1. make  runtest -jX                 #注意,这里也可直接运行测试,make runtest  
注意:这里如果使用MAC OS系统终端登陆服务器配置的话在runtest的时候会有一些Field,原因是字符集不匹配,在终端执行以下命令再运行runtest就好.

export LC_ALL="C"

如果上面4行某一行报错之后想要重试,建议先make clean再重新开始。

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

  • 确保pip已经安装
sudo apt-get install python-pip
  • 执行安装依赖

在caffe根目录的python文件夹下,有一个requirements.txt的清单文件,上面列出了需要的依赖库,按照这个清单安装就可以了。

在安装scipy库的时候,需要fortran编译器(gfortran),如果没有这个编译器就会报错,因此,我们可以先安装一下。

首先回到caffe的根目录,然后执行安装代码:

 
  
cd ~/ caffe
sudo add -apt -repository ppa:kirillshkrogalev/ffmpeg -next
sudo apt -get update
 
  
sudo apt-get install gfortran
cd ./python
for req in $( cat requirements.txt); do pip install $req; done

安装完成以后,再次回到caffe根目录我们可以执行:

sudo pip install -r python/requirements.txt

就会看到,安装成功的,都会显示Requirement already satisfied, 没有安装成功的,会继续安装。

  • 运行python结构
 
  
$ python2. 7
Python 2.7. 12 (default, Jul 1 2016, 15: 12: 24 )
[GCC 5.4. 0 20160609 ] on linux2
Type " help ", " copyright ", " credits " or " license " for more information.
>>> import caffe
>>>

如果没有报错,说明caffe安装全部完成

注意:如果import caffe提示找不到caffe,需要将 PYTHONPATH加入.bashrc中:

  1. vim ~/.bashrc
  2. 在文件末端添加下面两句
  3. #set PYTHONPATH                        设置caffe下的路径。否则caffe也找不到。  
  4. export PYTHONPATH="/home/tom/caffe/python:$PYTHONPATH"  标红的地方换成自己的caffe路径
加完后执行:
source ~/.bashrc

5.在Mnist运行Lenet

  • 获取数据源
 
  
./data/mnist/get_mnist. sh
./examples/mnist/create_mnist. sh
  • 因为是CPU运行,所以修改在examples文件下的Mnist下的lenet_solver.prototxt中的solver_mode:CPU
solver_mode: CPU
  • 训练模型
./examples/mnist/train_lenet.sh

6.最后,我的文章是基于各位前辈大神们的文章,虽然按他们的过程走我都报错了,但是最终还是帮助我安装成功。为了表示对别人成果的尊重,这里留下大神们的参考链接:

http://www.linuxidc.com/Linux/2016-09/135034.htm

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

http://www.cnblogs.com/denny402/p/5679037.html

http://blog.csdn.net/u012029332/article/details/51098248

http://blog.csdn.net/mandagod/article/details/52337434

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值