Compiling D@rknet on Windows

Ankit Mishra
5 min readMar 21, 2021

All of us working in the domain of computer vision, at some time might have faced the challenge of compiling darknet in Windows.
With Linux compiling darknet is pretty much straight forward, editing the makefile and $ make in terminal.

This Blog is for fellow enthusiasts who don’t want to waste their Time researching any further as to how to get the Darknet Framework up and running on Windows.

Follow these instructions in the suggested order only, I have faced multiple challenges, wasted a lot of time on google and realized it wasn’t that difficult afterall.

  1. Install Git for windows : follow instructions here.
  2. install cmake ≥ 3.12
  3. For a GPU system install CUDA ≥ 10.0 and corresponding cuDNN
  4. install Visual Studio 2017 or 2019 based on the version of CUDA installation for a GPU system.
    For a CPU version either will work, but I suggest going forward with 2019.

Now that we have all the dependencies installed we start building the process for DArknet compilation.

5. Clone the darknet repository using git bash in the directory you want the darknet to be .
git clone https://github.com/AlexeyAB/darknet

In the makefile placed in the darknet folder, based on your CPU / GPU version of system make the required changes.

the makefile can be found inside the darknet folder cloned above.

6. Download OPENCV and OPENCV contrib source code, and follow the instructions from the link here, with some minor changes mentioned below.
a.) while configuring the opencv in cmake, make sure following are disabled.
ALSO, make sure opencv_world is Enabled

b.) setting the environment variables.

make sure the above are added to “Path” variable in System Variables.

7. For system with CPU.
Go to Darknet\build\darknet open darknet_no_gpu.sln file with Visual Studio and build the solution by right clicking on darknet_no_gpu in the solution Explorer.

8. For a GPU system go to Darknet\build\darknet open darknet.sln file and follow the above steps.

9. to get a dll file, to integrate with Python, go to
Darknet\build\darknet open yolo_cpp_dll.sln for a GPU system &
Darknet\build\darknet
open yolo_cpp_dll_no_gpu.sln for a CPU system.
Build these solutions in a similar fashion as above.

10. if during the above build process you get a LINK Error for opencv modules, there might be a chance that the path for opencv set in Environment variable is not same as in the property sheet of the solution build above.

In my case, I got the LINK Error because in the “additional library directories” section I did not have $(OPENCV_DIR)\x64\vc16\lib

I did have vc14 and vc15 instead, so I had to replace vc14 with vc16, since this is what we have set in Environment variable above — opencv section remember?

11. Now, go to darknet\build\darknet\x64 and check if you have got

a.) ‘darknet_no_gpu.exe’ : for CPU system
b.) ‘darknet.exe’ : for GPU system
c.) ‘yolo_cpp_dll.dll’ : based on solution build for GPU or CPU

12. ) Now test with the following command for a CPU system:
darknet_no_gpu.exe detector test cfg/coco.data cfg/yolov3.cfg <weights file path> dog.jpg

for a GPU system this would be :
darknet.exe detector test cfg/coco.data cfg/yolov3.cfg <weights file path> dog.jpg

13. Note to develop an application with Python only 3 files are needed to be present in the directory:

yolo_cpp_dll.dll
pthreadGC2.dll
pthreadVC2.dll

Now Go Break some Eggs!! Happy model building !! :D

Update March 29, 2021

while compiling on Windows 10 with GPU, I faced errors such as this.

1. this is the First error encounter with “success:0, failed:1” but gives NO error.

Solution :
copy following file world_451.dll, ffmpeg451_64.dll from OPEN_CV location (C:\opencv\build\install\x64\vc15\bin : MY opencv_location)
to Darknet cloned repos (D:\cloned_repos\darknet\build\darknet\x64 : My darknet cloned location)

AND

ALL files from
“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\visual_studio_integration\MSBuildExtensions”
to
“C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\BuildCustomizations”

2. After following above steps this is the 2nd Error is encountered.
Solution : since I have cuda 10.1 installed, I will not have CUDA 11.1.props — so we need to change that.

  • Right click on darknet.sln in Visual studio → Build dependencies → BuildCustomizations.
  • Uncheck CUDA 11.1 targets and select CUDA 10.1 targets.
  • open darknet.vcxproj in Notepad ++ find the cuda version and replace with your version (mine is 10.1).
  • Build the solution again

3. After resolving the above issue the next Error you will see is this ← on Left side.

Solution
Open darknet.vcxproj in Notepad++ → search for “CodeGeneration” and change the Architecture according to your system
* My GPU has a compute capability of compute_61 .
so the line 156: compute_35,sm_35;compute_86,sm_86 is changed to
compute_30,sm_30;compute_61,sm_61.

3a. Make sure following settings in property of the Solution exists :

right click on project →properties →C/C++ →Preprocessor → Preprocessor_Definitions

OPENCV → want to build without Opencv, remove this
CUDNN_HALF → if your GPU Arch supports it, mine dosen’t so I can remove
CUDNN → if you want to build without cuDNN remove this
_TIMESPEC_DEFINED
_SCL_SECURE_NO_WARNINGS
_CRT_SECURE_NO_WARNINGS
_CRT_RAND_S
GPU
WIN32
_CONSOLE
_LIB

right click on project →properties →C/C++ →General →Additional_Include_Directories

check if these values on Left side are added .

(right click on project) →properties →CUDA C/C++ →Device → code_generation → compute_30,sm_30;compute_61,sm_61.

(right click on project) →properties →Linker →General →Additional Library Directories:

check if this values in Left exist.

(right click on project) →properties →Linker →Input →Additional Dependencies:

check if this values in Left exist.

4. After following the above solution, the visual studio will try to reload the sln file, allow this to happen and then Build again.

5. Now the solution builds successfully !!!

--

--