Datasets:
File size: 2,511 Bytes
d6e83e9 e7816d4 d6e83e9 |
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 |
import os
import json
import datasets
class Spatial457(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="L1_single"),
datasets.BuilderConfig(name="L2_objects"),
datasets.BuilderConfig(name="L3_2d_spatial"),
datasets.BuilderConfig(name="L4_occ"),
datasets.BuilderConfig(name="L4_pose"),
datasets.BuilderConfig(name="L5_6d_spatial"),
datasets.BuilderConfig(name="L5_collision"),
]
def _info(self):
return datasets.DatasetInfo(
description="Spatial457: A multi-task spatial visual question answering dataset.",
features=datasets.Features({
"image": datasets.Image(), # 自动加载图片
"image_filename": datasets.Value("string"),
"question": datasets.Value("string"),
"answer": datasets.Value("string"), # 会自动处理 True / False / str
"question_index": datasets.Value("int32"),
"program": datasets.Sequence(
{
"type": datasets.Value("string"),
"inputs": datasets.Sequence(datasets.Value("int32")),
"_output": datasets.Value("string"),
"value_inputs": datasets.Sequence(datasets.Value("string")),
}
),
}),
supervised_keys=None,
)
def _split_generators(self, dl_manager):
data_dir = dl_manager.manual_dir # expects local `images/` and `questions/`
task_json = os.path.join(data_dir, "questions", f"{self.config.name}.json")
image_dir = os.path.join(data_dir, "images")
return [
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={"json_file": task_json, "image_dir": image_dir}
)
]
def _generate_examples(self, json_file, image_dir):
with open(json_file, "r", encoding="utf-8") as f:
all_data = json.load(f)["questions"]
for idx, q in enumerate(all_data):
img_path = os.path.join(image_dir, q["image_filename"])
yield idx, {
"image": img_path,
"image_filename": q["image_filename"],
"question": q["question"],
"answer": str(q["answer"]),
"question_index": q["question_index"],
"program": q["program"],
}
|