Skip to content

Commit

Permalink
[Examples] Add the wasmedge-tensorflow plug-in examples.
Browse files Browse the repository at this point in the history
Signed-off-by: YiYing He <[email protected]>
  • Loading branch information
q82419 committed Jul 21, 2023
1 parent 25b8859 commit 4f7ff0a
Show file tree
Hide file tree
Showing 17 changed files with 1,288 additions and 48 deletions.
60 changes: 14 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,13 @@

### Introduction

This project provides the examples of high-level [wasi-nn] bindings on Rust programming language. Developers can refer to this project to write their machine learning application in a high-level language using the bindings, compile it to WebAssembly, and run it with a WebAssembly runtime that supports the [wasi-nn] proposal, such as [WasmEdge].

> __NOTE__: These bindings are experimental (use at your own risk) and subject to upstream changes in the [wasi-nn] specification.
This project provides the examples of high-level [wasi-nn] bindings and WasmEdge-TensorFlow plug-ins on Rust programming language. Developers can refer to this project to write their machine learning application in a high-level language using the bindings, compile it to WebAssembly, and run it with a WebAssembly runtime that supports the [wasi-nn] proposal, such as [WasmEdge].

### Prerequisites

#### OpenVINO Installation

Developers should install the [OpenVINO] first before build and run WasmEdge with wasi-nn and the examples.
For this project, we use the version `2021.4.582`. Please refer to the [installation script](scripts/install_openvino.sh).

```bash
curl -sSL https://apt.repos.intel.com/openvino/2021/GPG-PUB-KEY-INTEL-OPENVINO-2021 | sudo gpg --dearmor > /usr/share/keyrings/GPG-PUB-KEY-INTEL-OPENVINO-2021.gpg
echo "deb [signed-by=/usr/share/keyrings/GPG-PUB-KEY-INTEL-OPENVINO-2021.gpg] https://apt.repos.intel.com/openvino/2021 all main" | sudo tee /etc/apt/sources.list.d/intel-openvino-2021.list
sudo apt update
sudo apt install -y intel-openvino-runtime-ubuntu20-2021.4.582
```

After the installation, you may need to add the path:

```bash
source /opt/intel/openvino_2021/bin/setupvars.sh
sudo ldconfig
```

[OpenVINO]: https://www.intel.com/content/www/us/en/developer/tools/openvino-toolkit/overview.html

#### Rust Installation

