Lab223服务器caffe装配过程

  1. 安装ubuntu 16.04 LTS x64
    使用工具 大白菜装机版 或者 UltroISO 或者 Uiversal-USB-install- 最新版 制作U盘启动盘,官方下载64位版本: ubuntu-16.04-desktop-amd64.iso,千万注意在装机过程中不要插网线,笔者因为给服务器插上网线很多次没有进入boot界面,但装win10没有出现这个问题,当时感觉很困恼…
  2. 更新源
cd /etc/apt/
sudo cp sources.list sources.list.bak
sudo gedit sources.list

在sources.list 文件头部添加如下源

deb http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse

然后更新源和安装的包:

sudo apt-get update
sudo apt-get upgrade
  1. 安装NVIDIA 显卡驱动
    采用ppa安装方式,没选择最新的nvidia-370,选择nvidia-367
    Ctrl+Alt+F1进入tty命令控制台,停止lightdm,然后开始安装驱动(用tty简称各种类型的终端设备)
sudo services lightdm stop

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get updates
sudo apt-get install nvidia-367
sudo apt-get install mesa-common-dev
sudo apt-get install freeglut3-dev
sudo reboot

  1. 安装CUDA8.0
    到官网下载cuda_8.0.44_linux.run,复制到根目录下。
sudo sh cuda_8.0.44_linux.run --tmpdir=/tmp/

遇到问题:incomplete installation,然后执行

sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
sudo sh cuda_8.0.44_linux.run -silent -driver

注:此时安装过程中提示是否要安装NVIDIA驱动时选择no。其他选择yes或默认即可。

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 361.62?
(y)es/(n)o/(q)uit: n

安装完毕后声明环境变量:

sudo gedit ~/.bashrc

在.bashrc尾部添加如下内容:

export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

测试下安装是否成功:
测试1:

cd NVIDIA_CUDA-8.0_Samples/
nvidia-smi

输出:

lab223@lab223:~$ nvidia-smi
Fri Oct  5 08:41:47 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.130                Driver Version: 384.130                   |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla K40c          Off  | 00000000:04:00.0 Off |                    0 |
| 23%   40C    P8    20W / 235W |      1MiB / 11439MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  GeForce GTX TIT...  Off  | 00000000:83:00.0  On |                  N/A |
| 22%   33C    P8    16W / 250W |    149MiB / 12205MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    1      1264      G   /usr/lib/xorg/Xorg                           106MiB |
|    1      2320      G   compiz                                        40MiB |
+-----------------------------------------------------------------------------+

测试2:

cd 1_Utilities/deviceQuery
make
........
./deviceQuery

输出:

lab223@lab223:~/NVIDIA_CUDA-8.0_Samples/1_Utilities/deviceQuery$ make
make: Nothing to be done for 'all'.
lab223@lab223:~/NVIDIA_CUDA-8.0_Samples/1_Utilities/deviceQuery$ ./deviceQuery
./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 2 CUDA Capable device(s)

Device 0: "GeForce GTX TITAN X"
  CUDA Driver Version / Runtime Version          9.0 / 8.0
  CUDA Capability Major/Minor version number:    5.2
  Total amount of global memory:                 12206 MBytes (12798787584 bytes)
  (24) Multiprocessors, (128) CUDA Cores/MP:     3072 CUDA Cores
  GPU Max Clock rate:                            1076 MHz (1.08 GHz)
  Memory Clock rate:                             3505 Mhz
  Memory Bus Width:                              384-bit
  L2 Cache Size:                                 3145728 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
  Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(16384, 16384), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 2 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  Device supports Unified Addressing (UVA):      Yes
  Device PCI Domain ID / Bus ID / location ID:   0 / 131 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

Device 1: "Tesla K40c"
  CUDA Driver Version / Runtime Version          9.0 / 8.0
  CUDA Capability Major/Minor version number:    3.5
  Total amount of global memory:                 11440 MBytes (11995578368 bytes)
  (15) Multiprocessors, (192) CUDA Cores/MP:     2880 CUDA Cores
  GPU Max Clock rate:                            745 MHz (0.75 GHz)
  Memory Clock rate:                             3004 Mhz
  Memory Bus Width:                              384-bit
  L2 Cache Size:                                 1572864 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
  Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(16384, 16384), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 2 copy engine(s)
  Run time limit on kernels:                     No
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Enabled
  Device supports Unified Addressing (UVA):      Yes
  Device PCI Domain ID / Bus ID / location ID:   0 / 4 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
> Peer access from GeForce GTX TITAN X (GPU0) -> Tesla K40c (GPU1) : No
> Peer access from Tesla K40c (GPU1) -> GeForce GTX TITAN X (GPU0) : No

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 9.0, CUDA Runtime Version = 8.0, NumDevs = 2, Device0 = GeForce GTX TITAN X, Device1 = Tesla K40c
Result = PASS
lab223@lab223:~/NVIDIA_CUDA-8.0_Samples/1_Utilities/deviceQuery$

测试3:

cd ../../5_Simulations/nbody/
make
.........
./nbody -benchmark -numbodies=256000 -device=0

输出:

lab223@lab223:~/NVIDIA_CUDA-8.0_Samples/5_Simulations/nbody$ ./nbody -benchmark -numbodies=256000 -device=0
Run "nbody -benchmark [-numbodies=<numBodies>]" to measure performance.
	-fullscreen       (run n-body simulation in fullscreen mode)
	-fp64             (use double precision floating point values for simulation)
	-hostmem          (stores simulation data in host memory)
	-benchmark        (run benchmark to measure performance)
	-numbodies=<N>    (number of bodies (>= 1) to run in simulation)
	-device=<d>       (where d=0,1,2.... for the CUDA device to use)
	-numdevices=<i>   (where i=(number of CUDA devices > 0) to use for simulation)
	-compare          (compares simulation results running once on the default GPU and once on the CPU)
	-cpu              (run n-body simulation on the CPU)
	-tipsy=<file.bin> (load a tipsy model file for simulation)

NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.

> Windowed mode
> Simulation data stored in video memory
> Single precision floating point simulation
> 1 Devices used for simulation
gpuDeviceInit() CUDA Device [0]: "GeForce GTX TITAN X
> Compute 5.2 CUDA device: [GeForce GTX TITAN X]
number of bodies = 256000
256000 bodies, total time for 10 iterations: 3103.036 ms
= 211.200 billion interactions per second
= 4223.992 single-precision GFLOP/s at 20 flops per interaction
lab223@lab223:~/NVIDIA_CUDA-8.0_Samples/5_Simulations/nbody$

  1. 安装OpenCV 3.1.0
    从官网下载zip源代码,解压到根目录下。
    安装依赖:
sudo apt-get -y remove ffmpeg x264 libx264-dev
sudo apt-get -y install libopencv-dev
sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm
sudo apt-get -y install libtiff4-dev libjpeg-dev libjasper-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev
sudo apt-get -y install python-dev python-numpy
sudo apt-get -y install libtbb-dev
sudo apt-get -y install libqt4-dev libgtk2.0-dev
sudo apt-get -y install libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev
sudo apt-get -y install x264 v4l-utils ffmpeg
sudo apt-get -y install libgtk2.0-dev

cd opencv-3.1.0
mkdir build
cd build/
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j4
sudo make install

遇到的错误:Errors

error: ‘NppiGraphcutState’ has not been declared
error: ‘NppiGraphcutState’ does not name a type
...

解决方法:(由于CUDA版本高于8.0,所以需要做如下修改。在源文件中找到“graphcuts.cpp”),此源文件自行百度在Linux中的位置。
将:

#include "precomp.hpp"
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)

改为:

#include "precomp.hpp"
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)  || (CUDART_VERSION >= 8000)

because graphcuts is not supported directly with CUDA8 anymore.
安装成功后配置环境:

sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

测试OpenCV安装是否成功:

mkdir DisplayImage
cd DisplayImage
gedit DisplayImage.cpp

添加代码:

#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;

int main(int argc, char** argv)
{
     if(argc!= 2)
     {
               printf("usage:DisplayImage.out <Image_Path>\n");
               return -1;
     }

     Mat image;
     image= imread(argv[1], 1);

    if(!image.data)
    {
               printf("Noimage data\n");
               return -1;
     }

     namedWindow("DisplayImage",CV_WINDOW_AUTOSIZE);
     imshow("DisplayImage",image);

     waitKey(0);
     return 0;
}

创建CMake文件:
gedit CMakeLists.txt
添加内容:

cmake_minimum_required(VERSION 2.8)
project(DisplayImage)
find_package(OpenCV REQUIRED)
add_executable(DisplayImage DisplayImage.cpp)
target_link_libraries(DisplayImage ${OpenCV_LIBS})

编译:

cmake .
make

执行:

./DisplayImage lena.jpg

结果:

lab223@lab223:~/opencv-3.1.0/build/DisplayImage$ ./DisplayImage lean.jpg
Noimage data

  1. 安装cudnn 5.1
    从官网下载cudnn-8.0-linux-x64-v5.1.tgz for CUDA 8.0. 解压到当前目录:(账户名:WG Wang
    有可能下载的文件不是tgz文件,可以自行改文件后缀然后解压)
tar -zxvf cudnn-8.0-linux-x64-v5.1.tgz

解压后的文件如下:

cuda/include/cudnn.h
cuda/lib64/libcudnn.so
cuda/lib64/libcudnn.so.5
cuda/lib64/libcudnn.so.5.1.5
cuda/lib64/libcudnn_static.a

然后执行:

sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
  1. 安装MATLAB 2014a
    需要注意的是Ubuntu16.04 LTS的gcc版本为5.4,而Matlab2014a支持的是gcc4.7。(此条没怎么注意)

用Crack文件中的install替换matlab2014安装目录下/java/jar/下的install文件,然后执行install程序
(在超级用户权限下执行此程序)

cd matlab
sudo ./install

注意:选择“不联网安装”;当出现密钥时,随意输入20个数字12345-67890-12345-67890即可;需要激活时选择不要联网激活,用Crack目录下的“license_405329_R2014a.lic”文件激活。
安装完成之后,将Crack/Linux目录下的libmwservices.so文件拷贝到/usr/local/MATLAB/R2014a/bin/glnxa64。

cd ..
cd Crack/Linux/
sudo cp libmwservices.so /usr/local/MATLAB/R2014a/bin/glnxa64

打开Matlab并激活:

cd /usr/local/MATLAB/R2014a/bin
sudo ./matlab # sudo不可缺少,否则选择激活文件后报错

  1. Python的安装

选用Ubuntu16.04默认的安装和配置,python版本2.7.12.
很多版本都推荐使用anaconda,因为anaconda里面自带很多库文件,但此文档并没有使用此方法,仅用于caffe的安装运行,此后安装anaconda遇到问题再说。

  1. BLAS安装与配置
    BLAS(基础线性代数集合)是一个应用程序接口的标准。caffe官网上推荐了三种实现:ATLAS, MKL, OpenBLAS。其中ATLAS可以直接通过命令行安装。MKL是微软开发的商业工具包,面向科研和学生免费开放。申请学生版的Parallel Studio BLAS(基础线性代数集合)是一个应用程序接口的标准。caffe官网上推荐了三种实现:ATLAS, MKL, OpenBLAS。其中ATLAS可以直接通过命令行安装。MKL是微软开发的商业工具包,面向科研和学生免费开放。申请学生版的Parallel Studio BLAS(基础线性代数集合)是一个应用程序接口的标准。caffe官网上推荐了三种实现:ATLAS, MKL, OpenBLAS。其中ATLAS可以直接通过命令行安装。MKL是微软开发的商业工具包,面向科研和学生免费开放。申请学生版的Parallel Studio BLAS(基础线性代数集合)是一个应用程序接口的标准。caffe官网上推荐了三种实现:ATLAS, MKL, OpenBLAS。其中ATLAS可以直接通过命令行安装。MKL是微软开发的商业工具包,面向科研和学生免费开放。申请学生版的Parallel Studio
    XE Cluster Edition,下载parallel_studio_xe_2017.tgz。注意接收邮件中的序列号(S4ZD-T4P7R6VF)。
