use std::collections::HashMap;
use std::path::Path;
use serde::Serialize;
use crate::params::{IouType, Params};
use super::EvalMode;
#[derive(Debug, Clone, Serialize)]
pub struct EvalParams {
pub iou_type: IouType,
pub iou_thresholds: Vec<f64>,
pub area_ranges: HashMap<String, [f64; 2]>,
pub max_dets: Vec<usize>,
pub eval_mode: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct EvalResults {
pub hotcoco_version: String,
pub params: EvalParams,
pub metrics: HashMap<String, f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub per_class: Option<HashMap<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(super) fn from_params(params: &Params, eval_mode: EvalMode) -> Self {
let area_ranges: HashMap<String, [f64; 2]> = params
.area_ranges
.iter()
.map(|ar| (ar.label.clone(), ar.range))
.collect();
let mode_str = match eval_mode {
EvalMode::Coco => "coco",
EvalMode::Lvis => "lvis",
EvalMode::OpenImages => "openimages",
};
EvalParams {
iou_type: params.iou_type,
iou_thresholds: params.iou_thrs.clone(),
area_ranges,
max_dets: params.max_dets.clone(),
eval_mode: mode_str.to_string(),
}
}
}