Please refer to the [Rust Official Site](https://www.rust-lang.org/tools/install) for the Rust installation.
For building the WASM files from rust source, please refer to the [Rust Official Site](https://www.rust-lang.org/tools/install) for the Rust installation.
After the installation, developers should add the `wasm32-wasi` target.

```bash
Expand All @@ -50,32 +27,22 @@ rustup target add wasm32-wasi

#### Download the `wasi-nn` Rust Crate

In Rust, download the [crate from crates.io][crates.io] by adding `wasi-nn = "0.1"` as a Cargo dependency.

[crates.io]: https://crates.io/crates/wasi-nn
In Rust, download the [crate from crates.io](https://crates.io/crates/wasi-nn) by adding `wasi-nn = "0.4.0"` as a Cargo dependency.

#### Build the WasmEdge with WASI-NN supporting
For using WasmEdge-TensorFlow plug-ins, please download the [crate from crates.io](https://crates.io/crates/wasmedge_tensorflow_interface) by adding `wasmedge_tensorflow_interface = "0.3.0"` as a Cargo dependency.

For running the examples, developers should [build WasmEdge from source](https://wasmedge.org/book/en/extend/build.html).
First developers should get the source:
#### WasmEdge Installation

```bash
git clone https://github.com/WasmEdge/WasmEdge.git
cd WasmEdge
```

And build with the `WASMEDGE_PLUGIN_WASI_NN_BACKEND` argument:

```bash
cmake -Bbuild -GNinja -WASMEDGE_PLUGIN_WASI_NN_BACKEND="OpenVINO" .
cmake --build build
# For the WASI-NN plugin, you should install this project.
cmake --install build
```
You can refer to [here to install WasmEdge](https://wasmedge.org/docs/start/install#install).

After the installation, developers can execute the `wasmedge` executable with WASI-NN plugin.
For the examples with different wasi-nn backends or using the WasmEdge-Tensorflow plug-ins, please install with plug-ins and their dependencies:

> Notice: If you didn't install the project, you should give the `WASMEDGE_PLUGIN_PATH` environment variable for specifying the WASI-NN plugin path (the built plugin is at `build/plugins/wasi_nn`).
- [wasi-nn plug-in with OpenVINO backend](https://wasmedge.org/docs/start/install#wasi-nn-plug-in-with-openvino-backend)
- [wasi-nn plug-in with PyTorch backend](https://wasmedge.org/docs/start/install#wasi-nn-plug-in-with-pytorch-backend)
- [wasi-nn plug-in with PyTorch backend](https://wasmedge.org/docs/start/install#wasi-nn-plug-in-with-tensorflow-lite-backend)
- [WasmEdge-Image plug-in](https://wasmedge.org/docs/start/install#wasmedge-image-plug-in)
- [WasmEdge-TensorFlow plug-in](https://wasmedge.org/docs/start/install#wasmedge-tensorflow-plug-in)
- [WasmEdge-TensorFlow-Lite plug-in](https://wasmedge.org/docs/start/install#wasmedge-tensorflow-lite-plug-in)

### Examples

Expand All @@ -86,6 +53,7 @@ After the installation, developers can execute the `wasmedge` executable with WA
- [WASI]
- [wasi-nn]
- [WasmEdge]
- [WasmEdge-TensorFlow rust interface](https://crates.io/crates/wasmedge_tensorflow_interface)
- [wasi-nn-guest](https://github.com/radu-matei/wasi-nn-guest)

[WasmEdge]: https://wasmedge.org/
Expand Down
2 changes: 1 addition & 1 deletion pytorch-mobilenet-image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ python3 gen_tensor input.jpg image-1x3x224x224.rgb

### Execute

Users should [install the WasmEdge with WASI-NN PyTorch backend plug-in](https://wasmedge.org/book/en/write_wasm/rust/wasinn.html#get-wasmedge-with-wasi-nn-plug-in-pytorch-backend).
Users should [install the WasmEdge with WASI-NN PyTorch backend plug-in](https://wasmedge.org/docs/start/install#wasi-nn-plug-in-with-pytorch-backend).

Execute the WASM with the `wasmedge` with PyTorch supporting:

Expand Down
2 changes: 1 addition & 1 deletion tflite-birds_v1-image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The `tflite` model is located at `./lite-model_aiy_vision_classifier_birds_V1_3.

### Execute

Users should [install the WasmEdge with WASI-NN TensorFlow-Lite backend plug-in](https://wasmedge.org/book/en/write_wasm/rust/wasinn.html#get-wasmedge-with-wasi-nn-plug-in-tensorflow-lite-backend).
Users should [install the WasmEdge with WASI-NN TensorFlow-Lite backend plug-in](https://wasmedge.org/docs/start/install#wasi-nn-plug-in-with-tensorflow-lite-backend).

Execute the WASM with the `wasmedge` with Tensorflow Lite supporting:

Expand Down
Binary file not shown.
59 changes: 59 additions & 0 deletions wasmedge-tf-mobilenet_v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Mobilenet Example For WasmEdge-Tensorflow plug-in

This package is a high-level Rust bindings for [WasmEdge-TensorFlow plug-in](https://wasmedge.org/docs/develop/rust/tensorflow) example of Mobilenet.

## Dependencies

This crate depends on the `wasmedge_tensorflow_interface` in the `Cargo.toml`:

```toml
[dependencies]
wasmedge_tensorflow_interface = "0.3.0"
```

## Build

Compile the application to WebAssembly:

```bash
cd rust && cargo build --target=wasm32-wasi --release
```

The output WASM file will be at [`rust/target/wasm32-wasi/release/wasmedge-tf-example-mobilenet.wasm`](wasmedge-tf-example-mobilenet.wasm).
To speed up the image processing, we can enable the AOT mode in WasmEdge with:

```bash
wasmedge compile rust/target/wasm32-wasi/release/wasmedge-tf-example-mobilenet.wasm wasmedge-tf-example-mobilenet_aot.wasm
```

## Run

### Test data

The testing image is located at `./grace_hopper.jpg`:

![grace_hopper](grace_hopper.jpg)

The frozen `pb` model is located at `./mobilenet_v2_1.4_224_frozen.pb`.

### Execute

Users should [install the WasmEdge with WasmEdge-TensorFlow and WasmEdge-Image plug-ins](https://wasmedge.org/docs/start/install#wasmedge-tensorflow-plug-in).

```bash
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- --plugins wasmedge_tensorflow wasmedge_image
```

Execute the WASM with the `wasmedge` with Tensorflow Lite supporting:

```bash
wasmedge --dir .:. wasmedge-tf-example-mobilenet.wasm mobilenet_v2_1.4_224_frozen.pb grace_hopper.jpg
```

You will get the output:

```console
653 : 0.3230184316635132
```

Which is index 653 (0-based index) with rate `0.33690106868743896`. The index 653 of label table (which is line 654 in `imagenet_slim_labels.txt`) is `military uniform`.
Binary file added wasmedge-tf-mobilenet_v2/grace_hopper.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4f7ff0a

Please sign in to comment.