How to Run ONNX Models in Rust

Run ONNX models in Rust by adding the ort crate and using SessionBuilder to load and execute the model file.

You run ONNX models in Rust by adding the ort crate to your project and using its session API to load and execute the model.

[dependencies]
ort = "2.0"
use ort::{Session, SessionBuilder, GraphOptimizationLevel};

fn main() -> ort::Result<()> {
    let session = SessionBuilder::new(&ort::Environment::new("ort")?)
        .with_optimization_level(GraphOptimizationLevel::Level3)?
        .commit_from_file("model.onnx")?;

    let inputs = ort::inputs![];
    let outputs = session.run(inputs)?;
    Ok(())
}