5 个版本
0.1.4 | 2021年5月19日 |
---|---|
0.1.3 | 2020年12月2日 |
0.1.2 | 2020年11月20日 |
0.1.1 | 2020年11月17日 |
0.1.0 | 2020年11月12日 |
#849 in WebAssembly
20KB
419 行
此存储库已弃用
此库已被重命名并移动到 wasmedge_tensorflow_interface
。请关注新库 wasmedge_tensorflow_interface 的进一步开发。此存储库将不再进行开发。
SSVM Tensorflow Interface
一个Rust库,为在SecondState的SSVM上执行Wasm时使用tensorflow功能提供Rust到WebAssembly开发者的语法
从高层次概述来看,我们实际上正在构建一个tensorflow接口,该接口将允许本地操作系统(SSVM运行在其上)在运行时执行中发挥作用。具体来说,在Wasm执行中使用tensorflow的图和输入输出张量时发挥作用。
如何使用此库
Rust依赖项
开发人员将 ssvm_interface_interface
crate 作为其 Rust -> Wasm
应用程序的依赖项。例如,将以下行添加到应用程序的 Cargo.toml
文件中。
[dependencies]
ssvm_tensorflow_interface = "^0.1.3"
开发人员将在他们的 Rust -> Wasm
应用程序代码中将 ssvm_tensorflow_interface
的功能引入作用域。例如,将以下代码添加到他们的 `main.rs` 文件顶部。
use ssvm_process_interface;
图像加载和转换
let mut file_img = File::open("sample.jpg").unwrap();
let mut img_buf = Vec::new();
file_img.read_to_end(&mut img_buf).unwrap();
let flat_img = ssvm_tensorflow_interface::load_jpg_image_to_rgb32f(&img_buf, 224, 224);
// The flat_img is a vec<f32> which contains normalized image in rgb32f format and resized to 224x224.
创建会话
// The mod_buf is a vec<u8> which contains model data.
let mut session = ssvm_tensorflow_interface::Session::new(&mod_buf, ssvm_tensorflow_interface::ModelType::TensorFlow);
或使用 ssvm_tensorflow_interface::ModelType::TensorFlowLite
来指定 tflite
模型。
准备输入张量
// The flat_img is a vec<f32> which contains normalized image in rgb32f format.
session.add_input("input", &flat_img, &[1, 224, 224, 3])
.add_output("MobilenetV2/Predictions/Softmax");
运行TensorFlow模型
session.run();
转换输出张量
let res_vec: Vec<f32> = session.get_output("MobilenetV2/Predictions/Softmax");
构建和执行
$ cargo build --target=wasm32-wasi
输出的WASM文件将在 target/wasm32-wasi/debug/
或 target/wasm32-wasi/release
。有关WASM执行,请参阅 SSVM与tensorflow扩展。
Crates.io
官方crate可在 crates.io 上找到。