#!/usr/bin/env python """ 从验证集中找出“多组 pair 干扰 + 我的方法找对 + baseline 找错”的样本: - query 描述多组 (noun, verb) / 多事件(易形成干扰) - FlashVTG 模型 top-1 预测正确(IoU_new >= new_iou_thr) - baseline top-1 预测错误(IoU_base < base_iou_thr) """ 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() # 多事件连接词(与 qid158 "before" 同型) if re.search(r"\b(before|after|and then|then\b|while\b|whilst| or )\b", q): return True # "X and Y" 结构(多主体/多动作) 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 multi-pair interference examples where FlashVTG is correct but baseline is wrong" ) parser.add_argument( "--val_jsonl", type=str, default="data/highlight_val_release.jsonl", help="Val GT jsonl", ) parser.add_argument( "--new_pred_jsonl", type=str, default="FlashVTG/results/qv_internvideo2-video_tef-baseline_strict-2026-02-21-18-59-41/best_qv_internvideo2_val_preds_nms_thd_0.7.jsonl", help="FlashVTG pred jsonl (nms)", ) parser.add_argument( "--base_pred_jsonl", type=str, default="best_qv_internvideo2_val_preds_nms_thd_0.7.jsonl", help="Baseline pred jsonl (nms)", ) parser.add_argument( "--ref_qid", type=int, default=158, help="Reference qid (same type as this)" ) parser.add_argument( "--topk", type=int, default=10, help="Number of examples to output" ) parser.add_argument( "--new_iou_thr", type=float, default=0.5, help="Min IoU for FlashVTG to be considered correct", ) parser.add_argument( "--base_iou_thr", type=float, default=0.3, help="Max IoU for baseline (must be below this to be considered wrong)", ) args = parser.parse_args() root = Path(__file__).resolve().parent.parent # ---- paths ---- 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}") new_pred_path = root / args.new_pred_jsonl if not new_pred_path.exists(): new_pred_path = Path(args.new_pred_jsonl) if not new_pred_path.exists(): raise FileNotFoundError(f"New pred jsonl not found: {new_pred_path}") base_pred_path = root / args.base_pred_jsonl if not base_pred_path.exists(): base_pred_path = Path(args.base_pred_jsonl) if not base_pred_path.exists(): raise FileNotFoundError(f"Baseline pred jsonl not found: {base_pred_path}") # ---- load ---- val_list = load_jsonl(val_path) new_list = load_jsonl(new_pred_path) base_list = load_jsonl(base_pred_path) val_by_qid = {item["qid"]: item for item in val_list} new_by_qid = {item["qid"]: item for item in new_list} base_by_qid = {item["qid"]: item for item in base_list} candidates = [] for qid, gt_item in val_by_qid.items(): if qid not in new_by_qid or qid not in base_by_qid: continue new_item = new_by_qid[qid] base_item = base_by_qid[qid] query = gt_item.get("query", "") gt_windows = gt_item.get("relevant_windows", []) if not gt_windows: continue new_windows = new_item.get("pred_relevant_windows", []) base_windows = base_item.get("pred_relevant_windows", []) if not new_windows or not base_windows: continue new_top1 = new_windows[0] base_top1 = base_windows[0] new_seg = [new_top1[0], new_top1[1]] base_seg = [base_top1[0], base_top1[1]] new_iou = max(iou_segment(new_seg, gw) for gw in gt_windows) base_iou = max(iou_segment(base_seg, gw) for gw in gt_windows) # 条件 1: 我的方法正确 if new_iou < args.new_iou_thr: continue # 条件 2: baseline 错误 if base_iou >= args.base_iou_thr: continue # 条件 3: query 有多组 pair 干扰 if not has_multiple_pairs_interference(query): continue candidates.append( { "qid": qid, "query": query, "vid": gt_item.get("vid", ""), "relevant_windows": gt_windows, "new_pred_top1": new_seg, "new_score": new_top1[2] if len(new_top1) >= 3 else None, "new_iou": new_iou, "base_pred_top1": base_seg, "base_score": base_top1[2] if len(base_top1) >= 3 else None, "base_iou": base_iou, } ) # 排序:优先 ref_qid,其次 new_iou 高、base_iou 低 candidates.sort( key=lambda c: ( c["qid"] != args.ref_qid, -c["new_iou"], c["base_iou"], ) ) print( f"# Found {len(candidates)} candidates (multi-pair, FlashVTG correct IoU>={args.new_iou_thr}, baseline wrong IoU<{args.base_iou_thr})." ) # 仅用于终端打印:显示前 topk 个 selected = candidates[: args.topk] print(f"# Selected top-{len(selected)} (for printing):\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']}\n" f" FlashVTG top1: {c['new_pred_top1']} (score={c['new_score']:.4f}) IoU_new={c['new_iou']:.3f}\n" f" Baseline top1: {c['base_pred_top1']} (score={c['base_score']:.4f}) IoU_base={c['base_iou']:.3f}" ) print() # JSON 中保存全部 candidates,方便后续用 topk 控制可视化数量 out_json = root / "results" / "pair_interference_vs_baseline.json" out_json.parent.mkdir(parents=True, exist_ok=True) with open(out_json, "w", encoding="utf-8") as f: json.dump(candidates, f, indent=2, ensure_ascii=False) print(f"Saved {len(candidates)} examples to {out_json}") if __name__ == "__main__": main()