Sub topic 7
Annotation Formats: COCO, YOLO and Pascal VOC
Prerequisites and format choices
This chapter assumes you know Python lists and dictionaries, can read JSON, and understand image width, height, and pixel coordinates. You should also know that supervised learning uses labelled examples to learn a mapping from images to predictions.
An annotation format is a file structure that stores the labels for an image. The best format depends on the task and the tools in your workflow.
| Format | Typical storage | Box convention | Segmentation support |
|---|---|---|---|
| COCO | One JSON file for a dataset | `[x_min, y_min, width, height]` | Polygons and run-length encoding |
| YOLO | One text file per image | Normalised centre and size | Detection and, in extensions, polygons |
| Pascal VOC | One XML file per image | `xmin, ymin, xmax, ymax` | Usually separate mask files |
COCO: one indexed JSON dataset
COCO stores images, categories, and annotations in related arrays. IDs connect an annotation to its image and category. This structure works well for datasets containing many images and multiple task types.
A COCO bounding box starts at the top-left corner. Its values are pixel coordinates:
xandyidentify the top-left corner.widthandheightidentify the box size.areais usually the labelled area in pixels.iscrowdindicates whether the annotation represents a crowd region.
A polygon segmentation is a flat list of alternating x, y coordinates.
{
"images": [
{"id": 1, "file_name": "street_001.jpg", "width": 640, "height": 480}
],
"categories": [
{"id": 1, "name": "car"}
],
"annotations": [
{
"id": 1,
"image_id": 1,
"category_id": 1,
"bbox": [50, 40, 100, 50],
"segmentation": [[50, 40, 150, 40, 150, 90, 50, 90]],
"area": 5000,
"iscrowd": 0
}
]
}COCO category IDs do not have to start at zero. Always use the category mapping from the file instead of assuming that an ID equals a class index.
YOLO: compact files for each image
A standard YOLO detection label has one line per object:
class_id x_center y_center box_width box_heightAll four box values are normalised to the range from 0 to 1. Normalisation divides horizontal values by image width and vertical values by image height. class_id is usually a zero-based integer.
For example, the COCO box [50, 40, 100, 50] in a 640 × 480 image becomes:
0 0.15625 0.1354167 0.15625 0.1041667The class ID is 0 because YOLO commonly stores classes in the order defined by a separate class-name file.
def coco_bbox_to_yolo(bbox, img_w, img_h):
x, y, w, h = bbox
x_center = (x + w / 2) / img_w
y_center = (y + h / 2) / img_h
return x_center, y_center, w / img_w, h / img_h
print(coco_bbox_to_yolo([50, 40, 100, 50], 640, 480))The most common conversion errors are swapping width and height, using the top-left corner as the YOLO centre, and forgetting to normalise.
Pascal VOC: readable XML records
Pascal VOC stores one XML file per image. Its object element contains a class name and an axis-aligned box:
<annotation>
<filename>street_001.jpg</filename>
<size>
<width>640</width>
<height>480</height>
</size>
<object>
<name>car</name>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>50</xmin>
<ymin>40</ymin>
<xmax>150</xmax>
<ymax>90</ymax>
</bndbox>
</object>
</annotation>The truncated field describes an object cut off by the image boundary. The difficult field indicates an object that may be hard to recognise. Tool support for these fields varies, so preserve them when converting.
Pascal VOC segmentation commonly uses a separate indexed PNG mask rather than putting polygons in the XML file. Check the target tool's expected coordinate convention, especially whether maximum coordinates are treated as inclusive.
Validation before training
Validate every converted record before using it:
- Check that class IDs map to the intended class names.
- Check that boxes have positive width and height.
- Check that boxes remain inside the image.
- Draw several converted labels over their images.
- Compare object counts before and after conversion.
- Test a few crowded, truncated, and boundary-touching objects.
Exercise
A 640 × 480 image has a COCO box [50, 40, 100, 50]. What are its YOLO centre coordinates and normalised size?
Show answer
The centre is (100, 65), so the YOLO values are x=0.15625, y=0.1354167, width=0.15625, and height=0.1041667.