Commit ·
2248546
1
Parent(s): 94788d7
Export all splits (incl density splits) using streaming
Browse files- tools/export_coco.py +29 -40
tools/export_coco.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import argparse
|
| 2 |
from pathlib import Path
|
| 3 |
import orjson
|
| 4 |
-
from datasets import load_dataset
|
| 5 |
|
| 6 |
|
| 7 |
-
def
|
| 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)
|
|
@@ -14,9 +14,8 @@ def export_streaming(split_iter, out_dir: Path, split_name: str):
|
|
| 14 |
annotations = []
|
| 15 |
categories = None
|
| 16 |
ann_id = 1
|
| 17 |
-
n_images = 0
|
| 18 |
|
| 19 |
-
for ex in
|
| 20 |
image_id = int(ex["image_id"])
|
| 21 |
filename = ex["filename"]
|
| 22 |
width = int(ex["width"])
|
|
@@ -24,16 +23,13 @@ def export_streaming(split_iter, out_dir: Path, split_name: str):
|
|
| 24 |
|
| 25 |
(images_dir / filename).write_bytes(ex["image_bytes"])
|
| 26 |
|
| 27 |
-
images.append(
|
| 28 |
-
"id": image_id,
|
| 29 |
-
|
| 30 |
-
"width": width,
|
| 31 |
-
"height": height,
|
| 32 |
-
})
|
| 33 |
|
| 34 |
-
annos = orjson.loads(ex["coco_annotations"]
|
| 35 |
if categories is None:
|
| 36 |
-
categories = orjson.loads(ex["coco_categories"]
|
| 37 |
|
| 38 |
for a in annos:
|
| 39 |
a = dict(a)
|
|
@@ -41,46 +37,39 @@ def export_streaming(split_iter, out_dir: Path, split_name: str):
|
|
| 41 |
ann_id += 1
|
| 42 |
annotations.append(a)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
print(f"[{split_name}] exported {n_images} images...")
|
| 47 |
|
| 48 |
coco = {"images": images, "annotations": annotations, "categories": categories or []}
|
| 49 |
(ann_dir / f"instances_{split_name}.json").write_bytes(orjson.dumps(coco))
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def try_load(repo: str, config: str, split: str):
|
| 54 |
-
try:
|
| 55 |
-
return load_dataset(repo, config, split=split, streaming=True)
|
| 56 |
-
except Exception:
|
| 57 |
-
return None
|
| 58 |
|
| 59 |
|
| 60 |
def main():
|
| 61 |
ap = argparse.ArgumentParser()
|
| 62 |
-
ap.add_argument("--repo", required=True)
|
| 63 |
-
ap.add_argument("--config", required=True)
|
| 64 |
-
ap.add_argument("--out", required=True)
|
|
|
|
| 65 |
args = ap.parse_args()
|
| 66 |
|
| 67 |
out_dir = Path(args.out) / args.config
|
| 68 |
out_dir.mkdir(parents=True, exist_ok=True)
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
print(f"COCO export written to: {out_dir}")
|
| 86 |
|
|
|
|
| 1 |
import argparse
|
| 2 |
from pathlib import Path
|
| 3 |
import orjson
|
| 4 |
+
from datasets import load_dataset, get_dataset_split_names
|
| 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)
|
|
|
|
| 14 |
annotations = []
|
| 15 |
categories = None
|
| 16 |
ann_id = 1
|
|
|
|
| 17 |
|
| 18 |
+
for i, ex in enumerate(ds, 1):
|
| 19 |
image_id = int(ex["image_id"])
|
| 20 |
filename = ex["filename"]
|
| 21 |
width = int(ex["width"])
|
|
|
|
| 23 |
|
| 24 |
(images_dir / filename).write_bytes(ex["image_bytes"])
|
| 25 |
|
| 26 |
+
images.append(
|
| 27 |
+
{"id": image_id, "file_name": filename, "width": width, "height": height}
|
| 28 |
+
)
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
annos = orjson.loads(ex["coco_annotations"])
|
| 31 |
if categories is None:
|
| 32 |
+
categories = orjson.loads(ex["coco_categories"])
|
| 33 |
|
| 34 |
for a in annos:
|
| 35 |
a = dict(a)
|
|
|
|
| 37 |
ann_id += 1
|
| 38 |
annotations.append(a)
|
| 39 |
|
| 40 |
+
if i % 1000 == 0:
|
| 41 |
+
print(f"[{split_name}] exported {i} images...")
|
|
|
|
| 42 |
|
| 43 |
coco = {"images": images, "annotations": annotations, "categories": categories or []}
|
| 44 |
(ann_dir / f"instances_{split_name}.json").write_bytes(orjson.dumps(coco))
|
| 45 |
+
print(f"[{split_name}] images={len(images)} annotations={len(annotations)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
def main():
|
| 49 |
ap = argparse.ArgumentParser()
|
| 50 |
+
ap.add_argument("--repo", required=True, help="HF dataset repo id (e.g. aadityabuilds/tree-distribution-shift)")
|
| 51 |
+
ap.add_argument("--config", required=True, help="Config name (e.g. in_state_train_Karnataka__ood_Rajasthan)")
|
| 52 |
+
ap.add_argument("--out", required=True, help="Output directory (writes <out>/<config>/...)")
|
| 53 |
+
ap.add_argument("--revision", default=None, help="Optional HF revision/commit sha")
|
| 54 |
args = ap.parse_args()
|
| 55 |
|
| 56 |
out_dir = Path(args.out) / args.config
|
| 57 |
out_dir.mkdir(parents=True, exist_ok=True)
|
| 58 |
|
| 59 |
+
# Discover splits from the Hub (so we don't hardcode)
|
| 60 |
+
splits = get_dataset_split_names(args.repo, args.config, revision=args.revision)
|
| 61 |
+
print("Splits:", splits)
|
| 62 |
+
|
| 63 |
+
for split in splits:
|
| 64 |
+
# streaming=True avoids huge RAM mmap / pyarrow pressure
|
| 65 |
+
ds = load_dataset(
|
| 66 |
+
args.repo,
|
| 67 |
+
args.config,
|
| 68 |
+
split=split,
|
| 69 |
+
streaming=True,
|
| 70 |
+
revision=args.revision,
|
| 71 |
+
)
|
| 72 |
+
export_split(ds, out_dir, split)
|
| 73 |
|
| 74 |
print(f"COCO export written to: {out_dir}")
|
| 75 |
|