Skip to content

Commit

Permalink
Adding more images to the demo (#46)
Browse files Browse the repository at this point in the history
Now the demos will run the identification on 5 images.
  • Loading branch information
brianjjones authored May 8, 2021
1 parent aedd452 commit 76ce651
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 51 deletions.
1 change: 1 addition & 0 deletions assemblyscript/demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ FIXTURE=https://github.com/intel/openvino-rs/raw/main/crates/openvino/tests/fixt
wget --no-clobber --directory-prefix=$DOWNLOAD_DIR $FIXTURE/mobilenet.bin
wget --no-clobber --directory-prefix=$DOWNLOAD_DIR $FIXTURE/mobilenet.xml
wget --no-clobber --directory-prefix=$DOWNLOAD_DIR $FIXTURE/tensor-1x224x224x3-f32.bgr
cp -rn images $DOWNLOAD_DIR

# Run the demo
wasmtime run build/optimized.wasm --dir build
35 changes: 19 additions & 16 deletions assemblyscript/examples/object-classification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,27 @@ import { IMAGENET_CLASSES } from "../assembly/imagenet_classes";
*/
export function main(): i32 {
Console.log("Loading graph...");

const graph = Graph.load([readBytes("mobilenet.xml"), readBytes("mobilenet.bin")], GraphEncoding.openvino, ExecutionTarget.cpu);

Console.log("Setting up execution context...");
const context = graph.initExecutionContext();
const input = new Tensor([1, 3, 224, 224], TensorType.f32, readBytes("tensor-1x224x224x3-f32.bgr"));
context.setInput(0, input);

Console.log("Running classification...");
context.compute();
let maxBufferLength = 4004; // Size of our output buffer
const output = context.getOutput(0, new Array<u8>(4004).fill(0));
for (let i = 0; i < 5; i++) {
const input = new Tensor([1, 3, 224, 224], TensorType.f32, readBytes("images/" + i.toString() + ".bgr"));
context.setInput(0, input);

const results = sortResults(output, 5);
Console.log("Top 5 results: ");
// TODO figure out why we cannot use `forEach` here.
for (let i = 0; i < results.length; i++) {
Console.log((i + 1).toString() + ".) " + IMAGENET_CLASSES[results[i].id] + " : (" + results[i].id.toString() + ", " + results[i].probability.toString() + ")");
Console.log("Running classification...");
context.compute();
let maxBufferLength = 4004; // Size of our output buffer
const output = context.getOutput(0, new Array<u8>(4004).fill(0));

const results = sortResults(output, 5);
Console.log("Top 5 results: ");

// TODO figure out why we cannot use `forEach` here.
for (let i = 0; i < results.length; i++) {
Console.log((i + 1).toString() + ".) " + IMAGENET_CLASSES[results[i].id] + " : (" + results[i].id.toString() + ", " + results[i].probability.toString() + ")");
}
}
return 0;
}
Expand Down Expand Up @@ -76,10 +79,10 @@ class Result {
* https://github.com/jedisct1/as-wasi/blob/master/assembly/as-wasi.ts#L1100); that function should
* be exported in as-wasi's `index` (TODO) to make it accessible using `--use
* abort=as-wasi/wasi_abort` (see https://www.assemblyscript.org/debugging.html#overriding-abort).
* @param message
* @param fileName
* @param lineNumber
* @param columnNumber
* @param message
* @param fileName
* @param lineNumber
* @param columnNumber
*/
export function wasi_abort(
message: string = "",
Expand Down
Binary file added assemblyscript/images/0.bgr
Binary file not shown.
Binary file added assemblyscript/images/1.bgr
Binary file not shown.
Binary file added assemblyscript/images/2.bgr
Binary file not shown.
Binary file added assemblyscript/images/3.bgr
Binary file not shown.
Binary file added assemblyscript/images/4.bgr
Binary file not shown.
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ else
cargo build --release --target=wasm32-wasi
mkdir -p $WASI_NN_DIR/rust/examples/classification-example/build
RUST_BUILD_DIR=$(realpath $WASI_NN_DIR/rust/examples/classification-example/build/)
cp -rn images $RUST_BUILD_DIR
pushd examples/classification-example
cargo build --release --target=wasm32-wasi
cp target/wasm32-wasi/release/wasi-nn-example.wasm $RUST_BUILD_DIR
Expand Down
73 changes: 38 additions & 35 deletions rust/examples/classification-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,48 @@ 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-1x224x224x3-f32.bgr").unwrap();
println!("Read input tensor, size in bytes: {}", tensor_data.len());
let tensor = wasi_nn::Tensor {
dimensions: &[1, 3, 224, 224],
r#type: wasi_nn::TENSOR_TYPE_F32,
data: &tensor_data,
};
unsafe {
wasi_nn::set_input(context, 0, tensor).unwrap();
}
for i in 0..4 {
let filename: String = format!("{}{}{}", "fixture/images/", i, ".bgr");
let tensor_data = fs::read(filename).unwrap();
println!("Read input tensor, size in bytes: {}", tensor_data.len());
let tensor = wasi_nn::Tensor {
dimensions: &[1, 3, 224, 224],
r#type: wasi_nn::TENSOR_TYPE_F32,
data: &tensor_data,
};
unsafe {
wasi_nn::set_input(context, 0, tensor).unwrap();
}

// Execute the inference.
unsafe {
wasi_nn::compute(context).unwrap();
}
println!("Executed graph inference");
// Execute the inference.
unsafe {
wasi_nn::compute(context).unwrap();
}
println!("Executed graph inference");

// Retrieve the output.
let mut output_buffer = vec![0f32; 1001];
unsafe {
wasi_nn::get_output(
context,
0,
&mut output_buffer[..] as *mut [f32] as *mut u8,
(output_buffer.len() * 4).try_into().unwrap(),
)
.unwrap();
}
let results = sort_results(&output_buffer);
println!(
"Found results, sorted top 5: {:?}",
&results[..5]
);
// Retrieve the output.
let mut output_buffer = vec![0f32; 1001];
unsafe {
wasi_nn::get_output(
context,
0,
&mut output_buffer[..] as *mut [f32] as *mut u8,
(output_buffer.len() * 4).try_into().unwrap(),
)
.unwrap();
}

let results = sort_results(&output_buffer);
println!(
"Found results, sorted top 5: {:?}",
&results[..5]
);

for i in 0..5 {
println!("{}.) {}", i + 1, imagenet_classes::IMAGENET_CLASSES[results[i].0]);
for i in 0..5 {
println!("{}.) {}", i + 1, imagenet_classes::IMAGENET_CLASSES[results[i].0]);
}
}

}

// Sort the buffer of probabilities. The graph places the match probability for each class at the
Expand Down
Binary file added rust/images/0.bgr
Binary file not shown.
Binary file added rust/images/1.bgr
Binary file not shown.
Binary file added rust/images/2.bgr
Binary file not shown.
Binary file added rust/images/3.bgr
Binary file not shown.
Binary file added rust/images/4.bgr
Binary file not shown.

0 comments on commit 76ce651

Please sign in to comment.