Skip to content

Commit fd5ca04

Browse files
committed
fix: update the openvino exmaple
Signed-off-by: Sylveon <[email protected]>
1 parent 1426b10 commit fd5ca04

File tree

7 files changed

+63
-44
lines changed

7 files changed

+63
-44
lines changed

openvino-mobilenet-image/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Please refer to [WasmEdge Docs](https://wasmedge.org/docs/contribute/source/plug
5757
cargo build --target wasm32-wasip1 --release
5858
cd ..
5959

60-
wasmedge --dir .:. ./rust/target/wasm32-wasip1/release/wasmedge-wasinn-example-mobilenet.wasm mobilenet.xml mobilenet.bin input.jpg
60+
wasmedge --dir .:. ./rust/target/wasm32-wasip1/release/wasmedge-wasinn-example-mobilenet-image.wasm mobilenet.xml mobilenet.bin input.jpg
6161
```
6262

6363
If the commands above run successfully, you will get the output:
Binary file not shown.

openvino-mobilenet-raw/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Please refer to [WasmEdge Docs](https://wasmedge.org/docs/contribute/source/plug
5757
cargo build --target wasm32-wasip1 --release
5858
cd ..
5959

60-
wasmedge --dir .:. ./rust/target/wasm32-wasip1/release/wasmedge-wasinn-example-mobilenet-image.wasm mobilenet.xml mobilenet.bin input.jpg
60+
wasmedge --dir .:. ./rust/target/wasm32-wasip1/release/wasmedge-wasinn-example-mobilenet.wasm mobilenet.xml mobilenet.bin tensor-1x224x224x3-f32.bgr
6161
```
6262

6363
If the commands above run successfully, you will get the output:

openvino-mobilenet-raw/rust/src/main.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,36 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
2222

2323
// Load a tensor that precisely matches the graph input tensor (see
2424
// `fixture/frozen_inference_graph.xml`).
25-
print!("Set input tensor ...");
25+
print!("Load input tensor ...");
2626
let input_dims = vec![1, 3, 224, 224];
2727
let tensor_data = fs::read(tensor_name).unwrap();
28-
context.set_input(0, TensorType::F32, &input_dims, tensor_data)?;
28+
println!("done");
29+
30+
print!("Transpose input tensor ...");
31+
// Transpose from [height, width, 3] to [3, height, width]
32+
// For the historical reasons, the input tensor is in the format of [height, width, channels],
33+
// but the graph expects it in the format of [channels, height, width].
34+
// The input tensor is 224x224x3, so we need to transpose it
35+
// to 3x224x224.
36+
let height = 224;
37+
let width = 224;
38+
println!("tensor_data.len() = {}", tensor_data.len());
39+
let mut transposed: Vec<u8> = vec![0; tensor_data.len()];
40+
for ch in 0..3 {
41+
for y in 0..height {
42+
for x in 0..width {
43+
let loc = y * height + x;
44+
for b in 0..4 {
45+
transposed[(ch * width * height + loc) * 4 + b as usize] =
46+
tensor_data[(loc * 3 + ch) * 4 + b as usize];
47+
}
48+
}
49+
}
50+
}
51+
println!("done");
52+
53+
print!("Set input tensor ...");
54+
context.set_input(0, TensorType::F32, &input_dims, transposed)?;
2955
println!("done");
3056

3157
print!("Perform graph inference ...");

openvino-road-segmentation-adas/openvino-road-seg-adas/src/main.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@ fn image_to_tensor(path: String, height: u32, width: u32) -> Vec<u8> {
7777
let bytes_required = raw_u8_arr.len() * 4;
7878
let mut u8_f32_arr: Vec<u8> = vec![0; bytes_required];
7979

80-
for i in 0..raw_u8_arr.len() {
81-
// Read the number as a f32 and break it into u8 bytes
82-
let u8_f32: f32 = raw_u8_arr[i] as f32;
83-
let u8_bytes = u8_f32.to_ne_bytes();
84-
85-
for j in 0..4 {
86-
u8_f32_arr[(i * 4) + j] = u8_bytes[j];
80+
// put in NCHW format
81+
let block_size: usize = (height * width) as usize;
82+
for c in 0..3 {
83+
for i in 0..block_size {
84+
// Read the number as a f32 and break it into u8 bytes
85+
let u8_f32: f32 = raw_u8_arr[i * 3 + c] as f32;
86+
let u8_bytes = u8_f32.to_ne_bytes();
87+
88+
for j in 0..4 {
89+
u8_f32_arr[(c * block_size + i) * 4 + j] = u8_bytes[j];
90+
}
8791
}
8892
}
8993
return u8_f32_arr;
Binary file not shown.

openvino-road-segmentation-adas/visualize_inference_result.ipynb

Lines changed: 22 additions & 33 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)