Skip to content

Instantly share code, notes, and snippets.

@richardjortega
Last active April 23, 2020 06:16
Show Gist options
  • Save richardjortega/dfd5170e91fb9df165cfae03ac2afa97 to your computer and use it in GitHub Desktop.
Save richardjortega/dfd5170e91fb9df165cfae03ac2afa97 to your computer and use it in GitHub Desktop.
Setting up NVIDIA on Linux laptop

Configuration for GPU-accelerated Machine Learning system (using Ubuntu 18.04)

  • Setup Ubuntu x86_64 18.04
  • Disable UEFI Safe Boot in BIOS (for NVIDIA module signing, may not be necessary)

Setup NVIDIA

Install latest NVIDIA driver

Download the latest driver for your NVIDIA: https://www.nvidia.com/Download/index.aspx?lang=en-us

  • Take note of the driver (ex. 430.30)

Verify NVIDIA driver

Run cat /proc/driver/nvidia/version:

NVRM version: NVIDIA UNIX x86_64 Kernel Module  430.40  Sun Jul 21 04:53:48 CDT 2019
GCC version:  gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1) 

Run nvidia-smi:

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 430.40       Driver Version: 430.40       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| 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 RTX 2080    On   | 00000000:01:00.0 Off |                  N/A |
| N/A   43C    P8     5W /  N/A |    211MiB /  7982MiB |      6%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1195      G   /usr/lib/xorg/Xorg                           128MiB |
|    0      1599      G   /usr/bin/gnome-shell                          81MiB |
+-----------------------------------------------------------------------------+

Install CUDA Toolkit

Install CUDA using Package Manager method (this ensure you're not forced to swap NVIDIA driver to unsupported lower driver)

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
sudo apt-get update
sudo apt-get -y install cuda

Add CUDA to PATH

export PATH=/usr/local/cuda-10.1/bin:/usr/local/cuda-10.1/NsightCompute-2019.1${PATH:+:${PATH}}

Verify completion

Run systemctl status nvidia-persistenced and verify active state:

nvidia-persistenced.service - NVIDIA Persistence Daemon
   Loaded: loaded (/lib/systemd/system/nvidia-persistenced.service; enabled; ven
   Active: active (running) since Thu 2019-08-22 19:05:55 PDT; 22min ago
  Process: 1238 ExecStart=/usr/bin/nvidia-persistenced --verbose (code=exited, s
 Main PID: 1239 (nvidia-persiste)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/nvidia-persistenced.service
           └─1239 /usr/bin/nvidia-persistenced --verbose

Install NVIDIA support for Docker containers

# Add the package repositories
$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
$ curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
$ curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list

$ sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
$ sudo systemctl restart docker

Test nvidia-smi with the latest official CUDA image

$ docker run --gpus all nvidia/cuda:9.0-base nvidia-smi

Install OpenCV

sudo apt-get install python-opencv
sudo apt-get install libopencv-dev

Install Darknet/YOLO

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

Enable GPU and OpenCV

Edit the Makefile via vim:

  • Update GPU=1
  • Update OPENCV=1

Run make

Test YOLO

Assumes testing within darknet download git folder.

Inference Photo

./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg

Inference Webcam

GPU-accelerated (ESC to escape)

20 FPS

./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights

30 FPS

./darknet detector demo cfg/coco.data cfg/yolov2.cfg yolov2.weights

Inference Video (AVI/MP4)

./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights <video_file>

Test YOLOv3 Tiny

Dwonload YOLOv3 Tiny weight

wget https://pjreddie.com/media/files/yolov3-tiny.weights

Inference Photo

./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights data/dog.jpg

Inference Webcam

GPU-accelerated (ESC to escape)

30-40 FPS

./darknet detector demo cfg/yolov3-tiny.cfg yolov3-tiny.weights
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment