ubuntu安装caffe+cuda+cudnn+nvidia驱动

1、nvidia驱动安装

先卸载原有N卡驱动
sudo apt-get remove --purge nvidia*
sudo apt-get --purge remove xserver-xorg-video-nouveau
若已经安装nvidia驱动,需则要卸载
sudo ./NVIDIA-Linux-x86_64-384.59.run --uninstall

下载官方驱动程序  
http://www.geforce.cn/drivers(找到自己显卡所需要的版本)

禁止集成的nouveau驱动  
修改属性  sudo chmod 666  /etc/modprobe.d/blacklist.conf
sudo vim /etc/modprobe.d/blacklist.conf
在该文件后添加一下几行:
blacklist vga16fb
blacklist nouveau
blacklist rivafb
blacklist rivatv
blacklist nvidiafb

开始安装
关闭当前图形环境 Ctrl + Alt + F1
sudo service lightdm stop
给驱动run文件赋予执行权限:
sudo chmod +x NVIDIA-Linux-x86_64-384.59.run(对应自己所下载的驱动版本)
后面的参数非常重要,不可省略:
sudo ./NVIDIA-Linux-x86_64-384.59.run –no-opengl-files
最后重新启动图形环境 Ctrl + Alt + F7

查看显卡驱动版本  cat /proc/driver/nvidia/version
若列出GPU的信息列表,表示驱动安装成功  nvidia-smi
若弹出设置对话框,亦表示驱动安装成功  nvidia-settings

2、CUDA安装

下载官方cuda
https://developer.nvidia.com/cuda-downloads (如cuda_8.0.61_375.26_linux.run)

sudo sh cuda_8.0.44_linux.run --override
启动安装程序,键入q可跳过条款目录,输入accept接受条款
输入n不再安装nvidia驱动,之前已经安装过了   (除了这里输入n,其他都是y确认通过)
输入y安装cuda 8.0工具
回车确认cuda默认安装路径:/usr/local/cuda-8.0
输入y用sudo权限运行安装,输入密码
输入y或者n安装或者不安装指向/usr/local/cuda的符号链接
输入y安装CUDA 8.0 Samples,以便后面测试
回车确认CUDA 8.0 Samples默认安装路径,该安装路径测试完可以删除

添加环境变量     sudo vim ~/.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}}

然后设置环境变量和动态链接库    sudo vim /etc/profile
export PATH=/usr/local/cuda/bin:$PATH
使生效    sudo ldconfig

编译并测试设备 deviceQuery:
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery

3、cudnn安装

下载官方相应的cudnn包,并解压
https://developer.nvidia.com/cudnn

cd cuda/include/
sudo cp cudnn.h /usr/local/cuda/include/    #复制头文件
sudo cp ../lib64/lib* /usr/local/cuda/lib64/    #复制动态链接库
cd /usr/local/cuda/lib64/
sudo rm -rf libcudnn.so libcudnn.so.7    #删除原有动态文件
sudo ln -s libcudnn.so.7.0.5 libcudnn.so.7  #生成软衔接
sudo ln -s libcudnn.so.7 libcudnn.so      #生成软链接

4、opencv安装

见opencv3.2的安装教程

https://blog.csdn.net/u012114438/article/details/83578933

5、caffe安装

安装依赖库
sudo apt install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler

sudo apt install -y --no-install-recommends libboost-all-dev libgflags-dev libgoogle-glog-dev liblmdb-dev python-numpy

sudo apt install -y python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb

sudo apt install -y python-networkx python-nose python-pandas python-gflags cython ipython libatlas-base-dev

更新  sudo apt-get update

下载官方caffe
https://github.com/BVLC/caffe.git
进入 caffe 将 Makefile.config.example 文件复制一份并更名为 Makefile.config
sudo cp Makefile.config.example Makefile.config
打开文件  sudo vim Makefile.config
取消以下行注释
USE_CUDNN := 1
OPENCV_VERSION := 3 (如果使用opencv3)
WITH_PYTHON_LAYER := 1

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib  /usr/lib
修改为:
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

编译 make all -j8
测试 sudo make runtest -j8

若要使用caffe的python接口的话,执行 make pycaffe

将编译好的caffe库进行打包发布到一个文件夹 make distribute

错误

查看caffe的Makefile.config,使用的是python3.5的版本,但是本机安装了anaconda3,编译时可能报错

//home/oyrq/anaconda3/lib/libpng16.so.16: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:630: recipe for target '.build_release/examples/cifar10/convert_cifar_data.bin' failed
make: *** [.build_release/examples/cifar10/convert_cifar_data.bin] Error 1
make: *** Waiting for unfinished jobs....
//home/oyrq/anaconda3/lib/libpng16.so.16: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:625: recipe for target '.build_release/tools/upgrade_net_proto_text.bin' failed
make: *** [.build_release/tools/upgrade_net_proto_text.bin] Error 1
//home/oyrq/anaconda3/lib/libpng16.so.16: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:625: recipe for target '.build_release/tools/extract_features.bin' failed
make: *** [.build_release/tools/extract_features.bin] Error 1
//home/oyrq/anaconda3/lib/libpng16.so.16: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:625: recipe for target '.build_release/tools/upgrade_solver_proto_text.bin' failed
make: *** [.build_release/tools/upgrade_solver_proto_text.bin] Error 1
//home/oyrq/anaconda3/lib/libpng16.so.16: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:625: recipe for target '.build_release/tools/upgrade_net_proto_binary.bin' failed
make: *** [.build_release/tools/upgrade_net_proto_binary.bin] Error 1
//home/oyrq/anaconda3/lib/libpng16.so.16: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:625: recipe for target '.build_release/tools/caffe.bin' failed
make: *** [.build_release/tools/caffe.bin] Error 1
//home/oyrq/anaconda3/lib/libpng16.so.16: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:625: recipe for target '.build_release/tools/convert_imageset.bin' failed
make: *** [.build_release/tools/convert_imageset.bin] Error 1
//home/oyrq/anaconda3/lib/libpng16.so.16: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status

错误貌似是链接不到zlib库的问题,在Makefile.config中添加下面一句话,再编译,则成功了,可能anaconda3中有需要的正确库吧

LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib

在编译好caffe之后,使用pycaffe时,出现Segmentation fault (core dumped)

>>> import caffe
Segmentation fault (core dumped)

 这个问题就有点难找了,研究了许久,猜测是本机自带的python3.5和anaconda3中的python冲突了,于是决定把刚才加的那句和anaconda3有关的那句话删除掉,既然是链接不到zlib库,就重新安装一个zlib库。

先删除本机原有不可用的zlib库

sudo apt autoremove zlib1g-dev

再执行下面的命令

git clone https://github.com/madler/zlib
cd path/to/zlib
./configure
make
sudo make install

ok,在编译caffe,执行import caffe,成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值