Skip to content

Commit

Permalink
Merge pull request #1455 from jasonrandrews/review2
Browse files Browse the repository at this point in the history
Review MNIST Learning Path
  • Loading branch information
jasonrandrews authored Dec 18, 2024
2 parents 0668d9f + e75d6ef commit a6a9231
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ title: Create and train a PyTorch model for digit classification

minutes_to_complete: 160

who_is_this_for: This is an introductory topic for software developers interested in learning how to use PyTorch to create and train a feedforward neural network for digit classification. Also you will learn how to use the trained model in an android app. Last, you will discover model optimisations.
who_is_this_for: This is an advanced topic for software developers interested in learning how to use PyTorch to create and train a feedforward neural network for digit classification. You will also learn how to use the trained model in an Android application. Finally, you will apply model optimizations.

learning_objectives:
- Prepare a PyTorch development environment.
- Download and prepare the MNIST dataset.
- Create a neural network architecture using PyTorch.
- Train a neural network using PyTorch.
- Creating an Android app and loading the pre-trained mdoel.
- Preparing an input dataset.
- Measuring the inference time.
- Optimise a neural network architecture using quantization and fusing.
- Use an optimised model in an Android app.
- Create an Android app and loading the pre-trained model.
- Prepare an input dataset.
- Measure the inference time.
- Optimize a neural network architecture using quantization and fusing.
- Use an optimized model in the Android application.

prerequisites:
- A computer that can run Python3 and Visual Studio Code. The OS can be Windows, Linux, or macOS.
- A computer that can run Python3, Visual Studio Code, and Android Studio. The OS can be Windows, Linux, or macOS.


author_primary: Dawid Borycki

### Tags
skilllevels: Introductory
skilllevels: Advanced
subjects: ML
armips:
- Cortex-A
Expand All @@ -35,6 +36,7 @@ operatingsystems:
tools_software_languages:
- Android Studio
- Coding
- VS Code
shared_path: true
shared_between:
- servers-and-cloud-computing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ review:
Which loss function was used to train the PyTorch model on the MNIST dataset?
answers:
- Mean Squared Error Loss
- CrossEntropyLoss
- Cross Entropy Loss
- Hinge Loss
- Binary Cross-Entropy Loss
correct_answer: 2
explanation: >
The CrossEntropyLoss function was used to train the model because it is suitable for multi-class classification tasks like digit classification. It measures the difference between the predicted probabilities and the true class labels, helping the model learn to make accurate predictions.
Cross Entropy Loss was used to train the model because it is suitable for multi-class classification tasks like digit classification. It measures the difference between the predicted probabilities and the true class labels, helping the model learn to make accurate predictions.
# ================================================================================
# FIXED, DO NOT MODIFY
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
---
# User change
title: "Running an Application"
title: "Run the Application"

weight: 10

layout: "learningpathall"
---

You are now ready to run the application. You can use either an emulator or a physical device. In this guide, we will use an emulator.
You are now ready to run the Android application. You can use an emulator or a physical device.

The screenshots below show an emulator.

To run the app in Android Studio using an emulator, follow these steps:

To run an app in Android Studio using an emulator, follow these steps:
1. Configure the Emulator:
* Go to Tools > Device Manager (or click the Device Manager icon on the toolbar).
* Click Create Device to set up a new virtual device (if you haven’t done so already).
* Choose a device model (e.g., Pixel 4) and click Next.
* Select a system image (e.g., Android 11, API level 30) and click Next.
* Choose a device model, such as Pixel 4, and click Next.
* Select a system image, such as Android 11, API level 30, and click Next.
* Review the settings and click Finish to create the emulator.

2. Run the App:
Expand All @@ -29,4 +32,4 @@ Once the application is started, click the Load Image button. It will load a ran

![img](Figures/06.png)

In the next step you will learn how to further optimise the model.
In the next step you will learn how to further optimize the model.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
---
# User change
title: "Datasets and training"
title: "Perform training and save the model"

weight: 5

layout: "learningpathall"
---

## Prepare the MNIST data

Start by downloading the MNIST dataset. Proceed as follows:

1. Open the pytorch-digits.ipynb you created earlier.
Expand Down Expand Up @@ -60,7 +62,7 @@ The certifi Python package provides the Mozilla root certificates, which are ess

