RF-DETR supports training on datasets in two popular formats: COCO and YOLO. The format is automatically detected based on your dataset’s directory structure — simply pass your dataset directory to the train() method.
When you call model.train(dataset_dir="<path>"), RF-DETR checks the following:
- COCO format: Looks for
train/_annotations.coco.json
- YOLO format: Looks for
data.yaml (or data.yml) and train/images/ directory
If neither format is detected, an error is raised with instructions on what’s expected.
Roboflow can export datasets in both COCO and YOLO formats. You can also explore Roboflow Universe to find pre-labeled datasets for a range of use cases.
Use the supervision library to convert between COCO and YOLO formats:
YOLO to COCO
COCO to YOLO
import supervision as sv
# Load YOLO dataset
dataset = sv.DetectionDataset.from_yolo(
images_directory_path="path/to/images",
annotations_directory_path="path/to/labels",
data_yaml_path="path/to/data.yaml",
)
# Save as COCO
dataset.as_coco(
images_directory_path="output/images",
annotations_path="output/annotations.json",
)
import supervision as sv
# Load COCO dataset
dataset = sv.DetectionDataset.from_coco(
images_directory_path="path/to/images",
annotations_path="path/to/annotations.json",
)
# Save as YOLO
dataset.as_yolo(
images_directory_path="output/images",
annotations_directory_path="output/labels",
data_yaml_path="output/data.yaml",
)
Both formats work equally well with RF-DETR. Choose based on your workflow:
| Consideration | COCO | YOLO |
|---|
| Annotation storage | Single JSON file per split | One text file per image |
| Human readability | JSON structure, verbose | Simple text, compact |
| Other framework compatibility | DETR family, MMDetection | Ultralytics YOLO |
| Segmentation support | Full polygon support | Full polygon support |
| Editing annotations | Requires JSON parsing | Simple text editing |
If you’re exporting from Roboflow or already have a dataset in one format, simply use that format. RF-DETR handles both identically.
Troubleshooting
If you see an error like Could not detect dataset format in /path/to/dataset, check:
For COCO format:
train/_annotations.coco.json exists
- The JSON file is valid
For YOLO format:
data.yaml or data.yml exists at the root
train/images/ directory exists with images
Empty annotations
COCO format: Include the image in the images array but don’t add any annotations for it.
YOLO format: Create an empty .txt file (0 bytes) for the image, or omit the label file entirely.
Class ID mismatch
COCO format: Category IDs in annotations must match IDs defined in the categories array.
YOLO format: Class IDs in label files must be valid indices (0 to nc-1) based on the names list in data.yaml.