File size: 3,639 Bytes
60e7f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import argparse
import json
import os
from collections import Counter


def canonical_answer(answers):
    if not answers:
        return ""
    counts = Counter(answers)
    best = max(counts.values())
    for answer in answers:
        if counts[answer] == best:
            return answer
    return answers[0]


def ensure_symlink(src, dst):
    if os.path.islink(dst) or os.path.exists(dst):
        return
    os.symlink(src, dst)


def convert_split(src_json, image_dir, out_jsonl):
    with open(src_json, "r") as f:
        data = json.load(f)["data"]

    with open(out_jsonl, "w") as writer:
        for item in data:
            answers = item.get("answers", [])
            record = {
                "image": os.path.join(image_dir, f"{item['image_id']}.jpg"),
                "question": item["question"],
                "question_id": item["question_id"],
                "answer": canonical_answer(answers),
            }
            writer.write(json.dumps(record) + "\n")
    return data


def build_val_questions(val_data, out_path):
    payload = {
        "questions": [
            {
                "image_id": item["image_id"],
                "question": item["question"],
                "question_id": item["question_id"],
            }
            for item in val_data
        ]
    }
    with open(out_path, "w") as f:
        json.dump(payload, f)


def build_val_annotations(val_data, out_path):
    payload = {
        "annotations": [
            {
                "image_id": item["image_id"],
                "question_id": item["question_id"],
                "answers": [{"answer": ans} for ans in item.get("answers", [])],
            }
            for item in val_data
        ]
    }
    with open(out_path, "w") as f:
        json.dump(payload, f)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--official-root", type=str, default="data/textvqa_official")
    parser.add_argument("--output-root", type=str, default="data/textvqa")
    args = parser.parse_args()

    official_root = args.official_root
    output_root = args.output_root
    os.makedirs(output_root, exist_ok=True)

    train_images_src = os.path.abspath(os.path.join(official_root, "train_images"))
    test_images_src = os.path.abspath(os.path.join(official_root, "test_images"))
    train_images_dst = os.path.join(output_root, "train_images")
    test_images_dst = os.path.join(output_root, "test_images")
    ensure_symlink(train_images_src, train_images_dst)
    ensure_symlink(test_images_src, test_images_dst)

    train_json = os.path.join(official_root, "TextVQA_0.5.1_train.json")
    val_json = os.path.join(official_root, "TextVQA_0.5.1_val.json")
    test_json = os.path.join(official_root, "TextVQA_0.5.1_test.json")

    train_data = convert_split(
        train_json,
        os.path.join(output_root, "train_images"),
        os.path.join(output_root, "textvqa_train.jsonl"),
    )
    val_data = convert_split(
        val_json,
        os.path.join(output_root, "train_images"),
        os.path.join(output_root, "textvqa_val.jsonl"),
    )
    _ = convert_split(
        test_json,
        os.path.join(output_root, "test_images"),
        os.path.join(output_root, "textvqa_test.jsonl"),
    )

    build_val_questions(val_data, os.path.join(output_root, "textvqa_val_questions.json"))
    build_val_annotations(val_data, os.path.join(output_root, "textvqa_val_annotations.json"))

    print(f"wrote {len(train_data)} train samples")
    print(f"wrote {len(val_data)} val samples")
    print(f"prepared TextVQA data under {output_root}")


if __name__ == "__main__":
    main()