0%

Caffe配置记录

前言

安装caffe与配置环境详尽教程,针对我遇到的坑提供了一些解决办法。

这里涉及的环境配置为:ubuntu16.04,cuda8.0,cudnn5,opencv3.1。

1. 安装依赖包

1
2
3
4
5
6
7
8
9
10
sudo apt-get update 
sudo apt-get install -y build-essential cmake git pkg-config
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install -y libatlas-base-dev
sudo apt-get install -y --no-install-recommends
sudo apt-get install -y libboost-all-dev
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install -y python-pip
sudo apt-get install -y python-dev
sudo apt-get install -y python-numpy python-scipy

2. 复制文件,修改配置文件

1
2
3
4
5
6
7
8
9
$ sudo apt-get install git

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

$ cd caffe

caffe$ cp Makefile.config.example Makefile.config

caffe$ gedit Makefile.config
  • 如果采用CPU模式:

    # CPU_ONLY := 1 取消注释 CPU_ONLY := 1

  • 使用GPU:

    USE_CUDNN :=1

  • 使用opencv3.0

    # OPENCV_VERSION := 3 修改为:OPENCV_VERSION := 3

  • 若要使用python来编写layer,则需要

    # WITH_PYTHON_LAYER := 1修改为WITH_PYTHON_LAYER := 1

  • 修改hdf5

1
# Whatever else you find you need goes here.
1
2
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

修改为

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 /usr/lib/x86_64-linux-gnu/hdf5/serial

这是因为ubuntu16.04的文件包含位置发生了变化,尤其是需要用到的hdf5的位置,所以需要更改这一路径

1
caffe$ find . -type f -exec sed -i -e 's^"hdf5.h"^"hdf5/serial/hdf5.h"^g' -e 's^"hdf5_hl.h"^"hdf5/serial/hdf5_hl.h"^g' '{}' \;
1
2
3
4
5
cd /usr/lib/x86_64-linux-gnu

~gnu$ sudo ln -s libhdf5_serial.so.10.1.0 libhdf5.so

~gnu$ sudo ln -s libhdf5_serial_hl.so.10.0.2 libhdf5_hl.so
  • 修改nvcc
1
sudo gedit Makefile

1
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)

替换为

1
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
  • 编辑
1
sudo gedit /usr/local/cuda/include/host_config.h
  • 注释掉高版本gcc报错
1
#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!

改为

1
//#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!

3. 编译caffe

1
2
3
4
5
6
7
caffe$ find . -type f -exec sed -i -e 's^"hdf5.h"^"hdf5/serial/hdf5.h"^g' -e 's^"hdf5_hl.h"^"hdf5/serial/hdf5_hl.h"^g' '{}' \;

$ cd /usr/lib/x86_64-linux-gnu

~gnu$ sudo ln -s libhdf5_serial.so.10.1.0 libhdf5.so

~gnu$ sudo ln -s libhdf5_serial_hl.so.10.0.2 libhdf5_hl.so
  • 编译开始:
1
2
3
4
5
6
7
8
9
$ cd ..

caffe$ sudo make all -j4

caffe$ sudo make test -j4

caffe$ sudo make runtest -j4

caffe$ sudo make distribute
  • 生成数据:
1
2
3
caffe$ cd data/mnist

mnist$ sudo sh ./get_mnist.sh
  • 处理成LDB:
1
2
3
$ cd ..

caffe$ sudo sh ./examples/mnist/create_mnist.sh

注意要在原始caffe下进行

  • 训练开始

如果CPU:

1
2
3
caffe$ cd examples/mnist/

mnist$ gedit lenet_solver.prototxt

将文件最后的solver_mode:GPU改为solver_mode: CPU

1
2
3
$ cd ..

caffe$ sudo sh ./examples/mnist/train_lenet.sh
  • 测试数据:
1
2
3
4
5
caffe$ cd examples/mnist/

mnist$ touch test_lenet.sh #生成测试文件

mnist$ gedit test_lenet.sh

编辑的内容如下:

1
2
3
#!/usr/bin/env sh

./build/tools/caffe test --model=examples/mnist/lenet_train_test.prototxt --weights=examples/mnist/lenet_iter_10000.caffemodel -iterations 100

注意一定是两行,注意后面-前面的空格

1
caffe$ sudo sh ./examples/mnist/test_lenet.sh

测试成功!

1
caffe$ sudo make pycaffe

不要提前make!

4. 报错问题

  • 如果出现错误:
1
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: 没有那个文件或目录

可以:

1
sudo apt-get install python-numpy

成功!

  • 如果import caffe找不到module:
1
sudo gedit ~/.bashrc

在最后面添加路径:

1
export PYTHONPATH=/home/cherrie/caffe/python:$PYTHONPATH

对于anaconda:

1
2
3
LD_LIBRARY_PATH=/home/cherrie/anaconda2/lib:$LD_LIBRARY_PATH

export LD_LIBRARY_PATH

返回终端:

1
source .bashrc

使环境变量立即生效

  • 如果
1
ImportError: No module named google.protobuf.internal

安装

1
sudo apt install python-protobuf

在python里面

1
2
3
import sys
>>> sys.path.append('/usr/lib/python2.7/dist-packages/')
>>> import caffe

或者永久办法:

/usr/lib/python2.7/dist-packages里面的google文件夹

复制到

/home/cherrie/anaconda2/lib/python2.7/site-packages

  • import cv2找不到模块

/usr/lib/python2.7/dist-packages里面的cv.py cv.pyc cv2.so

复制到

/home/cherrie/anaconda2/lib/python2.7/site-packages