ubuntu18.04安装CUDA+cuDNN+YOLOv3

ubuntu18.04安装CUDA+cuDNN+YOLOv3

  • 显卡驱动
  • CUDA
  • cuDNN
  • YOLO

显卡驱动

先看下电脑驱动情况:ubuntu-drivers devices

$ubuntu-drivers devices

安装驱动:sudo ubuntu-drivers autoinstall
查看驱动版本:nvidia-smi

$nvidia-smi
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 390.116                Driver Version: 390.116                   |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 860M    Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   48C    P8    N/A /  N/A |    320MiB /  2004MiB |      7%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1308      G   /usr/lib/xorg/Xorg                           162MiB |
|    0      1484      G   /usr/bin/gnome-shell                          97MiB |
|    0      1883      G   ...quest-channel-token=7978872242666023036    58MiB |
+-----------------------------------------------------------------------------+

查看Nouveau是否禁用:lsmod | grep nouveau

CUDA

查看显卡CUDA支持情况:
https://developer.nvidia.com/cuda-gpus
根据驱动版本选择CUDA版本:
https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html
我的可用9.0,下面是不同版本的下载链接
https://developer.nvidia.com/cuda-toolkit-archive
CUDA9.0下载:
https://developer.nvidia.com/cuda-90-download-archive?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1704&target_type=runfilelocal
CUDA需要GCC和G++6.0及以下版本
GCC降级
手动降级:

sudo apt-get install gcc-5
sudo apt-get install g++-5
cd /usr/bin
ls gcc* g++*
sudo mv gcc gcc.bak
sudo ln -s gcc-5 gcc
sudo mv g++ g++.bak
sudo ln -s g++5 g++
gcc -v 
g++ -v

CUDA安装:
chmod +x ./cuda.run
./cuda.run
等待出现文本一直按空格到99%,输入accept,选择y,驱动n ,路径默认,其他y

vi  ~/.bash

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

source ~/.bashrc 

nvcc -V 查看安装是否成功

$nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_21:08:03_CDT_2017
Cuda compilation tools, release 9.0, V9.0.176

cuda9.0还有四个补丁,同样安装。

卸载命令:

sudo /usr/local/cuda-9.0/bin/uninstall_cuda_9.0.pl 

cuDNN

下载地址
https://developer.nvidia.com/rdp/cudnn-download
需要注册,下载for linux版本,最好能,不然有可能下载很慢
tar -xzvf 解压
复制到CUDA

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   /usr/local/cuda/lib64/libcudnn*

验证:

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

出现如下结果表示成功:

#define CUDNN_MAJOR 7
#define CUDNN_MINOR 6
#define CUDNN_PATCHLEVEL 0
--
#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

#include "driver_types.h"

卸载命令:
删除复制的文件即可

sudo rm -rf /usr/local/cuda-9.0/lib64/libcudnn*
sudo rm -rf /usr/local/cuda-9.0/include/cudnn.h

YOLO

git clone https://github.com/pjreddie/darknet
cd darknet
make
wget https://pjreddie.com/media/files/yolov3.weights

使用GPU,需要修改Makefile文件,重点是如下几项

GPU=1
CUDNN=1
OPENCV=0
OPENMP=0
DEBUG=0
#ARCH= -gencode arch=compute_30,code=sm_30 \
#      -gencode arch=compute_35,code=sm_35 \
#      -gencode arch=compute_50,code=[sm_50,compute_50] \
#      -gencode arch=compute_52,code=[sm_52,compute_52]
#      -gencode arch=compute_20,code=[sm_20,sm_21] \ This one is deprecated?
# 根据自己显卡算力设置
ARCH= -gencode arch=compute_50,code=[sm_50,compute_50]
#根据自己安装地址设置
NVCC=/usr/local/cuda/bin/nvcc 

最后测试检测效果:

 ./darknet detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights data/test.jpg
 watch nvidia-smi #查看显卡消耗情况

ubuntu18.04安装CUDA+cuDNN+YOLOv3_第1张图片
效果还可以,可惜没识别出“球”来,看来YOLOv对小物件的识别还有待提高。

如果出错,注意以下几点排查

1、CUDA版本和显卡驱动匹配关系
2、显卡和算力配置的匹配关系
3、cfg/yolov3.cfg文件的配置,这个要试了
我显卡2G显存,配置偏低,参数如下:
[net]
# Testing
batch=64
subdivisions=64
# Training
#batch=64
#subdivisions=16
width=128
height=128

4、使用opencv4,可以在Makefile中的g++编译命令里添加-std=c++11的flag。

CPP = g++ -std=c++11

使用openCV4的话,有很多坑,参考第8条打个补丁就可以了

YOLO的一些参考文章
[1]. 论文
https://pjreddie.com/media/files/papers/YOLOv3.pdf
[2]. 翻译
https://zhuanlan.zhihu.com/p/34945787
[3]. 代码
https://github.com/pjreddie/darknet
[4]. 官网
https://pjreddie.com/darknet/yolo/
[5]. YouTube
https://www.youtube.com/watch?v=MPU2HistivI
[6]. 旧版
https://pjreddie.com/darknet/yolov2/
https://pjreddie.com/darknet/yolov1/
[7]. 源码分享
https://github.com/muyiguangda/darknet
[8]. YOLOv3结合openCV的patch
https://gist.github.com/tiagoshibata/f322466e8b31c14a4b98d53bf74e4f6c/revisions?diff=unified

你可能感兴趣的:(ubuntu18.04安装CUDA+cuDNN+YOLOv3)