RuoliuYang's picture
Upload folder using huggingface_hub
e3cb0cb verified
Raw
History Blame Contribute Delete
3.35 kB
#!/usr/bin/env python
"""Prepare ULVR_v2_clean scene_graph parquet for Monet step1 (precompute_teacher_reps).
Reads scene_graph(+val) parquet (cols: sample_id, category, source_dataset, question,
answer, input_image, intermediate_image_1..3, num_intermediate_steps, messages_json),
and produces:
<out_root>/train.jsonl (and val.jsonl) -- {"metadata":{dataset_name,sample_id}, "data":[...]}
<out_root>/images/input/scene_graph/*.png
<out_root>/images/intermediate/scene_graph/*.png
Image relative paths are taken verbatim from messages_json so precompute can resolve them
against --dataset_root=<out_root>.
"""
import argparse, glob, json, os
import pyarrow.parquet as pq
def run(parquet_glob, out_root, split_name):
files=sorted(glob.glob(parquet_glob))
assert files, f"no parquet matched {parquet_glob}"
os.makedirs(out_root, exist_ok=True)
jsonl_path=os.path.join(out_root, f"{split_name}.jsonl")
n=0; nimg=0
IMGCOLS=["input_image","intermediate_image_1","intermediate_image_2","intermediate_image_3"]
with open(jsonl_path,"w") as jf:
for fp in files:
t=pq.read_table(fp, columns=["sample_id","messages_json"]+IMGCOLS).to_pylist()
for r in t:
mj=json.loads(r["messages_json"])
# normalize to {metadata, data}
if "data" in mj and "metadata" in mj:
rec=mj
else:
rec={"metadata":{"dataset_name":"scene_graph","sample_id":r["sample_id"]},"data":mj if isinstance(mj,list) else mj.get("data",mj)}
rec["metadata"].setdefault("dataset_name","scene_graph")
rec["metadata"]["sample_id"]=r["sample_id"]
jf.write(json.dumps(rec,ensure_ascii=False)+"\n"); n+=1
# collect image rel-paths in order: user image -> input_image ; assistant images -> intermediate_*
inpath=None; interpaths=[]
for msg in rec["data"]:
for c in msg.get("content",[]):
if c.get("type")=="image":
if msg["role"]=="user": inpath=c["image"]
elif msg["role"]=="assistant": interpaths.append(c["image"])
def dump(relpath, cell):
nonlocal nimg
if not relpath or not cell or not isinstance(cell,dict): return
b=cell.get("bytes")
if not b: return
out=os.path.join(out_root, relpath)
os.makedirs(os.path.dirname(out), exist_ok=True)
if not os.path.exists(out) or os.path.getsize(out)!=len(b):
with open(out,"wb") as fp2: fp2.write(b)
nimg+=1
dump(inpath, r.get("input_image"))
for i,ip in enumerate(interpaths, start=1):
dump(ip, r.get(f"intermediate_image_{i}"))
print(f"[{split_name}] wrote {n} samples -> {jsonl_path} ; images written={nimg}")
if __name__=="__main__":
ap=argparse.ArgumentParser()
ap.add_argument("--train-glob", required=True)
ap.add_argument("--val-glob", default="")
ap.add_argument("--out-root", required=True)
a=ap.parse_args()
run(a.train_glob, a.out_root, "train")
if a.val_glob: run(a.val_glob, a.out_root, "val")