tar zxvf parallel_studio_xe_2017.tgz   #解压下载文件
 chmod 777 parallel_studio_xe_2017 -R   #获取文件权限
cd parallel_studio_xe_2017/
 sudo ./install_GUI.sh

安装完成之后,进行相关文件的链接:

sudo gedit /etc/ld.so.conf.d/intel_mkl.conf

添加库文件:

/opt/intel/lib/intel64
/opt/intel/mkl/lib/intel64

编译链接使lib文件生效:

sudo ldconfig

如果选择安装ATLAS,在终端输入sudo apt-get install libatlas-base-dev即可。
(笔者使用的是MKL安装,安装极慢,有兴趣的话可以试一下使用ATLAS安装)

  1. Caffe的安装与配置
    Caffe是由BVLC开发的一个深度学习框架,主要由贾扬清在UC Berkeley攻读PhD期间完成。参考官网上的教程以及Github上针对Ubuntu15.04和16.04的教程。从官方下载caffe源包caffe-master。
    安装库文件:
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler

安装依赖:

sudo apt-get install -y build-essential cmake git pkg-config
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler   libatlas-base-dev libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install --no-install-recommends libboost-all-dev

Python接口依赖:

sudo apt-get install  the python-dev
sudo apt-get install -y python-pip
sudo apt-get install -y python-dev
sudo apt-get install -y python-numpy python-scipy # (Python 2.7 development files)
sudo apt-get install -y python3-dev
sudo apt-get install -y python3-numpy python3-scipy # (Python 3.5 development files)

在python文件夹内,使用root执行依赖项的检查与安装:

sudo su
cd caffe-master/python
for req in $(cat requirements.txt); do pip install $req; done

Makefile.config:

cd ~/caffe-master
cp Makefile.config.example Makefile.config

配置如下:

## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!

# cuDNN acceleration switch (uncomment to build with cuDNN).
 USE_CUDNN := 1

# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1

# uncomment to disable IO dependencies and corresponding data layers
  USE_OPENCV := 1
# USE_LEVELDB := 0
# USE_LMDB := 0

# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
#    You should not set this flag if you will be reading LMDBs with any
#    possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1

# Uncomment if you're using OpenCV 3
 OPENCV_VERSION := 3

# 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++

# CUDA directory contains bin/ and lib/ directories that we need.
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.
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 := mkl
# 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.
# MATLAB directory should contain the mex binary in /bin.
  MATLAB_DIR := /usr/local/MATLAB/R2014a
# 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 \
        /usr/local/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)/anaconda
# 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
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial /usr/local/share/OpenCV/3rdparty/lib/

# 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

# N.B. both build and distribute dirs are cleared on `make clean`
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

# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0

# enable pretty build (comment to see full commands)
Q ?= @

在Makefile中配置:

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5 opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

hdf5的配置:官方说这对于Ubuntu 16.04是必须的。libhdf5的版本号需要根据实际来修改下。(可使用locate libhdf5 来查看对应版本号)

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
sudo ln -s libhdf5_serial.so.10.1.0 libhdf5.so
sudo ln -s libhdf5_serial_hl.so.10.0.2 libhdf5_hl.so

编译:

cd ~/caffe-master
make clean
make all -j4
make test -j4
make runtest -j4
make pycaffe -j4
make matcaffe -j4

出现如下结果:

编译接口matcaffe时,有如下警告:

Warning: You are using gcc version '5.4.0'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
Warning: You are using gcc version '5.4.0-6ubuntu1~16.04.2)'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
MEX completed successfully.

若OpenCV安装不正确则会在caffe编译过程中遇到如下错误:

/usr/bin/ld: cannot find -lopencv_imgcodecs
collect2: error: ld returned 1 exit status
Makefile:566: recipe for target '.build_release/lib/libcaffe.so.1.0.0-rc3' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0-rc3] Error 1

