second-state / wasmedge-extensions

WasmEdge for Node.js Addon with extensions
Apache License 2.0
3 stars 1 forks source link

Returned image value from RunUint8Array function is not an actual image #8

Open tpmccallum opened 2 years ago

tpmccallum commented 2 years ago

Hi, Please see the following in relation to performing the FaaS Face Detection via wasmedge-extensions.

Compiled the lib.rs as per this standard source code

Installed wasmedge-extensions on Ubuntu 20 and created the following environment to run the wasm file via NodeJs

// Import file system library
const fs = require('fs');

// Create wasmedge instance
const wasmedge = require("wasmedge-extensions");

// Load the wasm binary
const path = "/home/tpmccallum/wasm.wasm";
vm = new wasmedge.VM(path, { args:process.argv, env:process.env, preopens:{"/": "/tmp"} });

// Define an AOT path
aot_path = "/home/tpmccallum/wasm.so";

// Create an AOT binary
vm.Compile(aot_path);

// Instantiate VM using AOT mode
var vm_aot = new wasmedge.VM(aot_path, { EnableAOT:true, rgs:process.argv, env:process.env, preopens:{"/": "/tmp"} });

// Open image
var img_src = fs.readFileSync("/home/tpmccallum/solvay.jpg");

// Run function by passing in the image and calling the infer function
var return_value = vm_aot.RunUint8Array("infer", img_src);

// Write image to file
fs.writeFileSync("res.jpg", return_value);

The output from this is error free (no obvious errors or issue), however the res.jpg is not actually an image. Please find the following output for your convenience.

tpmccallum@tpmccallum:$ node tester.js
[2022-01-01 10:39:57.548] [info] compile start
[2022-01-01 10:39:57.619] [info] verify start
[2022-01-01 10:39:57.681] [info] optimize start
[2022-01-01 10:40:04.028] [info] codegen start
[2022-01-01 10:40:09.950] [info] output start
[2022-01-01 10:40:09.957] [info] compile done
[2022-01-01 10:40:09.964] [info] output start
Loaded image in ... 37.909333ms
2022-01-01 10:40:10.109754: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE3 SSE4.1 SSE4.2 AVX AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Parsed results in ... 533.284485ms
Drawing box: 30 results ...
Drawn on image in ... 582.29647ms

The Rust/Wasm is able to detect 30 faces which is correct, however for some reason the return value is not correct. Please see a snippet of the returned value in its raw form; just using vi (hope this helps).

Screen Shot 2022-01-01 at 11 25 54 am

tpmccallum commented 2 years ago

The same issue exists if non AOT mode is used i.e. just run the .wasm file (instead of the .so file)

var return_value = vm.RunUint8Array("infer", img_src);

Thanks so much!

juntao commented 2 years ago

It is most likely a wasm-bindgen implementation bug in our NAPI. I would prefer to wait for node.js support for wasmedge-bindgen and update all examples to use wasmedge-bindgen.