data / code /prepare_data.py
afs07fda89sdfas90's picture
Add zoom-in crops and source-mapping images; drop cue-removed and unmasked images
bd36b5c verified
Raw History Blame Contribute Delete
4.23 kB
"""Lay out the released images the way the experiment scripts expect.
Run once from this folder after downloading the dataset:
python prepare_data.py
Creates, in data/ next to this script:
combined_output/ <id>.<ext>, <id>_occluded.png, annotations.json
combined_output_blue/ <id>_occluded.png, annotations.json (App. C box style)
annotated/ <id>.<ext> (highlighted cue region)
Image categories (3).xlsx image id / direct or inferential / subcategory
and, for the zoomed-in recognition and source-mapping probes,
sec4_2_why_vlms_fail/stage2_zoomed_recognition/ crops/, crop_meta.json
sec4_2_why_vlms_fail/stage3_source_mapping/ annotated_reverse/, regions_reverse.json
Files are hard-linked where possible, copied otherwise.
"""
import json
import os
import shutil
import openpyxl
HERE = os.path.dirname(os.path.abspath(__file__))
DATASET = os.path.dirname(HERE)
def link(src_rel, dst):
src = os.path.join(DATASET, src_rel)
os.makedirs(os.path.dirname(dst), exist_ok=True)
if os.path.exists(dst):
os.remove(dst)
try:
os.link(src, dst)
except OSError:
shutil.copyfile(src, dst)
def main():
records = json.load(open(os.path.join(DATASET, "annotations.json")))
# Annotation format read by the experiment scripts
legacy = [{
"id": r["id"],
"original_file": os.path.basename(r["images"]["original"]),
"url": r["source_url"],
"answer": r["answer"],
"reasoning": r["annotator_note"],
"physical": r["type"] == "direct",
"annotations": r["occluders"],
} for r in records]
out = os.path.join(HERE, "data")
dirs = {
"main": os.path.join(out, "combined_output"),
"blue": os.path.join(out, "combined_output_blue"),
"highlighted": os.path.join(out, "annotated"),
}
for r in records:
rid, im = r["id"], r["images"]
link(im["original"], os.path.join(dirs["main"], os.path.basename(im["original"])))
link(im["occluded"], os.path.join(dirs["main"], f"{rid}_occluded.png"))
link(im["blue_box"], os.path.join(dirs["blue"], f"{rid}_occluded.png"))
if im["highlighted_cue"]:
link(im["highlighted_cue"], os.path.join(dirs["highlighted"], os.path.basename(im["highlighted_cue"])))
for key in ("main", "blue"):
with open(os.path.join(dirs[key], "annotations.json"), "w") as f:
json.dump(legacy, f, indent=2, ensure_ascii=False)
wb = openpyxl.Workbook()
ws = wb.active
ws.append(["image id", "direct or inferential", "subcategory"])
for r in records:
ws.append([r["id"], r["type"], r["fine_category"]])
wb.save(os.path.join(out, "Image categories (3).xlsx"))
stage2 = os.path.join(HERE, "sec4_2_why_vlms_fail", "stage2_zoomed_recognition")
crop_meta = []
for r in records:
for c in r["cue_crops"]:
crop_file = f"crops/{os.path.basename(c['image'])}"
link(c["image"], os.path.join(stage2, crop_file))
crop_meta.append({"id": r["id"], "ground_truth": r["answer"], "label": "", "crop_box": c["box"],
"crop_file": crop_file, "original_file": os.path.basename(r["images"]["original"])})
with open(os.path.join(stage2, "crop_meta.json"), "w") as f:
json.dump(crop_meta, f, indent=2, ensure_ascii=False)
stage3 = os.path.join(HERE, "sec4_2_why_vlms_fail", "stage3_source_mapping")
regions = []
for r in records:
if r["reverse_mapping"]:
link(r["reverse_mapping"]["image"], os.path.join(stage3, "annotated_reverse", f"{r['id']}.png"))
regions.append({"id": r["id"], "original_file": os.path.basename(r["images"]["original"]),
"ground_truth": r["answer"], "status": "auto", "correct_letter": r["reverse_mapping"]["answer"]})
with open(os.path.join(stage3, "regions_reverse.json"), "w") as f:
json.dump(regions, f, indent=2, ensure_ascii=False)
print(f"Prepared {len(records)} items under {out}, {len(crop_meta)} crops, {len(regions)} source-mapping images")
if __name__ == "__main__":
main()