File size: 1,557 Bytes
0c3aa26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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);