Datasets:
Download evaluation/utils.py from leequeue/MIPR: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/datasets/leequeue/MIPR/resolve/main/evaluation/utils.py
- Command line
-
hf download hf://datasets/leequeue/MIPR/evaluation/utils.py
-
curl -L -o utils.py https://huggingface.co/datasets/leequeue/MIPR/resolve/main/evaluation/utils.py
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()) | |