situate-diffuse / make_situate_plus.py
Schnowflake's picture
geneval2, situate_plus and judge_evaluation subsets
455fe45 verified
Raw History Blame Contribute Delete
2.29 kB
"""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}")