File size: 2,290 Bytes
455fe45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Writes the situate_plus subset: one row per rendered picture, with all its questions and their ground truth; splits train and test.

    python make_situate_plus.py ../SITUATE-DIFFUSER26/data/situate_plus
"""
import json
import sys
from pathlib import Path

from datasets import Dataset, Features, Image, Sequence, Value

HERE = Path(__file__).resolve().parent
SOURCE = Path(sys.argv[1])
OUT = HERE / "situate_plus"
SHARDS = {"train": 20, "test": 2}  # parquet files of about 250 MB
PICTURE = ["category", "scene", "camera", "source"]  # the same for every question of a picture
QUESTION = {"question": Value("string"),
            "subtask": Value("string"),
            "qtype": Value("string"),
            "adversarial": Value("bool"),
            "present_items": Value("int32"),
            "groundtruth": Value("string"),
            "groundtruth_short": Value("string"),
            "groundtruth_number": Value("int32")}


def features():

    fields = {"image": Image(), "file_name": Value("string")}

    for key in PICTURE:
        fields[key] = Value("string")

    fields["questions"] = [QUESTION]

    return Features(fields)


def rows(split):

    pictures = {}

    for line in (SOURCE / f"{split}.jsonl").read_text().splitlines():
        item = json.loads(line)
        name = item["image-filename"]

        if name not in pictures:
            pictures[name] = {key: item.get(key) for key in PICTURE}  # the nuts pictures have no camera
            pictures[name]["questions"] = []

        pictures[name]["questions"].append({key: item.get(key) for key in QUESTION})  # nor groundtruth, qtype, present_items

    for name, picture in pictures.items():
        image = SOURCE / "media" / name
        yield {"image": {"bytes": image.read_bytes(), "path": name}, "file_name": name, **picture}


OUT.mkdir(exist_ok=True)

for split, shards in SHARDS.items():
    data = Dataset.from_generator(rows, gen_kwargs={"split": split}, features=features())

    for index in range(shards):
        shard = data.shard(shards, index, contiguous=True)
        shard.to_parquet(OUT / f"{split}-{index:05d}-of-{shards:05d}.parquet")

    questions = sum(len(questions) for questions in data["questions"])
    print(f"{split}: {len(data)} pictures, {questions} questions -> {OUT}")