Topk

Struct Topk 

Source
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

Source

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));
Source

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();
Source

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);
Source

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();
Source

pub fn get_class_name(&self, class_id: usize) -> Option<&String>

Gets the class name for a given class ID.

§Arguments
  • class_id - The class ID to look up.
§Returns
  • Option<&String> - The class name if available.
Source

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.
Source

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.
Source

pub fn set_class_mapping( &mut self, class_id_map: Option<HashMap<usize, String>>, )

Updates the class name mapping.

§Arguments
  • class_id_map - New class ID to name mapping.
Source

pub fn process_single( &self, prediction: &[f32], k: usize, ) -> Result<TopkResult, String>

Processes a single prediction vector.

§Arguments
  • prediction - Vector of confidence scores for all classes.
  • k - Number of top predictions to extract.
§Returns
  • Ok(TopkResult) - The top-k results for the single prediction.
  • Err(String) - If k is 0 or if the input is invalid.

Trait Implementations§

Source§

impl Debug for Topk

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Topk

Source§

fn default() -> Self

Creates a default Topk processor without class name mapping.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

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§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

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 SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more