Skip to content

Import and export YOLO, COCO and Hugging Face datasets - #3

Merged
shellbear merged 3 commits into
mainfrom
feat/dataset-interop
Nov 28, 2026
Merged

shellbear merged 3 commits into
mainfrom
feat/dataset-interop

Conversation

@shellbear

Copy link
Copy Markdown
Owner

What this does

Two commands let a labelkit dataset trade with the rest of the ecosystem:

  • labelkit import <source> --into <dataset> brings in a YOLO, COCO or Hugging Face image dataset.
  • labelkit export <dataset> --format yolo|coco|huggingface --out <dir> writes one out.

Plus docs/datasets.md with recipes built on them: publishing on Hugging Face, keeping images in an R2/S3 bucket with only annotations.json in git, and using a large model as a teacher to label data for a small Create ML model.

Why these formats and nothing more

A survey of what small computer-vision teams use (storage tools, dataset hubs, pricing, existing manifest conventions) pointed to: Hugging Face for both private and public sharing, a plain bucket with rclone for teams that keep data in-house, and DVC as something that already works. So labelkit converts formats and leaves transfer and credentials to the tools that own them. There is deliberately no S3 client, no token handling and no sync protocol here.

Behaviour worth reviewing

  • import is built from existing parts (ImageImporter for file names, AnnotationMerge for boxes), so it inherits their guarantees: it never overwrites a file (a clashing name becomes a-2.jpg and its boxes follow), repeating it changes nothing, and a source with unusable boxes imports nothing unless --lenient.
  • Splits are merged. train/, valid/, test/ come in together, since a Create ML dataset is one folder.
  • Source images without boxes are skipped unless --negatives, because in a downloaded dataset that can mean "negative" or "unlabelled".
  • export only reads the dataset and refuses an --out that is non-empty or is the dataset folder.
  • The YOLO export puts everything under train and says so in data.yaml; splitting is left to YOLO tooling.
  • Hugging Face categories are written as names, not ids, so metadata.jsonl needs no side table. Import accepts names or ids, and both categories and category.

Tried on a real dataset

I imported COCO8 (Ultralytics' 8-image YOLO sample) from its public zip: 8 images, 30 boxes, validate clean, and the rendered boxes sit correctly on the objects. That trial found a gap, fixed here: the zip has no class names, so labels came in as class_23. --names coco8.yaml now supplies them. The README quick start uses exactly this dataset.

How it was checked

  • swift test: 196 tests, 0 failures (21 new), including a round trip through each format and reading other tools' layouts (Ultralytics split folders, segmentation rows, COCO with images in a sibling folder, Hugging Face integer categories).
  • scripts/smoke-scripted.sh now exports to each format, imports it back, and checks a repeated import leaves the file byte-identical.
  • The COCO8 trial above, run against the release binary.

Not checked:

  • Uploading to or downloading from Hugging Face. The export follows their documented image-folder layout; I did not push a repository or confirm their viewer renders it.
  • Parquet-based Hugging Face datasets. Many published detection datasets are Parquet, which import does not read; it handles the image-folder layout only.
  • A real model as teacher. The detect-then-annotate pipeline was run with a built-in Vision detector, which found little; no YOLO-to-Core-ML model was exported and tried.

Left for later

  • A checksum manifest, so a dataset whose images live in a bucket can be verified after download.
  • Reading Parquet-based Hugging Face datasets.

🤖 Generated with Claude Code

A labelkit dataset is Apple's Create ML format and nothing more; these two
commands are how it trades with everything else.

- import: reads a YOLO, COCO or Hugging Face image-folder dataset (format
  detected, splits merged), copies its images in and merges its boxes. Built
  from ImageImporter and AnnotationMerge, so it never overwrites a file (a
  clashing name is renamed and its boxes follow), repeating it changes
  nothing, and a source with unusable boxes imports nothing unless --lenient.
  --names supplies class names for a source that only has numeric ids, which
  a first trial on a public YOLO download showed is common.
- export: writes YOLO (images/, labels/, classes.txt, data.yaml), COCO
  (_annotations.coco.json) or a Hugging Face image dataset (metadata.jsonl
  plus a dataset card, ready for 'hf upload') into a new folder. Class ids
  follow the labels alphabetically; coordinates are written as short decimals.
- docs/datasets.md: recipes for publishing on Hugging Face, keeping images in
  an R2/S3 bucket with annotations.json in git, and using a large model as a
  teacher to label data for a small Create ML model
- README: a quick start that imports a public 8-image dataset

The smoke test round-trips each format and checks a repeated import leaves
the file byte-identical.
A metadata.jsonl row's file_name was joined to the folder and used if the
file existed, so a row in an untrusted download could name a file outside
the dataset (../../x.png, or an absolute path) and have it copied in. Rows
are now resolved against the listing of the source folder, so only image
files found under it are accepted; anything else is reported as missing.
COCO and YOLO already resolved images through that listing.
A correctness review, a simplification pass and a trial with real tools
found these before merging:

- symlinked images were copied as links, leaving dangling files; the file a
  link points to is copied, under the dataset's name for it
- a copy failing midway left earlier images behind, and a refused import
  still created its target folder; the folder is now created only when the
  import goes through, and a failed run removes what it copied
- source images without boxes were copied in even without --negatives; they
  are left out and counted as withoutBoxes
- overlapping same-class boxes within a source were dropped as duplicates;
  import now de-duplicates only against what the dataset already had
- an image renamed on a first import (a.jpg -> a-2.jpg) was copied again on
  the next; the earlier copy is recognised by content
- COCO file names that share a basename across folders resolved to one file;
  the path is now kept and resolved exactly
- class names: trailing comments, quoted commas and colons, doubled quotes
  and lists wrapped over lines are read correctly, so labels round-trip
- a YOLO class id of nan, 1e30, 1.5 or -1 crashed or was truncated; it is a
  reported error
- COCO sizes recorded before an EXIF rotation distorted boxes; reported
  as a problem instead
- a symlinked source folder is read; --into inside the source is refused
- export: data.yaml no longer sets a relative path (YOLO tooling resolved it
  against its own datasets folder; verified loading with Ultralytics), a
  failed export removes its folder, hidden files don't make --out non-empty,
  and a file-name clash exits 1 rather than 64
- small cleanups from the simplification pass
@shellbear
shellbear merged commit c02ac33 into main Nov 28, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant