Training a YOLO Object Detector with Darknet: A Comprehensive Guide

Prathamesh Amrutkar
2 min readDec 17, 2023

--

Introduction

You’ve decided to train a YOLO (You Only Look Once) object detector using Darknet, a popular open-source neural network framework. This comprehensive guide will walk you through various aspects of training, testing, and optimizing your YOLO model.

1. Training the Detector

1.1 Single GPU Training

If you have a single GPU, you can train your YOLO detector using the following command:

!./darknet detector train 'obj.data' 'obj.cfg' 'net.conv.14' -map -dont_show

1.2 Multi-GPU Training

For multiple GPUs, specify the GPUs to use:

!./darknet detector train 'obj.data' 'obj.cfg' 'net.conv.14' -map -dont_show -gpus 0,1,2

1.3 Monitoring Training Progress on a Remote Server

To monitor mAP (mean Average Precision) and Loss during training without a GUI:

!./darknet detector train 'obj.data' 'obj.cfg' 'net.conv.14' -map -dont_show -mjpeg_port 8090

2. Running the Detector

2.1 Image Inference

Run the detector on an image with a confidence threshold of 0.25:

!./darknet detector test data/obj.data cfg/yolov4.cfg yolov4.weights -thresh 0.25

2.2 Image Inference with Output Display

Run the detector on an image, show output, and save the result:

!./darknet detector test data/obj.data yolov4.cfg yolov4.weights -ext_output {img_path}

2.3 Video Inference and Display

Run the detector on a video and display the output:

!./darknet detector demo data/obj.data cfg/yolov4.cfg yolov4.weights -ext_output test.mp4

2.4 GPU Selection for Video Inference

Run the detector on a specific GPU for video inference:

!./darknet detector demo data/coco.data cfg/yolov4-tiny.cfg yolov4-tiny.weights -i 1 test.mp4

2.5 Webcam Inference

Run the detector on webcam #0:

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

2.6 Save Video Results

Save the results of video detection to a file:

!./darknet detector demo data/coco.data cfg/yolov4.cfg yolov4.weights test.mp4 -out_filename res.avi

3. Model Optimization and Analysis

3.1 Calculate Anchors for YOLOv4

Calculate anchors for YOLOv4 model:

!./darknet detector calc_anchors data/obj.data -num_of_clusters 9 -width 416 -height 416

3.2 Check mAP@IoU=75%

Check accuracy with mAP at IoU=75%:

!./darknet detector map data/obj.data yolo-obj.cfg backup/yolo-obj_7000.weights -iou_thresh 0.7

4. Additional Processes

4.1 Save Results to JSON

Process a list of images and save detection results to a JSON file:

!./darknet detector test data/obj.data cfg/yolov4.cfg yolov4.weights -ext_output -dont_show -out result.json < data/train.txt

4.2 Save Results to TXT

Process a list of images and save detection results to a TXT file:

!./darknet detector test data/obj.data cfg/yolov4.cfg yolov4.weights -dont_show -ext_output < data/train.txt > result.txt

4.3 Pseudo-labeling

Pseudo-labeling for YOLO training format:

!./darknet detector test data/obj.data cfg/yolov4.cfg yolov4.weights -thresh 0.25 -dont_show -save_labels < data/new_train.txt

4.4 Get Pre-trained Weights

Get pre-trained weights for YOLOv4-tiny:

./darknet partial cfg/yolov4-tiny.cfg yolov4-tiny.weights yolov4-tiny.conv.15 15

Conclusion

This guide covers essential commands and techniques for training and using YOLO object detectors with Darknet. Experiment with different configurations, fine-tune hyperparameters and optimize your model for the best results. Good luck with your object detection project!

--

--