Definitionadvanced
IDF1 score
Also called identity F1 score.
The F1 score of detections correctly identified after a global one-to-one matching of predicted and true trajectories, a measure of identity preservation.
The IDF1 score measures how well a multi-object tracker keeps the identities of the objects over a whole video. Each true trajectory is matched to at most one computed trajectory, once, for the whole video, by a one-to-one matching that maximizes the number of frames in which the two agree. A detection counts as correct only if it belongs to the trajectory matched to its object. IDF1 is the F1 score, the harmonic mean of precision and recall, of these identity-correct detections. Ristani et al. introduced it in 2016 together with the identity precision IDP and recall IDR. Where MOTA counts an identity switch once, in one frame, IDF1 counts every frame that carries the wrong identity.
Definition
The ground truth has true trajectories , each a set of frames with a box, and the tracker has computed trajectories . In one frame, the detections of and agree when both are present and the intersection over union of their boxes is at least a threshold , such as ; the paper also allows a distance on the ground plane, with meter. Let
and let and be the numbers of frames in which they are present. For a one-to-one matching of true to computed trajectories, the identity true positives are . The matching of IDF1 is one that makes this sum largest, and
The computation is a maximum weight matching in a bipartite graph:
IDF1(the true and the computed trajectories, the threshold Δ)- 1
for each frame, each true detection and each computed detection: if IoU ≥ Δ, add 1 to n(o, h) - 2
find a one-to-one matching μ of true to computed trajectories with the largest sum of n (Hungarian) - 3
IDTP ← that sum; IDFN ← (true detections) - IDTP; IDFP ← (computed detections) - IDTP - 4
IDP ← IDTP / (computed detections); IDR ← IDTP / (true detections); IDF1 ← 2 IDTP / (both)
The paper writes line L2 as a minimum cost problem. It adds a false negative node for each true trajectory and a false positive node for each computed one, so that a trajectory can stay unmatched. Matching to costs the frames in which they do not agree, ; leaving unmatched costs , and leaving unmatched costs . These facts follow from the definition and are proved in the correctness file of this page:
- Theorem: the two forms agree. The minimum cost of the form of the paper is , which is . So both forms give the same IDTP.
- Theorem: the bound by detection. IDF1 is at most the F1 score of the detections, where each frame uses its own best one-to-one matching. Identity can only lower the score.
- Theorem: symmetry. Swapping the roles of the ground truth and the tracker keeps IDTP and IDF1 and swaps IDP and IDR.
With trajectories on the larger side, the Hungarian algorithm of line L2 costs , and line L1 costs one IoU for each pair of detections in the same frame.
Examples and non-examples
- A swap. Two objects stand still for 6 frames, and the tracker follows them with and . After frame 4 the two identities swap. Then , , and , so the best matching is , with . There are 12 true and 12 computed detections, so and . MOTA counts 2 switches in 12 objects and gives .
- A late switch. One object over 100 frames, tracked by in the first 50 frames and by in the last 50. The best matching takes one of them, so and , while MOTA is . A tracker that keeps one identity for all 100 frames but adds one false positive gets and the same MOTA.
- Not the F1 of detections. In both examples every object is detected in every frame, so the F1 score of detections is 1. IDF1 is lower because it also asks for the right identity.
- Not a frame by frame measure. The matching is global: a computed trajectory that switches to a new object late in the video can still be matched to its first object, and the frames after the switch are errors.
- More correct boxes can lower IDF1. A computed trajectory is matched to object 1 and also covers object 2 for 3 frames in which nothing else covers object 2. Those 3 boxes are correct detections, but they count as IDFP, and object 2 stays IDFN either way. A tracker that leaves them out has the same IDTP and a smaller denominator, so a higher IDF1.
Related definitions
MOTA uses a matching in each frame and counts identity switches as events; IDF1 uses one matching for the whole video and counts frames. The two often rank trackers in different orders. HOTA also measures association over the whole video, but for each true positive pair it looks at the overlap of the two whole trajectories, and it averages over the threshold ; its authors point out that IDF1 ignores the association of the parts that are not matched, and that it can fall when the detection gets better, as in the example above. Detection F1 and precision and recall are the same formulas without identity. Trackers with appearance features, such as Deep SORT, aim to raise IDF1 by keeping identities through occlusions, and the ByteTrack paper reports IDF1 together with MOTA and HOTA. The Python code of this page computes the identity measures with exact fractions, and its tests compare the matching with all matchings, check that the form of the paper with extra nodes gives the same minimum, and check the bound by detection and the symmetry on random sequences.
Proof: The maximum overlap matching and the minimum cost form of the paper give the same IDTP, and IDF1 is at most the F1 score of detectionsfull
Notation as on the page. True trajectories have lengths , computed trajectories have lengths , and is the number of frames in which they agree. Let .
Claim 1: line L2 finds IDTP
The Hungarian algorithm on the costs , with the pairs of then removed, returns a one-to-one matching with the largest total .
Proof. Every one-to-one matching of rows and columns extends to a full assignment of the smaller side by adding pairs, and since this does not lower the total. So the largest total over matchings equals the largest total over assignments, which the Hungarian algorithm finds (its optimality is proved on the SORT proof and the Hungarian algorithm page). Removing pairs with does not change the total.
Claim 2: the form of the paper gives the same IDTP
Build the square cost matrix of size of the code. Rows are and the false positive nodes ; columns are and the false negative nodes . The costs are for , for , for , 0 for , and a cost for every other pair. Then the minimum cost of a perfect assignment is .
Proof. First, every matching of true to computed trajectories gives a perfect assignment of cost . Take the pairs of ; match each unmatched to and each unmatched to . The of the matched and the of the matched are left; there are of each, and they pair up at cost 0. Every trajectory appears in exactly one paid pair, so the cost is .
Second, every perfect assignment of cost at most is of this form. A cost of is too much, so uses only the allowed pairs. Then each goes to some or to , and each comes from some or from . The pairs of form a matching , and the cost is, as above, plus the zero pairs.
The matching with no pairs gives cost , so the minimum is at most , and by the two parts it is . Finally .
The paper links two regular nodes only when the trajectories overlap in time. Here every pair has an edge, but a pair with costs , the same as leaving both unmatched, so the minimum does not change. The tests compute both forms on random sequences.
Claim 3: IDF1 is at most the F1 score of detections
For each frame , let be the size of a largest one-to-one matching of the true and the computed detections of the frame with IoU at least , and let . Then , so .
Proof. Fix the matching of IDTP. In frame , take the pairs that agree in . Since is one-to-one, these pairs form a one-to-one matching of the detections of , and each has IoU at least . So their number is at most . Summing over the frames, the number of agreeing frames of the pairs of , which is IDTP, is at most .
Claim 4: symmetry and the harmonic mean
Exchanging the ground truth and the tracker keeps IDTP and IDF1 and swaps IDP and IDR. If , IDF1 is the harmonic mean of IDP and IDR.
Proof. IoU is symmetric, so the exchange turns the matrix into its transpose, and a matching of the transpose has the same total. So IDTP is the same, is the same, and IDF1 is the same. IDP divides IDTP by the computed detections and IDR by the true ones, so they swap. For the harmonic mean, and , so , and .
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/idf1-score/. The authors are listed in prov/QK543C.jsonl.
"""The identity measures IDP, IDR and IDF1 (Ristani et al. 2016), with IoU.
A sequence is a list of frames; a frame is a dict from an identity to a box
(x1, y1, x2, y2). A true trajectory o and a computed trajectory h agree in a
frame when both are present and IoU >= delta. n(o, h) is the number of such
frames. One global one-to-one matching of true to computed trajectories with
the largest total n gives IDTP; then
IDFN = (true detections) - IDTP, IDFP = (computed detections) - IDTP,
IDF1 = 2 IDTP / (true detections + computed detections).
The paper states the matching as a minimum cost problem with extra nodes for
the unmatched trajectories; min_cost_with_dummy_nodes builds that form, and
the tests check that both forms agree. All values are exact fractions.
"""
from fractions import Fraction
def iou(a, b):
w = min(a[2], b[2]) - max(a[0], b[0])
h = min(a[3], b[3]) - max(a[1], b[1])
inter = w * h if w > 0 and h > 0 else 0
union = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - inter
return Fraction(inter) / union if union > 0 else Fraction(0)
def hungarian_min(cost):
"""Minimum cost assignment of a rectangular matrix; exact for integers and fractions.
Returns the sorted pairs (row, column), min(rows, columns) of them."""
r = len(cost)
if r == 0 or len(cost[0]) == 0:
return []
c = len(cost[0])
flip = r > c
if flip:
cost = [list(col) for col in zip(*cost)]
r, c = c, r
inf = float("inf")
u, v = [0] * (r + 1), [0] * (c + 1)
p, way = [0] * (c + 1), [0] * (c + 1)
for i in range(1, r + 1):
p[0], j0 = i, 0
minv, used = [inf] * (c + 1), [False] * (c + 1)
while True:
used[j0] = True
i0, delta, j1 = p[j0], inf, 0
for j in range(1, c + 1):
if not used[j]:
cur = cost[i0 - 1][j - 1] - u[i0] - v[j]
if cur < minv[j]:
minv[j], way[j] = cur, j0
if minv[j] < delta:
delta, j1 = minv[j], j
for j in range(c + 1):
if used[j]:
u[p[j]] += delta
v[j] -= delta
else:
minv[j] -= delta
j0 = j1
if p[j0] == 0:
break
while True:
j1 = way[j0]
p[j0] = p[j1]
j0 = j1
if j0 == 0:
break
pairs = [(p[j] - 1, j - 1) for j in range(1, c + 1) if p[j] != 0]
if flip:
pairs = [(b, a) for a, b in pairs]
return sorted(pairs)
def overlap_counts(gt_frames, hyp_frames, delta=Fraction(1, 2)):
"""Returns (true ids, computed ids, n, true lengths, computed lengths)."""
gt_ids = sorted({o for f in gt_frames for o in f}, key=repr)
hyp_ids = sorted({h for f in hyp_frames for h in f}, key=repr)
gi = {o: i for i, o in enumerate(gt_ids)}
hi = {h: j for j, h in enumerate(hyp_ids)}
n = [[0] * len(hyp_ids) for _ in gt_ids]
glen, hlen = [0] * len(gt_ids), [0] * len(hyp_ids)
for gt, hyp in zip(gt_frames, hyp_frames): # @line L1
for o, a in gt.items():
glen[gi[o]] += 1
for h, b in hyp.items():
if iou(a, b) >= delta:
n[gi[o]][hi[h]] += 1
for h in hyp:
hlen[hi[h]] += 1
return gt_ids, hyp_ids, n, glen, hlen
def max_overlap_matching(n):
"""A one-to-one matching of rows to columns with the largest total n.
Returns (pairs, total); pairs with n = 0 are left out."""
if not n or not n[0]:
return [], 0
pairs = [(i, j) for i, j in hungarian_min([[-x for x in row] for row in n]) if n[i][j] > 0] # @line L2
return pairs, sum(n[i][j] for i, j in pairs)
def idf1(gt_frames, hyp_frames, delta=Fraction(1, 2)):
"""Returns a dict with idtp, idfn, idfp, idp, idr, idf1 and the matched identities."""
gt_ids, hyp_ids, n, glen, hlen = overlap_counts(gt_frames, hyp_frames, delta)
pairs, idtp = max_overlap_matching(n)
total_gt, total_hyp = sum(glen), sum(hlen)
idfn = total_gt - idtp # @line L3
idfp = total_hyp - idtp
idp = Fraction(idtp, total_hyp) if total_hyp else None # @line L4
idr = Fraction(idtp, total_gt) if total_gt else None
f1 = Fraction(2 * idtp, total_gt + total_hyp) if total_gt + total_hyp else None
return {"idtp": idtp, "idfn": idfn, "idfp": idfp, "idp": idp, "idr": idr, "idf1": f1,
"matching": [(gt_ids[i], hyp_ids[j]) for i, j in pairs]}
def min_cost_with_dummy_nodes(n, glen, hlen):
"""The form of the paper: rows are the true trajectories and one false positive
node per computed trajectory; columns are the computed trajectories and one false
negative node per true trajectory. Matching o to h costs the misses of both,
glen + hlen - 2 n; o to its own false negative node costs glen; the false
positive node of h to h costs hlen; two extra nodes cost 0. Returns the minimum."""
t, c = len(glen), len(hlen)
size = t + c
big = sum(glen) + sum(hlen) + 1 # no edge
cost = [[big] * size for _ in range(size)]
for i in range(t):
for j in range(c):
cost[i][j] = glen[i] + hlen[j] - 2 * n[i][j]
cost[i][c + i] = glen[i]
for j in range(c):
cost[t + j][j] = hlen[j]
for i in range(t):
cost[t + j][c + i] = 0
pairs = hungarian_min(cost)
return sum(cost[i][j] for i, j in pairs)
test_idf1_score.py 125 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/idf1-score/. The authors are listed in prov/QK543C.jsonl.
import itertools
import random
from fractions import Fraction
from idf1_score import idf1, iou, max_overlap_matching, min_cost_with_dummy_nodes, overlap_counts
HALF = Fraction(1, 2)
P1 = (0, 0, 10, 10)
P2 = (40, 0, 50, 10)
def brute_max_total(n):
rows = len(n)
cols = len(n[0]) if rows else 0
best = 0
for k in range(min(rows, cols) + 1):
for rs in itertools.combinations(range(rows), k):
for cs in itertools.permutations(range(cols), k):
best = max(best, sum(n[i][j] for i, j in zip(rs, cs)))
return best
def test_page_example_swap():
# object 1 stays at P1 and object 2 at P2; a and b swap after frame 4
gt = [{1: P1, 2: P2}] * 6
hyp = [{"a": P1, "b": P2}] * 4 + [{"a": P2, "b": P1}] * 2
r = idf1(gt, hyp)
assert r["matching"] == [(1, "a"), (2, "b")]
assert (r["idtp"], r["idfn"], r["idfp"]) == (8, 4, 4)
assert r["idf1"] == r["idp"] == r["idr"] == Fraction(2, 3)
def test_one_switch_against_one_false_positive():
gt = [{1: P1}] * 100
switch = [{"a": P1}] * 50 + [{"b": P1}] * 50
assert idf1(gt, switch)["idf1"] == HALF
extra = [{"a": P1}] * 100
extra[37] = {"a": P1, "z": P2}
assert idf1(gt, extra)["idf1"] == Fraction(200, 201)
def test_perfect_and_empty():
gt = [{1: P1, 2: P2}] * 3
assert idf1(gt, gt)["idf1"] == 1
r = idf1(gt, [{}] * 3)
assert (r["idf1"], r["idr"], r["idp"]) == (0, 0, None)
def test_matching_against_brute_force():
rng = random.Random(1)
for _ in range(300):
r, c = rng.randint(1, 4), rng.randint(1, 4)
n = [[rng.randint(0, 6) for _ in range(c)] for _ in range(r)]
pairs, total = max_overlap_matching(n)
assert len({i for i, _ in pairs}) == len(pairs) == len({j for _, j in pairs})
assert total == brute_max_total(n)
def random_box(rng):
x, y = rng.randint(0, 10), rng.randint(0, 10)
return (x, y, x + rng.randint(3, 6), y + rng.randint(3, 6))
def random_sequence(rng):
start = {o: random_box(rng) for o in range(3)}
names = {o: "abc"[o] for o in range(3)}
gt_frames, hyp_frames = [], []
for t in range(rng.randint(1, 8)):
gt, hyp = {}, {}
for o, b in start.items():
if rng.random() < 0.8:
gt[o] = (b[0] + t, b[1], b[2] + t, b[3])
if rng.random() < 0.2:
names[o] = rng.choice("abcde")
if rng.random() < 0.8 and names[o] not in hyp:
dx = rng.randint(-1, 1)
hyp[names[o]] = (gt[o][0] + dx, gt[o][1], gt[o][2] + dx, gt[o][3])
if rng.random() < 0.3:
hyp["z"] = random_box(rng)
gt_frames.append(gt)
hyp_frames.append(hyp)
return gt_frames, hyp_frames
def frame_max_matches(gt, hyp):
objs, hyps = list(gt), list(hyp)
best = 0
for k in range(min(len(objs), len(hyps)) + 1):
for rs in itertools.combinations(objs, k):
for cs in itertools.permutations(hyps, k):
if all(iou(gt[o], hyp[h]) >= HALF for o, h in zip(rs, cs)):
best = max(best, k)
return best
def test_dummy_node_form_and_detection_bound():
rng = random.Random(2)
for _ in range(80):
gt_frames, hyp_frames = random_sequence(rng)
_, _, n, glen, hlen = overlap_counts(gt_frames, hyp_frames)
r = idf1(gt_frames, hyp_frames)
assert r["idtp"] == brute_max_total(n)
# the minimum cost of the form of the paper is IDFN + IDFP
assert min_cost_with_dummy_nodes(n, glen, hlen) == r["idfn"] + r["idfp"]
assert r["idfn"] + r["idtp"] == sum(glen)
assert r["idfp"] + r["idtp"] == sum(hlen)
# IDF1 is at most the F1 of detections with the best matching in each frame
tp = sum(frame_max_matches(g, h) for g, h in zip(gt_frames, hyp_frames))
total = sum(glen) + sum(hlen)
if total:
assert r["idf1"] <= Fraction(2 * tp, total)
assert r["idf1"] == 2 / (1 / r["idp"] + 1 / r["idr"]) if r["idtp"] else r["idf1"] == 0
def test_symmetry():
rng = random.Random(3)
for _ in range(40):
gt_frames, hyp_frames = random_sequence(rng)
a = idf1(gt_frames, hyp_frames)
b = idf1(hyp_frames, gt_frames)
assert a["idtp"] == b["idtp"] and a["idf1"] == b["idf1"]
assert (a["idp"], a["idr"]) == (b["idr"], b["idp"])
References
- Ergys Ristani, Francesco Solera, Roger S. Zou, Rita Cucchiara and Carlo Tomasi, Performance measures and a data set for multi-target, multi-camera tracking, ECCV 2016 Workshops, 17-35, Section 3.3 (the bipartite graph with false positive and false negative nodes, Eq. 3 and Eq. 4), Section 3.4 (IDFN, IDFP and IDTP, Eq. 6 to 8, and IDP, IDR and IDF1, Eq. 9 to 11), Figure 1 (the comparison with the counts of events)
- Keni Bernardin and Rainer Stiefelhagen, Evaluating multiple object tracking performance: the CLEAR MOT metrics, EURASIP Journal on Image and Video Processing 2008, article 246309, the definitions of MOTA and of the mismatch count, which IDF1 is compared with
- Jonathon Luiten, Aljosa Osep, Patrick Dendorfer, Philip Torr, Andreas Geiger, Laura Leal-Taixe and Bastian Leibe, HOTA: a higher order metric for evaluating multi-object tracking, International Journal of Computer Vision 129(2), 2021, 548-578, Section 4.2 (IDF1 in the notation of HOTA) and Section 7 (detection matching against trajectory matching, and why IDF1 can fall when detection improves)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H2 full proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag QK543C Edit this page History Report an error Markdown