前记

由于是在一台用了很久的机器上安装caffe,过程比较复杂,网上说再干净的机器上装比较简单。如果能有干净的机器,就不用再过这么多坑了,希望大家好运!介绍这里就不说了,直接进入正题:

Caffe 主页  http://caffe.berkeleyvision.org/

github主页 https://github.com/BVLC/caffe

机器配置: 

[root@cdh-nn-182 build]# lsb_release -a
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID:    RedHatEnterpriseServer
Description:    Red Hat Enterprise Linux Server release 6.3 (Santiago)
Release:    6.3

gcc 版本 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)

Python 2.7.10 

 python已安装numpy,没有GPU

Prerequisites
  • CUDA is required for GPU mode.

    • library version 7.0 and the latest driver version are recommended, but 6.* is fine too
    • 5.5, and 5.0 are compatible but considered legacy
  • BLAS via ATLAS, MKL, or OpenBLAS.
  • Boost >= 1.55
  • OpenCV >= 2.4 including 3.0
  • protobufgloggflags
  • IO libraries hdf5leveldbsnappylmdb

Pycaffe and Matcaffe interfaces have their own natural needs.

  • For Python Caffe: Python 2.7 or Python 3.3+numpy (>= 1.7), boost-provided boost.python
  • For MATLAB Caffe: MATLAB with the mex compiler.

1.  安装各种依赖包

yum install -y gcc gcc-c++ gtk+-devel libjpeg-devel libtiff-devel jasper-devel libpng-devel zlib-devel cmake
yum install git gtk2-devel pkgconfig numpy python python-pip python-devel gstreamer-plugins-base-devel libv4l ffmpeg-devel mplayer mencoder flvtool2
yum install libdc1394 libdc1394-devel.x86_64
yum install gtk*

2. python包安装

下载Caffe源码,按照./caffe/caffe-master/python/requirements.txt 安装所需要的包,用pip安装比较方便,不行就自己下载手动安装,没什么问题。

3. 安装protobufgloggflags

先从比较简单的来:

 

4. 安装CUDA

 从nvidia网站上下载最新的CUDA7.5,按自己的操作系统进行选择,这里选择下载cuda_7.5.18_linux.run,直接运行:

./cuda_6.5.14_linux_64.run

运行后会出现选择安装的项目,选择不安装驱动,否则会出错(driver installation is unable to locate the kernel source),也就是第一个选项No

5. 安装OpenBLAS

 ATLAS, MKL, or OpenBLAS都可以安装,以前用过OpenBLAS,这次就还装他吧

下载OpenBLAS源码,安装也很简单,make && make install即可,更多请参考 OpenBLAS编译和安装简介

6. 安装OpenCV

 OpenCV装起来比较麻烦,中间遇到了很多问题,参考安装文档,也可以参考网上很多人给的 自动安装配置脚本,由于我安装时出了很多问题,所以基本是自己手动装的。

首先将自己的CMake升级到最新版本,yum默认装的默认不行,只能手动升级了,否则在CMake阶段就会出现各种警告什么的。

下载OpenCV-3.0.0

unzip opencv-3.0.0.zip
cd opencv-3.0.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

##如果不出问题
make -j 32
sudo make install
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

下面说说我在make的时候碰到的问题:

Q1:已经安装了ffmpeg,出现错误AVCodecID未声明

cap_ffmpeg_impl.hpp:1556:83:错误:使用枚举‘AVCodecID’前没有给出声明

A1: 解决的方法是,添加make参数 -D WITH_FFMPEG=OFF,参考

 

Q2:出现parallel_for_pthreads undefined reference 错误,貌似是只有在CentOs中才会出现的

A2: 需要更改modules/core/src/parallel.cpp文件,参考1参考2,我这里只按照参考2给了parallel.cpp文件

 

Q3: 出现undefined reference to `jpeg_default_qtables'

../../../lib/libopencv_imgcodecs.so.3.0.0: undefined reference to `jpeg_default_qtables'

A3:安装,jpegsrc.v9a.tar.gz, 参考1参考2参考3

tar -xzvf jpegsrc.v9.tar.gz
cd jpeg-9
./configure
make libdir=/usr/lib64
make libdir=/usr/lib64 install

 

Q4:编译已完成,但是还是有问题:

[100%] Linking CXX shared library ../../lib/cv2.so
/usr/bin/ld: /usr/local/lib/libpython2.7.a(abstract.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libpython2.7.a: could not read symbols: Bad value
collect2: ld 返回 1
make[2]: *** [lib/cv2.so] 错误 1
make[1]: *** [modules/python2/CMakeFiles/opencv_python2.dir/all] 错误 2
make: *** [all] 错误 2

A4:重新编译安装python,configure时添加--enable-shared,参考

./configure --enable-shared
make
make install

 重新安装完以后可能会出现,执行python时error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory,解决方法是:

 vi /etc/ld.so.conf 
 #如果是非root权限帐号登录,使用 sudo vi /etc/ld.so.conf 
 #添加上python2.7的lib库地址,如我的/usr/local/Python2.7/lib,保存文件

/sbin/ldconfig

 

7. 安装Caffe

如果以上安装没有什么问题,这一不应该不会出错

unzip caffe-master.zip
cd caffe-master
cp Makefile.config.example Makefile.config

vim Makefile.config 
# 按照实际情况修改配置 CPU_ONLY :
= 1 BLAS := open

make all

8. 运行MINIST例子

参考

cd $CAFFE_ROOT
./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh

vim ./examples/mnist/lenet_solver.prototxt

solver_mode: CPU
./examples/mnist/train_lenet.sh

 就可以运行了

 

I0916 17:43:44.016604 63362 solver.cpp:571] Iteration 9900, lr = 0.00596843
I0916 17:44:05.355252 63362 solver.cpp:449] Snapshotting to binary proto file examples/mnist/lenet_iter_10000.caffemodel
I0916 17:44:05.371235 63362 solver.cpp:734] Snapshotting solver state to binary proto fileexamples/mnist/lenet_iter_10000.solverstate
I0916 17:44:05.464294 63362 solver.cpp:326] Iteration 10000, loss = 0.00184362
I0916 17:44:05.464337 63362 solver.cpp:346] Iteration 10000, Testing net (#0)
I0916 17:44:11.869861 63362 solver.cpp:414]     Test net output #0: accuracy = 0.9907
I0916 17:44:11.869920 63362 solver.cpp:414]     Test net output #1: loss = 0.0280591 (* 1 = 0.0280591 loss)
I0916 17:44:11.869931 63362 solver.cpp:331] Optimization Done.
I0916 17:44:11.869940 63362 caffe.cpp:214] Optimization Done.