Skip to content

Commit b201db0

Browse files
committed
openvino-mobilenet-image: fix tensor shape
tested with: * wasmtime * wasm-micro-runtime with bytecodealliance/wasm-micro-runtime#4308 openvino-mobilenet-raw has the same issue. it can be fixed by transposing the tensor file as suggested in: as described inhttps://github.com/bytecodealliance/wasmtime/issues/10867#issuecomment-2927998250 i guess these tests have the same origin as wasmtime's classification-example and share the same bug. see bytecodealliance/wasmtime#10867 for details of the bug.
1 parent b84e9ce commit b201db0

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

openvino-mobilenet-image/rust/src/imagenet_classes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,4 +1018,4 @@ pub const IMAGENET_CLASSES: [&str; 1000] = [
10181018
"bolete",
10191019
"ear, spike, capitulum",
10201020
"toilet tissue, toilet paper, bathroom tissue"
1021-
];
1021+
];

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,26 @@ fn image_to_tensor(path: String, height: u32, width: u32) -> Vec<u8> {
7474
let bgr_img = dyn_img.to_bgr8();
7575
// Get an array of the pixel values
7676
let raw_u8_arr: &[u8] = &bgr_img.as_raw()[..];
77+
78+
// Transpose from [height, width, 3] to [3, height, width]
79+
let mut transposed: Vec<u8> = vec![0; raw_u8_arr.len()];
80+
for ch in 0..3 {
81+
for y in 0..height {
82+
for x in 0..width {
83+
let loc = y * height + x;
84+
transposed[(ch * width * height + loc) as usize] =
85+
raw_u8_arr[(loc * 3 + ch) as usize];
86+
}
87+
}
88+
}
89+
7790
// Create an array to hold the f32 value of those pixels
78-
let bytes_required = raw_u8_arr.len() * 4;
91+
let bytes_required = transposed.len() * 4;
7992
let mut u8_f32_arr: Vec<u8> = vec![0; bytes_required];
8093

81-
for i in 0..raw_u8_arr.len() {
94+
for i in 0..transposed.len() {
8295
// Read the number as a f32 and break it into u8 bytes
83-
let u8_f32: f32 = raw_u8_arr[i] as f32;
96+
let u8_f32: f32 = transposed[i] as f32;
8497
let u8_bytes = u8_f32.to_ne_bytes();
8598

8699
for j in 0..4 {

0 commit comments

Comments
 (0)