Class InferenceSession
java.lang.Object
smile.onnx.InferenceSession
- All Implemented Interfaces:
AutoCloseable
Represents an ONNX Runtime inference session for a single model.
An InferenceSession loads an ONNX model, compiles it
(with optional graph optimisations), and exposes the run(Map) method
for executing inference.
Typical usage
try (var session = InferenceSession.create("resnet50.onnx")) {
// Inspect the model
session.inputNames().forEach(System.out::println);
// Build inputs
float[] pixels = ...; // 1 × 3 × 224 × 224
Map<String, OrtValue> inputs = Map.of(
"input", OrtValue.fromFloatArray(pixels, new long[]{1, 3, 224, 224})
);
// Run inference and read the output
try (var inputs = ...) {
OrtValue[] outputs = session.run(inputs);
float[] scores = outputs[0].toFloatArray();
// close outputs when done
for (var v : outputs) v.close();
}
}
Thread safety
A single InferenceSession may be used concurrently from multiple
threads. Each call to run(Map) is independent.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()static InferenceSessioncreate(byte[] modelBytes) Creates anInferenceSessionfrom a model byte array using default session options.static InferenceSessioncreate(byte[] modelBytes, SessionOptions sessionOptions) Creates anInferenceSessionfrom a model already loaded into a byte array (e.g.static InferenceSessionCreates anInferenceSessionfrom a model file path using default session options.static InferenceSessioncreate(String modelPath, SessionOptions sessionOptions) Creates anInferenceSessionfrom a model file path using the supplied session options.intReturns the number of model inputs.Returns the input node information list.Returns the ordered list of input names.metadata()Returns metadata associated with the model (producer name, version, etc.).intReturns the number of model outputs.Returns the output node information list.Returns the ordered list of output names.OrtValue[]Runs inference using all model inputs and all model outputs with default run options.OrtValue[]Runs inference for a selected set of outputs with default run options.OrtValue[]Runs inference with explicit run options.toString()
-
Method Details
-
create
Creates anInferenceSessionfrom a model file path using default session options.- Parameters:
modelPath- path to the.onnxmodel file.- Returns:
- the loaded session.
-
create
Creates anInferenceSessionfrom a model file path using the supplied session options.- Parameters:
modelPath- path to the.onnxmodel file.sessionOptions- session configuration.- Returns:
- the loaded session.
-
create
Creates anInferenceSessionfrom a model already loaded into a byte array (e.g. from a JAR resource).- Parameters:
modelBytes- the serialized ONNX model bytes.sessionOptions- session configuration.- Returns:
- the loaded session.
-
create
Creates anInferenceSessionfrom a model byte array using default session options.- Parameters:
modelBytes- the serialized ONNX model bytes.- Returns:
- the loaded session.
-
run
-
run
Runs inference for a selected set of outputs with default run options.- Parameters:
inputs- a map of input name →OrtValue.outputNames- the names of the outputs to compute.- Returns:
- the requested outputs in the supplied order.
-
run
Runs inference with explicit run options.- Parameters:
inputs- a map of input name →OrtValue.outputNames- the names of the outputs to compute.runOptions- per-run options, ornullfor defaults.- Returns:
- the requested outputs in the supplied order.
-
inputCount
public int inputCount()Returns the number of model inputs.- Returns:
- input count.
-
outputCount
public int outputCount()Returns the number of model outputs.- Returns:
- output count.
-
inputInfos
-
outputInfos
-
inputNames
-
outputNames
-
metadata
Returns metadata associated with the model (producer name, version, etc.).- Returns:
- the
ModelMetadata.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
toString
-