Creating Datasets for YOLOv4 in Darknet
Last updated
Last updated
Guide to Labeling Images and Training YOLOv4 / YOLOv4-Tiny in Darknet
Before starting, you will need:
Windows or Linux system
Darknet (Download from )
Python (Recommended: Python 3.8 or newer)
OpenCV (Optional, but improves image processing)
Labeling Tool (We recommend LabelImg: )
Gather Images
Collect at least 500-1000 images per object class.
The more diverse the images, the better the model will perform.
Ensure images are clear, high-quality, and cover different angles, lighting conditions, and environments.
Save images in .jpg
or .png
format.
Install and Use LabelImg for Labeling
Install LabelImg:
Run LabelImg:
Open LabelImg and select the folder containing images.
Choose YOLO
format in the settings (this ensures labels are saved correctly for YOLO training).
Use the bounding box tool to draw boxes around objects.
Each bounding box should tightly fit the object with minimal extra space.
Assign each object the correct class label.
Save annotations (.txt
files) in the same directory as images.
Understanding YOLO Labeling Format
Each .txt
annotation file corresponds to an image and contains:
class_id: The ID number of the object class (starts from 0).
x_center, y_center: The normalized center coordinates of the bounding box (values between 0 and 1).
width, height: The normalized width and height of the bounding box (values between 0 and 1).
Example annotation for an image with a car:
If an image contains multiple objects, each object has its own line.
Organize Data Properly
Create a structured dataset folder:
Create Required Files
obj.names
: List of object classes, one per line.
train.txt
and val.txt
: Contains full paths to training and validation images.
obj.data
: Configuration file with dataset details:
Download Pretrained Weights
Modify Configuration File (.cfg
)
Use yolov4.cfg
or yolov4-tiny.cfg
.
Change width=416 height=416
(or 256x256, 128x128).
Set max_batches = (number_of_classes * 2000)
, but at least 4000.
Set steps = 80% and 90% of max_batches
.
Change filters
in [convolutional]
before [yolo]
to (number_of_classes + 5) * 3
.
Start Training
(Use yolov4-tiny.cfg
for Tiny model.)
Monitor Training
Training loss should gradually decrease.
Model weights are saved in backup/
.
Check chart.png
to visualize training progress.
Run Detection on Test Images
Interpret Results
Bounding boxes should correctly detect objects with confidence scores.
If false detections occur, refine dataset with more diverse images.
Convert to ONNX or TensorRT for better performance if needed.
YOLO models can be used in OpenCV, TensorFlow, or Jetson devices.
Use at least 80% of images for training, 20% for validation.
Label images as accurately as possible.
Train on multiple sizes (416x416, 256x256, 128x128) to test different speed-accuracy trade-offs.
Augment dataset with flipped, rotated, and brightened images for robustness.
Increase dataset size if overfitting occurs.
Collect and label images properly.
Organize dataset and create annotation files.
Train the model with YOLOv4 or YOLOv4-Tiny.
Test and optimize the model for accuracy.
Convert model for deployment in real-world applications.
YOLOv4:
YOLOv4-Tiny: