Datasets:
Download scripts/materialize.py from naatee/genimage-subset-9: direct link, hf CLI and curl.
- Browser
- Download file 3.87 kB
-
https://huggingface.co/datasets/naatee/genimage-subset-9/resolve/main/scripts/materialize.py
- Command line
-
hf download hf://datasets/naatee/genimage-subset-9/scripts/materialize.py
-
curl -L -o materialize.py https://huggingface.co/datasets/naatee/genimage-subset-9/resolve/main/scripts/materialize.py
3.87 kB
| """materialize.py —— 把 parquet 展开成目录树(按字节原样写出,不重编码)。 | |
| 目录形状(与原 GenImage 一致,方便直接喂 ImageFolder): | |
| <out>/<generator>/<split>/ai/<class_name>/*.PNG # 假图 | |
| <out>/nature/<split>/<class_name>/*.JPEG # 真图池,全局只有一份 | |
| 用法: | |
| python scripts/materialize.py --root . --out ./data | |
| python scripts/materialize.py --root . --out ./data --config tiny | |
| python scripts/materialize.py --root . --out ./data --splits test --generators ADM nature | |
| python scripts/materialize.py --root . --out ./data --dry-run | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import sys | |
| from pathlib import Path | |
| import pyarrow.parquet as pq | |
| def image_bytes(cell): | |
| """兼容两种写法:struct{bytes,path} 或裸 binary。""" | |
| if isinstance(cell, dict): | |
| return cell.get("bytes") | |
| if isinstance(cell, (bytes, bytearray, memoryview)): | |
| return bytes(cell) | |
| return None | |
| def guess_ext(orig_path: str, fmt: str) -> str: | |
| suf = Path(orig_path).suffix | |
| if suf: | |
| return suf.lower() | |
| return {"PNG": ".png", "JPEG": ".jpg"}.get(fmt, ".bin") | |
| def main() -> None: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--root", required=True, help="仓库根目录(含 data/ 与 meta/)") | |
| ap.add_argument("--out", required=True, help="输出目录树") | |
| ap.add_argument("--config", default="default", choices=["default", "tiny"]) | |
| ap.add_argument("--splits", nargs="*", default=None) | |
| ap.add_argument("--generators", nargs="*", default=None) | |
| ap.add_argument("--limit", type=int, default=None, help="每个 (生成器, split) 最多导出多少张") | |
| ap.add_argument("--dry-run", action="store_true") | |
| args = ap.parse_args() | |
| root = Path(args.root) | |
| sub = "data" if args.config == "default" else "tiny" | |
| files = sorted((root / sub).glob("*.parquet")) | |
| if not files: | |
| sys.exit(f"[!] {root / sub} 下没有 parquet,检查 --root / --config") | |
| out = Path(args.out) | |
| total = written = 0 | |
| counts: dict[tuple, int] = {} | |
| for f in files: | |
| pf = pq.ParquetFile(f) | |
| for batch in pf.iter_batches(batch_size=128): | |
| d = batch.to_pydict() | |
| for i in range(batch.num_rows): | |
| split = d["split"][i] | |
| gen = d["generator"][i] | |
| if args.splits and split not in args.splits: | |
| continue | |
| if args.generators and gen not in args.generators: | |
| continue | |
| key = (gen, split) | |
| if args.limit and counts.get(key, 0) >= args.limit: | |
| continue | |
| label = int(d["label"][i]) | |
| cls = str(d["class_name"][i] or f"class_{d['class_id'][i]}").replace("/", "_") | |
| orig = d["image_path"][i] | |
| stem = Path(orig).stem | |
| ext = guess_ext(orig, d["format"][i]) | |
| branch = "ai" if label == 1 else "nature" | |
| dst = out / gen / split / branch / cls / f"{stem}{ext}" | |
| total += 1 | |
| if args.dry_run: | |
| continue | |
| dst.parent.mkdir(parents=True, exist_ok=True) | |
| b = image_bytes(d["image"][i]) | |
| if b is None: | |
| continue | |
| dst.write_bytes(b) | |
| written += 1 | |
| counts[key] = counts.get(key, 0) + 1 | |
| print(f"\r 处理 {f.name} … 已导出 {written:,}", end="", flush=True) | |
| print() | |
| if args.dry_run: | |
| print(f"[dry-run] 将导出 {total:,} 张到 {out}") | |
| return | |
| print(f"完成:{written:,} 张 -> {out.resolve()}") | |
| for (gen, split), n in sorted(counts.items()): | |
| print(f" {gen:10s} {split:6s} {n:>7,}") | |
| if __name__ == "__main__": | |
| main() | |