Sub topic 9
Preparing a Dataset in YOLO Format
Prerequisites
Assume you know basic Python, understand images as arrays of pixels, and have a basic idea of neural-network training. This chapter focuses on the labels that connect your images to a YOLO detector.
Dataset structure
A YOLO dataset separates images from label files. Each image has a text file with the same base name.
For example:
images/train/img001.jpglabels/train/img001.txt
The validation split follows the same pattern:
images/val/img101.jpglabels/val/img101.txt
A label file contains one line for each object in its image. Each line uses this format:
class_id x_center y_center width heightThe class ID is an integer starting at zero. The four coordinates are normalized, meaning they are fractions of the image dimensions and must be between 0 and 1.
| Value | Meaning |
|---|---|
| `class_id` | Zero-based index of the object class |
| `x_center` | Box center's horizontal position divided by image width |
| `y_center` | Box center's vertical position divided by image height |
| `width` | Box width divided by image width |
| `height` | Box height divided by image height |
For example:
# labels/train/img001.txt
0 0.512 0.430 0.200 0.350
1 0.150 0.700 0.100 0.220This image contains two objects. The first belongs to class 0; the second belongs to class 1. Do not include pixel coordinates, commas, brackets, or class names in the label file. Use spaces between values.
Converting corner coordinates
Annotation tools often export a box as (x1, y1, x2, y2), where the values are pixel coordinates. For an image with width W and height H, convert it as follows:
def xyxy_to_yolo(x1, y1, x2, y2, image_width, image_height):
box_width = x2 - x1
box_height = y2 - y1
x_center = (x1 + x2) / 2
y_center = (y1 + y2) / 2
return (
x_center / image_width,
y_center / image_height,
box_width / image_width,
box_height / image_height,
)
print(xyxy_to_yolo(100, 50, 300, 250, 1000, 500))Check that x2 > x1, y2 > y1, and every normalized value is in the range 0 to 1. A box that extends outside the image should be clipped before conversion.
Dataset configuration
The trainer needs a YAML configuration file that maps split directories to class names:
path: datasets/my_data
train: images/train
val: images/val
names:
0: cat
1: dogThe class order in names must match the IDs in every label file. If ID 0 means cat in the YAML file but your labels use it for dogs, training metrics and predictions become misleading.
Validation checklist
Before training, inspect several images with their boxes drawn over them. Confirm that:
- Every object has the correct class ID.
- Every image and label use the same base filename.
- Boxes are inside the image and have positive width and height.
- The YAML class order matches the label IDs.
- Train and validation images do not duplicate one another.
Exercise
An image is 1280 pixels wide and 720 pixels high. Its box is (128, 144, 640, 504). What YOLO values should you write after the class ID?
Show answer
The center is (384, 324), and the size is (512, 360). Normalize by width and height: xcenter=0.300, ycenter=0.450, width=0.400, height=0.500.