-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a benchmark for wasi-nn ONNX models.
- Loading branch information
Showing
5 changed files
with
124 additions
and
106 deletions.
There are no files selected for viewing
143 changes: 45 additions & 98 deletions
143
benchmarks/image-classification/rust-benchmark/Cargo.lock
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
benchmarks/image-classification/rust-benchmark/src/onnx.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use image::io::Reader; | ||
use image::DynamicImage; | ||
use sightglass_api as bench; | ||
use std::convert::TryInto; | ||
use std::fs; | ||
use wasi_nn; | ||
mod imagenet_classes; | ||
|
||
pub fn main() { | ||
// Convert image to tensor data. | ||
let tensor_data = fs::read("./kitten.rgb").unwrap(); | ||
|
||
// Load model from a file. | ||
let graph = | ||
wasi_nn::GraphBuilder::new(wasi_nn::GraphEncoding::Onnx, wasi_nn::ExecutionTarget::CPU) | ||
.build_from_files(["./mobilenet.onnx"]) | ||
.unwrap(); | ||
|
||
let mut context = graph.init_execution_context().unwrap(); | ||
context | ||
.set_input(0, wasi_nn::TensorType::F32, &[1, 3, 224, 224], &tensor_data) | ||
.unwrap(); | ||
|
||
bench::start(); | ||
|
||
// Execute the inference. | ||
context.compute().unwrap(); | ||
|
||
bench::end(); | ||
|
||
|
||
// Retrieve the output. | ||
let mut output_buffer = vec![0f32; 1000]; | ||
context.get_output(0, &mut output_buffer[..]).unwrap(); | ||
|
||
let result = sort_results(&output_buffer); | ||
} | ||
|
||
// Sort the buffer of probabilities. The graph places the match probability for each class at the | ||
// index for that class (e.g. the probability of class 42 is placed at buffer[42]). Here we convert | ||
// to a wrapping InferenceResult and sort the results. | ||
fn sort_results(buffer: &[f32]) -> Vec<InferenceResult> { | ||
let mut results: Vec<InferenceResult> = buffer | ||
.iter() | ||
.skip(1) | ||
.enumerate() | ||
.map(|(c, p)| InferenceResult(c, *p)) | ||
.collect(); | ||
results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap()); | ||
results | ||
} | ||
|
||
// A wrapper for class ID and match probabilities. | ||
#[derive(Debug, PartialEq)] | ||
struct InferenceResult(usize, f32); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters