环境:Ubuntu18.04LTS + cuda10.0(1080Ti) + cuDNN7.6.5 + OpenCV3.4.11

本文不涉及Python和MATLAB。

官方教程:http://caffe.berkeleyvision.org/installation.html

依赖:

sudo apt install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt install --no-install-recommends libboost-all-dev
sudo apt install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt install libopenblas-dev 		(openblas和atlas的依赖库不同)

CUDA

下载:https://developer.nvidia.com/cuda-10.0-download-archive?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1804&target_type=deblocal

安装:https://docs.nvidia.com/cuda/archive/10.0/cuda-installation-guide-linux/index.html

验证:

# 查看cuda版本
nvcc -V
# 样例
cp -r /usr/local/cuda-10.0/samples~/
make -j
# 按照https://docs.nvidia.com/cuda/archive/10.0/cuda-installation-guide-linux/index.html#compiling-examples中的说明运行测试程序。

cuDNN

下载:https://developer.nvidia.com/rdp/cudnn-download

安装:https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#install-linux

验证:

# 查看版本
cat /usr/include/x86_64-linux-gnu/cudnn_v*.h | grep CUDNN_MAJOR -A 2

OpenBLAS

源代码下载:https://github.com/xianyi/OpenBLAS

make
sudo make install

添加搜索路径:

export LD_LIBRARY_PATH=/opt/OpenBLAS/lib:$LD_LIBRARY_PATH

则在/opt/OpenBLAS可见。
例程:

//test_cblas_dgemm.c
#include <cblas.h>
#include <stdio.h>

void main()
{
  int i=0;
  double A[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};         
  double B[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};  
  double C[9] = {.5,.5,.5,.5,.5,.5,.5,.5,.5}; 
  cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans,3,3,2,1,A, 3, B, 3,2,C,3);

  for(i=0; i<9; i++)
  printf("%lf ", C[i]);
  printf("\n");
}

编译:

gcc -o test_cblas_open test_cblas_dgemm.c -I /opt/OpenBLAS/include/ -L/opt/OpenBLAS/lib -lopenblas -lpthread -lgfortran

如果没有安装gfortran,还要单独安装:

sudo apt install gfortran

执行:

./test_cblas_open 
11.000000 -9.000000 5.000000 -9.000000 21.000000 -1.000000 5.000000 -1.000000 3.000000 

Boost

下载解压源代码并转到文件夹下:https://www.boost.org/users/history/version_1_67_0.html

./bootstrap.sh
sudo ./b2 install

添加搜索路径

export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}

例程:

//example.cpp
#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
    std::string line;
    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

    while (std::cin)
    {
        std::getline(std::cin, line);
        boost::smatch matches;
        if (boost::regex_match(line, matches, pat))
            std::cout << matches[2] << std::endl;
    }
}

jayne.txt :

To: George Shmidlap
From: Rita Marlowe
Subject: Will Success Spoil Rock Hunter?
---
See subject.
# 运行
c++ -I /usr/local example.cpp -o example /usr/local/lib/libboost_regex.a
./example < ./jayne.txt 
Will Success Spoil Rock Hunter?

OpenCV-3.4.11

安装一堆依赖:

sudo apt install cmake build-essential
sudo apt install git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev ccache

E: 无法定位软件包 libjasper-dev:

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

下载:https://github.com/opencv/opencv/archive/3.4.11.zip

安装:

解压并切到源代码文件夹下,新建临时文件夹:

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
# 看看缺什么补什么,如openjpeg、手动下载ippicv、vtk等
# 这篇比较详细 https://www.jianshu.com/p/259a6140da9d
make -j
sudo make install
# 卸载: sudo make unsinstall

切换到前面的build文件夹下运行测试文件:

./bin/opencv_test_core

*如果测试结果有一个或多个未通过:

[----------] Global test environment tear-down
[==========] 10522 tests from 208 test cases ran. (478989 ms total)
[  PASSED  ] 10521 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] Core_globbing.accuracy

不要紧的 http://answers.opencv.org/question/23356/opencv-300-dev-core_globbing-failure/

Caffe

http://caffe.berkeleyvision.org/installation.html#compilation

由于常见的编辑器中缺少针对配置文件类型的语法高亮,所以修改配置文件的时候一定要仔细。

cp Makefile.config.example Makefile.config
# Adjust Makefile.config (for example, if using Anaconda Python, or if cuDNN is desired)
make all -j
make test -j
make runtest -j

然后即可运行MNIST和CIFAR10测试样例:
https://caffe.berkeleyvision.org/gathered/examples/mnist.html

https://caffe.berkeleyvision.org/gathered/examples/cifar10.html

*问题及解决:

  1. 编译过程中会有报错找不到hdf5.h,处理方案见:https://blog.csdn.net/xue_wenyuan/article/details/52037121
    链接过程找不到-lhdf5的处理方案见:https://github.com/BVLC/caffe/issues/4333。
  2. 如果用OpenCV4.x会报一些错误,如对函数imread()未定义的引用等,目前未找到有效解决办法。
  3. 编译完成后如果不能成功运行mnist例程,即找不到$CAFFE_ROOT/build/examples/mnist/convert_mnist_data.bin,make clean后重新编译即可解决。