Train YOLO with DarkNet

Complete guide to train YOLO with DarkNet

Darknet is an open source framework to train neural networks. It was implemented in C and CUDA. Here you can find a tutorial to train YOLO model for your own dataset. An interactive demo about the installation and running DarkNet can be found on the Colaboratory Notebook here: https://drive.google.com/open?id=1wtIQMypZQiIsDaX9aF_cC6pd3aVw6vzA.

First you have to clone the darknet repository from GitHub. Use the following commands:

  • $ cd /content/
  • $ rm -rf darknet
  • $ git clone https://github.com/pjreddie/darknet.git
  • $ cd darknet

Optional: enable CUDA and cuDNN usage before installing DarkNet. If you do not have these two libraries installed follow the steps described here: Install nVidia CUDA and cuDNN.

Configure DarkNet by changing the following lines in the Makefile:

  • change GPU=0 to GPU=1;
  • change CUDNN=0 to CUDNN=1;

Run the $ make command to install the framework.

It takes very long time to train YOLO from scratch. For this reason we will use some pre-trained weights.

In this example we are going to retrain our model for two classes. We will use the dataset generator, what we described on the page Dataset generator. By using it we will generate some learning data with Winnie-the-pooh and Tiger. Let's create a configuration file for tiny-yolo with two classes. Modify the file cfg/yolov2-tiny-voc.cfg and set the classes=2 value in the line 124 and features=35 in line 118.

Clone the dataset generator from the repo, install it and generate some learning data.

  • $ git clone https://github.com/szaza/dataset-generator.git
  • $ cd dataset-generator
  • $ git checkout feature/colaboratory-tutorial
  • $ ./gradlew clean build -xtest
  • $ ./gradlew run

After this step you will have a training dataset with 5000 items.

Configure darknet by copying the voc.data and voc.names files to the right place.

  • $ cp <path of the dataset-generator>/dataset-generator/dataset/output/DarknetConf/voc.data <path of the darknet>/darknet/cfg/voc.data
  • $ cp <path of the dataset-generator>/dataset-generator/dataset/output/DarknetConf/voc.names <path of the darknet>/darknet/data/voc.names

You're ready to start training your model. Run the following command from the DarkNets root folder:

  • $ ./darknet detector train cfg/voc.data cfg/yolov2-tiny-voc.cfg weights/darknet19_448.conv.23

You should get similar outputs like on the image below:

Train YOLO with darknet

Train YOLO with DarkNet.

Stop training by pressing CTR+C when the value of the IOU is close enough to 1,0. We propose to run 10000 steps for this dataset. The retrained model can be tested with this command:

  • $ ./darknet detector test cfg/voc.data cfg/yolov2-tiny-voc.cfg backup/yolov2-tiny-voc_<number>.weights test/<some image>.jpg

The output should looks similar like this:

Predictions with the retrained tiny-yolo.

Predictions with the retrained tiny-yolo.

Here you can see the input image and the predictions for it. For more accurate score use the yolov2 and yolov3 models, which are deeper neural networks. They should be trained for a longer period: 20000 - 40000 steps are required.

Input image for object detection with winnie-the-pooh and tiger.

Input image for object detection with Winnie-the-Pooh and Tiger.

Predicted image by using YOLO. Tiger and Winnie the pooh were detected.

Predicted image by using YOLO. Tiger and Winnie-the-Pooh were detected.


The retrained weights can be downloaded from the following link: https://drive.google.com/file/d/1eq0iaq8xjuf7FLRyfE2-K_HHt8EFA354/view?usp=sharing.