Skip to content

Commit b59b1cf

Browse files
committed
Init version
1 parent 39d75f4 commit b59b1cf

File tree

7 files changed

+346
-0
lines changed

7 files changed

+346
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
# User change
3+
title: "Introduction"
4+
5+
weight: 2
6+
7+
layout: "learningpathall"
8+
---
9+
10+
## What is ONNX?
11+
Open Neural Network Exchange (ONNX) provides a portable, interoperable standard for machine learning (ML). It is an open-source format that enables representation of ML models through a common set of operators and a standardized file format. These operators serve as fundamental building blocks in machine learning and deep learning models, facilitating compatibility and ease of integration across various platforms.
12+
13+
Machine learning practitioners frequently create ML models using diverse frameworks such as PyTorch, TensorFlow, scikit-learn, Core ML, and Azure AI Custom Vision. However, each framework has its own unique methods for creating, storing, and utilizing models. This diversity poses significant challenges when deploying trained models across different environments or executing inference tasks using hardware-accelerated tools or alternate programming languages. For example, deploying models on edge devices or Arm64-powered hardware often necessitates using programming languages other than Python to fully exploit specialized hardware features, such as dedicated neural network accelerators.
14+
15+
By defining a unified and standardized format, ONNX effectively addresses these interoperability challenges. With ONNX, you can easily develop models using your preferred framework and export these models to the ONNX format. Additionally, the ONNX ecosystem includes robust runtime environments (such as ONNX Runtime), enabling efficient inference across multiple hardware platforms and diverse programming languages, including C++, C#, and Java, beyond Python alone.
16+
17+
ONNX Runtime, in particular, provides optimized inference capabilities, supporting execution on CPUs, GPUs, and specialized accelerators, thus significantly improving performance and efficiency for deployment in production environments and edge devices.
18+
19+
Several major ML frameworks currently support exporting models directly to the ONNX format, including Azure AI Custom Vision, Core ML, PyTorch, TensorFlow, and scikit-learn, streamlining the workflow from model development to deployment.
20+
21+
## Objective
22+
In this hands-on learning path, you will explore the practical aspects of running inference on an ONNX-formatted model for an image classification task. Specifically, the demonstration uses the widely used Modified National Institute of Standards and Technology (MNIST) dataset, illustrating how ONNX can be applied to accurately recognize handwritten digits, showcasing both the flexibility and simplicity offered by this standardized format.
23+
24+
## Before you begin
25+
You need the following to complete the tutorial:
26+
### Python
27+
At the time of this writing Python 3.13.3 was available. Use the installer for ARM64, which is available [here](https://www.python.org/ftp/python/3.13.3/python-3.13.3-arm64.exe). After running the installer, select "Add python.exe to PATH", and click Install Now:
28+
29+
[img1]
30+
31+
### A virtual environment with all the required packages
32+
33+
### A code editor
34+
35+
The companion code is available for download [here]().
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: How to Use ONNX Runtime with Windows on Arm
3+
4+
minutes_to_complete: 30
5+
6+
who_is_this_for: This is an introductory topic for developers who are interested in ONNX.
7+
8+
learning_objectives:
9+
- Use pre-trained ONNX model for inference
10+
- Leverage native Arm64 for Python applications
11+
12+
prerequisites:
13+
- A Windows on Arm computer such as the Lenovo Thinkpad X13s running Windows 11 or a Windows on Arm [virtual machine](/learning-paths/cross-platform/woa_azure/).
14+
- Any code editor, we recommend using [Visual Studio Code for Arm64](https://code.visualstudio.com/docs/?dv=win32arm64user).
15+
16+
author: Dawid Borycki
17+
18+
### Tags
19+
skilllevels: Introductory
20+
subjects: Migration to Arm
21+
armips:
22+
- Cortex-A
23+
operatingsystems:
24+
- Windows
25+
tools_software_languages:
26+
- Python
27+
- Visual Studio Code
28+
29+
further_reading:
30+
- resource:
31+
title: ONNX
32+
link: https://onnx.ai
33+
type: website
34+
- resource:
35+
title: ONNX Repository
36+
link: https://github.com/onnx/onnx
37+
type: blog
38+
39+
40+
### FIXED, DO NOT MODIFY
41+
# ================================================================================
42+
weight: 1 # _index.md always has weight of 1 to order correctly
43+
layout: "learningpathall" # All files under learning paths have this same wrapper
44+
learning_path_main_page: "yes" # This should be surfaced when looking for related content. Only set for _index.md of learning path content.
45+
---
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# ================================================================================
3+
# FIXED, DO NOT MODIFY THIS FILE
4+
# ================================================================================
5+
weight: 21 # Set to always be larger than the content in this path to be at the end of the navigation.
6+
title: "Next Steps" # Always the same, html page title.
7+
layout: "learningpathall" # All files under learning paths have this same wrapper for Hugo processing.
8+
---
384 KB
Loading
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
# User change
3+
title: "Using ONNX Runtime"
4+
5+
weight: 3
6+
7+
layout: "learningpathall"
8+
---
9+
10+
## Objective
11+
Next, you will implement Python code that accomplishes the following tasks:
12+
* Downloads a pre-trained ONNX model specifically trained on the MNIST dataset, along with the MNIST dataset itself, which is widely used for benchmarking machine learning models.
13+
* Executes predictions (inference) using the pre-trained ONNX model on test images containing handwritten digits from the MNIST dataset.
14+
* Evaluates and measures the performance of the inference process, providing insights into the efficiency and speed of the neural network model on your specific system architecture.
15+
16+
This practical demonstration will illustrate the end-to-end workflow of deploying and evaluating ONNX-formatted machine learning models.
17+
18+
## Implementation
19+
20+
### Model
21+
Create a file named main.py. At the beginning of this file, include the following import statements:
22+
23+
```Python
24+
import onnxruntime as ort
25+
import numpy as np
26+
import matplotlib.pyplot as plt
27+
import wget, time, os, urllib
28+
import torchvision
29+
import torchvision.transforms as transforms
30+
```
31+
32+
These statements import the necessary Python libraries:
33+
* onnxruntime - enables running inference with ONNX models.
34+
* numpy - facilitates numerical computations and handling of arrays.
35+
* matplotlib - used for visualizing results such as classification outputs.
36+
* wget, urllib, and os - provide utilities for downloading files and interacting with the file system.
37+
* torchvision - allows easy access to datasets like MNIST.
38+
39+
Next, add the following function immediately below the import statements in your main.py file:
40+
41+
```Python
42+
def download_model(model_name):
43+
if not os.path.exists(model_name):
44+
base_url = 'https://github.com/dawidborycki/ONNX.WoA/raw/refs/heads/main/models/'
45+
url = urllib.parse.urljoin(base_url, model_name)
46+
wget.download(url)
47+
```
48+
49+
This function, download_model, accepts one parameter, model_name. It first checks whether a file with this name already exists in your local directory. If the file does not exist, it downloads the specified ONNX model file from the given GitHub repository URL. This automated check ensures that you won't repeatedly download the model unnecessarily.
50+
51+
### Inference
52+
Next, you will implement a Python function to perform neural inference. Add the following code to your main.py file below the previously defined download_model function:
53+
54+
```Python
55+
def onnx_predict(onnx_session, input_name, output_name,
56+
test_images, test_labels, image_index, show_results):
57+
58+
test_image = np.expand_dims(test_images[image_index], [0,1])
59+
60+
onnx_pred = onnx_session.run([output_name], {input_name: test_image.astype('float32')})
61+
62+
predicted_label = np.argmax(np.array(onnx_pred))
63+
actual_label = test_labels[image_index]
64+
65+
if show_results:
66+
plt.figure()
67+
plt.xticks([])
68+
plt.yticks([])
69+
plt.imshow(test_images[image_index], cmap=plt.cm.binary)
70+
71+
plt.title('Actual: %s, predicted: %s'
72+
% (actual_label, predicted_label), fontsize=22)
73+
plt.show()
74+
75+
return predicted_label, actual_label
76+
```
77+
78+
The onnx_predict function prepares a single test image from the dataset by reshaping it to match the input shape expected by the ONNX model, which is (1, 1, 28, 28). This reshaping is achieved using NumPy's expand_dims function. Next, the function performs inference using the ONNX runtime (onnx_session.run). The inference results are probabilities (scores) for each digit class, and the function uses np.argmax to select the digit class with the highest probability, returning it as the predicted label. Optionally, the function visually displays the image along with its actual and predicted labels.
79+
80+
### Performance measurements
81+
Next, add the following performance-measuring function below onnx_predict in your main.py file:
82+
83+
```Python
84+
def measure_performance(onnx_session, input_name, output_name,
85+
test_images, test_labels, execution_count):
86+
87+
start = time.time()
88+
89+
image_indices = np.random.randint(0, test_images.shape[0] - 1, execution_count)
90+
91+
for i in range(1, execution_count):
92+
onnx_predict(onnx_session, input_name, output_name,
93+
test_images, test_labels, image_indices[i], False)
94+
95+
computation_time = time.time() - start
96+
97+
print('Computation time: %.3f ms' % (computation_time*1000))
98+
```
99+
100+
This measure_performance function assesses the inference speed by repeatedly invoking the onnx_predict function. It measures the total computation time (in milliseconds) required for the specified number of inference executions (execution_count) and outputs this measurement to the console.
101+
102+
### Putting Everything Together
103+
Finally, integrate all previously defined functions by adding these statements at the bottom of your main.py file:
104+
```Python
105+
if __name__ == "__main__":
106+
# Download and prepare the model
107+
model_name = 'mnist-12.onnx'
108+
download_model(model_name)
109+
110+
# Set up ONNX inference session
111+
onnx_session = ort.InferenceSession(model_name)
112+
113+
input_name = onnx_session.get_inputs()[0].name
114+
output_name = onnx_session.get_outputs()[0].name
115+
116+
# Load the MNIST dataset using torchvision
117+
transform = transforms.Compose([transforms.ToTensor()])
118+
mnist_dataset = torchvision.datasets.MNIST(root='./data', train=False,
119+
download=True, transform=transform)
120+
121+
test_images = mnist_dataset.data.numpy()
122+
test_labels = mnist_dataset.targets.numpy()
123+
124+
# Normalize images
125+
test_images = test_images / 255.0
126+
127+
# Perform a single prediction and display the result
128+
image_index = np.random.randint(0, test_images.shape[0] - 1)
129+
onnx_predict(onnx_session, input_name, output_name,
130+
test_images, test_labels, image_index, True)
131+
132+
# Measure inference performance
133+
measure_performance(onnx_session, input_name, output_name,
134+
test_images, test_labels, execution_count=1000)
135+
```
136+
137+
This script first initializes an ONNX inference session with the downloaded model (mnist-12.onnx). It then retrieves the model's input and output details, loads the MNIST dataset for testing, runs a sample inference showing visual results, and finally measures the performance of the inference operation over multiple runs.
138+
139+
## Summary
140+
In this section, you implemented Python code to download a pre-trained ONNX model and the MNIST dataset, perform inference to recognize handwritten digits, and measure inference performance. In the next step, you will install all required dependencies and run the code.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
# User change
3+
title: "Running Inference"
4+
5+
weight: 4
6+
7+
layout: "learningpathall"
8+
---
9+
10+
## Objective
11+
You will now use the implemented code to run inference.
12+
13+
## Packages
14+
Start by activating the virtual environment and installing Python packages:
15+
```console
16+
venv-x64\Scripts\activate.bat
17+
```
18+
19+
Then, type:
20+
```console
21+
py -V:3.13 -m pip install onnxruntime numpy matplotlib wget torchvision torch
22+
```
23+
24+
## Running Inference
25+
To run the inference type the following
26+
```console
27+
py -V:3.13 .\main.py
28+
```
29+
30+
The code will display the sample inference result like shown below:
31+
32+
If you close the image, you will see the computation time:
33+
```output
34+
PS C:\Users\db\onnx> py -V:3.13 .\main.py
35+
Computation time: 95.854 ms
36+
PS C:\Users\db\onnx> py -V:3.13 .\main.py
37+
Computation time: 111.230 ms
38+
```
39+
![fig1](figures/01.png)
40+
41+
To compare these with Windows Arm 64, repeat the following for the Arm-64 Python architecture:
42+
```console
43+
venv-arm64\Scripts\activate.bat
44+
py -V:3.13-arm64 -m pip install onnxruntime numpy matplotlib wget torchvision torch
45+
py -V:3.13-arm64 main.py
46+
```
47+
48+
Note the above will work, when the onnx runtime will become available for Windows on Arm 64.
49+
50+
## Summary
51+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
# User change
3+
title: "Introduction"
4+
5+
weight: 2
6+
7+
layout: "learningpathall"
8+
---
9+
10+
## What is ONNX?
11+
Open Neural Network Exchange (ONNX) provides a portable, interoperable standard for machine learning (ML). It is an open-source format that enables representation of ML models through a common set of operators and a standardized file format. These operators serve as fundamental building blocks in machine learning and deep learning models, facilitating compatibility and ease of integration across various platforms.
12+
13+
Machine learning practitioners frequently create ML models using diverse frameworks such as PyTorch, TensorFlow, scikit-learn, Core ML, and Azure AI Custom Vision. However, each framework has its own unique methods for creating, storing, and utilizing models. This diversity poses significant challenges when deploying trained models across different environments or executing inference tasks using hardware-accelerated tools or alternate programming languages. For example, deploying models on edge devices or Arm64-powered hardware often necessitates using programming languages other than Python to fully exploit specialized hardware features, such as dedicated neural network accelerators.
14+
15+
By defining a unified and standardized format, ONNX effectively addresses these interoperability challenges. With ONNX, you can easily develop models using your preferred framework and export these models to the ONNX format. Additionally, the ONNX ecosystem includes robust runtime environments (such as ONNX Runtime), enabling efficient inference across multiple hardware platforms and diverse programming languages, including C++, C#, and Java, beyond Python alone.
16+
17+
ONNX Runtime, in particular, provides optimized inference capabilities, supporting execution on CPUs, GPUs, and specialized accelerators, thus significantly improving performance and efficiency for deployment in production environments and edge devices.
18+
19+
Several major ML frameworks currently support exporting models directly to the ONNX format, including Azure AI Custom Vision, Core ML, PyTorch, TensorFlow, and scikit-learn, streamlining the workflow from model development to deployment.
20+
21+
The companion code is available [here](https://github.com/dawidborycki/ONNX.WoA/tree/main)
22+
23+
## Objective
24+
In this hands-on learning path, you will explore the practical aspects of running inference on an ONNX-formatted model for an image classification task. Specifically, the demonstration uses the widely used Modified National Institute of Standards and Technology (MNIST) dataset, illustrating how ONNX can be applied to accurately recognize handwritten digits, showcasing both the flexibility and simplicity offered by this standardized format.
25+
26+
## Before you Begin
27+
Ensure you have the following prerequisites installed to complete this tutorial:
28+
29+
### Python
30+
At the time of writing, Python 3.13.3 is available. You can download it using the links below
31+
1. [Windows x64 (64-bit)](https://www.python.org/ftp/python/3.13.3/python-3.13.3-amd64.exe)
32+
2. [Windows ARM64](https://www.python.org/ftp/python/3.13.3/python-3.13.3-arm64.exe)
33+
34+
Install both Python versions as required. After installation, confirm both are available by running the following command in your console
35+
36+
```console
37+
py --list
38+
```
39+
40+
The output should look like this:
41+
42+
```output
43+
py --list
44+
-V:3.13 * Python 3.13 (64-bit)
45+
-V:3.13-arm64 Python 3.13 (ARM64)
46+
```
47+
48+
### Creating a Virtual Environment
49+
Create a virtual environment tailored to your Python installation's architecture. For the 64-bit Python environment, use:
50+
```console
51+
py -V:3.13 -m venv venv-x64
52+
```
53+
54+
For arm64 use the following command:
55+
```console
56+
py -V:3.13-arm64 -m venv venv-arm64
57+
```
58+
59+
By using different virtual environments, you can compare the performance of the same code across different architectures. However, at the time of writing, ONNX Runtime is unavailable as a Python wheel for Windows ARM64. Therefore, the subsequent instructions apply only to Windows x64.
60+
61+
### A Code Editor
62+
For this demonstration we use Visual Studio Code. Available for download [here](https://code.visualstudio.com/download)
63+
64+
## Pre-trained Models
65+
Many pre-trained models are available in the ONNX format ([ONNX Model Zoo](https://github.com/onnx/models)). There are models for image classification, image recognition, machine translation, language modeling, speech, audio processing, and more.
66+
67+
In this tutorial, you will use the pre-trained model for [handwritten digit recognition](https://github.com/onnx/models/tree/main/validated/vision/classification/mnist) — the deep learning convolutional neural model trained on the popular MNIST dataset

0 commit comments

Comments
 (0)