pub struct Topk { /* private fields */ }Expand description
A processor for extracting top-k results from classification outputs.
The Topk struct processes classification model outputs to extract the
top-k most confident predictions along with their class names (if available).
Implementations§
Source§impl Topk
impl Topk
Sourcepub fn new(class_id_map: Option<HashMap<usize, String>>) -> Self
pub fn new(class_id_map: Option<HashMap<usize, String>>) -> Self
Creates a new Topk processor with optional class name mapping.
§Arguments
class_id_map- Optional mapping from class IDs to human-readable class names.
§Examples
use std::collections::HashMap;
use oar_ocr::utils::topk::Topk;
let mut class_map = HashMap::new();
class_map.insert(0, "cat".to_string());
class_map.insert(1, "dog".to_string());
let topk = Topk::new(Some(class_map));Sourcepub fn without_class_names() -> Self
pub fn without_class_names() -> Self
Creates a new Topk processor without class name mapping.
§Examples
use oar_ocr::utils::topk::Topk;
let topk = Topk::without_class_names();Sourcepub fn from_class_names(class_names: Vec<String>) -> Self
pub fn from_class_names(class_names: Vec<String>) -> Self
Creates a new Topk processor with class names from a vector.
The vector index corresponds to the class ID.
§Arguments
class_names- Vector of class names where index = class ID.
§Examples
use oar_ocr::utils::topk::Topk;
let class_names = vec!["cat".to_string(), "dog".to_string(), "bird".to_string()];
let topk = Topk::from_class_names(class_names);Sourcepub fn process(
&self,
predictions: &[Vec<f32>],
k: usize,
) -> Result<TopkResult, String>
pub fn process( &self, predictions: &[Vec<f32>], k: usize, ) -> Result<TopkResult, String>
Processes classification outputs to extract top-k results.
§Arguments
predictions- 2D vector where each inner vector contains the confidence scores for all classes for one prediction.k- Number of top predictions to extract (must be > 0).
§Returns
Ok(TopkResult)- The top-k results with indexes, scores, and optional class names.Err(String)- If k is 0 or if the input is invalid.
§Examples
use oar_ocr::utils::topk::Topk;
let topk = Topk::without_class_names();
let predictions = vec![
vec![0.1, 0.8, 0.1], // Prediction 1: class 1 has highest score
vec![0.7, 0.2, 0.1], // Prediction 2: class 0 has highest score
];
let result = topk.process(&predictions, 2).unwrap();Sourcepub fn get_class_name(&self, class_id: usize) -> Option<&String>
pub fn get_class_name(&self, class_id: usize) -> Option<&String>
Sourcepub fn has_class_names(&self) -> bool
pub fn has_class_names(&self) -> bool
Checks if class name mapping is available.
§Returns
true- If class name mapping is available.false- If no class name mapping is available.
Sourcepub fn num_classes(&self) -> Option<usize>
pub fn num_classes(&self) -> Option<usize>
Gets the number of classes in the mapping.
§Returns
Option<usize>- Number of classes if mapping is available.
Sourcepub fn process_single(
&self,
prediction: &[f32],
k: usize,
) -> Result<TopkResult, String>
pub fn process_single( &self, prediction: &[f32], k: usize, ) -> Result<TopkResult, String>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Topk
impl RefUnwindSafe for Topk
impl Send for Topk
impl Sync for Topk
impl Unpin for Topk
impl UnwindSafe for Topk
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian().Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.