IMPACTSeg / extension /src /lib.rs
HCedric's picture
Run the networks as KonfAI apps: fine-3d, fine-2.5d and coarse
0c3aa26 verified
Raw History Blame Contribute Delete
1.56 kB
// SPDX-License-Identifier: Apache-2.0
//! IMPACT-Seg's extension of KonfAI Edge.
#![allow(unsafe_code)]
use konfai_edge_ext::{Opened, PatchCombine};
/// Each pixel keeps the label of the patch surest of it: a patch is `[label, score]`, the higher score stays, the
/// later patch's at a tie, and the labels are written.
struct MostConfident;
impl PatchCombine for MostConfident {
fn output_channels(&self, _channels: usize) -> usize {
1
}
fn fold(&self, held: &mut [&mut [f32]], patch: &[&[f32]]) {
let ([labels, scores], [patch_labels, patch_scores]) = (held, patch) else {
panic!("MostConfident folds [label, score]")
};
let patch = patch_labels.iter().zip(patch_scores.iter());
for ((label, score), (&patch_label, &patch_score)) in labels.iter_mut().zip(scores.iter_mut()).zip(patch) {
let keep = patch_score >= *score;
*label = if keep { patch_label } else { *label };
*score = if keep { patch_score } else { *score };
}
}
fn leave(&self, held: &[&[f32]], output: &mut [&mut [f32]]) {
output[0].copy_from_slice(held[0]);
}
}
/// The classes of the app's own, by the classpath its configs name each by. The classes the model ignores are
/// zeroed as a patch enters the fold, in the model's graph: the fold reads none of its parameters.
fn open(name: &str, _params: &str) -> Option<Opened> {
(name == "fusion:MostConfident").then(|| Opened::PatchCombine(Box::new(MostConfident)))
}
konfai_edge_ext::export!(open);