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.

search_python
Then we look for the version that interests us.
search_python_version
Then download the installer for Windows 64bits.
select_windows_installer
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.
python_installer_step_1
python_installer_close
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.

search_visual_studio
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.
select_visual_studio_community
Launch the VisualStudio installer, then select “Desktop development with C++” when choosing the modules to install.
install_visual_studio
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.

search_cmake
Then we select the Windows 64bits installer.
select_cmake_windows_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.
cmake_installer_step_1
cmake_installer_step_4
cmake_installer_step_2
cmake_installer_step_5
cmake_installer_step_3
cmake_installer_step_6
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.

search_cuda_toolkit
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)
select_cuda_toolkit_version
Run the CUDA Toolkit installation program, then “Next” to complete the installation without modifying the parameters.
cuda_toolkit_step_0
cuda_toolkit_step_1
cuda_toolkit_step_3
cuda_toolkit_step_2
cuda_toolkit_step_4
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.
search_cudnn
We then select v8.9.7 because it is compatible for darknet training and because future versions may not be.
select_cudnn_version
Run the CUDA Toolkit installation program, then “Next” to complete the installation without modifying the parameters.
select_cudnn_windows_installer
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.
cudnn_log_in
Once you’ve downloaded the cudnn zip, you can unzip it wherever you like. We’ll unzip it directly in the download area.
extract_cudnn

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.

copy_cudnn_folder
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.

search_opencv
Here we’re choosing version 4.9.0 because, once again, it’s currently compatible with Darknet training.
select_opencv_windows_installer
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.
opencv_installer

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

opencv_env_step_1
From now on, we have configured our environment properly.
Congratulations !
We can now use it to train YOLO models on Custom Datasets.
opencv_env_step_3
opencv_env_step_2
opencv_env_step_4
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.

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

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.

unzip_darknet

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.

darknet_cmake_start

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.

darknet_cmake_modify_param
Firstly, modify the execution from Debug to Release.
darknet_visual_studio_step_1

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

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

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”.

darknet_setup_to_detect_with_yolo

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

darknet_test
For Further Assistance: