How to split a Pascal VOC dataset

Split a Pascal VOC dataset into train, valid and test sets in the Roboflow app or with a few lines of Python, then train RF-DETR on it.

Overview

Before you train a computer vision model, split your labeled images into three sets: train, which the model learns from; valid, which it is checked against while it trains; and test, which you hold back to measure the finished model. Keeping the sets apart is what makes the scores honest.

There are two ways to split a Pascal VOC dataset:

  • In the Roboflow app, with no code. You set the percentages and Roboflow moves the images.
  • In Python, with the open source supervision (opens in a new tab) package.

Either way, you finish by training an RF-DETR (opens in a new tab) model on the split dataset.

Step 1: Split a dataset in the Roboflow app

Create a free Roboflow account, make a project, and drag in your images and Pascal VOC annotation files. Roboflow reads the annotations and assigns each image to train, valid or test.

To change the balance, open "Versions" in your project, click "Generate New Version", and at "Train/Test Split" click "Rebalance". Set the train, valid and test percentages (70/20/10 is a strong starting point), then choose "Move as few as possible" to keep most images where they are, or "Random shuffle" to reassign them all. See Create a Dataset Version (opens in a new tab).

Generate the version, and you can train RF-DETR on it in Roboflow or export it as Pascal VOC with the splits in place.

Step 2: Install supervision

To split the dataset in code instead, install the supervision pip package:

Shell
pip install supervision

Step 3: Split into train, valid and test sets

Load the dataset into a sv.DetectionDataset and split it with DetectionDataset.split(). Each call returns two datasets, so split twice: 70% for training, then the remaining 30% into valid and test, for a 70/20/10 split.

Python
import supervision as sv

ds = sv.DetectionDataset.from_pascal_voc(
    images_directory_path="dataset/images",
    annotations_directory_path="dataset/annotations",
)

train_ds, rest_ds = ds.split(split_ratio=0.7, random_state=42, shuffle=True)
valid_ds, test_ds = rest_ds.split(split_ratio=2 / 3, random_state=42, shuffle=True)

print(len(train_ds), len(valid_ds), len(test_ds))
# 700 200 100 for a dataset of 1,000 images

random_state makes the split repeatable: the same seed puts the same images in each set every time you run it.

Step 4: Save the splits

Write each set to its own folder, in Pascal VOC:

Python
for name, split_ds in [("train", train_ds), ("valid", valid_ds), ("test", test_ds)]:
    split_ds.as_pascal_voc(
        images_directory_path=f"dataset-split/{name}/images",
        annotations_directory_path=f"dataset-split/{name}/annotations",
    )

dataset-split now holds train, valid and test folders, each with its images and XML annotations.

Step 5: Train RF-DETR on the split dataset

RF-DETR trains on COCO JSON and YOLO datasets, not Pascal VOC, so write the same three sets once more as COCO JSON:

Python
for name, split_ds in [("train", train_ds), ("valid", valid_ds), ("test", test_ds)]:
    split_ds.as_coco(
        images_directory_path=f"dataset-coco/{name}",
        annotations_path=f"dataset-coco/{name}/_annotations.coco.json",
    )

Then install the training extras and point train() at the folder:

Shell
pip install "rfdetr[train]"
Python
from rfdetr import RFDETRMedium

model = RFDETRMedium()
model.train(
    dataset_dir="dataset-coco",
    epochs=100,
    batch_size="auto",
    output_dir="output",
)

For a full walkthrough, see How to Train RF-DETR on a Custom Dataset. Or skip the setup and train RF-DETR in Roboflow on the version you generated in Step 1.

Start where you are

Speak with an AI expert

Our team will help you start solving business problems on the first call.

  • Solution architecting
  • Live demonstration
  • Pricing and specifications
  • Feasibility assessment

Over 80,000 organizations build with Roboflow.

Rivian
Pella
Chobani
USG Corporation
BNSF Railway
American Woodmark
Outcomes
Patrick Industries
Peer Robotics