File size: 4,227 Bytes
f79f2ee
 
 
 
 
 
bd36b5c
f79f2ee
 
 
bd36b5c
 
 
f79f2ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd36b5c
f79f2ee
 
 
 
 
 
 
 
 
 
bd36b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f79f2ee
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""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()