MNIST测试:

sh data/mnist/get_mnist.sh  #数据预处理
sh examples/mnist/create_mnist.sh #重建lmdb文件。Caffe支持多种数据格式: Image(.jpg, .png等),leveldb,lmdb,HDF5. 生成mnist-train-lmdb 和 mnist-train-lmdb文件夹,这里包含了lmdb格式的数据集
sh examples/mnist/train_lenet.sh #训练mnist

输出:

I1019 21:48:30.078994 20063 caffe.cpp:217] Using GPUs 0
I1019 21:48:30.092034 20063 caffe.cpp:222] GPU 0: GeForce GTX TITAN X
...
....
.....
I1019 21:48:49.415398 20063 solver.cpp:317] Iteration 10000, loss = 0.00242468
I1019 21:48:49.415410 20063 solver.cpp:337] Iteration 10000, Testing net (#0)
I1019 21:48:49.479605 20063 solver.cpp:404] Test net output #0: accuracy = 0.9914
I1019 21:48:49.479625 20063 solver.cpp:404] Test net output #1: loss = 0.0284448 (* 1 = 0.0284448 loss)
I1019 21:48:49.479629 20063 solver.cpp:322] Optimization Done.
I1019 21:48:49.479632 20063 caffe.cpp:254] Optimization Done.

(这一步的 make matcaffe 我没有做,运行失败,但前几步均运行成功,暂时发现其实没有matlab这个接口其实运行example model 也是可以的,包括下面的caffe下matlab接口demo测试其实也并没有做)
11. Caffe下Matlab接口Demo测试
在使用Matlab运行caffe库时,即运行文件”caffe-master/matlab/demo/classification_demo.m”。遇到的错误信息如下:

Invalid MEX-file 'caffe-master/matlab/+caffe/private/caffe_.mexa64': libcudart.so.8.0: cannot open shared object file: No such file or directory

错误原因是由于Matlab找不到caffe.mexa64所依赖的所有库文件的路径,此时可以使用ldd命令来查看caffe.mexa64内库文件的地址:
//1. 在Ubuntu系统的命令终端

ldd *caffe_.mexa64

结果输出的是库文件对应的地址,与下文相对的缺失的库文件的地址可在此找到:

libcudart.so.8.0 => /usr/local/cuda-8.0/lib64/libcudart.so.8.0
libcublas.so.8.0 => /usr/local/cuda-8.0/lib64/libcublas.so.8.0
libcurand.so.8.0 => /usr/local/cuda-8.0/lib64/libcurand.so.8.0
libcudnn.so.5 => /usr/local/cuda-8.0/lib64/libcudnn.so.5

//2. 在Matlab命令窗口输入

!ldd *caffe_.mexa64

结果在Matlab窗口的输出信息中发现:

libcudart.so.8.0 => not found
libcublas.so.8.0 => not found
libcurand.so.8.0 => not found
libcudnn.so.5 => not found

解决方法:通过如下命令将默认路径链接到真实路径下:

sudo ln -s /usr/local/cuda-8.0/lib64/libcudart.so.8.0 /usr/local/MATLAB/R2014a/sys/os/glnxa64/libcudart.so.8.0
sudo ln -s /usr/local/cuda-8.0/lib64/libcublas.so.8.0 /usr/local/MATLAB/R2014a/sys/os/glnxa64/libcublas.so.8.0
sudo ln -s /usr/local/cuda-8.0/lib64/libcurand.so.8.0 /usr/local/MATLAB/R2014a/sys/os/glnxa64/libcurand.so.8.0
sudo ln -s /usr/local/cuda-8.0/lib64/libcudnn.so.5 /usr/local/MATLAB/R2014a/sys/os/glnxa64/libcudnn.so.5

重新启动Matlab使之生效。
另外,运行此例需要下载CaffeNet模型(Please download CaffeNet from Model Zoo before you run this demo.)https://github.com/BVLC/caffe/wiki/Model-Zoo

10-05 10:56