Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasi-nn: improve the example #2831

Merged
merged 2 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ci/run-wasi-nn-example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# executed with the Wasmtime CLI.
set -e
WASMTIME_DIR=$(dirname "$0" | xargs dirname)
FIXTURE=https://github.com/intel/openvino-rs/raw/main/crates/openvino/tests/fixtures/alexnet
FIXTURE=https://github.com/intel/openvino-rs/raw/main/crates/openvino/tests/fixtures/mobilenet
if [ -z "${1+x}" ]; then
# If no temporary directory is specified, create one.
TMP_DIR=$(mktemp -d -t ci-XXXXXXXXXX)
Expand All @@ -26,9 +26,9 @@ source /opt/intel/openvino/bin/setupvars.sh
OPENVINO_INSTALL_DIR=/opt/intel/openvino cargo build -p wasmtime-cli --features wasi-nn

# Download all necessary test fixtures to the temporary directory.
wget --no-clobber --directory-prefix=$TMP_DIR $FIXTURE/alexnet.bin
wget --no-clobber --directory-prefix=$TMP_DIR $FIXTURE/alexnet.xml
wget --no-clobber --directory-prefix=$TMP_DIR $FIXTURE/tensor-1x3x227x227-f32.bgr
wget --no-clobber $FIXTURE/mobilenet.bin --output-document=$TMP_DIR/model.bin
wget --no-clobber $FIXTURE/mobilenet.xml --output-document=$TMP_DIR/model.xml
wget --no-clobber $FIXTURE/tensor-1x224x224x3-f32.bgr --output-document=$TMP_DIR/tensor.bgr

# Now build an example that uses the wasi-nn API.
pushd $WASMTIME_DIR/crates/wasi-nn/examples/classification-example
Expand All @@ -42,4 +42,4 @@ OPENVINO_INSTALL_DIR=/opt/intel/openvino cargo run --features wasi-nn -- run --m
# Clean up the temporary directory only if it was not specified (users may want to keep the directory around).
if [[ $REMOVE_TMP_DIR -eq 1 ]]; then
rm -rf $TMP_DIR
fi
fi
5 changes: 3 additions & 2 deletions crates/wasi-nn/examples/classification-example/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2018"
publish = false

[dependencies]
wasi-nn = { path = "../wasi-nn-rust-bindings", version = "0.1.0" }
wasi-nn = "0.1.0"

# This crate is built with the wasm32-wasi target, so it's separate
# from the main Wasmtime build, so use this directive to exclude it
Expand Down
15 changes: 9 additions & 6 deletions crates/wasi-nn/examples/classification-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::fs;
use wasi_nn;

pub fn main() {
let xml = fs::read_to_string("fixture/alexnet.xml").unwrap();
let xml = fs::read_to_string("fixture/model.xml").unwrap();
println!("Read graph XML, first 50 characters: {}", &xml[..50]);

let weights = fs::read("fixture/alexnet.bin").unwrap();
let weights = fs::read("fixture/model.bin").unwrap();
println!("Read graph weights, size in bytes: {}", weights.len());

let graph = unsafe {
Expand All @@ -24,10 +24,10 @@ pub fn main() {

// Load a tensor that precisely matches the graph input tensor (see
// `fixture/frozen_inference_graph.xml`).
let tensor_data = fs::read("fixture/tensor-1x3x227x227-f32.bgr").unwrap();
let tensor_data = fs::read("fixture/tensor.bgr").unwrap();
println!("Read input tensor, size in bytes: {}", tensor_data.len());
let tensor = wasi_nn::Tensor {
dimensions: &[1, 3, 227, 227],
dimensions: &[1, 3, 224, 224],
r#type: wasi_nn::TENSOR_TYPE_F32,
data: &tensor_data,
};
Expand All @@ -42,7 +42,7 @@ pub fn main() {
println!("Executed graph inference");

// Retrieve the output.
let mut output_buffer = vec![0f32; 1000];
let mut output_buffer = vec![0f32; 1001];
unsafe {
wasi_nn::get_output(
context,
Expand All @@ -60,10 +60,13 @@ pub fn main() {

// 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.
// to a wrapping InferenceResult and sort the results. It is unclear why the MobileNet output
// indices are "off by one" but the `.skip(1)` below seems necessary to get results that make sense
// (e.g. 763 = "revolver" vs 762 = "restaurant")
fn sort_results(buffer: &[f32]) -> Vec<InferenceResult> {
let mut results: Vec<InferenceResult> = buffer
.iter()
.skip(1)
.enumerate()
.map(|(c, p)| InferenceResult(c, *p))
.collect();
Expand Down
1 change: 0 additions & 1 deletion crates/wasi-nn/examples/wasi-nn-rust-bindings/.gitignore

This file was deleted.

5 changes: 0 additions & 5 deletions crates/wasi-nn/examples/wasi-nn-rust-bindings/Cargo.lock

This file was deleted.

14 changes: 0 additions & 14 deletions crates/wasi-nn/examples/wasi-nn-rust-bindings/Cargo.toml

This file was deleted.

201 changes: 0 additions & 201 deletions crates/wasi-nn/examples/wasi-nn-rust-bindings/LICENSE

This file was deleted.

65 changes: 0 additions & 65 deletions crates/wasi-nn/examples/wasi-nn-rust-bindings/README.md

This file was deleted.

Loading