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