1# Image Classification Example
2
3This example project demonstrates using the `wasi-nn` API to perform machine
4learning (ML) inference. It shows how to collect and use the various parts of a
5wasi-nn program:
6- an ML __framework__ ("backend" in wasi-nn terms)
7- an ML __model__ ("graph" in wasi-nn terms)
8- a WebAssembly __program__
9- a wasi-nn-compatible __engine__
10
11### Pre-requisite: Framework
12
13This example uses the OpenVINO framework: [installation
14instructions][openvino-install]. If you're interested in how the engine forwards
15calls from the WebAssembly program to this framework, see the
16[backend][openvino-backend] source code.
17
18[openvino-install]: https://docs.openvino.ai/2025/get-started/install-openvino.html
19[openvino-backend]: ../../src/backend/openvino.rs
20
21### Pre-requisite: Model
22
23MobileNet is a small, common model for classifying images; it returns the
24probabilities for words that best describe the image. To retrieve the files
25needed to use this model on OpenVINO, download:
26
27```
28wget https://download.01.org/openvinotoolkit/fixtures/mobilenet/mobilenet.bin -O fixture/model.bin
29wget https://download.01.org/openvinotoolkit/fixtures/mobilenet/mobilenet.xml -O fixture/model.xml
30wget https://download.01.org/openvinotoolkit/fixtures/mobilenet/tensor-1x224x224x3-f32.bgr -O fixture/tensor.bgr
31```
32
33The `.bgr` file is a tensor representation of an image file (more details
34[here]) which is the input to this model, i.e., the image to be classified.
35
36[here]: https://download.01.org/openvinotoolkit/fixtures/mobilenet
37
38### Pre-requisite: Program
39
40Compile this Rust [example] to a WebAssembly program using the `wasm32-wasip1`
41target. This requires a Rust toolchain (e.g., [rustup]) and the appropriate
42compilation target (e.g., `rustup target add wasm32-wasip1`). To compile the
43program to a `*.wasm` file in the `target` directory:
44
45```
46cargo build --target=wasm32-wasip1
47```
48
49[example]: src/main.rs
50[rustup]: https://rustup.rs
51
52### Pre-requisites: Engine
53
54This example uses Wasmtime, which contains a wasi-nn [implementation][crate]. To
55use Wasmtime, follow the instructions to either [build] or [install] it.
56
57[build]: https://docs.wasmtime.dev/contributing-building.html
58[crate]: ../..
59[install]: https://docs.wasmtime.dev/cli-install.html
60
61### Run
62
63With the pre-requisites in place, run the example:
64
65```
66<path>/<to>/wasmtime run --wasi=nn --dir=fixture target/wasm32-wasip1/debug/wasi-nn-example.wasm
67```
68
69Some words of explanation: the `--wasi` flag enables the wasi-nn proposal (see
70`-S help`), the `--dir` maps our host-side `fixture` directory to a directory of
71the same name in the guest, and we pass the `*.wasm` module as the sole
72argument. For this model (see the [source][example]), we expect to see the list
73of tags that mostly likely describe the image:
74
75```
76...
77Found results, sorted top 5: [InferenceResult(885, 0.3958254), InferenceResult(904, 0.36464655), InferenceResult(84, 0.010480323), InferenceResult(911, 0.0082290955), InferenceResult(741, 0.007244849)]
78```
79