Make sure to replace `x` with the number of Python version you have installed.

After running the code you will see the output that might look like shown below:
After running the code you see output similar to the screenshot below:

![image](Figures/01.png)

Expand Down Expand Up @@ -122,18 +124,18 @@ for t in range(epochs):
test_loop(test_dataloader, model, loss_fn)
```

After running this code, you will see the following output that shows the training progress.
After running the code, you see the following output showing the training progress.

![image](Figures/02.png)

Once the training is complete, you will see something like the following:
Once the training is complete, you see output similar to:

```output
Epoch 10:
Accuracy: 95.4%, Avg loss: 1.507491
```

which shows the model achieved around 95% of accuracy.
The output shows the model achieved around 95% accuracy.

# Save the model

Expand Down Expand Up @@ -174,4 +176,4 @@ Setting the model to evaluation mode before tracing is important for several rea

3. Correct Tracing. Tracing captures the operations performed by the model using a given input. If the model is in training mode, the traced graph may include operations related to dropout and batch normalization updates. These operations can affect the correctness and performance of the model during inference.

In the next step, you will use the saved model for inference.
In the next step, you will use the saved model for ML inference.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
# User change
title: "Inference"
title: "Use the model for inference"

weight: 6

Expand All @@ -26,7 +26,7 @@ Start by installing matplotlib package:
pip install matplotlib
```

Then, in Visual Studio Code create a new file named `pytorch-digits-inference.ipynb` and modify the file to include the code below:
Use Visual Studio Code to create a new file named `pytorch-digits-inference.ipynb` and modify the file to include the code below:

```python
import torch
Expand Down Expand Up @@ -101,14 +101,14 @@ After running the code, you should see results similar to the following figure:

![image](Figures/03.png)

# What you have learned
# What have you learned?

In this exercise, you went through the complete process of training and using a PyTorch model for digit classification on the MNIST dataset. Using the training dataset, you optimized the model’s weights and biases over multiple epochs. You employed the CrossEntropyLoss function and the Adam optimizer to minimize prediction errors and improve accuracy. You periodically evaluated the model on the test dataset to monitor its performance, ensuring it was learning effectively without overfitting.
You have completed the process of training and using a PyTorch model for digit classification on the MNIST dataset. Using the training dataset, you optimized the model’s weights and biases over multiple epochs. You employed the CrossEntropyLoss function and the Adam optimizer to minimize prediction errors and improve accuracy. You periodically evaluated the model on the test dataset to monitor its performance, ensuring it was learning effectively without overfitting.

After training, you saved the model using TorchScript, which captures both the model’s architecture and its learned parameters. This made the model portable and independent of the original class definition, simplifying deployment.

Next, you performed inference. You loaded the saved model and set it to evaluation mode to ensure that layers like dropout and batch normalization behaved correctly during inference. You randomly selected 16 images from the MNIST test dataset to evaluate the model’s performance on unseen data. For each selected image, you used the model to predict the digit, comparing the predicted labels with the actual ones. You displayed the images alongside their actual and predicted labels in a 4x4 grid, visually assessing the model’s accuracy and performance.

This comprehensive process, from model training and saving to inference and visualization, illustrates the end-to-end workflow for building and deploying a machine learning model in PyTorch. It demonstrates how to train a model, save it in a portable format, and then use it to make predictions on new data.

In the next step, you will learn how to use the model in the mobile Android application.
In the next step, you will learn how to use the model in an Android application.
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
---
# User change
title: "Background for running inference on Android"
title: "Understand inference on Android"

weight: 7

layout: "learningpathall"
---

Running pre-trained machine learning models on mobile and edge devices has become increasingly common as it enables these devices to gain intelligence and perform complex tasks directly on-device. This capability allows smartphones, IoT devices, and embedded systems to execute advanced functions such as image recognition, natural language processing, and real-time decision-making without relying on cloud-based services. By leveraging on-device inference, applications can offer faster responses, reduced latency, enhanced privacy, and offline functionality, making them more efficient and capable of handling sophisticated tasks in various environments.
Running pre-trained machine learning models on mobile and edge devices has become increasingly common as it enables these devices to gain intelligence and perform complex tasks directly on-device. This capability allows smartphones, IoT devices, and embedded systems to execute advanced functions such as image recognition, natural language processing, and real-time decision-making without relying on cloud-based services.

Arm provides a wide range of hardware and software accelerators designed to optimize the performance of machine learning (ML) models on edge devices. These include specialized processors like Arm's Neural Processing Units (NPUs) and Graphics Processing Units (GPUs), as well as software frameworks like the Arm Compute Library and Arm NN, which are tailored to leverage these hardware capabilities. Arm's technology is ubiquitous, powering a vast array of devices from smartphones and tablets to IoT gadgets and embedded systems. With Arm chips being the core of many Android-based smartphones and other devices, running ML models efficiently on this hardware is crucial for enabling advanced applications such as image recognition, voice assistance, and real-time analytics. By utilizing Arm’s accelerators, developers can achieve lower latency, reduced power consumption, and enhanced performance, making on-device AI both practical and powerful for a wide range of applications.
By leveraging on-device inference, applications can offer faster responses, reduced latency, enhanced privacy, and offline functionality, making them more efficient and capable of handling sophisticated tasks in various environments.

Running a machine learning model on Android involves a few key steps. First, you need to train and save the model in a mobile-friendly format, such as TensorFlow Lite, ONNX, or TorchScript, depending on the framework you are using. Next, you add the model file to your Android project’s assets directory. In your app’s code, use the corresponding framework’s Android library, such as TensorFlow Lite or PyTorch Mobile, to load the model. You then prepare the input data, ensuring it is formatted and preprocessed in the same way as during model training. The input data is passed through the model, and the output predictions are retrieved and interpreted accordingly. For improved performance, you can leverage hardware acceleration using Android’s Neural Networks API (NNAPI) or use GPU support if available. This process enables the Android app to make real-time predictions and execute complex machine learning tasks directly on the device.
Arm provides a wide range of hardware and software accelerators designed to optimize the performance of machine learning (ML) models on edge devices. These include specialized processors like Arm's Neural Processing Units (NPUs) and Graphics Processing Units (GPUs), as well as software frameworks like the Arm Compute Library and Arm NN, which are tailored to leverage these hardware capabilities.

In this Learning Path, you will learn how to perform such inference in the Android app using a pre-trained digit classifier, created [here](learning-paths/cross-platform/pytorch-digit-classification-training).
Running a machine learning model on Android involves a few key steps.

First, you train and save the model in a mobile-friendly format, such as TensorFlow Lite, ONNX, or TorchScript, depending on the framework you are using.

Next, you add the model file to your Android project’s assets directory. In your app’s code, use the corresponding framework’s Android library, such as TensorFlow Lite or PyTorch Mobile, to load the model.

You then prepare the input data, ensuring it is formatted and preprocessed in the same way as during model training. The input data is passed through the model, and the output predictions are retrieved and interpreted accordingly. For improved performance, you can leverage hardware acceleration using Android’s Neural Networks API (NNAPI) or use GPU support if available. This process enables the Android app to make real-time predictions and execute complex machine learning tasks directly on the device.

In this Learning Path, you will learn how to perform inference in an Android application using the pre-trained digit classifier from the previous sections.

## Before you begin
Before you begin make sure Python3, [Visual Studio Code](https://code.visualstudio.com/download) and [Android Studio](https://developer.android.com/studio/install) are installed on your system.

## Source code
The complete source code is available [here](https://github.com/dawidborycki/Arm.PyTorch.MNIST.Inference.git).
Before you begin make [Android Studio](https://developer.android.com/studio/install) is installed on your system.

## Project source code

The following steps explain how to build an Android application for MNIST inference. The application can be constructed from scratch, but there are two GitHub repositories available if you need to copy any files from them as you learn how to create the Android application.

The complete source code for the [Android application](https://github.com/dawidborycki/Arm.PyTorch.MNIST.Inference.git) is available on GitHub.

The Python scripts are available [here](https://github.com/dawidborycki/Arm.PyTorch.MNIST.Inference.Python.git)
The [Python scripts](https://github.com/dawidborycki/Arm.PyTorch.MNIST.Inference.Python.git) used in the previous steps are also available on GitHub.
Loading

0 comments on commit a6a9231

Please sign in to comment.