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).  
     CPU_ONLY := 1        #一定需要打开。  
    # Uncomment if you're using OpenCV 3  
    # OPENCV_VERSION := 3                    #用的是2.4版本不需要打开。  
      
    # To customize your choice of compiler, uncomment and set the following.  
    # N.B. the default for Linux is g++ and the default for OSX is clang++  
    # CUSTOM_CXX := g++                                               #已经更新到至少4.8,选择默认的就好。  
      
    # CUDA directory contains bin/ and lib/ directories that we need.    #这是GPU用到的,注释掉就可以。  
    #CUDA_DIR := /usr/local/cuda  
    # On Ubuntu 14.04, if cuda tools are installed via  
    # "sudo apt-get install nvidia-cuda-toolkit" then use this instead:  
    # CUDA_DIR := /usr  
      
    # CUDA architecture setting: going with all of them.  
    # For CUDA < 6.0, comment the *_50 lines for compatibility.        #这些部分全部注释,和我们cpu家族没有关系。  
    #CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \  
    ##      -gencode arch=compute_20,code=sm_21 \  
    #       -gencode arch=compute_30,code=sm_30 \  
    #       -gencode arch=compute_35,code=sm_35 \  
    #       -gencode arch=compute_50,code=sm_50 \  
    #       -gencode arch=compute_50,code=compute_50  
      
    # BLAS choice:  
    # atlas for ATLAS (default)  
    # mkl for MKL  
    # open for OpenBlas  
    BLAS := atlas                      #如果安装mkl并且设置好路径的可以试一下mkl,不过一般会出错,找不到lmkl文件,建议采用atlas,简单易行。  
    # Custom (MKL/ATLAS/OpenBLAS) include and lib directories.  
    # Leave commented to accept the defaults for your choice of BLAS  
    # (which should work)!  
    # BLAS_INCLUDE := /path/to/your/blas  
    # BLAS_LIB := /path/to/your/blas  
      
    # Homebrew puts openblas in a directory that is not on the standard search path  
    # BLAS_INCLUDE := $(shell brew --prefix openblas)/include  
    # BLAS_LIB := $(shell brew --prefix openblas)/lib  
      
    # This is required only if you will compile the matlab interface.     #python很流行,matlab暂时没安装,也就用不到了。  
    # MATLAB directory should contain the mex binary in /bin.  
    # MATLAB_DIR := /usr/local  
    # MATLAB_DIR := /Applications/MATLAB_R2012b.app  
      
    # NOTE: this is required only if you will compile the python interface.
    # We need to be able to find Python.h and numpy/arrayobject.h.  
    PYTHON_INCLUDE := /usr/include/python2.7 \  #因为下安装的时候采用的时2.7版本的python,所以这里打开2.7版本的,但是要确保python安装在系统默认路径下。
           /usr/lib/python2.7/dist-packages/numpy/core/include  
    # Anaconda Python distribution is quite popular. Include path:  
    # Verify anaconda location, sometimes it's in root.  
    # ANACONDA_HOME := $(HOME)/anaconda2                               
    # PYTHON_INCLUDE := $(ANACONDA_HOME)/include \  
             $(ANACONDA_HOME)/include/python2.7 \              
             $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \  
      
    # Uncomment to use Python 3 (default is Python 2)  
    # PYTHON_LIBRARIES := boost_python3 python3.5m  
    # PYTHON_INCLUDE := /usr/include/python3.5m \  
    #                 /usr/lib/python3.5/dist-packages/numpy/core/include  
      
    # We need to be able to find libpythonX.X.so or .dylib.  
      PYTHON_LIB := /usr/lib  
    # PYTHON_LIB := $(ANACONDA_HOME)/lib                                 
      
    # Homebrew installs numpy in a non standard path (keg only)  
    # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include  
    # PYTHON_LIB += $(shell brew --prefix numpy)/lib
    
    
       
      
    # Uncomment to support layers written in Python (will link against Python libs)  
    # WITH_PYTHON_LAYER := 1  
      
    # Whatever else you find you need goes here.  
    INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial    #增加hdf5路径
    LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib  /usr/lib/x86_64-linux-gnu/hdf5/serial  
    # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies  
    # INCLUDE_DIRS += $(shell brew --prefix)/include  
    # LIBRARY_DIRS += $(shell brew --prefix)/lib  
      
    # Uncomment to use `pkg-config` to specify OpenCV library paths.  
    # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)  
    # USE_PKG_CONFIG := 1  
      
    BUILD_DIR := build                            #编译分配用到的路径,打开即可。  
    DISTRIBUTE_DIR := distribute  
      
    # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171  
    # DEBUG := 1                       #挑错模式,喜欢debug的朋友不妨试试。
    # Uncomment to support layers written in Python (will link against Python libs)  
    # WITH_PYTHON_LAYER := 1  
      
    # Whatever else you find you need goes here.  
    INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial    #增加hdf5路径
    LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib  /usr/lib/x86_64-linux-gnu/hdf5/serial  
    # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies  
    # INCLUDE_DIRS += $(shell brew --prefix)/include  
    # LIBRARY_DIRS += $(shell brew --prefix)/lib  
      
    # Uncomment to use `pkg-config` to specify OpenCV library paths.  
    # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)  
    # USE_PKG_CONFIG := 1  
      
    BUILD_DIR := build                            #编译分配用到的路径,打开即可。  
    DISTRIBUTE_DIR := distribute  
      
    # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171  
    # DEBUG := 1                       #挑错模式,喜欢debug的朋友不妨试试。

     

 

(3)编译 Caffe
[plain] view plain copy

  1. make pycaffe -jX                  #为了提高编译速度,这里的x设成自己的cpu核数  
    make all -jX  
    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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值