Skip to main content
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.

Automatic format detection

When you call model.train(dataset_dir="<path>"), RF-DETR checks the following:
  1. COCO format: Looks for train/_annotations.coco.json
  2. 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.

Format structures

COCO (Common Objects in Context) format uses JSON files to store annotations in a structured format with images, categories, and annotations.

Directory structure

dataset/
├── train/
│   ├── _annotations.coco.json
│   ├── image1.jpg
│   ├── image2.jpg
│   └── ...
├── valid/
│   ├── _annotations.coco.json
│   ├── image1.jpg
│   └── ...
└── test/
    ├── _annotations.coco.json
    ├── image1.jpg
    └── ...

Annotation file structure

Each _annotations.coco.json file contains:
{
  "info": {
    "description": "Dataset description",
    "version": "1.0"
  },
  "licenses": [],
  "images": [
    {
      "id": 1,
      "file_name": "image1.jpg",
      "width": 640,
      "height": 480
    }
  ],
  "categories": [
    {
      "id": 1,
      "name": "cat",
      "supercategory": "animal"
    },
    {
      "id": 2,
      "name": "dog",
      "supercategory": "animal"
    }
  ],
  "annotations": [
    {
      "id": 1,
      "image_id": 1,
      "category_id": 1,
      "bbox": [100, 150, 200, 180],
      "area": 36000,
      "iscrowd": 0
    }
  ]
}
FieldDescription
imagesList of image metadata including id, file_name, width, height
categoriesList of object categories with id and name
annotationsList of object annotations linking images to categories
bboxBounding box in [x, y, width, height] format (top-left corner)
areaArea of the bounding box
iscrowd0 for individual objects, 1 for crowd regions

Segmentation annotations

For training segmentation models, your COCO annotations must include a segmentation key with polygon coordinates:
{
  "id": 1,
  "image_id": 1,
  "category_id": 1,
  "bbox": [100, 150, 200, 180],
  "area": 36000,
  "iscrowd": 0,
  "segmentation": [
    [100, 150, 150, 150, 200, 200, 150, 250, 100, 200]
  ]
}
The segmentation field contains a list of polygons, where each polygon is a flat list of coordinates: [x1, y1, x2, y2, x3, y3, ...].

Converting between formats

Use the supervision library to convert between COCO and YOLO formats:
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",
)

Which format should I use?

Both formats work equally well with RF-DETR. Choose based on your workflow:
ConsiderationCOCOYOLO
Annotation storageSingle JSON file per splitOne text file per image
Human readabilityJSON structure, verboseSimple text, compact
Other framework compatibilityDETR family, MMDetectionUltralytics YOLO
Segmentation supportFull polygon supportFull polygon support
Editing annotationsRequires JSON parsingSimple 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

Format detection fails

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.

Build docs developers (and LLMs) love