Darknet with CUDA: Train YOLO Model for QR Code Detection on Windows

In my previous article, I shared how to integrate Dynamsoft Barcode Reader to LabelImg for annotating barcode objects. It is time to take a further step to make some custom models for barcodes. In this article, I will go through the process that I used Darknet to train YOLO v3 models for QR code detection.

About Darknet and YOLO

  • Darknet is an open-source neural network framework.
  • YOLO is a real-time object detection system.

Building Darknet on Windows

Let’s get the source code of Darknet:

git clone https://github.com/AlexeyAB/darknet --depth 1

To build Darknet on Windows, we have to install the following tools beforehand:

  • CMake 3.18.4
  • Visual Studio 2019 Community edition
  • OpenCV 4.5.0. Add “OpenCV_DIR = C:\opencv\build” to system environment variables and add “C:\opencv\build\x64\vc15\bin” to PATH.
  • CUDA 10.1. Copy C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\visual_studio_integration\MSBuildExtensions to C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations
  • cuDNN 7.6.5. Copy cuda\bin*.dll to NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin. Copy cuda\include*.h to NVIDIA GPU Computing Toolkit\CUDA\v10.1\include. Copy cuda\lib\x64*.lib to NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib.

Note: NVIDIA CUDA Visual Studio Integration may not be installed to the expected Visual Studio directory if you have multiple editions of the VC++ toolset installed. If the integration is not installed in C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations, CMake will fail to find the CUDA compiler even though the “nvcc” command can work.

cuda not found

When running the build.ps1 script file in PowerShell first time, you will see a security warning.

powershell policy

If the PowerShell script file fails to run, you need to re-open PowerShell as administrator and change the security policy:

> Set-ExecutionPolicy Bypass
> build.ps1

Once the build is done, you can check the status of Darknet.

CPU edition

> darknet.exe detector test
 GPU isn't used
 Used AVX
 Used FMA & AVX2

GPU edition

> darknet.exe detector test
 CUDA-version: 10010 (10010), cuDNN: 7.6.5, CUDNN_HALF=1, GPU count: 1
 CUDNN_HALF=1

Training YOLO v3 Model for QR Code

Now, let’s get our hands dirty to train a model for QR code detection. If you don’t have GPU, skip this section, for training with CPU is a nightmare.

QR code images and label files

For saving time, I only prepared about 250 QR code images and corresponding label files generated by labelImg. The more data you train, the more accurate the model will be.

I created a new folder named qrcode and copy all training files to the folder.

yolo qr image

In the same way, I created a new folder darknet\data\qrcode-valid and copied a bunch of files to the folder for model validation.

Configuration files

Before training models, I created some config files including qrcode.data, qrcode.txt, qrcode-valid.txt, qrcode.names, qrcode-yolo3v.cfg and qrcode-yolov3-tiny.cfg.

The qrcode.data file contains the dataset information:

classes = 1
train = data/qrcode.txt
valid = data/qrcode-valid.txt
names = data/qrcode.names
backup = backup/

The qrcode.txt and qrcode-valid.txt are auto-generated by the Python script https://github.com/theAIGuysCode/YoloGenerateTrainingFile/blob/master/generate_train.py.

The qrcode.names file includes the class names. I only have one class QR_CODE.

The qrcode-yolo3v.cfg and qrcode-yolov3-tiny.cfg are based on data\yolov3.cfg and data\yolov3-tiny.cfg.

According to the online documentation https://github.com/AlexeyAB/darknet#how-to-train-to-detect-your-custom-objects, I changed some parameters as follows:

classes=1
filters=18
max_batches = 4000
steps=3200,3600

Weight file and training command

Download darknet53.conv.74 to train the model:

> darknet.exe detector train data/qrcode.data cfg/qrcode-yolov3.cfg darknet53.conv.74

YOLOv3

darknet.exe detector train data/qrcode.data cfg/qrcode-yolov3-tiny.cfg darknet53.conv.74

YOLOv3-Tiny

Performance Test: CPU vs GPU

After successfully training the model, I made a quick test with the QR code image captured by my cellphone.

Hardware

GPU: NVIDIA RTX2060
CPU: Intel(R) Core(TM) i5-4460  CPU @ 3.20GHz, 3201 Mhz, 4 Core(s), 4 Logical Processor(s)

YOLOv3-tiny Test

Run the command:

darknet.exe detector test qrcode.data qrcode-yolov3-tiny.cfg qrcode-yolov3-tiny_last.weights 20201105151910.jpg

CPU

Done! Loaded 24 layers from weights-file
 Detection layer: 16 - type = 28
 Detection layer: 23 - type = 28
20201105151910.jpg: Predicted in 160.487000 milli-seconds.
QR_CODE: 97%

GPU

Done! Loaded 24 layers from weights-file
 Detection layer: 16 - type = 28
 Detection layer: 23 - type = 28
20201105151910.jpg: Predicted in 3.717000 milli-seconds.
QR_CODE: 97%

YOLO v3 tiny model for QR code detection

YOLOv3 Test

Run the command:

darknet.exe detector test qrcode.data qrcode-yolov3.cfg qrcode-yolov3_last.weights 20201105151910.jpg 

CPU

Done! Loaded 107 layers from weights-file
 Detection layer: 82 - type = 28
 Detection layer: 94 - type = 28
 Detection layer: 106 - type = 28
20201105151910.jpg: Predicted in 3322.188000 milli-seconds.
QR_CODE: 100%

GPU

Done! Loaded 107 layers from weights-file
 Detection layer: 82 - type = 28
 Detection layer: 94 - type = 28
 Detection layer: 106 - type = 28
20201105151910.jpg: Predicted in 31.717000 milli-seconds.
QR_CODE: 100%

YOLO v3 model for QR code detection

Download Pre-trained YOLO v3 Model for QR Code Detection

If you are interested in QR code detection, you can try my models.

References