ubuntu 16.04 上安装支持GPU的caffe

来源:互联网 发布:李涛疯狂淘宝怎么样 编辑:程序博客网 时间:2024/06/09 20:04

这两天在ubuntu 16.04上安装caffe-gpu碰了很多壁,老是出现很多莫名其妙的问题,搞得我焦头烂额;直到我找到一个博客教程,很细节,特此梳理一下。

该教程中的caffe来自github的资源(记得检查Makefile是否和caffe版本符合)。

硬件和系统需求

硬件只要能支持cuda8.0和cudnn v6.0以上的就行。系统Ubuntu 16.04。

编译器和Python环境

确保系统有gcc-5 和g++ -5

$ echo $PATH  # Should not contain anaconda$ gcc --version  # 5.4.0$ g++ --version  # 5.4.0$ which python   # /usr/bin/python

Nvidia 驱动、cuda-8.0和cudnn安装

因为本文主要是caffe安装,cuda安装参考官方文档和https://ywpkwon.github.io/post/ubuntu-setting/


caffe安装

安装依赖

这里是基于Python2.7安装的,所有依赖也是基于Python2.7:

sudo apt-get updatesudo apt-get upgradesudo apt-get install build-essential cmake git pkg-configsudo apt-get install libleveldb-dev libsnappy-dev libhdf5-serial-devsudo apt-get install libatlas-base-devsudo apt-get install --no-install-recommends libboost-all-devsudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-devsudo apt-get install python-dev  # Python 2.7sudo apt-get install python-numpy python-scipy python-pipsudo apt-get install libopencv-dev

protobuf-3.2.0的建立是来自资源。 下载完毕之后解压到本地并用cd命令进入解压后的文件夹。

$ sudo apt-get install autoconf automake libtool curl make unzip$ ./autogen.sh$ ./configure --prefix=/usr/local/ CC=/usr/bin/gcc$ make$ make check$ sudo make install$ sudo ldconfig  # refresh shared library cache
注意:使用make指令时可以在后面增加 -j8 来用8个cpu内核同时编译,
$ make -j8
检查protoc安装结果:

$  protoc --version

下载caffe资源并安装

$ sudo apt-get install git  # Run this if git is not installed$ sudo apt-get install vim  # Run this if vim is not installed (optional)$ git clone https://github.com/BVLC/caffe

配置 Makefile.config文件

$ cd caffe$ cp Makefile.config.example Makefile.config$ vim Makefile.config  # Replace vim by any text editor
我这里是安装caffe支持GPU的,所以配置文件里USE_CUDNN := 1这行要去掉注释,配置完后的Makefile.config文件大体如下:

USE_CUDNN := 1CUDA_DIR := /usr/local/cudaCUDA_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_52,code=sm_52 \        -gencode arch=compute_60,code=sm_60 \        -gencode arch=compute_61,code=sm_61 \        -gencode arch=compute_61,code=compute_61BLAS := atlasPYTHON_INCLUDE := /usr/include/python2.7 \                /usr/lib/python2.7/dist-packages/numpy/core/include \                /usr/local/lib/python2.7/dist-packages/numpy/core/includePYTHON_LIB := /usr/libWITH_PYTHON_LAYER := 1INCLUDE_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/BUILD_DIR := buildDISTRIBUTE_DIR := distributeTEST_GPUID := 0Q ?= @

注意:你安装的cuda版本可能不支持架构compute_20或者其他架构,只要将CUDA_ARCH := ....这里包含compute_20的那一行删除就行。

为hdf5建立符号链接:

$ cd /usr/lib/x86_64-linux-gnu$ sudo ln -s libhdf5_serial.so.8.0.2 libhdf5.so$ sudo ln -s libhdf5_serial_hl.so.8.0.2 libhdf5_hl.so

这一步很重要,如果这不没有,会报/usr/bin/ld: -lhdf5找不到。

编译

$ make all -j8$ make test$ make runtest$ make distribute  # To use with python
编译make all -j8时可能还会报找不到opencv的错,比如:

.build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'.build_release/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'.build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)'collect2: error: ld returned 1 exit statusMakefile:617: recipe for target '.build_release/test/test_convolution_layer.testbin' failedmake: *** [.build_release/test/test_convolution_layer.testbin] Error 1
这里只要修改Makefile文件(不是Makefile.config文件),增加一行就行:

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

最后当执行到make test时仍然可能会报上面这个错误,直接忽视,直接执行make runtest这一条命令,如果全部通过就行大功告成了。

本文参考教程https://gear.github.io/2017-03-30-caffe-gpu-installation/

原创粉丝点击