Class OnnxInferenceEngine
- Namespace
- VisioForge.Core.ONNX
- Assembly
- VisioForge.Core.ONNX.dll
Wraps a Microsoft.ML.OnnxRuntime.InferenceSession and provides reusable RGBA-frame preprocessing (letterbox resize + NCHW conversion + optional normalization) and execution.
public class OnnxInferenceEngine : IDisposableInheritance
Implements
Inherited Members
Remarks
This engine is the shared building block for ONNX-based blocks. It loads the model, selects the execution provider, exposes the output metadata, preprocesses a VisioForge.Core.Types.X.VideoFrameX into a Microsoft.ML.OnnxRuntime.Tensors.DenseTensor`1, and runs inference. The YOLO detector builds on top of it. Inference itself is synchronous and CPU/GPU bound; callers run it on the frame-processing thread.
Constructors
OnnxInferenceEngine(OnnxInferenceSettings)
Initializes a new instance of the VisioForge.Core.ONNX.OnnxInferenceEngine class.
public OnnxInferenceEngine(OnnxInferenceSettings settings)Parameters
settingsOnnxInferenceSettings-
The inference settings.
Exceptions
- ArgumentNullException
-
Thrown when
settingsis null.
Properties
ActiveProvider
Gets the execution provider that the session actually engaged. When the requested provider is VisioForge.Core.Types.X.AI.OnnxExecutionProvider.Auto this reflects the auto-selected backend; when a hardware provider was requested but is unavailable, this reports VisioForge.Core.Types.X.AI.OnnxExecutionProvider.CPU. Valid after VisioForge.Core.ONNX.OnnxInferenceEngine.Initialize; defaults to VisioForge.Core.Types.X.AI.OnnxExecutionProvider.CPU beforehand.
public OnnxExecutionProvider ActiveProvider { get; }Property Value
- OnnxExecutionProvider
InputHeight
Gets the configured model input height, in pixels.
public int InputHeight { get; }Property Value
InputWidth
Gets the configured model input width, in pixels.
public int InputWidth { get; }Property Value
IsInitialized
Gets a value indicating whether the inference session has been created successfully.
public bool IsInitialized { get; }Property Value
OutputNames
Gets the output tensor names exposed by the loaded model.
public IReadOnlyList<string> OutputNames { get; }Property Value
Methods
Dispose()
Releases the inference session and session options.
public void Dispose()Dispose(bool)
Releases unmanaged and - optionally - managed resources.
protected virtual void Dispose(bool disposing)Parameters
disposingbool-
trueto release both managed and unmanaged resources;falseto release only unmanaged resources.
GetAvailableProviders()
Gets the names of the execution providers compiled into the loaded ONNX Runtime native build
(for example, "DmlExecutionProvider", "CUDAExecutionProvider", "CPUExecutionProvider").
An application can call this before building a pipeline to detect whether GPU acceleration is available.
public static IReadOnlyList<string> GetAvailableProviders()Returns
- IReadOnlyList<string>
-
The available execution provider names; an empty list if the environment cannot be queried.
Initialize()
Loads the model and creates the inference session using the configured execution provider. Safe to call more than once; subsequent calls are no-ops.
public void Initialize()Exceptions
- FileNotFoundException
-
Thrown when the model file does not exist.
Preprocess(VideoFrameX, out float, out float, out float)
Preprocesses an RGBA video frame into an NCHW float tensor using letterbox resizing.
public DenseTensor<float> Preprocess(VideoFrameX frame, out float scale, out float padX, out float padY)Parameters
frameVideoFrameX-
The source RGBA frame.
scalefloat-
Receives the uniform scale factor applied to the source frame.
padXfloat-
Receives the horizontal padding (in model-input pixels) added on the left.
padYfloat-
Receives the vertical padding (in model-input pixels) added on the top.
Returns
- DenseTensor<float>
-
The preprocessed tensor with shape [1, 3, InputHeight, InputWidth].
Run(DenseTensor<float>, out IReadOnlyDictionary<string, int[]>)
Runs inference on a preprocessed input tensor and returns the raw outputs flattened to float arrays. The engine is lazily initialized on first use.
public IReadOnlyDictionary<string, float[]> Run(DenseTensor<float> input, out IReadOnlyDictionary<string, int[]> shapes)Parameters
inputDenseTensor<float>-
The preprocessed input tensor.
shapesIReadOnlyDictionary<string, int[]>-
Receives the shape of each output tensor, keyed by output name.
Returns
- IReadOnlyDictionary<string, float[]>
-
The flattened raw outputs, keyed by output name.
SetContext(BaseContext)
Sets the processing context used for logging.
public void SetContext(BaseContext context)Parameters
contextBaseContext-
The context.