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
use crate::coco::COCO;
use crate::params::Params;
use crate::primitives::sim::{self, SimKind};
use crate::types::Rle;
use super::{COCOeval, EvalMode};
impl COCOeval {
/// Compute the IoU/OKS matrix for a given image and category.
pub(super) fn compute_iou_static(
coco_gt: &COCO,
coco_dt: &COCO,
params: &Params,
img_id: u64,
cat_id: u64,
eval_mode: EvalMode,
) -> Vec<Vec<f64>> {
let gt_anns = Self::get_anns_static(coco_gt, params, img_id, cat_id);
let dt_anns = Self::get_anns_static(coco_dt, params, img_id, cat_id);
if gt_anns.is_empty() || dt_anns.is_empty() {
return Vec::new();
}
// Dispatch on the geometry axis, not the eval-config one: `SimKind` is
// what selects a kernel, and every family (detection here, tracking and
// panoptic later) branches on the same four values. The helpers below do
// marshaling only — reshaping annotations into the kernel's input types.
match SimKind::from(params.iou_type) {
SimKind::Mask => {
Self::compute_segm_iou_static(coco_gt, coco_dt, dt_anns, gt_anns, eval_mode)
}
SimKind::Bbox => {
Self::compute_bbox_iou_static(coco_gt, coco_dt, dt_anns, gt_anns, eval_mode)
}
SimKind::Oks => Self::compute_oks_static(coco_gt, coco_dt, params, dt_anns, gt_anns),
SimKind::Obb => {
Self::compute_obb_iou_static(coco_gt, coco_dt, dt_anns, gt_anns, eval_mode)
}
}
}
/// Get annotation IDs for an image, optionally filtered by category.
pub(super) fn get_anns_static<'a>(
coco: &'a COCO,
params: &Params,
img_id: u64,
cat_id: u64,
) -> &'a [u64] {
if params.use_cats {
coco.get_ann_ids_for_img_cat(img_id, cat_id)
} else {
coco.get_ann_ids_for_img(img_id)
}
}
/// Compute segmentation mask IoU by converting annotations to RLE and calling `sim::mask_iou`.
pub(super) fn compute_segm_iou_static(
coco_gt: &COCO,
coco_dt: &COCO,
dt_ids: &[u64],
gt_ids: &[u64],
eval_mode: EvalMode,
) -> Vec<Vec<f64>> {
let dt_rles: Vec<Rle> = dt_ids
.iter()
.filter_map(|&id| {
let ann = coco_dt.get_ann(id)?;
coco_dt.ann_to_rle(ann)
})
.collect();
let (gt_rles, iscrowd): (Vec<Rle>, Vec<bool>) = gt_ids
.iter()
.filter_map(|&id| {
let ann = coco_gt.get_ann(id)?;
// OID: iscrowd is irrelevant — always use standard IoU
let crowd = if eval_mode == EvalMode::OpenImages {
false
} else {
ann.iscrowd
};
Some((coco_gt.ann_to_rle(ann)?, crowd))
})
.unzip();
sim::mask_iou(&dt_rles, >_rles, &iscrowd)
}
/// Compute bounding box IoU by extracting bbox arrays and calling `sim::bbox_iou`.
pub(super) fn compute_bbox_iou_static(
coco_gt: &COCO,
coco_dt: &COCO,
dt_ids: &[u64],
gt_ids: &[u64],
eval_mode: EvalMode,
) -> Vec<Vec<f64>> {
let dt_bbs: Vec<[f64; 4]> = dt_ids
.iter()
.filter_map(|&id| coco_dt.get_ann(id)?.bbox)
.collect();
let (gt_bbs, iscrowd): (Vec<[f64; 4]>, Vec<bool>) = gt_ids
.iter()
.filter_map(|&id| {
let ann = coco_gt.get_ann(id)?;
// OID: iscrowd is irrelevant — always use standard IoU
let crowd = if eval_mode == EvalMode::OpenImages {
false
} else {
ann.iscrowd
};
Some((ann.bbox?, crowd))
})
.unzip();
sim::bbox_iou(&dt_bbs, >_bbs, &iscrowd)
}
/// Compute OKS (Object Keypoint Similarity) between detection and GT keypoints.
///
/// OKS = mean_k[ exp( -d_k^2 / (2 * s_k^2 * area) ) ] where d_k is the Euclidean
/// distance for keypoint k, s_k is the per-keypoint sigma, and area is the GT area.
/// Only visible GT keypoints contribute. When no GT keypoints are visible, distance
/// is measured to the GT bounding box boundary instead.
pub(super) fn compute_oks_static(
coco_gt: &COCO,
coco_dt: &COCO,
params: &Params,
dt_ids: &[u64],
gt_ids: &[u64],
) -> Vec<Vec<f64>> {
// The OKS math lives in the shared `primitives::sim::oks_matrix` kernel
// (COCO-decoupled, matrix-shaped). Here we only marshal the annotations
// into the kernel's flat-slice form. A missing `keypoints` field maps to
// an empty slice, which the kernel skips — matching the previous
// `None => continue` behavior that left that row/column zero.
let gt_anns: Vec<_> = gt_ids
.iter()
.filter_map(|&id| coco_gt.get_ann(id))
.collect();
let dt_anns: Vec<_> = dt_ids
.iter()
.filter_map(|&id| coco_dt.get_ann(id))
.collect();
let gt: Vec<crate::primitives::sim::GtPose<'_>> = gt_anns
.iter()
.map(|a| crate::primitives::sim::GtPose {
keypoints: a.keypoints.as_deref().unwrap_or(&[]),
area: a.area.unwrap_or(0.0),
bbox: a.bbox.unwrap_or([0.0; 4]),
})
.collect();
let dt_keypoints: Vec<&[f64]> = dt_anns
.iter()
.map(|a| a.keypoints.as_deref().unwrap_or(&[]))
.collect();
crate::primitives::sim::oks_matrix(&dt_keypoints, >, ¶ms.kpt_oks_sigmas)
}
/// Compute oriented bounding box IoU by extracting OBB arrays and calling `sim::obb_iou`.
pub(super) fn compute_obb_iou_static(
coco_gt: &COCO,
coco_dt: &COCO,
dt_ids: &[u64],
gt_ids: &[u64],
eval_mode: EvalMode,
) -> Vec<Vec<f64>> {
let dt_obbs: Vec<[f64; 5]> = dt_ids
.iter()
.filter_map(|&id| coco_dt.get_ann(id)?.obb)
.collect();
let (gt_obbs, iscrowd): (Vec<[f64; 5]>, Vec<bool>) = gt_ids
.iter()
.filter_map(|&id| {
let ann = coco_gt.get_ann(id)?;
let crowd = if eval_mode == EvalMode::OpenImages {
false
} else {
ann.iscrowd
};
Some((ann.obb?, crowd))
})
.unzip();
sim::obb_iou(&dt_obbs, >_obbs, &iscrowd)
}
}