Scholarus commited on
Commit ·
933adf4
1
Parent(s): 7a429db
fix1
Browse files- MMB_dataset.py +0 -173
MMB_dataset.py
DELETED
|
@@ -1,173 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Hugging Face datasets loading script for the MMB Counterfactual Dataset.
|
| 3 |
-
|
| 4 |
-
This dataset contains counterfactual visual question answering examples with:
|
| 5 |
-
- Original images and counterfactual variants
|
| 6 |
-
- Questions for each image variant
|
| 7 |
-
- Answer matrices showing how each image answers each question
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
from __future__ import annotations
|
| 11 |
-
|
| 12 |
-
import csv
|
| 13 |
-
from pathlib import Path
|
| 14 |
-
from typing import Iterator
|
| 15 |
-
|
| 16 |
-
import datasets
|
| 17 |
-
from datasets import Image, Value
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
_DESCRIPTION = """
|
| 21 |
-
MMB-style counterfactual visual question answering dataset.
|
| 22 |
-
|
| 23 |
-
This dataset contains scenes with:
|
| 24 |
-
- Original images and counterfactual variants
|
| 25 |
-
- Questions for each image variant
|
| 26 |
-
- Answer matrices showing how each image answers each question
|
| 27 |
-
"""
|
| 28 |
-
|
| 29 |
-
_HOMEPAGE = "https://huggingface.co/datasets/scholo/MMB_dataset"
|
| 30 |
-
|
| 31 |
-
_LICENSE = "mit"
|
| 32 |
-
|
| 33 |
-
_CITATION = """
|
| 34 |
-
@misc{mmb_counterfactual_dataset,
|
| 35 |
-
title={MMB Counterfactual Dataset},
|
| 36 |
-
author={MMB-Dataset authors},
|
| 37 |
-
year={2026}
|
| 38 |
-
}
|
| 39 |
-
"""
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
class MMB_dataset(datasets.GeneratorBasedBuilder):
|
| 43 |
-
"""MMB counterfactual dataset."""
|
| 44 |
-
|
| 45 |
-
VERSION = datasets.Version("1.0.0")
|
| 46 |
-
|
| 47 |
-
def _info(self) -> datasets.DatasetInfo:
|
| 48 |
-
return datasets.DatasetInfo(
|
| 49 |
-
description=_DESCRIPTION,
|
| 50 |
-
features=datasets.Features(
|
| 51 |
-
{
|
| 52 |
-
# Simple scene identifier derived from the original image filename
|
| 53 |
-
"scene_id": Value("string"),
|
| 54 |
-
# Image triplet (paths resolved relative to dataset_1k_720p_2/images/)
|
| 55 |
-
"original_image": Image(decode=True),
|
| 56 |
-
"counterfactual1_image": Image(decode=True),
|
| 57 |
-
"counterfactual2_image": Image(decode=True),
|
| 58 |
-
# Questions
|
| 59 |
-
"original_question": Value("string"),
|
| 60 |
-
"counterfactual1_question": Value("string"),
|
| 61 |
-
"counterfactual2_question": Value("string"),
|
| 62 |
-
# Difficulties
|
| 63 |
-
"original_question_difficulty": Value("string"),
|
| 64 |
-
"counterfactual1_question_difficulty": Value("string"),
|
| 65 |
-
"counterfactual2_question_difficulty": Value("string"),
|
| 66 |
-
# Answer matrix (9 entries, image × question)
|
| 67 |
-
"original_image_answer_to_original_question": Value("string"),
|
| 68 |
-
"original_image_answer_to_cf1_question": Value("string"),
|
| 69 |
-
"original_image_answer_to_cf2_question": Value("string"),
|
| 70 |
-
"cf1_image_answer_to_original_question": Value("string"),
|
| 71 |
-
"cf1_image_answer_to_cf1_question": Value("string"),
|
| 72 |
-
"cf1_image_answer_to_cf2_question": Value("string"),
|
| 73 |
-
"cf2_image_answer_to_original_question": Value("string"),
|
| 74 |
-
"cf2_image_answer_to_cf1_question": Value("string"),
|
| 75 |
-
"cf2_image_answer_to_cf2_question": Value("string"),
|
| 76 |
-
}
|
| 77 |
-
),
|
| 78 |
-
homepage=_HOMEPAGE,
|
| 79 |
-
license=_LICENSE,
|
| 80 |
-
citation=_CITATION,
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
def _split_generators(
|
| 84 |
-
self, dl_manager: datasets.DownloadManager
|
| 85 |
-
) -> list[datasets.SplitGenerator]:
|
| 86 |
-
"""We expose a single train split backed by dataset_1k_720p_2/image_mapping_with_questions.csv."""
|
| 87 |
-
data_dir = Path(__file__).parent.resolve()
|
| 88 |
-
csv_path = data_dir / "dataset_1k_720p_2" / "image_mapping_with_questions.csv"
|
| 89 |
-
images_dir = data_dir / "dataset_1k_720p_2" / "images"
|
| 90 |
-
|
| 91 |
-
return [
|
| 92 |
-
datasets.SplitGenerator(
|
| 93 |
-
name=datasets.Split.TRAIN,
|
| 94 |
-
gen_kwargs={
|
| 95 |
-
"csv_path": str(csv_path),
|
| 96 |
-
"images_dir": str(images_dir),
|
| 97 |
-
},
|
| 98 |
-
)
|
| 99 |
-
]
|
| 100 |
-
|
| 101 |
-
def _generate_examples(
|
| 102 |
-
self, csv_path: str, images_dir: str
|
| 103 |
-
) -> Iterator[tuple[int, dict]]:
|
| 104 |
-
"""Yield (key, example) tuples from the wide-format CSV."""
|
| 105 |
-
csv_path = Path(csv_path)
|
| 106 |
-
images_root = Path(images_dir)
|
| 107 |
-
|
| 108 |
-
if not csv_path.exists():
|
| 109 |
-
raise FileNotFoundError(f"CSV file not found: {csv_path}")
|
| 110 |
-
|
| 111 |
-
with csv_path.open("r", encoding="utf-8", newline="") as f:
|
| 112 |
-
reader = csv.DictReader(f)
|
| 113 |
-
for idx, row in enumerate(reader):
|
| 114 |
-
# Resolve scene_id from original_image filename, e.g. scene_0000_original.png -> scene_0000
|
| 115 |
-
original_image_name = row.get("original_image", "") or ""
|
| 116 |
-
scene_id = original_image_name.split("_original", 1)[0]
|
| 117 |
-
|
| 118 |
-
def _image_field(col_name: str) -> dict | None:
|
| 119 |
-
filename = (row.get(col_name) or "").strip()
|
| 120 |
-
if not filename:
|
| 121 |
-
return None
|
| 122 |
-
path = images_root / filename
|
| 123 |
-
if not path.exists():
|
| 124 |
-
return None
|
| 125 |
-
return {"path": str(path)}
|
| 126 |
-
|
| 127 |
-
example = {
|
| 128 |
-
"scene_id": scene_id,
|
| 129 |
-
"original_image": _image_field("original_image"),
|
| 130 |
-
"counterfactual1_image": _image_field("counterfactual1_image"),
|
| 131 |
-
"counterfactual2_image": _image_field("counterfactual2_image"),
|
| 132 |
-
"original_question": row.get("original_question", ""),
|
| 133 |
-
"counterfactual1_question": row.get("counterfactual1_question", ""),
|
| 134 |
-
"counterfactual2_question": row.get("counterfactual2_question", ""),
|
| 135 |
-
"original_question_difficulty": row.get(
|
| 136 |
-
"original_question_difficulty", ""
|
| 137 |
-
),
|
| 138 |
-
"counterfactual1_question_difficulty": row.get(
|
| 139 |
-
"counterfactual1_question_difficulty", ""
|
| 140 |
-
),
|
| 141 |
-
"counterfactual2_question_difficulty": row.get(
|
| 142 |
-
"counterfactual2_question_difficulty", ""
|
| 143 |
-
),
|
| 144 |
-
"original_image_answer_to_original_question": row.get(
|
| 145 |
-
"original_image_answer_to_original_question", ""
|
| 146 |
-
),
|
| 147 |
-
"original_image_answer_to_cf1_question": row.get(
|
| 148 |
-
"original_image_answer_to_cf1_question", ""
|
| 149 |
-
),
|
| 150 |
-
"original_image_answer_to_cf2_question": row.get(
|
| 151 |
-
"original_image_answer_to_cf2_question", ""
|
| 152 |
-
),
|
| 153 |
-
"cf1_image_answer_to_original_question": row.get(
|
| 154 |
-
"cf1_image_answer_to_original_question", ""
|
| 155 |
-
),
|
| 156 |
-
"cf1_image_answer_to_cf1_question": row.get(
|
| 157 |
-
"cf1_image_answer_to_cf1_question", ""
|
| 158 |
-
),
|
| 159 |
-
"cf1_image_answer_to_cf2_question": row.get(
|
| 160 |
-
"cf1_image_answer_to_cf2_question", ""
|
| 161 |
-
),
|
| 162 |
-
"cf2_image_answer_to_original_question": row.get(
|
| 163 |
-
"cf2_image_answer_to_original_question", ""
|
| 164 |
-
),
|
| 165 |
-
"cf2_image_answer_to_cf1_question": row.get(
|
| 166 |
-
"cf2_image_answer_to_cf1_question", ""
|
| 167 |
-
),
|
| 168 |
-
"cf2_image_answer_to_cf2_question": row.get(
|
| 169 |
-
"cf2_image_answer_to_cf2_question", ""
|
| 170 |
-
),
|
| 171 |
-
}
|
| 172 |
-
|
| 173 |
-
yield idx, example
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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