Setup Darknet Environment

Table of Contents

The aim of this post is to enable you to train the YOLO detection model on the Darknet environment.

In this post, we are going to assume that we haven’t already installed the various software packages presented. If any of the software is already installed on your computer, you can skip the corresponding part.

Let’s start !

Installing Python

In this part I’m going to get you to install Python and one of the latest versions currently available (3.11.8).
To do this, we’re going to the official Python site.
Then we look for the version that interests us.
Then download the installer for Windows 64bits.
Then launch the installer you have just downloaded.
Here we have a checkbox to choose whether to “Add python.exe to PATH”, you can check it but in our case it’s not necessary so you can click Install Now.
That’s it for Python, let’s move on to Visual Studio.

Installing Visual Studio

In this section, I’m going to take you through installing Visual Studio Community 2022.
To do this, we’re going to visit the official Microsoft Visual Studio website.
Here too there’s a tick box for integrating Github Copilot, but it’s not necessary in our case, so you can untick it.
Then we select the Community version.
Launch the VisualStudio installer, then select “Desktop development with C++” when choosing the modules to install.
That’s it for Visual Studio, let’s move on to Cmake.

Installing CMake

In this section, I’m going to take you through installing CMake.
To do this, we’re going to visit the official CMake website.
Then we select the Windows 64bits installer.
Run the CMake installation program, then “Next” to complete the installation without modifying the parameters.
Once again, you can choose to add CMake to the PATH during installation, but this is not compulsory in our case.
That’s it for CMake, let’s move on to CUDA/CUDNN.

Installing CUDA/CUDNN

In this section, I’m going to take you through installing CUDA and CUDNN.
To do this, we’re going to visit the official NVIDIA CUDA Toolkit website.
We then select the right installer for your version of Windows.
With regard to the type of :
– local means that it will download everything needed to install cuda in the installation file. (no need for an internet connection to install)
– network, it downloads the minimum and the installer will download the majority during installation. (internet connection required but the installation file is much lighter)
Run the CUDA Toolkit installation program, then “Next” to complete the installation without modifying the parameters.
That’s all for CUDA, but to use Darknet we also need CUDNN.
CUDNN isn’t really anything to install, in fact you just need to copy files from one folder to another, but first we need these famous files.
To do this, we’re going to visit the official NVIDIA CUDNN website.
We then select v8.9.7 because it is compatible for darknet training and because future versions may not be.
Run the CUDA Toolkit installation program, then “Next” to complete the installation without modifying the parameters.
If you do not have an NVIDIA account, you will need to create one to download CUDNN.
Otherwise, simply connect to your account and repeat the above steps.
Once you’ve downloaded the cudnn zip, you can unzip it wherever you like. We’ll unzip it directly in the download area.

Then go to the unzipped folder, select the three bin/include/lib directories and paste them into “C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3”.
At the end you should have in bin :
cudnn64_8.dll/cudnn_adv_infer64_8.dll/etc…
Similarly for include and lib you should have cudnn files.

That’s it for CUDA and CUDNN, let’s move on to OpenCV.

Installing OpenCV

In this section, I’m going to take you through installing OpenCV. The version we will be installing is CPU only, but this has no impact on training.
To do this, we’re going to visit the official OpenCV website.
Here we’re choosing version 4.9.0 because, once again, it’s currently compatible with Darknet training.
The OpenCV installer is in fact a kind of disguised zip, you can choose to unzip where you want, we chose to put it at the root of the C drive.

To complete the OpenCV installation, you need to add the OpenCV paths to the PATH environment variable.
To do this, open the Windows environment manager, select Path and press “Edit…”.
Finally we add 3 new paths, if you have unzipped OpenCV to the root of C, the paths to add are the following:
C:\opencv\build\x64\vc16\lib
C:\opencv\build\x64\vc16\bin
C:\opencv\build\include

That’s it for OpenCV, let’s move on to Darknet.

Installing Darknet

In this section, we are going to take you through installing Darknet.
To do this, we’re going to visit the github of AlexeyAB.

We then select the darknet repository.
Then download a copy of the directory.

Once you’ve downloaded the darknet-master.zip, you can unzip it wherever you like. We decided to unzip it at the root of the C drive.

Now we’re going to build darknet with CMake.
To do this, we run cmake-gui, then select the folder where we unzipped darkner-master for the “Where is the source code” and “Where to build the binaries” boxes.
Then click on “Configure“, a window will open, we’ll change the “Optional platform for generator” parameter to x64 and finally press “Finish” to close the window.

After a slight load, a list of parameters will appear in red in the centre.
Among these parameters, modify the “CMAKE_CUDA_ARCHITECTURES” parameter by setting the following value: “52;60;61;70;75;80;86;89;90“. This modification is not necessarily compulsory, but theoretically it will optimise execution for all the latest graphics cards.
After clicking on Generate, when Open Project becomes clickable, you can click on it, which will open Visual Studio.

Firstly, modify the execution from Debug to Release.

Then modify the build configuration by going to Build->Configuration Manager
Tick “INSTALL” and close the window.

Finally, we build the solution and wait for it to finish, during the build logs will appear at the bottom.

From here, darknet is built.

Download YoloV4 Pretrained Weight

Next, download the weights of a yolov4 model pre-trained on COCO here.

Move “pthreadVC2.dll” and “yolov4.weights”

However, there is still one last step to test whether it works properly.
Go to the “C:\darknet-master\3rdparty\pthreads\bin” folder and copy “pthreadVC2.dll” and paste it into “C:\darknet-master”.
Then copy “yolov4.weights”, which you’ve just downloaded, into “C:\darknet-master”.

Run YoloV4 Inference

Now we’re going to test whether it works, to do this we’re going to run a yolov4 on an image.
To do this, open a command prompt, move to the location of your darknet directory and run a test detection :

cd C:\darknet-master
darknet.exe detect cfg/yolov4.cfg yolov4.weights data/dog.jpg

For Further Assistance: