Commit ·
52b353c
1
Parent(s): 72529fd
Add COCO export script
Browse files- tools/export_coco.py +81 -0
tools/export_coco.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import orjson
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def export_split(ds, out_dir: Path, split_name: str):
|
| 8 |
+
images_dir = out_dir / split_name / "images"
|
| 9 |
+
ann_dir = out_dir / split_name / "annotations"
|
| 10 |
+
images_dir.mkdir(parents=True, exist_ok=True)
|
| 11 |
+
ann_dir.mkdir(parents=True, exist_ok=True)
|
| 12 |
+
|
| 13 |
+
images = []
|
| 14 |
+
annotations = []
|
| 15 |
+
categories = None
|
| 16 |
+
ann_id = 1
|
| 17 |
+
|
| 18 |
+
for ex in ds:
|
| 19 |
+
image_id = int(ex["image_id"])
|
| 20 |
+
filename = ex["filename"]
|
| 21 |
+
width = int(ex["width"])
|
| 22 |
+
height = int(ex["height"])
|
| 23 |
+
|
| 24 |
+
# write image bytes
|
| 25 |
+
(images_dir / filename).write_bytes(ex["image_bytes"])
|
| 26 |
+
|
| 27 |
+
images.append({
|
| 28 |
+
"id": image_id,
|
| 29 |
+
"file_name": filename,
|
| 30 |
+
"width": width,
|
| 31 |
+
"height": height,
|
| 32 |
+
})
|
| 33 |
+
|
| 34 |
+
annos = orjson.loads(ex["coco_annotations"].encode("utf-8"))
|
| 35 |
+
if categories is None:
|
| 36 |
+
categories = orjson.loads(ex["coco_categories"].encode("utf-8"))
|
| 37 |
+
|
| 38 |
+
for a in annos:
|
| 39 |
+
a = dict(a)
|
| 40 |
+
a["id"] = ann_id
|
| 41 |
+
ann_id += 1
|
| 42 |
+
annotations.append(a)
|
| 43 |
+
|
| 44 |
+
coco = {
|
| 45 |
+
"images": images,
|
| 46 |
+
"annotations": annotations,
|
| 47 |
+
"categories": categories or [],
|
| 48 |
+
}
|
| 49 |
+
(ann_dir / f"instances_{split_name}.json").write_bytes(orjson.dumps(coco))
|
| 50 |
+
return len(images), len(annotations)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def main():
|
| 54 |
+
ap = argparse.ArgumentParser()
|
| 55 |
+
ap.add_argument("--repo", required=True, help="HF dataset repo id, e.g. aadityabuilds/tree-distribution-shift")
|
| 56 |
+
ap.add_argument("--config", required=True, help="config_name")
|
| 57 |
+
ap.add_argument("--out", required=True, help="output directory")
|
| 58 |
+
args = ap.parse_args()
|
| 59 |
+
|
| 60 |
+
out_dir = Path(args.out) / args.config
|
| 61 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 62 |
+
|
| 63 |
+
# streaming=False ensures export writes locally
|
| 64 |
+
train = load_dataset(args.repo, args.config, split="train", streaming=False)
|
| 65 |
+
idt = load_dataset(args.repo, args.config, split="id_test", streaming=False)
|
| 66 |
+
ood = load_dataset(args.repo, args.config, split="ood_test", streaming=False)
|
| 67 |
+
|
| 68 |
+
n_i, n_a = export_split(train, out_dir, "train")
|
| 69 |
+
print(f"[train] images={n_i} annotations={n_a}")
|
| 70 |
+
|
| 71 |
+
n_i, n_a = export_split(idt, out_dir, "id_test")
|
| 72 |
+
print(f"[id_test] images={n_i} annotations={n_a}")
|
| 73 |
+
|
| 74 |
+
n_i, n_a = export_split(ood, out_dir, "ood_test")
|
| 75 |
+
print(f"[ood_test] images={n_i} annotations={n_a}")
|
| 76 |
+
|
| 77 |
+
print(f"COCO export written to: {out_dir}")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
main()
|