Spatial457 / Spatial457.py
RyanWW's picture
updated Spatial457.py
e7816d4
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"],
}