use std::collections::HashMap;
use serde::Serialize;
use crate::coco::COCO;
use crate::params::Params;
pub(super) type IouMatrix = Vec<Vec<f64>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum FreqGroup {
Rare,
Common,
Frequent,
}
#[derive(Debug, Clone, Default)]
pub(super) struct FreqGroups {
pub rare: Vec<usize>,
pub common: Vec<usize>,
pub frequent: Vec<usize>,
}
impl FreqGroups {
pub fn get(&self, group: FreqGroup) -> &[usize] {
match group {
FreqGroup::Rare => &self.rare,
FreqGroup::Common => &self.common,
FreqGroup::Frequent => &self.frequent,
}
}
}
#[derive(Debug, Clone)]
pub struct ConfusionMatrix {
pub matrix: Vec<u64>,
pub num_cats: usize,
pub cat_ids: Vec<u64>,
pub cat_names: Vec<String>,
pub iou_thr: f64,
}
impl ConfusionMatrix {
pub fn get(&self, gt_idx: usize, pred_idx: usize) -> u64 {
let k = self.num_cats + 1;
self.matrix[gt_idx * k + pred_idx]
}
pub fn normalized(&self) -> Vec<f64> {
let k = self.num_cats + 1;
let mut norm = vec![0.0f64; k * k];
for row in 0..k {
let row_sum: u64 = (0..k).map(|col| self.matrix[row * k + col]).sum();
if row_sum > 0 {
let denom = row_sum as f64;
for col in 0..k {
norm[row * k + col] = self.matrix[row * k + col] as f64 / denom;
}
}
}
norm
}
}
#[derive(Debug, Clone, Serialize)]
pub struct TideErrors {
pub delta_ap: HashMap<String, f64>,
pub counts: HashMap<String, u64>,
pub ap_base: f64,
pub pos_thr: f64,
pub bg_thr: f64,
}
#[derive(Debug, Clone)]
pub struct EvalImg {
pub image_id: u64,
pub category_id: u64,
pub area_rng: [f64; 2],
pub max_det: usize,
pub dt_ids: Vec<u64>,
pub gt_ids: Vec<u64>,
pub dt_matches: Vec<Vec<u64>>,
pub gt_matches: Vec<Vec<u64>>,
pub dt_matched: Vec<Vec<bool>>,
pub gt_matched: Vec<Vec<bool>>,
pub dt_scores: Vec<f64>,
pub gt_ignore: Vec<bool>,
pub dt_ignore: Vec<Vec<bool>>,
}
#[derive(Debug, Clone, Copy)]
pub struct EvalShape {
pub t: usize,
pub r: usize,
pub k: usize,
pub a: usize,
pub m: usize,
}
impl EvalShape {
pub fn precision_idx(&self, t: usize, r: usize, k: usize, a: usize, m: usize) -> usize {
((((t * self.r + r) * self.k + k) * self.a + a) * self.m) + m
}
pub fn recall_idx(&self, t: usize, k: usize, a: usize, m: usize) -> usize {
(((t * self.k + k) * self.a + a) * self.m) + m
}
}
#[derive(Debug, Clone)]
pub struct AccumulatedEval {
pub precision: Vec<f64>,
pub recall: Vec<f64>,
pub scores: Vec<f64>,
pub shape: EvalShape,
}
impl AccumulatedEval {
pub fn precision_idx(&self, t: usize, r: usize, k: usize, a: usize, m: usize) -> usize {
self.shape.precision_idx(t, r, k, a, m)
}
pub fn recall_idx(&self, t: usize, k: usize, a: usize, m: usize) -> usize {
self.shape.recall_idx(t, k, a, m)
}
}
pub(super) struct EvalImgContext<'a> {
pub(super) coco_gt: &'a COCO,
pub(super) coco_dt: &'a COCO,
pub(super) params: &'a Params,
pub(super) ious: &'a HashMap<(u64, u64), IouMatrix>,
pub(super) eval_mode: super::EvalMode,
}