Repository navigation
Expand file tree
/
Copy path12_import_models.rs
More file actions
54 lines (47 loc) · 2.09 KB
/
Copy path12_import_models.rs
File metadata and controls
54 lines (47 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Chapter 12 — loading an ONNX model and running a batch through it.
//!
//! cargo run --release --features onnx --bin 12_import_models -- xor.onnx
//!
//! `xor.onnx` is whatever chapter 12.3 or 12.4 exported. Without a path the
//! program explains what it wants and exits.
use rusting_brain::onnx::{OnnxError, OnnxModel};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let Some(path) = std::env::args().nth(1) else {
eprintln!("usage: 12_import_models <model.onnx>");
eprintln!("export one first; see chapter 12.3 (Keras) or 12.4 (PyTorch)");
return Ok(());
};
// The four XOR inputs, row-major: [batch, features] = [4, 2].
let rows: [[f32; 2]; 4] = [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]];
let flat: Vec<f32> = rows.iter().flatten().copied().collect();
// 12.6 — a Keras export usually leaves the batch dimension symbolic, so
// the plain `load` fails on it and the shape has to be supplied. Try the
// easy path first and fall back rather than always passing a shape: a
// graph with a concrete shape does not need to be told its own.
let model = match OnnxModel::load(&path) {
Ok(model) => {
println!("loaded with the shape baked into the file");
model
}
Err(OnnxError::FeatureDisabled) => {
eprintln!("rebuild with `--features onnx`");
return Ok(());
}
Err(error) => {
println!("{error}; retrying with an explicit [4, 2]");
OnnxModel::load_with_input_shape(&path, &[4, 2])?
}
};
// One call over four rows, not four calls over one row: the whole batch is
// a single matrix multiply.
let outputs = model.predict(&flat)?;
for (row, output) in rows.iter().zip(&outputs) {
println!("{row:?} -> {output:.4}");
}
// 12.6 again — the length check is on the value count, not the row count.
match model.predict(&[0.0, 1.0]) {
Ok(_) => println!("this model takes a single row"),
Err(error) => println!("two values into a four-row model: {error}"),
}
Ok(())
}