caffe 安装及trouble shooting

1. 安装依赖库

执行以下命令:

1
2
3
4
$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 libgflags-dev libgoogle-glog-dev liblmdb-dev
$sudo apt-get install libatlas-base-dev

安装第一个就出问题:

1
sudo apt-get install libprotobuf-dev

返回错误:

由于解决问题后,将terminal的输出被clear掉了,所以用以下相似的问题来说明解决过程

1
2
The following packages have unmet dependencies:
libxml2-dev : Depends: libxml2 (= 2.7.8.dfsg-5.1ubuntu4) but 2.7.8.dfsg-5.1ubuntu4.6 is to be installed

解决方法:

  1. 安装aptitude:
1
2
$sudo apt-get install aptitude
$aptitude why-not libxml2
  1. 找到已经安装的对应的libxml2包:
1
$dpkg -l | grep libxml2
  1. 删除libxml2,并删除其他所有依赖包,--force-all参数不能少:
1
$sudo dpkg --purge --force-all libxml2
  1. 更正错误:
1
$sudo apt-get -f install
  1. 安装欠缺的包
1
$sudo apt-get install libxml2-dev

遇到相同的问题,将libxml2-devlibxml2,替换成自己问题中的对应库。

2. 安装NVIDIA驱动和CUDA,openCV

早已安装完成

3. 下载caffe源码

这里下载到home/XXX

修改Makefile.config文件。先复制一份:

1
cp Makefile.config.example Makefile.config

后修改配置文件Makefile.config,下面是我的修改:

  1. hdf5

    将hdf5的路径添加到INCLUDE_DIRSLIBRARY_DIRS之后:

    1
    2
    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
  2. sm_version

    由于我的机器的CUDA版本是9.0,所以要将下面的sm_20, sm_21注释掉,低版本的一些指令对于高版本的不适用。如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \
    -gencode arch=compute_35,code=sm_35 \
    -gencode arch=compute_50,code=sm_50 \
    -gencode arch=compute_52,code=sm_52 \
    -gencode arch=compute_60,code=sm_60 \
    -gencode arch=compute_61,code=sm_61 \
    -gencode arch=compute_61,code=compute_61 \
    # -gencode arch=compute_20,code=sm_20 \
    # -gencode arch=compute_20,code=sm_21 \
  3. OpenCV版本

    去掉下面的注释,表示使用openCV 版本3:

    1
    OPENCV_VERSION := 3
  4. CUDA路径

    检查机器CUDA路径是否正确:

    1
    CUDA_DIR := /usr/local/cuda

4. 编译测试

配置完成之后编译,-j4表示用4个核心执行任务:

1
2
3
$sudo make all -j4 
$sudo make test -j4
$sudo make runtest -j4

期待不出错。

如果有错,比如编译期间又修改了Makefile.config文件,返回类似错误:

1
undefined reference to `cv::imread(cv::String const&, int)

可是OpenCV已经修改过了,此时的处理情况:

将编译了一半的build文件夹删除,后重新编译:

1
2
$sudo rm -rf ./build/*
$sudo make all -j4

其中build/文件夹是编译后得到的,

最终期望的是sudo make runtest -j4运行无误,如果terminal最后返回:

表示caffe 安装配置成功完成(如果要使用python接口,还要其他操作,我使用CXX)。

5. 编译python接口

caffe使用python2.

  1. 第O步,使用linux自带python2,安装依赖库:

    1
    2
    sudo apt-get install python-pip
    sudo apt-get install python-numpy

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

    1
    2
    3
    sudo apt-get install gfortran
    cd ./python
    sudo cat python/requirements.txt|xargs -L 1 sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple

    检查

    1
    sudo pip install -r python/requirements.txt

    编译成功的会显示Requirement already satisfied,没有编译成功的会继续安装。

  2. 第一步,执行命令:

    1
    $ make pycaffe -j4
  3. 第二步,在~/.bashrc/etc/profile中添加caffe中的python 路径:

    1
    export PYTHONPATH=/home/junhui/caffe-master/python:$PYTHONPATH
  4. 第三步,source使之生效:

    1
    2
    $ source /etc/profile
    $ source ~/.bashrc

    现在在python脚本中就可以import caffe了。

可能的出错:

  1. numpy/arrayobject.h: missing

    1
    2
    3
    4
    5
    6
    7
    8
    junhui@gnome:~/caffe$ sudo make pytest –j4
    CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
    python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory
    #include <numpy/arrayobject.h>
    ^
    compilation terminated.
    Makefile:517: recipe for target 'python/caffe/_caffe.so' failed
    make: *** [python/caffe/_caffe.so] Error 1

    解决方法:

    在Makefile.config文件中的这个地方:

    1
    2
    3
    # We need to be able to find Python.h and numpy/arrayobject.h.
    PYTHON_INCLUDE := /usr/include/python2.7 \
    /usr/lib/python2.7/dist-packages/numpy/core/include

    添加一行:

    1
    2
    3
    4
    # We need to be able to find Python.h and numpy/arrayobject.h.
    PYTHON_INCLUDE := /usr/include/python2.7 \
    /usr/lib/python2.7/dist-packages/numpy/core/include \
    /usr/local/lib/python2.7/dist-packages/numpy/core/include

    再次编译成功

    1
    $ make pycaffe -j4
  2. Python.h: No suchfile or directory

    caffe 使用python2,需要在./bashrc中添加python2的路径:

    如果不是使用anaconda2中的python2,则添加如下第一句;如果使用用的anaconda2中的python2,则添加如下第二句

    1
    2
    export CPLUS_INCLUDE_PATH=/usr/include/python2.7:$CPLUS_INCLUDE_PATH
    export CPLUS_INCLUDE_PATH=/home/junhui/anaconda2/include/python2.7/:$CPLUS_INCLUDE_PATHort
  3. protobuf出错

    使用anaconda时出的错:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    CXX .build_release/src/caffe/proto/caffe.pb.cc
    In file included from .build_release/src/caffe/proto/caffe.pb.cc:5:0:
    .build_release/src/caffe/proto/caffe.pb.h:12:2: error: #error This file was generated by a newer version of protoc which is
    #error This file was generated by a newer version of protoc which is
    ^
    .build_release/src/caffe/proto/caffe.pb.h:13:2: error: #error incompatible with your Protocol Buffer headers. Please update
    #error incompatible with your Protocol Buffer headers. Please update
    ^
    .build_release/src/caffe/proto/caffe.pb.h:14:2: error: #error your headers.
    #error your headers.
    ^
    In file included from .build_release/src/caffe/proto/caffe.pb.cc:5:0:
    .build_release/src/caffe/proto/caffe.pb.h:26:55: fatal error: google/protobuf/generated_enum_reflection.h: No such file or directory
    #include <google/protobuf/generated_enum_reflection.h>

    compilation terminated.
    make: *** [.build_release/src/caffe/proto/caffe.pb.o] Error 1

    解决方法,将机器上的anaconda3删除,使用linux自带python2,并安装以来库。(其实可以使用anaconda2).

  4. make clean

    如果make中间出错,改错后,先make clean,后重新make

import caffe 成功后测试pycaffe

在mnist数据集上运行lenet

1
2
3
sudo ./data/mnist/get_mnist.sh
sudo ./examples/mnist/create_mnist.sh
sudo ./examples/mnist/train_lenet.sh