-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_hota.py
More file actions
273 lines (242 loc) · 8.56 KB
/
test_hota.py
File metadata and controls
273 lines (242 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# ------------------------------------------------------------------------
# Trackers
# Copyright (c) 2026 Roboflow. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 [see LICENSE for details]
# ------------------------------------------------------------------------
from __future__ import annotations
from typing import Any
import numpy as np
import pytest
from trackers.eval.hota import (
ALPHA_THRESHOLDS,
aggregate_hota_metrics,
compute_hota_metrics,
)
@pytest.mark.parametrize(
(
"gt_ids",
"tracker_ids",
"similarity_scores",
"expected",
),
[
# Empty GT and tracker
(
[np.array([])],
[np.array([])],
[np.zeros((0, 0))],
{"HOTA_TP": 0, "HOTA_FN": 0, "HOTA_FP": 0},
),
# No tracker detections - all FN
(
[np.array([0, 1]), np.array([0, 1])],
[np.array([]), np.array([])],
[np.zeros((2, 0)), np.zeros((2, 0))],
{"HOTA": 0.0, "DetA": 0.0, "HOTA_FN": 4 * 19, "HOTA_FP": 0, "HOTA_TP": 0},
),
# No GT detections - all FP
(
[np.array([]), np.array([])],
[np.array([10, 20]), np.array([10, 20])],
[np.zeros((0, 2)), np.zeros((0, 2))],
{"HOTA": 0.0, "DetA": 0.0, "HOTA_FP": 4 * 19, "HOTA_FN": 0, "HOTA_TP": 0},
),
# Perfect tracking with high IoU
(
[np.array([0, 1]), np.array([0, 1])],
[np.array([10, 20]), np.array([10, 20])],
[
np.array([[0.9, 0.0], [0.0, 0.9]]),
np.array([[0.9, 0.0], [0.0, 0.9]]),
],
{"HOTA_min": 0.8, "DetA_min": 0.8, "AssA_min": 0.9},
),
# ID switch reduces AssA
(
[np.array([0, 1]), np.array([0, 1])],
[np.array([10, 20]), np.array([10, 20])],
[
np.array([[0.8, 0.1], [0.1, 0.8]]), # Normal matching
np.array([[0.1, 0.8], [0.8, 0.1]]), # Swapped!
],
{"DetA_min": 0.5, "AssA_max": 0.8},
),
# Low IoU passes fewer alpha thresholds
(
[np.array([0])],
[np.array([10])],
[np.array([[0.3]])],
{"HOTA_min": 0.0, "HOTA_max": 0.5, "HOTA_TP_min": 1},
),
# Multiple objects partial match
(
[np.array([0, 1, 2])],
[np.array([10, 20])], # Only 2 trackers for 3 GTs
[
np.array(
[
[0.8, 0.0],
[0.0, 0.8],
[0.0, 0.0], # GT2 has no match
]
)
],
{"HOTA_FN_min": 19, "DetRe_max": 1.0, "DetPr_max": 1.0},
),
# Single frame perfect matching
(
[np.array([0])],
[np.array([10])],
[np.array([[0.8]])],
{"HOTA_TP_min": 1, "LocA_min": 0.8},
),
],
)
def test_compute_hota_metrics(
gt_ids: list[np.ndarray[Any, np.dtype[Any]]],
tracker_ids: list[np.ndarray[Any, np.dtype[Any]]],
similarity_scores: list[np.ndarray[Any, np.dtype[Any]]],
expected: dict[str, Any],
) -> None:
result = compute_hota_metrics(gt_ids, tracker_ids, similarity_scores)
# Verify all expected fields are present
for field in ["HOTA", "DetA", "AssA", "DetRe", "DetPr", "AssRe", "AssPr", "LocA"]:
assert field in result
assert isinstance(result[field], float)
for field in ["HOTA_TP", "HOTA_FN", "HOTA_FP"]:
assert field in result
assert isinstance(result[field], int)
# Check expected values
for key, value in expected.items():
if key.endswith("_min"):
actual_key = key[:-4]
assert result[actual_key] >= value, (
f"{actual_key} should be >= {value}, got {result[actual_key]}"
)
elif key.endswith("_max"):
actual_key = key[:-4]
assert result[actual_key] <= value, (
f"{actual_key} should be <= {value}, got {result[actual_key]}"
)
elif key.endswith("_approx"):
actual_key = key[:-7]
assert result[actual_key] == pytest.approx(value, rel=0.01), (
f"{actual_key} should be ~{value}, got {result[actual_key]}"
)
elif isinstance(value, float):
assert result[key] == pytest.approx(value), (
f"Mismatch for {key}: {result[key]} != {value}"
)
else:
assert result[key] == value, f"Mismatch for {key}: {result[key]} != {value}"
def test_compute_hota_metrics_result_structure() -> None:
"""Verify all expected fields are present in result with correct types."""
result = compute_hota_metrics(
gt_ids=[np.array([0])],
tracker_ids=[np.array([10])],
similarity_scores=[np.array([[0.8]])],
)
# Float summary fields
for field in [
"HOTA",
"DetA",
"AssA",
"DetRe",
"DetPr",
"AssRe",
"AssPr",
"LocA",
"OWTA",
]:
assert field in result
assert isinstance(result[field], float)
assert 0 <= result[field] <= 1
# Integer summary fields
for field in ["HOTA_TP", "HOTA_FN", "HOTA_FP"]:
assert field in result
assert isinstance(result[field], int)
assert result[field] >= 0
# Array fields for aggregation
for field in [
"HOTA_TP_array",
"HOTA_FN_array",
"HOTA_FP_array",
"AssA_array",
"AssRe_array",
"AssPr_array",
"LocA_array",
]:
assert field in result
assert isinstance(result[field], np.ndarray)
assert len(result[field]) == len(ALPHA_THRESHOLDS)
@pytest.mark.parametrize(
(
"sequence_metrics",
"expected",
),
[
# Empty list
(
[],
{"HOTA": 0.0, "HOTA_TP": 0, "HOTA_FN": 0, "HOTA_FP": 0},
),
],
)
def test_aggregate_hota_metrics(
sequence_metrics: list[dict[str, Any]],
expected: dict[str, Any],
) -> None:
result = aggregate_hota_metrics(sequence_metrics)
for key, value in expected.items():
if isinstance(value, float):
assert result[key] == pytest.approx(value), (
f"Mismatch for {key}: {result[key]} != {value}"
)
else:
assert result[key] == value, f"Mismatch for {key}: {result[key]} != {value}"
def test_aggregate_hota_metrics_single_sequence() -> None:
"""Single sequence aggregation should match original."""
seq_result = compute_hota_metrics(
gt_ids=[np.array([0, 1])],
tracker_ids=[np.array([10, 20])],
similarity_scores=[np.array([[0.8, 0.0], [0.0, 0.8]])],
)
agg_result = aggregate_hota_metrics([seq_result])
assert agg_result["HOTA"] == pytest.approx(seq_result["HOTA"], rel=1e-4)
assert agg_result["DetA"] == pytest.approx(seq_result["DetA"], rel=1e-4)
assert agg_result["AssA"] == pytest.approx(seq_result["AssA"], rel=1e-4)
def test_aggregate_hota_metrics_multiple_sequences() -> None:
"""Multiple sequences should aggregate correctly."""
seq_result1 = compute_hota_metrics(
gt_ids=[np.array([0])],
tracker_ids=[np.array([10])],
similarity_scores=[np.array([[0.9]])],
)
seq_result2 = compute_hota_metrics(
gt_ids=[np.array([0])],
tracker_ids=[np.array([10])],
similarity_scores=[np.array([[0.9]])],
)
agg_result = aggregate_hota_metrics([seq_result1, seq_result2])
# TP/FN/FP should be summed
expected_tp = seq_result1["HOTA_TP"] + seq_result2["HOTA_TP"]
assert agg_result["HOTA_TP"] == expected_tp
# HOTA should be similar to individual sequences since they're identical
assert agg_result["HOTA"] == pytest.approx(seq_result1["HOTA"], rel=0.01)
def test_aggregate_hota_metrics_weighted_by_tp() -> None:
"""Aggregation should weight by TP count."""
# High quality sequence (many TPs)
high_quality = compute_hota_metrics(
gt_ids=[np.array([0, 1, 2, 3])],
tracker_ids=[np.array([10, 20, 30, 40])],
similarity_scores=[np.diag([0.9, 0.9, 0.9, 0.9])],
)
# Low quality sequence (few TPs)
low_quality = compute_hota_metrics(
gt_ids=[np.array([0])],
tracker_ids=[np.array([10])],
similarity_scores=[np.array([[0.3]])],
)
agg_result = aggregate_hota_metrics([high_quality, low_quality])
# Result should be closer to high_quality since it has more TPs
assert agg_result["HOTA"] > low_quality["HOTA"]