ubuntu16.04 cuda8.0 cudnn6.0 opencv3.2编译安装caffe

1,安装NVIDIA 显卡

根据自己的显卡驱动 选择对应的版本(我的NVIDIA-Linux-x86_64-384.69.run)

官网下载地址:www.nvidia.cn/Download/index.aspx

安装步骤:

cd ~/Downloads

chmod +x  NVIDIA-Linux-x86_64-384.69.run

sudo service lightdm stop

ctrl+alt+f1进入字符界面

sudo ./NVIDIA-Linux-x86_64-384.69.run -no-opengl-files -no-x-check -no-nouveau-check

安装好之后 sudo rm /etc/X11/xorg.conf   ~/.Xauthority (防止linux登陆界面无限循环或者分辨率不对)

sudo service lightdm start

回到桌面

输入nvidia-smi 输出如下:则安装成功

ubuntu16.04 cuda8.0 cudnn6.0 opencv3.2编译安装caffe_第1张图片

2.安装CUDA8.0

cuda8.0下载地址

ubuntu16.04 cuda8.0 cudnn6.0 opencv3.2编译安装caffe_第2张图片

1.下载完之后:sudo sh cuda_8.0.27_linux.run

sudo gedit /etc/profile

打开“profile”文件,在末尾处添加(注意不要有空格,不然会报错):

export PATH=/usr/local/cuda-8.0/bin:$PATH

export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH

2.重启电脑:

sudo reboot

3,测试

cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery

sudo make

./deviceQuery

输出是GPU的信息说明安装成功

3,安装cudnn6.0

cudnn下载

下载完成之后解压到指定目录

tar -zxvf cudnn-8.0-linux-x64-v6.0.tgz -C ../cudnn/

把下载好的cudnn.h文件和lib文件分别拷贝到cuda对应的路径下面。

sudo cp ~/cudnn/cuda/include/cudnn.h  /usr/local/cuda/include

sudo cp ~/cudnn/cuda/lib64/lib*   /usr/local/cuda/lib64/

做个链接 不然后面caffe make test 会报链接共享库错误的问题

ln -sf ~/cudnn/cuda/lib64/libcudnn.so.6 /usr/local/cuda/lib64/

4,opencv(包含contrib模块)编译安装 (直接建一个shell文件运行即可完成自动编译安装)

cd$HOME

sudo apt-get install -y \

libglew-dev \

libtiff5-dev \

zlib1g-dev \

libjpeg-dev \

libpng12-dev \

libjasper-dev \

libavcodec-dev \

libavformat-dev \

libavutil-dev \

libpostproc-dev \

libswscale-dev \

libeigen3-dev \

libtbb-dev \

libgtk2.0-dev \

cmake \

pkg-config

#Python 2.7

sudo apt-get install -y python-dev python-numpy python-py python-pytest -y

#GStreamer support

sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

git clone https://github.com/opencv/opencv.git

cd opencv

git checkout -b v3.2.0 3.2.0

#This is for the test data

cd$HOME

git clone https://github.com/opencv/opencv_extra.git

cd opencv_extra

git checkout -b v3.2.0 3.2.0

cd$HOME/opencv

mkdir build

cd build

cmake \

-DCMAKE_BUILD_TYPE=Release \

-DCMAKE_INSTALL_PREFIX=/usr \

-DBUILD_PNG=OFF \

-DBUILD_TIFF=OFF \

-DBUILD_TBB=OFF \

-DBUILD_JPEG=OFF \

-DBUILD_JASPER=OFF \

-DBUILD_ZLIB=OFF \

-DBUILD_EXAMPLES=ON \

-DBUILD_opencv_java=OFF \

-DBUILD_opencv_python2=ON \

-DBUILD_opencv_python3=OFF \

-DENABLE_PRECOMPILED_HEADERS=OFF \

-DWITH_OPENCL=OFF \

-DWITH_OPENMP=OFF \

-DWITH_FFMPEG=ON \

-DWITH_GSTREAMER=ON \

-DWITH_GSTREAMER_0_10=OFF \

-DWITH_CUDA=ON \

-DWITH_GTK=ON \

-DWITH_VTK=OFF \

-DWITH_TBB=ON \

-DWITH_1394=OFF \

-DWITH_OPENEXR=OFF \

-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \

-DCUDA_ARCH_BIN=6.2 \

-DCUDA_ARCH_PTX=""\

-DINSTALL_C_EXAMPLES=ON \

-DINSTALL_TESTS=ON \

-DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \

../

#Consider using all 6 cores; $ sudo nvpmodel -m 2 or $ sudo nvpmodel -m 0

make -j4

编译可能遇到的问题:cudalegacy/src/graphcuts.cpp:120:54: error: ‘NppiGraphcutState’ has not been declared:

解决方案:需要修改一处源码:

在graphcuts.cpp中将

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)

改为

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)

5,编译安装caffe

(1)将终端cd到要安装caffe的位置。

(2)从github上获取caffe:

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

(3)因为make指令只能make

Makefile.config文件,而Makefile.config.example是caffe给出的makefile例子,因此,首先将Makefile.config.example的内容复制到Makefile.config:

sudo  cp  Makefile.config.example  Makefile.config

(4)打开并修改配置文件:

sudo gedit Makefile.config #打开Makefile.config文件

根据个人情况修改文件:

a.若使用cudnn,则

将#USE_CUDNN := 1修改成: USE_CUDNN :=1

b.若使用的opencv版本是3的,则

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

c.若要使用python来编写layer,则

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

d.重要的一项:

将# Whatever else you find you need goes here.下面的

INCLUDE_DIRS:=$(PYTHON_INCLUDE) /usr/local/includeLIBRARY_DIRS:=$(PYTHON_LIB) /usr/local/lib /usr/lib 修改为: INCLUDE_DIRS:=$(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serialLIBRARY_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的位置,所以需要更改这一路径.

(5)修改makefile文件

打开makefile文件,做如下修改:

将:NVCCFLAGS+= -ccbin=$(CXX) -Xcompiler -fPIC$(COMMON_FLAGS)替换为:NVCCFLAGS+=-D_FORCE_INLINES      -ccbin=$(CXX)  -Xcompiler  -fPIC$(COMMON_FLAGS)

(6)编辑/usr/local/cuda/include/host_config.h

将其中的第115行注释掉:

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

(7)编译

make  all  -j8  #-j根据自己电脑配置决定

你可能感兴趣的:(ubuntu16.04 cuda8.0 cudnn6.0 opencv3.2编译安装caffe)