Datasets:
Add files using upload-large-folder tool
Browse files- MixBench25.py +36 -52
MixBench25.py
CHANGED
|
@@ -1,96 +1,80 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
-
import csv
|
| 4 |
import datasets
|
| 5 |
|
| 6 |
-
_DESCRIPTION = """
|
| 7 |
-
MixBench is a benchmark for mixed-modality retrieval across text, image, and image+text corpora.
|
| 8 |
-
Each config corresponds to a dataset (e.g., MSCOCO), and defines splits: query, corpus, mixed_corpus, and qrel.
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
_HOMEPAGE = "https://huggingface.co/datasets/mixed-modality-search/MixBench25"
|
| 12 |
|
|
|
|
| 13 |
_SUBSETS = ["MSCOCO", "Google_WIT", "VisualNews", "OVEN"]
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
class MixBench(datasets.GeneratorBasedBuilder):
|
| 16 |
-
BUILDER_CONFIGS = [
|
| 17 |
-
datasets.BuilderConfig(
|
| 18 |
-
name=subset,
|
| 19 |
-
version=datasets.Version("1.0.0"),
|
| 20 |
-
description=f"MixBench subset: {subset}"
|
| 21 |
-
) for subset in _SUBSETS
|
| 22 |
-
]
|
| 23 |
|
| 24 |
def _info(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return datasets.DatasetInfo(
|
| 26 |
description=_DESCRIPTION,
|
|
|
|
| 27 |
homepage=_HOMEPAGE,
|
| 28 |
-
features=datasets.Features({
|
| 29 |
-
"query_id": datasets.Value("string"),
|
| 30 |
-
"corpus_id": datasets.Value("string"),
|
| 31 |
-
"text": datasets.Value("string"),
|
| 32 |
-
"image": datasets.Value("string"),
|
| 33 |
-
"score": datasets.Value("int32"),
|
| 34 |
-
}),
|
| 35 |
)
|
| 36 |
|
| 37 |
def _split_generators(self, dl_manager):
|
| 38 |
-
subset
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
data_dir = os.path.join(dl_manager.download_and_extract(f"{subset}"))
|
| 42 |
|
| 43 |
return [
|
| 44 |
datasets.SplitGenerator(
|
| 45 |
name="query",
|
| 46 |
-
gen_kwargs={"path": os.path.join(
|
| 47 |
),
|
| 48 |
datasets.SplitGenerator(
|
| 49 |
name="corpus",
|
| 50 |
-
gen_kwargs={"path": os.path.join(
|
| 51 |
),
|
| 52 |
datasets.SplitGenerator(
|
| 53 |
name="mixed_corpus",
|
| 54 |
-
gen_kwargs={"path": os.path.join(
|
| 55 |
),
|
| 56 |
datasets.SplitGenerator(
|
| 57 |
name="qrel",
|
| 58 |
-
gen_kwargs={"path": os.path.join(
|
| 59 |
),
|
| 60 |
]
|
| 61 |
|
| 62 |
-
|
| 63 |
def _generate_examples(self, path, split):
|
| 64 |
-
if split == "
|
| 65 |
with open(path, encoding="utf-8") as f:
|
| 66 |
for idx, line in enumerate(f):
|
| 67 |
-
|
| 68 |
yield idx, {
|
| 69 |
-
"query_id":
|
| 70 |
-
"corpus_id":
|
| 71 |
-
"text":
|
| 72 |
-
"image":
|
| 73 |
-
"score":
|
| 74 |
}
|
| 75 |
-
|
| 76 |
with open(path, encoding="utf-8") as f:
|
| 77 |
for idx, line in enumerate(f):
|
| 78 |
-
|
| 79 |
-
yield idx, {
|
| 80 |
-
"query_id": "",
|
| 81 |
-
"corpus_id": item.get("corpus_id", ""),
|
| 82 |
-
"text": item.get("text", ""),
|
| 83 |
-
"image": item.get("image", ""),
|
| 84 |
-
"score": 0,
|
| 85 |
-
}
|
| 86 |
-
elif split == "qrel":
|
| 87 |
-
with open(path, encoding="utf-8") as f:
|
| 88 |
-
reader = csv.DictReader(f, delimiter="\t")
|
| 89 |
-
for idx, row in enumerate(reader):
|
| 90 |
yield idx, {
|
| 91 |
"query_id": row.get("query_id", ""),
|
| 92 |
"corpus_id": row.get("corpus_id", ""),
|
| 93 |
-
"text": "",
|
| 94 |
-
"image": "",
|
| 95 |
-
"score":
|
| 96 |
}
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
| 3 |
import datasets
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
_HOMEPAGE = "https://huggingface.co/datasets/mixed-modality-search/MixBench25"
|
| 6 |
|
| 7 |
+
|
| 8 |
_SUBSETS = ["MSCOCO", "Google_WIT", "VisualNews", "OVEN"]
|
| 9 |
|
| 10 |
+
class MixBenchConfig(datasets.BuilderConfig):
|
| 11 |
+
def __init__(self, name, **kwargs):
|
| 12 |
+
if name not in _SUBSETS:
|
| 13 |
+
raise ValueError(f"Unknown subset: {name}. Choose from {_SUBSETS}")
|
| 14 |
+
super().__init__(name=name, version=datasets.Version("1.0.0"), **kwargs)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
class MixBench(datasets.GeneratorBasedBuilder):
|
| 18 |
+
BUILDER_CONFIGS = [MixBenchConfig(name=subset) for subset in _SUBSETS]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def _info(self):
|
| 21 |
+
features = datasets.Features({
|
| 22 |
+
"query_id": datasets.Value("string"),
|
| 23 |
+
"corpus_id": datasets.Value("string"),
|
| 24 |
+
"text": datasets.Value("string"),
|
| 25 |
+
"image": datasets.Value("string"),
|
| 26 |
+
"score": datasets.Value("int32"),
|
| 27 |
+
})
|
| 28 |
return datasets.DatasetInfo(
|
| 29 |
description=_DESCRIPTION,
|
| 30 |
+
features=features,
|
| 31 |
homepage=_HOMEPAGE,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
def _split_generators(self, dl_manager):
|
| 35 |
+
# download entire repo root and go to current subset folder
|
| 36 |
+
data_dir = dl_manager.download_and_extract(".")
|
| 37 |
+
subset_dir = os.path.join(data_dir, self.config.name)
|
|
|
|
| 38 |
|
| 39 |
return [
|
| 40 |
datasets.SplitGenerator(
|
| 41 |
name="query",
|
| 42 |
+
gen_kwargs={"path": os.path.join(subset_dir, "queries.jsonl"), "split": "query"},
|
| 43 |
),
|
| 44 |
datasets.SplitGenerator(
|
| 45 |
name="corpus",
|
| 46 |
+
gen_kwargs={"path": os.path.join(subset_dir, "corpus.jsonl"), "split": "corpus"},
|
| 47 |
),
|
| 48 |
datasets.SplitGenerator(
|
| 49 |
name="mixed_corpus",
|
| 50 |
+
gen_kwargs={"path": os.path.join(subset_dir, "mixed_corpus.jsonl"), "split": "mixed_corpus"},
|
| 51 |
),
|
| 52 |
datasets.SplitGenerator(
|
| 53 |
name="qrel",
|
| 54 |
+
gen_kwargs={"path": os.path.join(subset_dir, "qrels", "qrels.tsv"), "split": "qrel"},
|
| 55 |
),
|
| 56 |
]
|
| 57 |
|
|
|
|
| 58 |
def _generate_examples(self, path, split):
|
| 59 |
+
if split == "qrel":
|
| 60 |
with open(path, encoding="utf-8") as f:
|
| 61 |
for idx, line in enumerate(f):
|
| 62 |
+
qid, _, did, score = line.strip().split()
|
| 63 |
yield idx, {
|
| 64 |
+
"query_id": qid,
|
| 65 |
+
"corpus_id": did,
|
| 66 |
+
"text": "",
|
| 67 |
+
"image": "",
|
| 68 |
+
"score": int(score),
|
| 69 |
}
|
| 70 |
+
else:
|
| 71 |
with open(path, encoding="utf-8") as f:
|
| 72 |
for idx, line in enumerate(f):
|
| 73 |
+
row = json.loads(line)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
yield idx, {
|
| 75 |
"query_id": row.get("query_id", ""),
|
| 76 |
"corpus_id": row.get("corpus_id", ""),
|
| 77 |
+
"text": row.get("text", ""),
|
| 78 |
+
"image": row.get("image", ""),
|
| 79 |
+
"score": 0,
|
| 80 |
}
|