Skip to main content
Welcome to the first demonstration in our deployment series. In this guide, we provide a comprehensive overview of working with ONNX and the ONNX Runtime. This demonstration will walk you through installing the necessary modules, modifying a pretrained model, exporting it to ONNX format, preparing input for inference, running the inference, and finally mapping the output to a human-readable label.
ONNX is an open standard format that allows interoperability of models across different platforms. With ONNX Runtime, you can execute these models efficiently in various deployment scenarios.

Installing ONNX and ONNX Runtime

Begin by installing both ONNX and the ONNX Runtime. For this demo, we assume that the required modules are already installed. If you encounter any issues, install the modules using pip and restart your notebook.
Console output:
Since all packages are present, we can proceed with the demonstration.

Loading and Modifying the Model

In this section, we import the necessary modules from PyTorch and TorchVision to load and modify a MobileNet V3 Large model. After loading the model with pretrained weights, we adjust the classifier’s final layer to output two classes. We then load a checkpoint to restore the model’s state.
Next, we load the state from the checkpoint into the model:
These steps ensure that MobileNet V3 is updated for our classification task with two classes and that the weights are successfully restored.

Exporting the Model to ONNX

PyTorch offers built-in support to export models to the ONNX format. By using the export function, we define the model’s input signature and create an ONNX file. Start by displaying the documentation for torch.onnx.export for available options:
After exporting, verify the correctness of the model by loading and checking the ONNX file:
The export process leverages a dummy input to define the model’s signature and generates the “image_classifier.onnx” file for further use.

Examining the Export Process

For clarity, the following example reiterates the export process: preparing an example input, exporting the model, and verifying the exported ONNX model.
This check confirms that the model is export-consistent and ready for deployment.

Preparing an Image for Inference

Before running inference on the ONNX model, the input image must be preprocessed. This involves resizing, normalizing, and converting the image to a tensor using Pillow and TorchVision transformations. The transformed image is then reshaped to include a batch dimension.
After transforming the image, convert it into a NumPy array to match the input requirements of ONNX Runtime:

Running Inference with ONNX Runtime

With the ONNX model prepared and the input image transformed, we can perform inference using ONNX Runtime. The process involves loading the model, creating an inference session, and using an input dictionary for the session run.
Prepare the input dictionary using the session’s first input name and run the inference:
The raw outputs represent logits, and applying argmax on these logits provides the index of the predicted class.

Mapping the Prediction to a Label

To present the inference result in a human-friendly format, we associate the numerical prediction with a corresponding class label. Here, the two classes are defined as “malignant” (0) and “benign” (1).
Console output:
This mapping step translates the model’s output into a readable class label.
The image shows a Jupyter Notebook interface with Python code related to exporting a model using the torch.onnx.export function. The help documentation for this function is displayed, detailing its usage and arguments.

Conclusion

In this demo, we explored the end-to-end process of working with ONNX and ONNX Runtime. We covered the following steps:
This guide provides a solid foundation for integrating ONNX models in a variety of deployment environments. For additional information, explore the ONNX Documentation.
Thank you for reading this demonstration on exporting and using ONNX models for inference in real-world applications.

Watch Video