Download prep_data.py from kaivoss/Clover-Preview-VL: direct link, hf CLI and curl.
- Browser
- Download file 3.94 kB
-
https://huggingface.co/kaivoss/Clover-Preview-VL/resolve/main/prep_data.py
- Command line
-
hf download hf://kaivoss/Clover-Preview-VL/prep_data.py
-
curl -L -o prep_data.py https://huggingface.co/kaivoss/Clover-Preview-VL/resolve/main/prep_data.py
3.94 kB
| """LLaVA-OneVision subsets -> single-turn image rows (no grounding/boxes): data/{train,heldout}.jsonl + data/img/.""" | |
| import argparse | |
| import io | |
| import json | |
| import os | |
| import random | |
| import re | |
| import pyarrow.parquet as pq | |
| from huggingface_hub import hf_hub_download, list_repo_files | |
| from PIL import Image | |
| REPO = "lmms-lab/LLaVA-OneVision-Data" | |
| MIX = { # subset: rows | |
| "sharegpt4v(coco)": 3000, "sharegpt4o": 1500, "allava_instruct_laion4v": 2000, "lrv_normal(filtered)": 1500, | |
| "textcaps": 1000, "textocr(gpt4v)": 1000, "st_vqa(cauldron,llava_format)": 800, | |
| "chartqa(cauldron,llava_format)": 1000, "ai2d(gpt4v)": 800, "infographic_vqa": 600, | |
| "aokvqa(cauldron,llava_format)": 1000, "tallyqa(cauldron,llava_format)": 600, "vision_flan(filtered)": 1500, | |
| "llava_wild_4v_39k_filtered": 700, | |
| } | |
| # Grounding-style targets are out of scope: drop rows whose answer carries coordinates. | |
| BOXY = re.compile(r"\[\s*0?\.\d+\s*,\s*0?\.\d+\s*,|<box>|<\|box_start\|>|\(\s*\d+\s*,\s*\d+\s*\)\s*,\s*\(\s*\d+") | |
| def first_turn(conv): | |
| for i in range(len(conv) - 1): | |
| a, b = conv[i], conv[i + 1] | |
| if a.get("from") == "human" and b.get("from") == "gpt" and "<image>" in a.get("value", ""): | |
| q = a["value"].replace("<image>", "").strip() | |
| ans = b["value"].strip() | |
| if q and ans: | |
| return q, ans | |
| # Cauldron-format subsets carry the image without an <image> tag in the text. | |
| if len(conv) >= 2 and conv[0].get("from") == "human" and conv[1].get("from") == "gpt": | |
| q, ans = conv[0]["value"].replace("<image>", "").strip(), conv[1]["value"].strip() | |
| if q and ans: | |
| return q, ans | |
| return None | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--out", default="/workspace/data") | |
| ap.add_argument("--heldout", type=int, default=300) | |
| ap.add_argument("--scale", type=float, default=1.0) | |
| args = ap.parse_args() | |
| os.makedirs(f"{args.out}/img", exist_ok=True) | |
| files = list_repo_files(REPO, repo_type="dataset") | |
| rows, stats = [], {} | |
| rng = random.Random(0) | |
| for subset, n in MIX.items(): | |
| n = int(n * args.scale) | |
| shards = sorted(f for f in files if f.startswith(subset + "/") and f.endswith(".parquet")) | |
| got = 0 | |
| for shard in shards: | |
| table = pq.read_table(hf_hub_download(REPO, shard, repo_type="dataset")) | |
| recs = table.to_pylist() | |
| rng.shuffle(recs) | |
| for r in recs: | |
| img = r.get("image") | |
| if not img or not img.get("bytes"): | |
| continue | |
| turn = first_turn(r.get("conversations") or []) | |
| if not turn or BOXY.search(turn[1]): | |
| continue | |
| try: | |
| im = Image.open(io.BytesIO(img["bytes"])) | |
| im.load() | |
| if min(im.size) < 28: | |
| continue | |
| except Exception: | |
| continue | |
| rid = f"{len(rows):06d}" | |
| path = f"{args.out}/img/{rid}.png" if im.mode in ("RGBA", "LA", "P") else f"{args.out}/img/{rid}.jpg" | |
| im.convert("RGB").save(path, quality=92) | |
| rows.append({"id": rid, "source": subset, "image": path, "question": turn[0], "answer": turn[1]}) | |
| got += 1 | |
| if got >= n: | |
| break | |
| if got >= n: | |
| break | |
| stats[subset] = got | |
| print(subset, got, flush=True) | |
| rng.shuffle(rows) | |
| held, train = rows[:args.heldout], rows[args.heldout:] | |
| for name, part in (("train", train), ("heldout", held)): | |
| with open(f"{args.out}/{name}.jsonl", "w", encoding="utf-8") as f: | |
| for r in part: | |
| f.write(json.dumps(r, ensure_ascii=False) + "\n") | |
| print(json.dumps({"train": len(train), "heldout": len(held), **stats})) | |
| if __name__ == "__main__": | |
| main() | |