Datasets:
Upload parquet_to_raw.py with huggingface_hub
Browse files- parquet_to_raw.py +76 -0
parquet_to_raw.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Convert a parquet HF RGBD-GSD dataset back to raw image files.
|
| 2 |
+
|
| 3 |
+
Restores the original structure:
|
| 4 |
+
{out}/{split}/images/{id}.jpg
|
| 5 |
+
{out}/{split}/masks/{id}.png
|
| 6 |
+
{out}/{split}/depths/{id}.png
|
| 7 |
+
|
| 8 |
+
Usage
|
| 9 |
+
-----
|
| 10 |
+
# From HuggingFace Hub:
|
| 11 |
+
python parquet_to_raw.py --repo garrying/RGBD-GSD
|
| 12 |
+
|
| 13 |
+
# From a local save_to_disk() directory:
|
| 14 |
+
python parquet_to_raw.py --local ./rgbd_gsd_disk
|
| 15 |
+
|
| 16 |
+
# Restore only the test split:
|
| 17 |
+
python parquet_to_raw.py --repo garrying/RGBD-GSD --splits test
|
| 18 |
+
|
| 19 |
+
# Change output directory:
|
| 20 |
+
python parquet_to_raw.py --repo garrying/RGBD-GSD --out RGBD-GSD_raw
|
| 21 |
+
"""
|
| 22 |
+
import argparse
|
| 23 |
+
from pathlib import Path
|
| 24 |
+
from datasets import load_dataset, load_from_disk
|
| 25 |
+
|
| 26 |
+
SPLITS = ["train", "test"]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def save_split(ds, split: str, out_dir: Path):
|
| 30 |
+
split_dir = out_dir / split
|
| 31 |
+
img_dir = split_dir / "images"; img_dir.mkdir(parents=True, exist_ok=True)
|
| 32 |
+
mask_dir = split_dir / "masks"; mask_dir.mkdir(parents=True, exist_ok=True)
|
| 33 |
+
depth_dir = split_dir / "depths"; depth_dir.mkdir(parents=True, exist_ok=True)
|
| 34 |
+
|
| 35 |
+
for i, sample in enumerate(ds):
|
| 36 |
+
stem = sample.get("image_id") or f"{i:08d}"
|
| 37 |
+
sample["image"].save(img_dir / f"{stem}.jpg")
|
| 38 |
+
sample["mask"].save(mask_dir / f"{stem}.png")
|
| 39 |
+
sample["depth"].save(depth_dir / f"{stem}.png")
|
| 40 |
+
|
| 41 |
+
if (i + 1) % 100 == 0:
|
| 42 |
+
print(f" {i + 1}/{len(ds)}")
|
| 43 |
+
|
| 44 |
+
print(f" saved {len(ds)} samples -> {split_dir}")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def main():
|
| 48 |
+
parser = argparse.ArgumentParser(
|
| 49 |
+
description="Convert parquet RGBD-GSD back to raw image files."
|
| 50 |
+
)
|
| 51 |
+
src = parser.add_mutually_exclusive_group(required=True)
|
| 52 |
+
src.add_argument("--repo", help="HuggingFace repo ID (e.g. garrying/RGBD-GSD)")
|
| 53 |
+
src.add_argument("--local", help="Path to a save_to_disk() directory")
|
| 54 |
+
parser.add_argument("--out", default="RGBD-GSD",
|
| 55 |
+
help="Output root directory (default: RGBD-GSD)")
|
| 56 |
+
parser.add_argument("--splits", nargs="+", choices=SPLITS, default=SPLITS,
|
| 57 |
+
help="Which splits to restore (default: all)")
|
| 58 |
+
args = parser.parse_args()
|
| 59 |
+
|
| 60 |
+
out_dir = Path(args.out)
|
| 61 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 62 |
+
|
| 63 |
+
for split in args.splits:
|
| 64 |
+
print(f"\nLoading {split}...")
|
| 65 |
+
if args.repo:
|
| 66 |
+
ds = load_dataset(args.repo, split=split)
|
| 67 |
+
else:
|
| 68 |
+
ds = load_from_disk(str(Path(args.local) / split))
|
| 69 |
+
print(f" {len(ds)} samples — saving images...")
|
| 70 |
+
save_split(ds, split, out_dir)
|
| 71 |
+
|
| 72 |
+
print(f"\nDone! Raw files in: {out_dir.resolve()}")
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
main()
|