#!/usr/bin/env python3 """Score predictions on TrackingBench. python evaluate.py predictions.jsonl [--truth truth.jsonl] [--queries queries.jsonl] predictions.jsonl has one JSON object per line: {"id": , "x": , "y": , "occluded": }. x and y are pixels in the target image, with the origin at the top-left corner. An answer of occluded needs no x and y. All 2,000 queries are scored. A query with no answer, or with an answer that cannot be read, counts as wrong everywhere. Positions are compared at the TAP-Vid 256 x 256 scale: a pixel offset is multiplied by 255 / (size - 1). AJ average Jaccard over the thresholds 1, 2, 4, 8 and 16 px (position and visibility together) APD average share of visible targets predicted visible and within each threshold OA occlusion accuracy: the share of queries with the right visible / occluded call EPE mean end-point error over visible targets, capped at 32 px (a visible target answered occluded counts 32) """ import argparse import json import math from collections import defaultdict from pathlib import Path THRESHOLDS = (1, 2, 4, 8, 16) EPE_CAP = 32.0 def read(path): with open(path) as stream: return [json.loads(line) for line in stream if line.strip()] def score(truths, answers): by_id = {a["id"]: a for a in answers} rows = [] # (truth visible, predicted visible, distance, answer missing) for truth in truths: answer = by_id.get(truth["id"]) readable = answer is not None and isinstance(answer.get("occluded"), bool) and ( answer["occluded"] or (answer.get("x") is not None and answer.get("y") is not None)) hidden = bool(answer["occluded"]) if readable else False distance = math.inf if readable and not hidden and not truth["occluded"]: sx, sy = 255.0 / (truth["width"] - 1), 255.0 / (truth["height"] - 1) distance = math.hypot((float(answer["x"]) - truth["x"]) * sx, (float(answer["y"]) - truth["y"]) * sy) rows.append((not truth["occluded"], readable and not hidden, distance, not readable)) visible = [r for r in rows if r[0]] out = {"queries": len(rows), "answered": sum(not r[3] for r in rows), "visible_queries": len(visible)} within, jaccard = [], [] for t in THRESHOLDS: within.append(sum(r[2] < t for r in visible) / len(visible) if visible else float("nan")) true_pos = sum(r[0] and r[1] and r[2] < t for r in rows) false_pos = sum(r[1] and (not r[0] or not r[2] < t) for r in rows) + sum(r[3] and not r[0] for r in rows) false_neg = sum(r[0] and not (r[1] and r[2] < t) for r in rows) jaccard.append(true_pos / max(true_pos + false_pos + false_neg, 1)) out[f"d_{t}"] = round(100 * within[-1], 2) out["apd"] = round(100 * sum(within) / len(within), 2) out["aj"] = round(100 * sum(jaccard) / len(jaccard), 2) out["oa"] = round(100 * sum(r[0] == r[1] and not r[3] for r in rows) / len(rows), 2) out["epe"] = round(sum(min(r[2], EPE_CAP) for r in visible) / len(visible), 2) if visible else float("nan") return out def main(): here = Path(__file__).resolve().parent parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("predictions") parser.add_argument("--truth", default=here / "truth.jsonl") parser.add_argument("--queries", default=here / "queries.jsonl", help="adds scores by domain and by source") args = parser.parse_args() truths, answers = read(args.truth), read(args.predictions) result = {"overall": score(truths, answers)} if Path(args.queries).exists(): queries = {q["id"]: q for q in read(args.queries)} for key in ("domain", "source"): groups = defaultdict(list) for truth in truths: groups[queries[truth["id"]][key]].append(truth) result[f"by_{key}"] = {name: score(group, answers) for name, group in sorted(groups.items())} print(json.dumps(result, indent=1)) if __name__ == "__main__": main()