visloc_objects/
mot_eval.rs1use std::collections::HashMap;
4
5use crate::assignment::assign_with_cost_limit;
6use crate::bbox::BBox;
7
8#[derive(Debug, Clone)]
15pub struct MotAccumulator {
16 iou_threshold: f64,
17 last_match: HashMap<u64, u64>,
18 summary: MotSummary,
19}
20
21#[derive(Debug, Clone, Copy, Default, PartialEq)]
23pub struct MotSummary {
24 pub frames: usize,
25 pub ground_truth: usize,
26 pub matches: usize,
27 pub misses: usize,
28 pub false_positives: usize,
29 pub id_switches: usize,
30 pub iou_sum: f64,
32}
33
34impl MotSummary {
35 pub fn mota(&self) -> f64 {
37 if self.ground_truth == 0 {
38 return 0.0;
39 }
40 1.0 - (self.misses + self.false_positives + self.id_switches) as f64
41 / self.ground_truth as f64
42 }
43
44 pub fn motp_iou(&self) -> f64 {
46 if self.matches == 0 {
47 0.0
48 } else {
49 self.iou_sum / self.matches as f64
50 }
51 }
52}
53
54impl MotAccumulator {
55 pub fn new(iou_threshold: f64) -> Self {
57 Self {
58 iou_threshold,
59 last_match: HashMap::new(),
60 summary: MotSummary::default(),
61 }
62 }
63
64 pub fn summary(&self) -> MotSummary {
65 self.summary
66 }
67
68 pub fn add_frame(&mut self, ground_truth: &[(u64, BBox)], hypotheses: &[(u64, BBox)]) {
70 let mut gt_to_hyp: Vec<Option<usize>> = vec![None; ground_truth.len()];
71 let mut hyp_used = vec![false; hypotheses.len()];
72 for (g, (gt_id, gt_box)) in ground_truth.iter().enumerate() {
73 let Some(&previous) = self.last_match.get(gt_id) else {
74 continue;
75 };
76 if let Some(h) = hypotheses.iter().position(|(id, _)| *id == previous) {
77 if !hyp_used[h] && gt_box.iou(&hypotheses[h].1) >= self.iou_threshold {
78 gt_to_hyp[g] = Some(h);
79 hyp_used[h] = true;
80 }
81 }
82 }
83
84 let free_gt: Vec<usize> = (0..ground_truth.len())
85 .filter(|&g| gt_to_hyp[g].is_none())
86 .collect();
87 let free_hyp: Vec<usize> = (0..hypotheses.len()).filter(|&h| !hyp_used[h]).collect();
88 let mut costs = Vec::with_capacity(free_gt.len() * free_hyp.len());
89 for &g in &free_gt {
90 for &h in &free_hyp {
91 let iou = ground_truth[g].1.iou(&hypotheses[h].1);
92 costs.push(if iou >= self.iou_threshold {
93 1.0 - iou
94 } else {
95 f64::INFINITY
96 });
97 }
98 }
99 let assignment = assign_with_cost_limit(free_gt.len(), free_hyp.len(), &costs, 2.0);
102 for (g, h) in assignment.matches {
103 gt_to_hyp[free_gt[g]] = Some(free_hyp[h]);
104 hyp_used[free_hyp[h]] = true;
105 }
106
107 let summary = &mut self.summary;
108 summary.frames += 1;
109 summary.ground_truth += ground_truth.len();
110 for (g, matched) in gt_to_hyp.iter().enumerate() {
111 let (gt_id, gt_box) = ground_truth[g];
112 let Some(h) = *matched else {
113 summary.misses += 1;
114 continue;
115 };
116 let (hyp_id, hyp_box) = hypotheses[h];
117 summary.matches += 1;
118 summary.iou_sum += gt_box.iou(&hyp_box);
119 if let Some(previous) = self.last_match.insert(gt_id, hyp_id) {
120 if previous != hyp_id {
121 summary.id_switches += 1;
122 }
123 }
124 }
125 summary.false_positives += hyp_used.iter().filter(|u| !**u).count();
126 }
127}
128
129#[cfg(test)]
130mod tests {
131 use super::*;
132
133 fn b(x: f64) -> BBox {
134 BBox::from_xywh(x, 0.0, 10.0, 10.0)
135 }
136
137 #[test]
138 fn perfect_tracking_scores_one() {
139 let mut acc = MotAccumulator::new(0.5);
140 for k in 0..5 {
141 let x = k as f64;
142 acc.add_frame(
143 &[(1, b(x)), (2, b(50.0 + x))],
144 &[(7, b(x)), (8, b(50.0 + x))],
145 );
146 }
147 let s = acc.summary();
148 assert_eq!(
149 (s.matches, s.misses, s.false_positives, s.id_switches),
150 (10, 0, 0, 0)
151 );
152 assert_eq!(s.mota(), 1.0);
153 assert_eq!(s.motp_iou(), 1.0);
154 }
155
156 #[test]
157 fn counts_misses_false_positives_and_switches() {
158 let mut acc = MotAccumulator::new(0.5);
159 acc.add_frame(&[(1, b(0.0))], &[(7, b(0.0))]);
160 acc.add_frame(&[(1, b(1.0))], &[(9, b(200.0))]);
162 acc.add_frame(&[(1, b(2.0))], &[(8, b(2.0))]);
164 let s = acc.summary();
165 assert_eq!(
166 (
167 s.ground_truth,
168 s.matches,
169 s.misses,
170 s.false_positives,
171 s.id_switches
172 ),
173 (3, 2, 1, 1, 1)
174 );
175 assert!((s.mota() - 0.0).abs() < 1e-12);
176 }
177
178 #[test]
179 fn keeps_an_existing_match_over_a_slightly_better_one() {
180 let mut acc = MotAccumulator::new(0.5);
181 acc.add_frame(&[(1, b(0.0))], &[(7, b(0.0))]);
182 acc.add_frame(&[(1, b(1.0))], &[(7, b(3.0)), (8, b(1.0))]);
184 let s = acc.summary();
185 assert_eq!((s.id_switches, s.false_positives), (0, 1));
186 }
187}