Wonnx command-line interface (nnx)
Command-line interface for inference using wonnx
ONNX defines a standardized format to exchange machine learning models. However, up to this point there is no easy way to perform one-off inference using such a model without resorting to Python. Installation of Python and the required libraries (e.g. TensorFlow and underlying GPU setup) can be cumbersome. Additionally specific code is always needed to transfer inputs (images, text, etc.) in and out of the formats required by the model (i.e. image classification models want their images as fixed-size tensors with the pixel values normalized to specific values, et cetera).
This project provides a very simple all-in-one binary command line tool that can be used to perform inference using ONNX models on the GPU. Thanks to wonnx, inference is performed on the GPU.
NNX tries to make educated guesses about how to transform input and output for a model. These guesses are a default - i.e. it should always be possible to override them. The goal is to reduce the amount of configuration required to be able to run a model. Currently the following heuristics are applied:
- The first input and first output specified in the ONNX file are used by default.
- Models taking inputs of shape (1,3,w,h) and (3,w,h) will be fed images resized to w*h with pixel values normalized to 0...1 (currently we also apply the SqueezeNet normalization)
- Similarly, models taking inputs of shape (1,1,w,h) and (1,w,h) will be fed black-and-white images with pixel values normalized to 0...1.
- When a label file is supplied, an output vector of shape (n,) will be interpreted as providing the probabilities for each class. The label for each class is taken from the n'th line in the label file.
Usage
| | |
| | |
| | |
| | |
| | |
| | |
| | | | | | | |
| | |
| | | | | | | |
| | |
| | |
| | | | | | | |
| | |
| | | | | | | |
| | |
| | |
| | | | | |
| | |
| | | | | |
| | |
| | | | transA=0 | |
| | | | transB=0 | |
| | | | beta=1 | |
| | | | alpha=1 | |
| | |
| | | | | |
| | |
| | | | auto_pad=SAME_UPPER | |
| | | | group=1 | |
| | | | strides=<INTS> | |
| | | | dilations=<INTS> | |
| | | | kernel_shape=<INTS> | |
| | |
| | | | strides=<INTS> | |
| | | | kernel_shape=<INTS> | |
| | | | auto_pad=NOTSET | |
| | | | pads=<INTS> | |
| | |
- Replace
nnxwithcargo run --release --to run development version - Prepend
RUST_LOG=wonnx-cli=infoto see useful logging from the CLI tool,RUST_LOG=wonnx=infoto see logging from WONNX.
CPU inference using tract
The nnx utility can use tract as CPU-based backend for ONNX inference. In order to use
this, nnx needs to be compiled with the cpu feature enabled. You can then specify one of the following arguments:
--backend cputo select the cpu backend--fallbackto select the cpu backend when the gpu backend cannot be used (e.g. because of an unsupported operation type)--compareto run inference on both CPU and GPU backends and compare the output--benchmarkto run the specified inference a hundred times, then report the performance--compare --benchmarkto run inference on both CPU and GPU a hundred times each, and compare the performance
A benchmarking example (the below result was obtained on an Apple M1 Max system):
# Run from workspace root
)
End-to-end example with Keras
-
pip install tensorflow onnx tf2onnx -
Create a very simple model for the MNIST digits:
, =
# train_images will be (60000,28,28) i.e. 60k black-and-white images of 28x28 pixels (which are ints between 0..255)
# train_labels will be (60000,) i.e. 60k integers ranging 0...9
# test_images/test_labels are similar but only have 10k items
# Build model
# Convert images to have pixel values as floats between 0...1
= / 255
=
# Train the model
- Save Keras model to ONNX with inferred dimensions:
=
, =
=
- Infer with NNX:
- compare inference result with what Keras would generate (
pip install numpy pillow matplotlib):
=
=