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