YOLOv4, CUDA, CUDNN, OPENCV Installations on Windows 11

Işınsu
6 min readJul 24, 2022

--

Most of us have heard of YOLO (You Only Look Once). YOLO detects objects in image or video using CNN. There are different methods we can apply to run YOLO. We can train our model using the GPU provided by Google COLAB or we can use our computer’s graphics card. However, COLAB will not give you a very long time to use the GPU unless you upgrade to the pro version in COLAB. At this point, you can get COLAB Pro or if your computer’s graphics card supports it, you can install it. I will install with NVIDIA RTX 3050Ti on Windows 11.

Before we start, I must say that while installing, you must download compatible versions in CUDA, cuDNN, OpenCV, python, YOLO, Cmake and Visual Studio. You can follow the versions I use or you can check the versions here https://docs.nvidia.com/deeplearning/cudnn/support-matrix/index.html and proceed accordingly.

First of all, if you don’t have Python installed on your computer, you need to install it. To install Python
You should go to https://www.python.org/downloads/ and download the version your computer supports. Then you can complete the installation by ticking ‘Add Python x.x to PATH’ in the setup file. To make sure Python is installed, we just need to open the command line and type ‘python’. There is no problem if you see the version you have installed as follows:

Next, write pip3 install numpy
We install numpy with the command.
Next is the CMAKE installation. https://cmake.org/download/ we go to this site and select the file supported by our computer and download it. We go through the setup file.
Next is the Visual Studio installation. If you don’t have VS on your computer, you can download it from Microsoft’s site. Just select the ‘Python Development’ and ‘Desktop development with C++’ options during installation.
Then we check if our graphics card has an update. We can do this from the computer or you can go to NVIDIA’s site and install the driver. (https://www.nvidia.com/download/index.aspx)
Next is the NVIDIA CUDA Toolkit installation. For this https://developer.nvidia.com/cuda-downloads
We are going to his site. We select the version supported by our computer and download it. When we open the setup file, we set the Extraction Path to C:\CUDA. Then we click next and complete the installation.
Next is the cuDNN installation. We go to https://developer.nvidia.com/cudnn and perform the installation. After installing, we extract the file from the zip and put it under the C drive. We can rename this file to cuda. Then we go to C:\ cuda \bin and copy the files there and paste them into C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\bin. The version number may differ depending on the version you downloaded.

Then we go to C:\cuda\include and copy the .h files and paste them into C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\include:

Finally, copy and paste the files from C:\cuda\lib to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\lib\x64:

Then we download the version suitable for us from https://opencv.org/releases/ and go to https://github.com/opencv/opencv_contrib and pull the project to our computer with git clone. After extracting these files, we open a folder called opencv on our C drive and put them both in this folder. We also create a new folder in this directory and name it ‘build’:

Next, we open CMake on our computer. In the source code part, we give the path of our opencv file as follows:

Then we click the ‘configure’ button. We mark ‘X64’ in the Optional platform for generator section. Click finish. Then we also mark the BUILD_opencv_world value:

After you say generate, if you have seen the ‘Configuring done. Generating done.’ text, it means that you have come this far without any mistakes. If your C:\opencv\build file is full, it’s OK. Next is to build OpenCV with Visual Studio.
We open the selected file under the following directory with VS.

We mark Release and x64:

Then we build the ALL_BUILD file and the INSTALL file under CMakeTarget:

To make sure we have installed OpenCV, we come to the command prompt and after typing python, we type import cv2 and then cv2.__version__ :

If you see the version number like this, it means you have successfully installed it.

Then we create a folder named Yolo_v4 on our C Drive and clone the project https://github.com/AlexeyAB/darknet into it.
Next, we go to C:\opencv\build\bin\Release and copy our opencv_ffmpeg460_64.dll and opencv_world460.dll files and paste them into C:\Yolo_v4\darknet\build\darknet\x64. Then we go to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\bin and copy our cudnn64_8.dll file and paste it into C:\Yolo_v4\darknet\build\darknet\x64. Including CuDNN in our darknet project will reduce our train time.
Go to C:\Yolo_v4\darknet\build\darknet and open the darknet.vcxproj file. Since CUDA 10 version is used in the github repo where we pulled the project, we need to change it. We do CTRL+F and search for CUDA 10. We are replacing CUDA 10s with CUDA 11.7, our own version. We save and close the file. Then we open the yolo_cpp_dll.vcxproj file. In the same way, we replace and save CUDA 10s with CUDA 11.7. After doing these, we need to compile YOLO with the new CUDA version. For this, we open the yolo_cpp_dll.vcxproj file with VS. Again at the top we mark Release and x64. Right click on our file and click build. If it has been successfully built, we continue. We close the file and open the darknet.sln file with VS. After making sure that Release and x64 are checked, we right-click on the darknet and open properties. We choose C/C++ and come to the additional include directory. We open it and add C:\opencv\build\install\include, then we click apply:

Then we select device in CUDA C/C++. We delete the second rows. It stays as follows:

Then we select linker->general and additional library directories. Adding C:\opencv\build\install\x64\vc17\lib. Click to Apply.

We go to C:\Yolo_v4\darknet and open the Makefile. We change the values in the first part as follows:

Then right click on the darknet and click build:

Then we come to the C:\Yolo_v4\darknet\build\darknet\x64 directory. Open Command Prompt and type ‘python’. Then we type ‘import darknet’. If we didn’t get any errors, it means we did everything properly.
So far, we have completed our installations. Now we are on our last step before we move on to the train. For that, we go to https://github.com/AlexeyAB/darknet#how-to-evaluate-fps-of-yolov4-on-gpu. Download the yolov4.weights file with the weights from Step 2. After downloading this file, we paste it to C:\Yolo_v4\darknet\build\darknet\x64.
Then go to C:\Yolo_v4\darknet\build\darknet\x64 directory and open command prompt and write

darknet.exe detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights:

It asks us the path of an image. We can select the dog.jpg that comes in the downloaded files and test it. We write C:\Yolo_v4\darknet\build\darknet\x64\dog.jpg. Our result is as follows:

You can also open your camera by typing:

darknet.exe detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights -c 0.

--

--