use std::collections::BTreeMap;
use std::path::Path;
use serde::Serialize;
use crate::params::{IouType, Params};
use crate::report::Provenance;
use super::EvalMode;
#[derive(Debug, Clone, Serialize)]
pub struct EvalParams {
pub iou_type: IouType,
pub iou_thresholds: Vec<f64>,
pub recall_thresholds: Vec<f64>,
pub area_ranges: BTreeMap<String, [f64; 2]>,
pub max_dets: Vec<usize>,
pub use_cats: bool,
pub kpt_oks_sigmas: Vec<f64>,
pub eval_mode: String,
pub reference_deviations: Vec<String>,
}
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct EvalResults {
pub hotcoco_version: String,
pub provenance: Provenance,
pub params: EvalParams,
pub metrics: BTreeMap<String, f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub per_class: Option<BTreeMap<String, f64>>,
}
impl EvalResults {
pub fn to_json(&self) -> crate::error::Result<String> {
Ok(serde_json::to_string_pretty(self)?)
}
pub fn save(&self, path: &Path) -> crate::error::Result<()> {
let file = std::fs::File::create(path)?;
let writer = std::io::BufWriter::new(file);
serde_json::to_writer_pretty(writer, self)?;
Ok(())
}
}
impl EvalParams {
pub(in crate::detection) fn from_eval(ev: &super::COCOeval) -> Self {
let params: &Params = &ev.params;
let area_ranges: BTreeMap<String, [f64; 2]> = params
.area_ranges
.iter()
.map(|ar| (ar.label.clone(), ar.range))
.collect();
let mode_str = match ev.eval_mode {
EvalMode::Coco => "coco",
EvalMode::Lvis => "lvis",
EvalMode::OpenImages => "openimages",
};
EvalParams {
iou_type: params.iou_type,
iou_thresholds: params.iou_thrs.clone(),
recall_thresholds: params.rec_thrs.clone(),
area_ranges,
max_dets: params.max_dets.clone(),
use_cats: params.use_cats,
kpt_oks_sigmas: params.kpt_oks_sigmas.clone(),
eval_mode: mode_str.to_string(),
reference_deviations: ev.reference_deviations(),
}
}
}