Skip to main content

InferenceConfig

Struct InferenceConfig 

Source
pub struct InferenceConfig {
    pub confidence_threshold: f32,
    pub iou_threshold: f32,
    pub max_det: usize,
    pub imgsz: Option<(usize, usize)>,
    pub batch: Option<usize>,
    pub num_threads: usize,
    pub half: bool,
    pub device: Option<Device>,
    pub save: bool,
    pub save_frames: bool,
    pub rect: bool,
    pub classes: Option<Vec<usize>>,
}
Expand description

Configuration for YOLO inference.

This struct is used to customize the behavior of the inference engine. It uses a builder pattern for convenient construction.

§Examples

Basic configuration:

use ultralytics_inference::InferenceConfig;

let config = InferenceConfig::new()
    .with_confidence(0.5)
    .with_iou(0.45)
    .with_max_det(300)
    .with_imgsz(640, 640);

With specific hardware device:

use ultralytics_inference::{InferenceConfig, Device};

let config = InferenceConfig::new()
    .with_confidence(0.5)
    .with_device(Device::Cuda(0));

Fields§

§confidence_threshold: f32

Confidence threshold for detections (0.0 to 1.0). Detections with confidence scores lower than this value will be discarded.

§iou_threshold: f32

Intersection over Union (IoU) threshold for Non-Maximum Suppression (NMS) (0.0 to 1.0). Used to merge overlapping boxes. Lower values filter more duplicates.

§max_det: usize

Maximum number of detections to return per image. The top-k detections sorted by confidence will be returned.

§imgsz: Option<(usize, usize)>

Explicit input image size (height, width). If None, the model’s metadata will be used to determine input size.

§batch: Option<usize>

Batch size for inference when using BatchProcessor. If None, defaults to 1 (single-image inference).

§num_threads: usize

Number of intra-op threads for ONNX Runtime. Setting this to 0 allows ONNX Runtime to choose the optimal number.

§half: bool

Whether to use FP16 (half-precision) inference. This can improve performance on compatible hardware (e.g., GPUs) but may result in slight precision loss.

§device: Option<Device>

Hardware device to use for inference. If None, the best available device will be automatically selected.

§save: bool

Whether to save annotated results. Defaults to true.

§save_frames: bool

Whether to save individual frames instead of a video file when input is video. Defaults to false (save as video).

§rect: bool

Whether to use minimal padding (rectangular inference). Defaults to true.

§classes: Option<Vec<usize>>

Class IDs to filter predictions. If None, all classes are returned. Useful for focusing on specific objects in multi-class detection tasks.

Implementations§

Source§

impl InferenceConfig

Source

pub const DEFAULT_CONF: f32 = 0.25

Default confidence threshold (0.0 to 1.0).

Source

pub const DEFAULT_IOU: f32 = 0.7

Default IoU threshold for NMS (0.0 to 1.0).

Source

pub const DEFAULT_MAX_DET: usize = 300

Default maximum number of detections per image.

Source

pub const DEFAULT_HALF: bool = false

Default for FP16 half-precision inference.

Source

pub const DEFAULT_SAVE: bool = true

Default for saving annotated results.

Source

pub const DEFAULT_SAVE_FRAMES: bool = false

Default for saving individual frames (vs video).

Source

pub const DEFAULT_RECT: bool = true

Default for rectangular (minimal padding) inference.

Source

pub fn new() -> Self

Create a new configuration with default values.

§Returns
  • A new InferenceConfig instance with default settings.
Source

pub const fn with_batch(self, batch: usize) -> Self

Set the batch size.

§Arguments
  • batch - The batch size.
§Returns
  • The modified InferenceConfig.
Source

pub const fn with_confidence(self, threshold: f32) -> Self

Set the confidence threshold.

Detections with a confidence score below this threshold will be filtered out.

§Arguments
  • threshold - The minimum confidence score (0.0 to 1.0).
§Returns
  • The modified InferenceConfig.
Source

pub const fn with_iou(self, threshold: f32) -> Self

Set the IoU threshold for Non-Maximum Suppression (NMS).

NMS suppresses overlapping bounding boxes. This threshold determines how much overlap is allowed before boxes are considered duplicates.

§Arguments
  • threshold - The IoU threshold (0.0 to 1.0).
§Returns
  • The modified InferenceConfig.
Source

pub const fn with_max_det(self, max: usize) -> Self

Set the maximum number of detections to return.

Only the top max detections (sorted by confidence) will be kept after NMS.

§Arguments
  • max - The maximum number of detections.
§Returns
  • The modified InferenceConfig.
Source

pub const fn with_imgsz(self, height: usize, width: usize) -> Self

Set the input image size.

This explicitly sets the size to resize images to before inference. If not set, the model’s internal metadata size will be used.

§Arguments
  • height - The target image height.
  • width - The target image width.
§Returns
  • The modified InferenceConfig.
Source

pub const fn with_threads(self, threads: usize) -> Self

Set the number of threads for inference.

§Arguments
  • threads - The number of intra-op threads. Set to 0 for auto-configuration.
§Returns
  • The modified InferenceConfig.
Source

pub const fn with_half(self, half: bool) -> Self

Enable or disable FP16 (half-precision) inference.

Using FP16 can significantly speed up inference on GPUs and some CPUS, at the cost of potential minor precision loss.

§Arguments
  • half - true to enable FP16, false for FP32.
§Returns
  • The modified InferenceConfig.
Source

pub const fn with_device(self, device: Device) -> Self

Set the hardware device for inference.

§Arguments
  • device - The device to use (e.g., CPU, CUDA, MPS).
§Example
use ultralytics_inference::{InferenceConfig, Device};

let config = InferenceConfig::new()
    .with_device(Device::Mps); // Use Apple Metal Performance Shaders
§Returns
  • The modified InferenceConfig.
Source

pub const fn with_save(self, save: bool) -> Self

Set whether to save annotated results.

§Arguments
  • save - true to save results, false to skip saving.
§Returns
  • The modified InferenceConfig.
Source

pub const fn with_save_frames(self, save_frames: bool) -> Self

Set whether to save individual frames for video inputs.

§Arguments
  • save_frames - true to save frames, false to save as video.
§Returns
  • The modified InferenceConfig.
Source

pub const fn with_rect(self, rect: bool) -> Self

Set whether to use minimal padding (rectangular inference).

§Arguments
  • rect - true to enable, false to disable.
§Returns
  • The modified InferenceConfig.
Source

pub fn with_classes(self, classes: Vec<usize>) -> Self

Set the class IDs to filter predictions.

Only detections belonging to the specified classes will be returned.

§Arguments
  • classes - A vector of class IDs to keep.
§Example
use ultralytics_inference::InferenceConfig;

// Only detect persons (class 0) and cars (class 2)
let config = InferenceConfig::new()
    .with_classes(vec![0, 2]);
§Returns
  • The modified InferenceConfig.
Source

pub fn keep_class(&self, class_id: usize) -> bool

Check if a class should be included in the results.

§Arguments
  • class_id - The class index to check.
§Returns
  • true if the class should be kept.
  • false if the class should be filtered out.

Trait Implementations§

Source§

impl Clone for InferenceConfig

Source§

fn clone(&self) -> InferenceConfig

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for InferenceConfig

Source§

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

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

impl Default for InferenceConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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