MIPR / evaluation /utils.py
leequeue's picture
Upload folder using huggingface_hub (part 38)
1dda194 verified
Raw History Blame Contribute Delete
1.86 kB
"""Shared benchmark data and image loading."""
import json
from pathlib import Path
DEFAULT_ROOT = Path(__file__).resolve().parent.parent / "data"
QUERY_FILE = "mipr_query.jsonl"
CORPUS_FILE = "mipr_corpus.jsonl"
ANNOTATION_FILE = "mipr_annotation.jsonl"
GRADES = ("L1", "L2", "N1", "N2", "N3")
def data_root(explicit=None):
return Path(explicit) if explicit else DEFAULT_ROOT
def load_jsonl(path):
items = []
with open(path, encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
items.append(json.loads(line))
return items
def load_queries(root=None):
"""List of {"idx", "query", "image_url"} in file order."""
return load_jsonl(data_root(root) / QUERY_FILE)
def load_corpus(root=None):
"""item_id -> corpus record (title, pict_url, treecate_full_path)."""
return {d["item_id"]: d for d in load_jsonl(data_root(root) / CORPUS_FILE)}
def load_treecate(root=None):
"""item_id -> leaf-extended category path (evaluation unit of SC-R)."""
return {d["item_id"]: d["treecate_full_path"]
for d in load_jsonl(data_root(root) / CORPUS_FILE)}
def load_annotation(root=None):
"""qid -> {item_id: final_grade}, all five grades kept."""
ann = {}
for d in load_jsonl(data_root(root) / ANNOTATION_FILE):
ann[str(d["idx"])] = {
it["item_id"]: it["final_grade"] for it in d["items"]
}
return ann
def query_image_path(root, image_url):
path = data_root(root) / "images" / "query_images" / image_url
if not path.is_file():
raise FileNotFoundError(path)
return str(path.resolve())
def product_image_path(root, pict_url):
path = data_root(root) / "images" / "product_images" / pict_url
if not path.is_file():
raise FileNotFoundError(path)
return str(path.resolve())