hotcoco 1.0.1

Perception evaluation in pure Rust — a pycocotools-compatible COCO/LVIS/Open Images engine with diagnostics and dataset tools
Documentation
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Regression tests for the 1.0 preflight detection fixes.
//!
//! Each test pins a behavioral fix: IoU row/column alignment when an
//! annotation has no usable geometry, sentinel degradation for missing
//! area-label / max-det lookups in `summarize`, `compare()` parameter
//! validation, graceful handling of empty `max_dets`, F-score key naming, and
//! the self-explaining `EvalParams` archive.

#![allow(clippy::unwrap_used)]

use std::path::PathBuf;

use hotcoco::detection::{CompareOpts, compare};
use hotcoco::params::IouType;
use hotcoco::types::{Annotation, Category, Dataset, Image};
use hotcoco::{AreaRange, COCO, COCOeval};

fn fixtures_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures")
}

fn image(id: u64) -> Image {
    Image {
        id,
        width: 200,
        height: 200,
        ..Default::default()
    }
}

fn cat(id: u64, name: &str) -> Category {
    Category {
        id,
        name: name.to_string(),
        ..Default::default()
    }
}

/// Ground-truth annotation with geometry; `bbox: None` models a real dataset's
/// geometry-less record.
fn ann(id: u64, bbox: Option<[f64; 4]>) -> Annotation {
    Annotation {
        id,
        image_id: 1,
        category_id: 1,
        bbox,
        area: bbox.map(|b| b[2] * b[3]),
        ..Default::default()
    }
}

/// Detection — an [`ann`] carrying a score.
fn det(id: u64, bbox: Option<[f64; 4]>, score: f64) -> Annotation {
    Annotation {
        score: Some(score),
        ..ann(id, bbox)
    }
}

fn coco_from(anns: Vec<Annotation>) -> COCO {
    COCO::from_dataset(Dataset {
        images: vec![image(1)],
        annotations: anns,
        categories: vec![cat(1, "thing")],
        ..Default::default()
    })
}

fn fixture_eval() -> COCOeval {
    let gt = COCO::new(&fixtures_dir().join("gt.json")).unwrap();
    let dt = gt.load_res(&fixtures_dir().join("dt.json")).unwrap();
    COCOeval::new(gt, dt, IouType::Bbox)
}

// ---------------------------------------------------------------------------
// A.3 — IoU row/column alignment with geometry-less annotations
// ---------------------------------------------------------------------------

/// A bbox-less ground truth between two valid ones must occupy a zero *column*
/// in the pair's IoU matrix, not vanish from it. Before the fix, the matrix
/// builders dropped it while `gather_pair` still counted it, so every later
/// ground truth read its neighbor's IoU column and the detection sitting
/// exactly on GT 3 went unmatched.
#[test]
fn bboxless_gt_between_valid_gts_does_not_shift_iou_columns() {
    let gt = coco_from(vec![
        ann(1, Some([0.0, 0.0, 10.0, 10.0])),
        ann(2, None), // no geometry — the column that used to vanish
        ann(3, Some([50.0, 50.0, 10.0, 10.0])),
    ]);
    let dt = coco_from(vec![
        det(101, Some([0.0, 0.0, 10.0, 10.0]), 0.9), // IoU 1.0 with GT 1
        det(102, Some([50.0, 50.0, 10.0, 10.0]), 0.8), // IoU 1.0 with GT 3
    ]);

    let mut ev = COCOeval::new(gt, dt, IouType::Bbox);
    ev.evaluate();

    let diag = ev.image_diagnostics(0.5, 0.5).unwrap();
    assert_eq!(
        diag.annotations.dt_match.get(&101),
        Some(&1),
        "detection 101 sits exactly on GT 1"
    );
    assert_eq!(
        diag.annotations.dt_match.get(&102),
        Some(&3),
        "detection 102 sits exactly on GT 3 and must not read GT 2's missing column"
    );
    // The geometry-less GT scores as an unmatched ground truth, not as a shift.
    assert_eq!(
        diag.annotations.gt_status.len(),
        3,
        "all three ground truths are classified"
    );
}

/// The detection-side twin: a bbox-less detection between two valid ones must
/// occupy a zero *row*, so the lower-scoring valid detection still reads its
/// own IoU row rather than falling off the end of a shortened matrix.
#[test]
fn bboxless_dt_between_valid_dts_does_not_shift_iou_rows() {
    let gt = coco_from(vec![
        ann(1, Some([0.0, 0.0, 10.0, 10.0])),
        ann(3, Some([50.0, 50.0, 10.0, 10.0])),
    ]);
    let dt = coco_from(vec![
        det(101, Some([0.0, 0.0, 10.0, 10.0]), 0.9), // IoU 1.0 with GT 1
        det(102, None, 0.85),                        // no geometry — zero row
        det(103, Some([50.0, 50.0, 10.0, 10.0]), 0.8), // IoU 1.0 with GT 3
    ]);

    let mut ev = COCOeval::new(gt, dt, IouType::Bbox);
    ev.evaluate();

    let diag = ev.image_diagnostics(0.5, 0.5).unwrap();
    assert_eq!(diag.annotations.dt_match.get(&101), Some(&1));
    assert_eq!(
        diag.annotations.dt_match.get(&103),
        Some(&3),
        "detection 103's row must not shift onto detection 102's missing slot"
    );
    use hotcoco::detection::DtStatus;
    assert_eq!(
        diag.annotations.dt_status.get(&102),
        Some(&DtStatus::Fp),
        "a geometry-less detection is an unmatched detection, not a shift"
    );
}

