4º- How to install YOLO Darknet with CUDA and OpenCV in Ubuntu

The task of installing the open source neural network framework Darknet with CUDA and OpenCV enabled in Ubuntu may not be an easy task. After having successfully installed it, in this tutorial I want to explain all the process in the simplest way and help you solve some common and not that common problems.

Ubuntu distribution

First of all, if you do not have an Ubuntu distribution installed, I recommend installing the Ubuntu version 18.04. I tried to install the 18.10 version and I was not able to install CUDA in the end. So, I encourage you to install Ubuntu 18.04.

Darknet installation

Darknet’s source is available on Github. However, after installing this repository I discovered a fork of it by AlexexeyAB. I strongly advise to install this one, it is more often updated and it has extra features such as:

  • Added support for Windows
  • Improved binary neural network performance 2x-4x times for Detection on CPU and GPU if you trained your own weights by using this XNOR-net model (bit-1 inference) : https://github.com/AlexeyAB/darknet/blob/master/cfg/yolov3-tiny_xnor.cfg
  • Improved neural network performance ~7% by fusing 2 layers into 1: Convolutional + Batch-norm
  • Improved neural network performance Detection 3x times, Training 2 x times on GPU Volta (Tesla V100, Titan V, …) using Tensor Cores if CUDNN_HALF defined in the Makefile or darknet.sln
  • Improved performance ~1.2x times on FullHD, ~2x times on 4K, for detection on the video.
  • Improved performance 3.5 X times of data augmentation for training.
  • Improved performance of detection and training on Intel CPU with AVX (Yolo v3 ~85%, Yolo v2 ~10%)
  • Added correct calculation of mAP, F1, IoU, Precision-Recall using command darknet detector map
  • Added drawing of a chart of average-Loss and accuracy-mAP (-map flag) during training
  • Added calculation of anchors for training

Once we have cloned this repository on our computer, we could compile the files by writing ‘make’ command. However, this will only use the CPU for training and detecting, and this method is extremely slow. For this reason, we see that in the Makefile, there are some important lines at the beginning:

  • GPU=1 to build with CUDA to accelerate by using GPU.
  • CUDNN=1 to build with cuDNN v5-v7 to accelerate training by using GPU.
  • CUDNN_HALF=1 to build for Tensor Cores (on Titan V / Tesla V100 / DGX-2 and later) speedup Detection 3x, Training 2x
  • OPENCV=1 to build with OpenCV 3.x/2.4.x – allows detecting on video files and video streams from network cameras or webcams. Besides, it enables the visualization of a chart of average loss and accuracy during training (recommended).

CUDA Installation

In respect of CUDA, I recommend installing the CUDA Toolkit 10.0. Unfortunately, I tried to install the CUDA Toolkit 10.1 and it did not work. In the installer type I recommend to choose runfile (local), for me, it was the easiest way to get it working.

Before installing it, verify that you are able to install CUDA Toolkit in your computer, you can follow some simple steps here. Once you have checked your computer satisfies all the requirements, you can run the following commands in the terminal:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install cmake

sudo sh cuda_10.0.130_410.48_linux.run

After that, you will be required to answer some command-line prompts, answer all to “yes” or accept and wait until the process has finished. Finally, you need to update the PATH and LD_LIBRARY_PATH variables. You can do that with the following commands: (Note that these are for cuda-10.0)

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

export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64\ ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64\ ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

Nvidia recommends doing the Power9 Setup too. First, we check if the NVIDIA Persistence Daemon has been started:

systemctl status nvidia-persistenced

If it is not active, run:

sudo systemctl enable nvidia-persistenced

Finally, we need to disable the udev rule that causes hot-pluggable memory to be automatically online when it is physically probed. We can do so with the following commands:

sudo cp /lib/udev/rules.d/40-vm-hotadd.rules /etc/udev/rules.d
sudo sed -i ‘/SUBSYSTEM==”memory”, ACTION==”add”/d’ /etc/udev/rules.d/40-vm-hotadd.rules

Installing OPENCV

For installing OPENCV, I only needed to install libopencv-dev library, so you can do it by running.

sudo apt-get install libopencv-dev

Before discovering it, I tried several tutorials like this one, but they are for installing OpenCV for Python3 and not as a command for the operating system.

Compiling Darknet

Now that we have everything installed, we only need to compile Darknet for being able to start training or detecting our YOLO model! Before doing so, we need to have the gcc compiler, so the easiest way to do so is by running:

sudo apt-get install build-essential

Now, we need to activate the GPU and OpenCV options in the Makefile, you can use your preferred editor such as nano or vi. Simply set the value of CUDA and OpenCV to 1.

GPU = 1

Now you can simply write ‘make’. Everything should work great! If you have errors such as:

compilation terminated due to -Wfatal-errors.
Makefile:152: recipe for target ‘obj/convolutional_kernels.o’ failed
make: *** [obj/convolutional_kernels.o] Error 1

After the command nvcc -gencode arch=compute_61

It is because you need to update the PATH and LD_LIBRARY_PATH variables as explained before.

I hope everything worked and happy training and detecting with Darknet! Don’t forget to follow me on Twitter to stay updated!:https://twitter.com/SaferAuto

Un comentario sobre “4º- How to install YOLO Darknet with CUDA and OpenCV in Ubuntu

Agrega el tuyo


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s

Un sitio web WordPress.com.

Subir ↑

Crea tu página web en WordPress.com
Empieza ahora
A %d blogueros les gusta esto: