agenthoi-eval / scripts /prepare_eval_jsons.py
ProAudience's picture
Upload folder using huggingface_hub
271f3e5 verified
Raw History Blame Contribute Delete
6.15 kB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Generate OBJ-DINO and TVA manifest JSONs from an HOI eval dataset."""
import argparse
import json
import os
import os.path as osp
_EVAL_ROOT = osp.abspath(osp.join(osp.dirname(__file__), ".."))
_DEFAULT_DATASET = osp.join(
_EVAL_ROOT, "benchmarks/datasets/hoi_crosstest_20251107.json"
)
_DEFAULT_OBJ_TABLE = osp.join(
_EVAL_ROOT, "benchmarks/datasets/prompt_translated3.json"
)
CAPTION_OBJ_NAME_FALLBACK = {
"selfcollect250509_img_1-1": "notebook",
"selfcollect250509_img_1-2": "notebook",
"selfcollect250509_img_4": "electric guitar",
"selfcollect250509_img_5-1": "tablet",
"selfcollect250509_img_5-2": "cup",
"selfcollect250509_img_7": "container",
"selfcollect250509_img_8": "smartphone",
"selfcollect250509_img_13": "laptop",
"selfcollect250509_img_14": "tablet",
"selfcollect250509_img_15": "camera",
"selfcollect250509_img_17": "paper cup",
"selfcollect250509_img_18": "glass of water",
"selfcollect250509_img_23": "shoes",
"selfcollect250509_img_25": "smartphone",
"selfcollect250509_img_27": "tablet",
"selfcollect250509_img_29-1": "handbag",
"selfcollect250509_img_29-2": "handbag",
"selfcollect250509_img_31-1": "sandal",
"selfcollect250509_img_31-2": "loafer",
"selfcollect250509_img_32": "jar",
"selfcollect250509_img_33": "tablet",
"selfcollect250509_img_38": "smartphone",
"selfcollect250509_img_42": "cup",
"selfcollect250509_img_48": "plastic bag",
"selfcollect250509_img_51-1": "compact mirror",
"selfcollect250509_img_51-2": "compact mirror",
"selfcollect250509_img_53": "acoustic guitar",
"selfcollect250509_img_54": "acoustic guitar",
"selfcollect250509_img_57": "smartphone",
"selfcollect250509_img_62": "electric guitar",
"selfcollect250509_img_67": "acoustic guitar",
"selfcollect250509_img_70": "water bottle",
"selfcollect250509_img_72": "water bottle",
"selfcollect250509_img_75": "laptop",
"selfcollect250509_img_76": "sneakers",
"selfcollect250509_img_81": "smartphone",
"selfcollect250509_img_85": "compact mirror",
"selfcollect250509_img_92": "smartphone",
"selfcollect250509_img_101": "smartphone",
"selfcollect250509_img_103": "tablet",
}
def convert_obj_table(obj_data):
return {item["id"]: item for item in obj_data}
def extract_obj_name_from_path(data):
for key in ("ref_obj_image_path", "ref_image_path"):
path = data.get(key) or ""
for marker in ("_##object##_", "_##human##_"):
if marker in path:
suffix = path.split(marker)[-1]
if suffix.endswith(".png"):
obj_raw = suffix[:-4].strip()
if obj_raw:
return obj_raw.replace("_", " ")
return None
def build_obj_dino_list(hoi_list, obj_dict, skip_missing_obj=False, fallback_obj_from_path=False):
out = []
for data in hoi_list:
video_id = data["image_id"]
parts = video_id.split("=")
obj_id = parts[1] if len(parts) >= 2 else video_id
if obj_id in obj_dict:
obj_name = obj_dict[obj_id]["obj_en"]
else:
if fallback_obj_from_path:
obj_name = extract_obj_name_from_path(data)
if obj_name is None:
obj_name = CAPTION_OBJ_NAME_FALLBACK.get(video_id)
if obj_name is None:
if skip_missing_obj:
continue
raise KeyError(
f"obj_id '{obj_id}' not in obj table (video_id={video_id})"
)
elif skip_missing_obj:
continue
else:
raise KeyError(f"obj_id '{obj_id}' not in obj table (video_id={video_id})")
out.append({
"video_id": data["image_id"],
"mp4_img": data["ref_image_path"],
"obj_img": data["ref_obj_image_path"],
"obj_name": obj_name,
})
return out
def build_tva_list(hoi_list):
return [
{"video_id": item["image_id"], "prompt": item["caption"]}
for item in hoi_list
]
def main():
parser = argparse.ArgumentParser(
description="Generate OBJ-DINO and TVA manifests from an HOI eval JSON"
)
parser.add_argument("--input", type=str, default=_DEFAULT_DATASET)
parser.add_argument("--obj_table", type=str, default=_DEFAULT_OBJ_TABLE)
parser.add_argument("--obj_dino_out", type=str, default=None)
parser.add_argument("--tva_out", type=str, default=None)
parser.add_argument("--skip_missing_obj", action="store_true")
parser.add_argument("--fallback_obj_from_path", action="store_true")
args = parser.parse_args()
if args.obj_dino_out is None:
args.obj_dino_out = osp.join(
_EVAL_ROOT, "benchmarks/manifests/obj_dino_crosstest.json"
)
if args.tva_out is None:
args.tva_out = osp.join(
_EVAL_ROOT, "benchmarks/manifests/tva_crosstest.json"
)
with open(args.input, "r", encoding="utf-8") as f:
hoi_list = json.load(f)
print(f"[prepare] loaded {len(hoi_list)} samples from {args.input}")
with open(args.obj_table, "r", encoding="utf-8") as f:
obj_dict = convert_obj_table(json.load(f))
obj_dino_list = build_obj_dino_list(
hoi_list,
obj_dict,
skip_missing_obj=args.skip_missing_obj,
fallback_obj_from_path=args.fallback_obj_from_path,
)
os.makedirs(osp.dirname(args.obj_dino_out), exist_ok=True)
with open(args.obj_dino_out, "w", encoding="utf-8") as f:
json.dump(obj_dino_list, f, ensure_ascii=False, indent=4)
print(f"[prepare] OBJ_DINO -> {args.obj_dino_out} ({len(obj_dino_list)} items)")
tva_list = build_tva_list(hoi_list)
os.makedirs(osp.dirname(args.tva_out), exist_ok=True)
with open(args.tva_out, "w", encoding="utf-8") as f:
json.dump(tva_list, f, ensure_ascii=False, indent=4)
print(f"[prepare] TVA -> {args.tva_out} ({len(tva_list)} items)")
if __name__ == "__main__":
main()