/// Same alignment contract on the direct `eval_imgs` surface: with the
/// geometry-less GT present, both detections match at every IoU threshold and
/// the matched ids are the geometrically correct ones.
#[test]
fn eval_imgs_matches_are_aligned_with_geometry_gaps() {
    let gt = coco_from(vec![
        ann(1, Some([0.0, 0.0, 10.0, 10.0])),
        ann(2, None),
        ann(3, Some([50.0, 50.0, 10.0, 10.0])),
    ]);
    let dt = coco_from(vec![
        det(101, Some([0.0, 0.0, 10.0, 10.0]), 0.9),
        det(102, Some([50.0, 50.0, 10.0, 10.0]), 0.8),
    ]);

    let mut ev = COCOeval::new(gt, dt, IouType::Bbox);
    ev.evaluate();

    // area = "all" cell at the default cap.
    let cell = ev
        .eval_imgs()
        .iter()
        .flatten()
        .find(|e| e.area_rng == [0.0, 1e10])
        .expect("the all-area cell exists");

    for t_idx in 0..10 {
        assert!(cell.dt_matched[(t_idx, 0)], "dt 101 matched at t={t_idx}");
        assert!(cell.dt_matched[(t_idx, 1)], "dt 102 matched at t={t_idx}");
        assert_eq!(cell.dt_matches[(t_idx, 0)], 1);
        assert_eq!(cell.dt_matches[(t_idx, 1)], 3);
    }
}

// ---------------------------------------------------------------------------
// B — summarize: missing area label / max_det degrade to the -1.0 sentinel
// ---------------------------------------------------------------------------

/// Renaming the "small" area range must make `APs`/`ARs` report the `-1.0`
/// "not computed" sentinel — not silently reuse index 0, which is the "all"
/// slice wearing a per-size metric's name.
#[test]
fn missing_area_label_reports_sentinel_not_all_slice() {
    let mut ev = fixture_eval();
    for ar in &mut ev.params.area_ranges {
        if ar.label == "small" {
            ar.label = "tiny".to_string();
        }
    }
    ev.run();

    let results = ev.get_results(None, false);
    assert!(
        results["AP"] >= 0.0,
        "headline AP is computable on the fixture"
    );
    assert_eq!(
        results["APs"], -1.0,
        "no 'small' range exists, so APs is not computed — it must not report the 'all' slice"
    );
    assert_eq!(results["ARs"], -1.0);
    // Pre-fix, APs silently equaled AP (both read index 0).
    assert_ne!(results["APs"], results["AP"]);
}

/// Empty `max_dets` must degrade — no metric is computable, every stat is the
/// sentinel — rather than panic, matching how the missing-area-label and
/// missing-threshold branches behave.
#[test]
fn empty_max_dets_degrades_to_sentinels_without_panicking() {
    let mut ev = fixture_eval();
    ev.params.max_dets = Vec::new();
    ev.run(); // pre-fix: assert! panic inside evaluate()

    let stats = ev.stats().expect("summarize ran");
    assert!(!stats.is_empty());
    assert!(
        stats.iter().all(|&v| v == -1.0),
        "with no max-det slots nothing is computable; got {stats:?}"
    );
}

// ---------------------------------------------------------------------------
// B — compare() validates the axes its shared catalog reads
// ---------------------------------------------------------------------------

#[test]
fn compare_rejects_mismatched_grids_and_ranges() {
    let mut ev_a = fixture_eval();
    ev_a.evaluate();

    // iou_thrs
    let mut ev_b = fixture_eval();
    ev_b.params.iou_thrs = vec![0.5];
    ev_b.evaluate();
    let err = compare(&ev_a, &ev_b, &CompareOpts::default()).unwrap_err();
    assert!(err.to_string().contains("iou_thrs"), "got: {err}");

    // rec_thrs
    let mut ev_b = fixture_eval();
    ev_b.params.rec_thrs = vec![0.0, 0.5, 1.0];
    ev_b.evaluate();
    let err = compare(&ev_a, &ev_b, &CompareOpts::default()).unwrap_err();
    assert!(err.to_string().contains("rec_thrs"), "got: {err}");

    // max_dets
    let mut ev_b = fixture_eval();
    ev_b.params.max_dets = vec![50];
    ev_b.evaluate();
    let err = compare(&ev_a, &ev_b, &CompareOpts::default()).unwrap_err();
    assert!(err.to_string().contains("max_dets"), "got: {err}");

    // area_ranges (bounds differ, labels identical — the label-only check
    // missed exactly this case elsewhere)
    let mut ev_b = fixture_eval();
    ev_b.params.area_ranges[1] = AreaRange {
        label: "small".to_string(),
        range: [0.0, 100.0],
    };
    ev_b.evaluate();
    let err = compare(&ev_a, &ev_b, &CompareOpts::default()).unwrap_err();
    assert!(err.to_string().contains("area_ranges"), "got: {err}");

    // Identical params still compare fine.
    let mut ev_b = fixture_eval();
    ev_b.evaluate();
    assert!(compare(&ev_a, &ev_b, &CompareOpts::default()).is_ok());
}

// ---------------------------------------------------------------------------
// E — F-score key naming: integer betas undecorated
// ---------------------------------------------------------------------------

#[test]
fn f_score_keys_use_minimal_digits() {
    let mut ev = fixture_eval();
    ev.run();

    let f2 = ev.f_scores(2.0);
    assert!(
        f2.contains_key("F2") && f2.contains_key("F2_50") && f2.contains_key("F2_75"),
        "integer beta must print undecorated; got keys {:?}",
        f2.keys().collect::<Vec<_>>()
    );
    let fh = ev.f_scores(0.5);
    assert!(fh.contains_key("F0.5") && fh.contains_key("F0.5_50"));
}

// ---------------------------------------------------------------------------
// E — EvalParams archives the whole configuration, deviations included
// ---------------------------------------------------------------------------

/// A saved `Extension` run must carry its own explanation: the archive holds
/// the recall grid, `use_cats`, the OKS sigmas, and the deviation strings that
/// drove the provenance bit.
#[test]
fn eval_params_archive_is_self_explaining() {
    let mut ev = fixture_eval();
    ev.params.iou_thrs = vec![0.25, 0.75]; // a deliberate deviation
    ev.run();

    let results = ev.results(false).unwrap();
    assert_eq!(results.params.recall_thresholds, ev.params.rec_thrs);
    assert!(results.params.use_cats);
    assert_eq!(results.params.kpt_oks_sigmas, ev.params.kpt_oks_sigmas);
    assert!(
        !results.params.reference_deviations.is_empty(),
        "custom iou_thrs is a deviation and the archive must say so"
    );
    assert!(
        results
            .params
            .reference_deviations
            .iter()
            .any(|d| d.contains("iou_thrs")),
        "the deviation names the parameter: {:?}",
        results.params.reference_deviations
    );

    // And the archived strings are the same ones the live predicate returns.
    assert_eq!(
        results.params.reference_deviations,
        ev.reference_deviations()
    );
}

// ---------------------------------------------------------------------------
// Found by scripts/fuzz_dropin.py: a keypoint GT without `num_keypoints`
// read the field as 0 and was ignored — every ground truth in a file that
// omits the field, so keypoint AP scored a dataset with nothing to match.
// ---------------------------------------------------------------------------

fn keypoint_pair(with_num_keypoints: bool) -> (COCO, COCO) {
    // 17 COCO keypoints, five labeled, the rest absent.
    let mut kps = vec![0.0; 51];
    for k in 0..5 {
        kps[k * 3] = 10.0 + k as f64;
        kps[k * 3 + 1] = 10.0 + k as f64;
        kps[k * 3 + 2] = 2.0;
    }
    let gt_ann = Annotation {
        id: 1,
        image_id: 1,
        category_id: 1,
        bbox: Some([5.0, 5.0, 20.0, 20.0]),
        area: Some(400.0),
        keypoints: Some(kps),
        num_keypoints: with_num_keypoints.then_some(5),
        ..Default::default()
    };
    let dt_ann = Annotation {
        score: Some(0.9),
        num_keypoints: None,
        ..gt_ann.clone()
    };
    (coco_from(vec![gt_ann]), coco_from(vec![dt_ann]))
}

#[test]
fn num_keypoints_is_derived_when_absent() {
    let ann = Annotation {
        keypoints: Some(vec![1.0, 1.0, 2.0, 0.0, 0.0, 0.0, 3.0, 3.0, 1.0]),
        ..Default::default()
    };
    assert_eq!(
        ann.num_visible_keypoints(),
        2,
        "derived from visibility flags"
    );
    let explicit = Annotation {
        num_keypoints: Some(7),
        ..ann.clone()
    };
    assert_eq!(
        explicit.num_visible_keypoints(),
        7,
        "the field wins when present"
    );
    assert_eq!(Annotation::default().num_visible_keypoints(), 0);

    let stats = |with_field: bool| {
        let (gt, dt) = keypoint_pair(with_field);
        let mut ev = COCOeval::new(gt, dt, IouType::Keypoints);
        ev.evaluate();
        ev.accumulate();
        ev.summarize_lines();
        ev.stats().unwrap().to_vec()
    };
    let with = stats(true);
    let without = stats(false);
    assert_eq!(with[0], 1.0, "identical keypoints must score AP 1.0");
    assert_eq!(
        with, without,
        "omitting num_keypoints must not change a single metric"
    );
}