Merging and Converting
SUMMARY
A worker run produces COCO shards. CocoShardMerger combines them into one annotation file, and DatasetConverter splits that into train, valid and test, in COCO or YOLO segmentation format. A worker does both in merge_shards(); use these classes directly to merge several runs or to export a second format.
Import
from telekinesis.illusion.dataset import CocoShardMerger, DatasetConverterCocoShardMerger
Merges the per-shard COCO files of a dataset into a single annotation file. BinPickingWorker.merge_shards() calls it for you.
merger = CocoShardMerger("output/gears_in_bins")
merged_file = merger.merge() # output/gears_in_bins/merged_coco_annotations.json
merger.preview() # optional: open the merged dataset in the built-in viewerThe merger discovers every sub-directory containing a coco_annotations.json, unions compatible categories, re-indexes image and annotation IDs to be globally unique, rewrites each file_name to <shard_dirname>/images/<frame>.<ext>, and writes merged_coco_annotations.json at the dataset root.
The per-shard files are left in place, so a merge never destroys the source data. Image paths inside the merged file resolve relative to the dataset root.
Categories must match
Shards may contain different category subsets. They cannot reuse one ID for different names or one name for different IDs. Keep each id and category_name pairing consistent across every dataset you intend to combine.
DatasetConverter
Reads merged_coco_annotations.json and writes a train, valid, and test split in COCO or YOLO format.
DatasetConverter(
merged_dataset_dir, # directory containing the merged annotations
output_dir, # destination for the split dataset
dataset_format, # "coco" | "yolo"
ratios=(0.7, 0.2, 0.1),
stratify=True,
seed=42,
merged_filename="merged_coco_annotations.json",
).split()Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
merged_dataset_dir | str | Path | required | Directory containing the merged annotation file. |
output_dir | str | Path | required | Destination for the split dataset. Created if missing. |
dataset_format | str | required | "coco" or "yolo". |
ratios | (float, float, float) | (0.7, 0.2, 0.1) | Train, valid, and test fractions. Must be non-negative and sum to 1.0. |
stratify | bool | True | Stratify by each image's dominant annotation category. False shuffles randomly. |
seed | int | 42 | RNG seed, for reproducible splits. |
merged_filename | str | "merged_coco_annotations.json" | Override when the merged file uses a different name. |
split() returns the output directory path.
When a stratum is too small to split at the requested ratio, the converter logs a warning and falls back to a random shuffle for that step, rather than failing the run.
COCO Output
RF-DETR-compatible, and readable by any COCO trainer:
<output_dir>/
├── train/
│ ├── _annotations.coco.json
│ ├── 000001.<ext>
│ └── ...
├── valid/
│ ├── _annotations.coco.json
│ └── ...
└── test/
├── _annotations.coco.json
└── ...Images are renamed to {img_id:06d}.<ext>. Every split keeps the source info, licenses, and the full categories list, so all three share identical category definitions.
YOLO Output
Ultralytics YOLO segmentation layout:
<output_dir>/
├── data.yaml
├── images/
│ ├── train/000001.<ext> ...
│ ├── val/ ...
│ └── test/ ...
└── labels/
├── train/000001.txt ...
├── val/ ...
└── test/ ...Each label line is class x1 y1 x2 y2 ... xn yn, with coordinates normalized to [0, 1]. RLE segmentations are decoded and the largest external contour is kept; multi-polygon annotations also keep only their largest polygon.
data.yaml follows the Ultralytics convention (path, train, val, test, nc, names). Class indices follow the category IDs in ascending order, and the YOLO validation split is named val (the COCO export calls it valid).

Usage
from telekinesis.illusion.dataset import DatasetConverter
DatasetConverter(
merged_dataset_dir="output/gears_in_bins",
output_dir="output/gears_in_bins/coco_format",
dataset_format="coco",
).split()This exports a worker dataset that was generated with dataset_format: yolo a second time, as COCO. Setting dataset.writer.dataset_format in the worker YAML runs exactly this step as part of merge_shards(), with split_ratios, stratify and split_seed from the same section.