| |
| """ |
| 从验证集中找出与 qid 158 同类型的 10 个例子: |
| - 有多组 (noun, verb) pair 形成干扰(query 描述多个可能时刻) |
| - 只有一组是 GT |
| - 模型 top-1 预测正确(IoU >= 0.5) |
| """ |
|
|
| import json |
| import argparse |
| import re |
| from pathlib import Path |
|
|
|
|
| def load_jsonl(path): |
| out = [] |
| with open(path, "r", encoding="utf-8") as f: |
| for line in f: |
| line = line.strip() |
| if not line: |
| continue |
| out.append(json.loads(line)) |
| return out |
|
|
|
|
| def iou_segment(pred, gt): |
| """pred/gt: [start, end] in seconds. Returns IoU.""" |
| p_s, p_e = float(pred[0]), float(pred[1]) |
| g_s, g_e = float(gt[0]), float(gt[1]) |
| inter_s = max(p_s, g_s) |
| inter_e = min(p_e, g_e) |
| inter = max(0, inter_e - inter_s) |
| union = (p_e - p_s) + (g_e - g_s) - inter |
| return inter / union if union > 0 else 0.0 |
|
|
|
|
| def has_multiple_pairs_interference(query): |
| """ |
| 启发式:query 是否描述多组事件/多组 pair,形成干扰。 |
| - 含 before / after / and / then / while / or 等多事件连接 |
| - 逗号分隔多动作、多名词多动词结构 |
| """ |
| q = (query or "").lower() |
| |
| if re.search(r"\b(before|after|and then|then\b|while\b|whilst| or )\b", q): |
| return True |
| |
| if re.search(r"\band\b", q) and len(q.split()) >= 6: |
| return True |
| |
| if q.count(",") >= 1 and (q.count("ing ") >= 2 or " and " in q): |
| return True |
| |
| words = q.split() |
| if len(words) >= 10 and (" is " in q or " are " in q or "ing " in q): |
| return True |
| return False |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="Find 10 examples similar to qid 158 (multi-pair, model correct)") |
| parser.add_argument("--val_jsonl", type=str, default="data/highlight_val_release.jsonl", help="Val GT jsonl") |
| parser.add_argument("--pred_jsonl", type=str, default=None, help="Pred jsonl (nms). Default: results/.../best_*_nms_thd_0.7.jsonl") |
| parser.add_argument("--results_dir", type=str, default="results/qv_internvideo2-video_tef-baseline_strict-2026-02-21-18-59-41", help="Results dir to find pred file") |
| parser.add_argument("--ref_qid", type=int, default=158, help="Reference qid (same type)") |
| parser.add_argument("--topk", type=int, default=10, help="Number of examples to output") |
| parser.add_argument("--iou_threshold", type=float, default=0.5, help="Min IoU for model correct") |
| args = parser.parse_args() |
|
|
| root = Path(__file__).resolve().parent.parent |
| val_path = root / args.val_jsonl |
| if not val_path.exists(): |
| val_path = Path(args.val_jsonl) |
| if not val_path.exists(): |
| raise FileNotFoundError(f"Val jsonl not found: {val_path}") |
|
|
| pred_path = None |
| if args.pred_jsonl: |
| pred_path = Path(args.pred_jsonl) |
| else: |
| res_dir = root / args.results_dir |
| cand = list(res_dir.glob("best_*_val_preds_nms_thd_0.7.jsonl")) |
| if cand: |
| pred_path = cand[0] |
| if not pred_path or not pred_path.exists(): |
| raise FileNotFoundError(f"Pred jsonl not found: {pred_path}") |
|
|
| val_list = load_jsonl(val_path) |
| pred_list = load_jsonl(pred_path) |
|
|
| val_by_qid = {item["qid"]: item for item in val_list} |
| pred_by_qid = {item["qid"]: item for item in pred_list} |
|
|
| |
| candidates = [] |
| for qid, gt_item in val_by_qid.items(): |
| if qid not in pred_by_qid: |
| continue |
| pred_item = pred_by_qid[qid] |
| query = gt_item.get("query", "") |
| gt_windows = gt_item.get("relevant_windows", []) |
| if not gt_windows: |
| continue |
| pred_windows = pred_item.get("pred_relevant_windows", []) |
| if not pred_windows: |
| continue |
| top1 = pred_windows[0] |
| pred_seg = [top1[0], top1[1]] |
| max_iou = max(iou_segment(pred_seg, gw) for gw in gt_windows) |
| if max_iou < args.iou_threshold: |
| continue |
| if not has_multiple_pairs_interference(query): |
| continue |
| candidates.append({ |
| "qid": qid, |
| "query": query, |
| "vid": gt_item.get("vid", ""), |
| "relevant_windows": gt_windows, |
| "pred_top1": pred_seg, |
| "pred_score": top1[2] if len(top1) >= 3 else None, |
| "max_iou": max_iou, |
| }) |
|
|
| |
| ref = next((c for c in candidates if c["qid"] == args.ref_qid), None) |
| ref_query = (ref or {}).get("query", "").lower() |
|
|
| def score_similarity(c): |
| q = c["query"].lower() |
| s = 0 |
| if "before" in ref_query and "before" in q: |
| s += 2 |
| if " and " in ref_query and " and " in q: |
| s += 1 |
| if " then " in ref_query or " then " in q: |
| s += 0.5 |
| return (s, c["max_iou"]) |
|
|
| candidates.sort(key=lambda c: (c["qid"] != args.ref_qid, -score_similarity(c)[0], -score_similarity(c)[1])) |
|
|
| |
| out = [] |
| for c in candidates: |
| if c["qid"] == args.ref_qid: |
| out.insert(0, c) |
| else: |
| out.append(c) |
| |
| seen = set() |
| unique = [] |
| for c in out: |
| if c["qid"] in seen: |
| continue |
| seen.add(c["qid"]) |
| unique.append(c) |
| selected = unique[: args.topk] |
|
|
| print(f"# Found {len(candidates)} candidates (multi-pair + model R1 correct @ IoU>={args.iou_threshold}). Selected {len(selected)} (ref_qid={args.ref_qid}):\n") |
| for i, c in enumerate(selected, 1): |
| mark = " <-- ref" if c["qid"] == args.ref_qid else "" |
| print(f"{i}. qid={c['qid']}{mark}") |
| print(f" query: {c['query']}") |
| print(f" vid: {c['vid']}") |
| print(f" GT: {c['relevant_windows']} | pred_top1: {c['pred_top1']} (score={c['pred_score']:.4f}) IoU={c['max_iou']:.3f}") |
| print() |
|
|
| out_json = root / "results" / "similar_to_qid158_examples.json" |
| out_json.parent.mkdir(parents=True, exist_ok=True) |
| with open(out_json, "w", encoding="utf-8") as f: |
| json.dump(selected, f, indent=2, ensure_ascii=False) |
| print(f"Saved {len(selected)} examples to {out_json}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|