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: f32Confidence threshold for detections (0.0 to 1.0). Detections with confidence scores lower than this value will be discarded.
iou_threshold: f32Intersection 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: usizeMaximum 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: usizeNumber of intra-op threads for ONNX Runtime.
Setting this to 0 allows ONNX Runtime to choose the optimal number.
half: boolWhether 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: boolWhether to save annotated results.
Defaults to true.
save_frames: boolWhether to save individual frames instead of a video file when input is video.
Defaults to false (save as video).
rect: boolWhether 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
impl InferenceConfig
Sourcepub const DEFAULT_CONF: f32 = 0.25
pub const DEFAULT_CONF: f32 = 0.25
Default confidence threshold (0.0 to 1.0).
Sourcepub const DEFAULT_IOU: f32 = 0.7
pub const DEFAULT_IOU: f32 = 0.7
Default IoU threshold for NMS (0.0 to 1.0).
Sourcepub const DEFAULT_MAX_DET: usize = 300
pub const DEFAULT_MAX_DET: usize = 300
Default maximum number of detections per image.
Sourcepub const DEFAULT_HALF: bool = false
pub const DEFAULT_HALF: bool = false
Default for FP16 half-precision inference.
Sourcepub const DEFAULT_SAVE: bool = true
pub const DEFAULT_SAVE: bool = true
Default for saving annotated results.
Sourcepub const DEFAULT_SAVE_FRAMES: bool = false
pub const DEFAULT_SAVE_FRAMES: bool = false
Default for saving individual frames (vs video).
Sourcepub const DEFAULT_RECT: bool = true
pub const DEFAULT_RECT: bool = true
Default for rectangular (minimal padding) inference.
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new configuration with default values.
§Returns
- A new
InferenceConfiginstance with default settings.
Sourcepub const fn with_batch(self, batch: usize) -> Self
pub const fn with_batch(self, batch: usize) -> Self
Sourcepub const fn with_confidence(self, threshold: f32) -> Self
pub const fn with_confidence(self, threshold: f32) -> Self
Sourcepub const fn with_max_det(self, max: usize) -> Self
pub const fn with_max_det(self, max: usize) -> Self
Sourcepub const fn with_imgsz(self, height: usize, width: usize) -> Self
pub const fn with_imgsz(self, height: usize, width: usize) -> Self
Sourcepub const fn with_threads(self, threads: usize) -> Self
pub const fn with_threads(self, threads: usize) -> Self
Sourcepub const fn with_device(self, device: Device) -> Self
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.
Sourcepub const fn with_save_frames(self, save_frames: bool) -> Self
pub const fn with_save_frames(self, save_frames: bool) -> Self
Sourcepub fn with_classes(self, classes: Vec<usize>) -> Self
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.
Trait Implementations§
Source§impl Clone for InferenceConfig
impl Clone for InferenceConfig
Source§fn clone(&self) -> InferenceConfig
fn clone(&self) -> InferenceConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for InferenceConfig
impl Debug for InferenceConfig
Auto Trait Implementations§
impl Freeze for InferenceConfig
impl RefUnwindSafe for InferenceConfig
impl Send for InferenceConfig
impl Sync for InferenceConfig
impl Unpin for InferenceConfig
impl UnsafeUnpin for InferenceConfig
impl UnwindSafe for InferenceConfig
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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>
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>
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>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
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
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.