Datasets:
Download scripts/load_example.py from naatee/genimage-subset-9: direct link, hf CLI and curl.
- Browser
- Download file 5.59 kB
-
https://huggingface.co/datasets/naatee/genimage-subset-9/resolve/main/scripts/load_example.py
- Command line
-
hf download hf://datasets/naatee/genimage-subset-9/scripts/load_example.py
-
curl -L -o load_example.py https://huggingface.co/datasets/naatee/genimage-subset-9/resolve/main/scripts/load_example.py
5.59 kB
| """load_example.py —— 五个场景的可跑示例,直接抄进你的训练脚本。 | |
| python scripts/load_example.py --repo naatee/genimage-subset-9 # 跑全部场景(用 tiny) | |
| python scripts/load_example.py --repo naatee/genimage-subset-9 --full # 用全量(要下载几十 GB) | |
| 注意:这里只用 filter 取子集,不重新切分。split 已经冻结在数据里。 | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| GEN = { | |
| "ADM": "ADM", | |
| "BigGAN": "BigGAN", | |
| "GLIDE": "GLIDE", | |
| "Midjourney": "Midjourney", | |
| "SD14": "stable_diffusion_v_1_4", | |
| "SD15": "stable_diffusion_v_1_5", | |
| "VQDM": "VQDM", | |
| "Wukong": "wukong", | |
| } | |
| DIFFUSION = {"ADM", "GLIDE", "Midjourney", "SD14", "SD15", "VQDM", "Wukong"} | |
| GAN = {"BigGAN"} | |
| def head(ds, name, k=3): | |
| print(f" {name}: {len(ds):,} 张") | |
| for i in range(min(k, len(ds))): | |
| r = ds[i] | |
| print(f" {r['generator']:24s} {r['split']:5s} label={r['label']} " | |
| f"{r['width']}x{r['height']} {r['format']:4s} {r['class_name']}") | |
| def main() -> None: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--repo", default="naatee/genimage-subset-9") | |
| ap.add_argument("--full", action="store_true", help="用全量配置(会真的下载)") | |
| ap.add_argument("--cache", default=None, help="指定 HF_HOME,建议放 D 盘") | |
| args = ap.parse_args() | |
| import os | |
| if args.cache: | |
| os.environ["HF_HOME"] = args.cache | |
| print(f"HF_HOME = {os.environ.get('HF_HOME', '(默认 ~/.cache/huggingface)')}\n") | |
| from datasets import load_dataset | |
| name = args.repo if args.full else args.repo | |
| if os.path.isdir(name): | |
| # 本地目录也能跑(上传前自测 / 验收) | |
| ds = load_dataset("parquet", data_dir=os.path.join(name, "data" if args.full else "tiny")) | |
| else: | |
| ds = load_dataset(name) if args.full else load_dataset(name, "tiny") | |
| print("顶层 splits:", {k: len(v) for k, v in ds.items()}, "\n") | |
| # HF 把文件名里的 val 规范化成 validation,两种写法都兼容 | |
| val_key = "validation" if "validation" in ds else "val" | |
| # ---------------------------------------------------------------- P1 在域 | |
| G = GEN["SD14"] | |
| print(f"[P1] 在域:{G}") | |
| head(ds["train"].filter(lambda x: x["generator"] in (G, "nature")), "train") | |
| head(ds["test"].filter(lambda x: x["generator"] in (G, "nature")), "test") | |
| # ---------------------------------------------------------------- P2 LOGO | |
| held = GEN["ADM"] | |
| print(f"\n[P2] 留一生成器:留出 {held}") | |
| head(ds["train"].filter(lambda x: x["generator"] != held), "train") | |
| head(ds[val_key].filter(lambda x: x["generator"] in (held, "nature")), val_key) | |
| head(ds["test"].filter(lambda x: x["generator"] in (held, "nature")), "test") | |
| # ---------------------------------------------------------------- P2 族间 | |
| print("\n[P2] 族间:GAN -> diffusion") | |
| head(ds["train"].filter(lambda x: x["family"] == "GAN"), "train(GAN)") | |
| head(ds["test"].filter(lambda x: x["family"] == "diffusion"), "test(diffusion)") | |
| # ---------------------------------------------------------------- P5 偏差审计 | |
| print() | |
| print("[P5] 偏差审计:尺寸 + JPEG 质量对齐(on-the-fly,不要落盘)") | |
| print(" 数据保留原始字节,捷径仍然在——这正是要审计的对象。") | |
| print(" 1) 逐图 jpeg_qf 在 meta/index.parquet 里(不在图像 parquet 里)") | |
| print(" qf = dict(zip(idx['md5'], idx['jpeg_qf']))") | |
| print(" 2) 尺寸对齐:ADM 假图是 256,真图取 230~282") | |
| print(" real = ds['test'].filter(lambda x: x['generator']=='nature'") | |
| print(" and 230 <= x['width'] <= 282)") | |
| print(" 3) QF 对齐:transform 里对 real 与 fake 都做一次 JPEG(q=96) 重编码") | |
| print(" 4) real-to-real 控制臂:real 也走同样重编码,用来区分") | |
| print(" 「捷径被移除」和「痕迹被重编码破坏」") | |
| print(" ⚠ 假图尺寸随生成器不同(BigGAN 128 / ADM·VQDM·GLIDE 256 /") | |
| print(" SD·Wukong 512 / MJ 1024),所以要逐生成器选真图子池。") | |
| # ---------------------------------------------------------------- P4 鲁棒性 | |
| print("\n[P4] 鲁棒性:test 集冻结,扰动在 transform 里做(不要落盘)") | |
| test = ds["test"] | |
| print(f" test 共 {len(test):,} 张;JPEG/缩放/裁剪 都在 __getitem__ 里施加") | |
| # ---------------------------------------------------------------- 转 torch | |
| print("\n[可选] 直接转 torch 格式:") | |
| print(" ds = ds.with_format('torch') # image 会变成 tensor(先 resize/crop)") | |
| print(" 或用 torchvision DataLoader + collate_fn 自己处理 PIL") | |
| # ---------------------------------------------------------------- 统计 | |
| print("\n[统计] 逐生成器 × split 计数(用 index 更快,不必下载图像):") | |
| try: | |
| import pyarrow.parquet as pq | |
| import pandas as pd | |
| from huggingface_hub import hf_hub_download | |
| p = hf_hub_download(args.repo, "meta/index.parquet", repo_type="dataset") | |
| idx = pq.read_table(p, columns=["generator", "label", "split"]).to_pandas() | |
| print(pd.crosstab(idx["generator"], idx["split"]).to_string()) | |
| except Exception as e: | |
| print(f" (跳过:{type(e).__name__}: {e})") | |
| if __name__ == "__main__": | |
| main() | |