Darknet — A Neural Network Framework written in C and CUDA

Liang Han Sheng
5 min readJul 31, 2021

--

Source: https://pjreddie.com/darknet/

In this article, I’m going to introduce to you what is Darknet.

Outcomes

  1. What is Darknet?
  2. What Darknet can do?
  3. How to build Darknet in Windows, Mac, and Ubuntu.

About Darknet

Darknet is an open-source neural network framework written in C and CUDA. It is fast, easy to install, and supports CPU and GPU computation. Users can find the source on GitHub. Darknet is installed with only two optional dependencies: OpenCV if the user wants a wider variety of supported image types or CUDA if they want GPU computation. Neither is compulsory but users can start by just installing the base system which has been tested in Windows, Linux, and Mac computers.

The framework features You Only Look Once (YOLO), a state-of-the-art, real-time object detection system. On a Titan X it processes images at 40–90 FPS and has a mAP on VOC 2007 of 78.6% and a mAP of 44.0% on COCO tets-dev. Users can use Darknet to classify images for the 1000-class ImageNet challenge.

Darknet displays information as it loads the config file and weights then it classifies the image and prints the top-10 classes for the image. Moreover, the framework can be used to run neural network backward in a feature appropriately named Nightmare.

Recurrent neural network are powerful models for representing data that changes over time and Darknet can handle them without making use of CUDA or OpenCV. The framework also allows its users to venture into game-playing neural networks.

It features a neural network that predicts the most likely next moves in a game of GO. Users can play along with professional games and see what moves are likely to happen next, make it play itself, or try to play against it.

Building/Compiling Darknet

1. Install Visual Studio Community 2017/2019

First, we need to install Visual Studio Community 2017/2019 to our device. The download here available here. Remember to install English language pack, this is mandatory for vcpkg!

2. Check CUDA Status

nvcc -V

Type command above in Command Prompt and check the output.

Check CUDA Status

If the output shown similar as above, CUDA is enabled in your device, otherwise no. If your device have a CUDA-capable GPU, you can follow tutorial here to install CUDA driver and enabling VS Integration during installation.

Also, we will need cuDNN installed. To download cuDNN, you will have to join NVIDIA Developer Program and select cuDNN package which match your CUDA version here. Then, follow the guide here to move the downloaded cuDNN files to target locations in CUDA folder.

3. Set up Darknet

Compile Darknet in Windows using Vcpkg

In Windows, the recommended approach to build Darknet is using vcpkg. Vcpkg is a free C/C++ package manager for acquiring and managing libraries. Choose from over 1500 open source libraries to download and build in a single step or add your own private libraries to simplify your build process. Maintained by the Microsoft C++ team and open source contributors.

Open Powershell (Start -> All programs -> Windows Powershell) and type these commands:

> git clone https://github.com/microsoft/vcpkg
> cd vcpkg
> $env:VCPKG_ROOT=$PWD
> .\bootstrap-vcpkg.bat
> .\vcpkg install darknet[opencv-base,cuda,cudnn]:x64-windows
> Set-ExecutionPolicy unrestricted -Scope CurrentUser -Force
> git clone https://github.com/AlexeyAB/darknet
> cd darknet
> .\build.ps1 -UseVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN

**add option -EnableOPENCV_CUDA if you want to build OpenCV with CUDA support - very slow to build! - or remove options like -EnableCUDA or -EnableCUDNN if you are not interested in them. If you open the build.ps1 script at the beginning you will find all available switches.

Build Darknet in Linux/MacOS

It is easy if you want to build Darknet in Linux or MacOS. You just need to run make in the darknet directory. Before make , you can set such options in the Makefile :

  • GPU=1 to build with CUDA to accelerate by using GPU (CUDA should be in /usr/local/cuda)
  • CUDNN=1 to build with cuDNN v5-v7 to accelerate training by using GPU (cuDNN should be in /usr/local/cudnn)
  • 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 4.x/3.x/2.4.x - allows to detect on video files and video streams from network cameras or web-cams
  • DEBUG=1 to build debug version of Yolo
  • OPENMP=1 to build with OpenMP support to accelerate Yolo by using multi-core CPU
  • LIBSO=1 to build a library darknet.so and binary runnable file uselib that uses this library. Or you can try to run so LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ./uselib test.mp4 How to use this SO-library from your own code - you can look at C++ example: https://github.com/AlexeyAB/darknet/blob/master/src/yolo_console_dll.cpp or use in such a way: LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ./uselib data/coco.names cfg/yolov4.cfg yolov4.weights test.mp4
  • ZED_CAMERA=1 to build a library with ZED-3D-camera support (should be ZED SDK installed), then run LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ./uselib data/coco.names cfg/yolov4.cfg yolov4.weights zed_camera
  • You also need to specify for which graphics card the code is generated. This is done by setting ARCH=. If you use a never version than CUDA 11 you further need to edit line 20 from Makefile and remove -gencode arch=compute_30,code=sm_30 \ as Kepler GPU support was dropped in CUDA 11. You can also drop the general ARCH= and just uncomment ARCH= for your graphics card.

4. Darknet Installation Verification

Open Command Prompt, use cd command redirect to Darknet repository. cd <</path/to/Darknet-repository>> Type darknet.exe and check the output.

Darknet Installation Verification

If the output shown exactly same as above, congratulations! Now Darknet installed on our device successfully!

In my next article, I’m going to show you how we can use Darknet to perform object detection by just using CLI. Stay Tuned!

References

About Author

This article is written by Han Sheng, Technical Lead in Arkmind, Malaysia. He has a passion for Software Design/Architecture related stuff, Computer Vision and also Edge Devices. He made several AI-based Web/Mobile Applications to help clients solving real-world problems. Feel free to read about him via his Github profile.

--

--

Liang Han Sheng
Liang Han Sheng

Written by Liang Han Sheng

Loan Origination Solutions Provider | Full Stack AI Application Development

No responses yet