InferenceSession class is the main entry point for running inference with ONNX models in C#.
Namespace
Class Declaration
Constructors
InferenceSession(string)
Constructs an InferenceSession from a model file.modelPath(string): Path to the ONNX or ORT model file
InferenceSession(string, SessionOptions)
Constructs an InferenceSession with custom session options.modelPath(string): Path to the model fileoptions(SessionOptions): Session configuration options
InferenceSession(byte[])
Constructs an InferenceSession from a model in a byte array.model(byte[]): ONNX model as byte array
InferenceSession(byte[], SessionOptions)
Constructs an InferenceSession from bytes with custom options.InferenceSession with PrePackedWeightsContainer
Constructs a session that shares pre-packed weights across multiple sessions.modelPath(string): Path to the modeloptions(SessionOptions): Session optionsprepackedWeightsContainer(PrePackedWeightsContainer): Shared weights container
Properties
InputMetadata
Gets metadata for input nodes.InputNames
Gets ordered list of input names.OutputMetadata
Gets metadata for output nodes.OutputNames
Gets ordered list of output names.OverridableInitializerMetadata
Gets metadata for overridable initializers.Methods
Run
Runs inference on the model.Run(IReadOnlyCollection<NamedOnnxValue>)
inputs: Collection of input tensors
Run(IReadOnlyCollection<NamedOnnxValue>, IReadOnlyCollection<string>)
Runs inference with specific output names.Run(IReadOnlyCollection<NamedOnnxValue>, RunOptions)
Runs inference with custom run options.RunAsync
Asynchronously runs inference.GetMemoryInfosForInputs
Gets memory information for all inputs.GetMemoryInfosForOutputs
Gets memory information for all outputs.Dispose
Releases resources used by the session.Complete Example
Image Classification
Batch Processing
Thread Safety
TheInferenceSession class is thread-safe for Run() operations. Multiple threads can call Run() concurrently on the same session instance.
Performance Tips
- Reuse sessions: Create session once, use many times
- Use SessionOptions: Configure optimizations appropriately
- Dispose properly: Always dispose sessions and results
- Batch when possible: Process multiple inputs together
- Use execution providers: Enable GPU acceleration when available