AI for ‌Truck APS Failure Detection ‌on‌ ‌a‌ ‌$4 MCU

by sumitml041 in Workshop > Cars

432 Views, 0 Favorites, 0 Comments

AI for ‌Truck APS Failure Detection ‌on‌ ‌a‌ ‌$4 MCU

Снимок экрана 2022-03-25 в 12.29.23.png
Снимок экрана 2022-03-25 в 13.36.14.png

How to apply the TinyML approach to detect truck failure related to the Air Pressure System (APS) in a timely manner.

Things which I used in this project:

Hardware components: Raspberry Pi Pico,ARM Cortex-M0,Bosch Sensortec TechnologyArduino Mega 2560

Software apps and online services : Neuton Tiny ML and Arduino IDE

Introduction:

The automotive industry is among the pioneers to adopt cutting-edge technologies, machine learning is no exception. With the help of ML solutions, engineers can now build neural networks to detect various car defects and breakages. In this article, I’d like to showcase how practitioners can easily apply the TinyML methodologies to create and deploy an AI-based solution on a primitive tiny device to predict truck failure related to the Air Pressure System (APS).

Being an essential part of a heavy-duty vehicle, APS generates pressurized air that is utilized in various functions in a truck, such as braking and gear changes, so timely failure detection can reduce the downtimes and the overall money spent in breakdowns up to a certain extent, as well as ease the process of truck inspections for drivers and employees, making it less error-prone.

Business Constraint

  • Latency: The time taken to make predictions after getting the data must be fairly low to avoid any unnecessary increase in the maintenance time and cost.
  • Cost of device: Having heavy GPUs, expensive edge devices would add unnecessary maintenance costs. Instead, the focus should be on better sensors and custom AutoML/TinyML solutions with higher accuracy and low memory footprints.
  • Cost of misclassification: The cost of misclassification is very high, especially if wrongly classifying a positive class datapoint, as it can lead to a complete breakdown of the truck and incur some serious costs.

Let's Build It

The goal of this tutorial is to demonstrate how you can easily build a compact ML model to solve a binary classification task in which the positive class means that the problem in the truck is due to a fault in the APS while the negative class means otherwise.

In our case, we utilize the dataset made using readings taken from Scania Trucks in their daily use (collected and provided by Scania themselves). The names of all the features are anonymized due to proprietary reasons. The dataset for this case study can be found here: https://archive.ics.uci.edu/ml/datasets/APS+Failure+at+Scania+Trucks

The experiment will be conducted on a $4 MCU, with no cloud computing carbon footprints :)

Dataset Description.

The dataset is divided into two parts, a train set,and a test set. The train set contains 60, 000 rows while the test set contains 16, 000 rows. There are 171 columns in the dataset, one of them is the class label of the datapoint, resulting in 170 features for each data point.




Creating a New Solution and Uploading the Dataset on the Neuton TinyML Platform

Снимок экрана 2022-03-25 в 13.40.21.png
Снимок экрана 2022-03-25 в 13.40.33.png
Снимок экрана 2022-03-25 в 13.40.48.png

Once you are signed in to your Neuton account, you should have a Solutions Home page, click on Add New Solution button.

Once the solution is created, as shown above, proceed to dataset uploading (keep in mind that the currently supported format is CSV only)

Select the target variable or the output you want for each prediction. In this case, we have class as Output Variable: 0 for 'negative' and 1 for 'positive'



Model Training and Parameters

Снимок экрана 2022-03-25 в 13.43.06.png
Снимок экрана 2022-03-25 в 13.54.49.png
Снимок экрана 2022-03-25 в 13.55.03.png
Снимок экрана 2022-03-25 в 13.55.56.png

Since we are going to embed the model onto a tiny MCU, we need to set the parameters accordingly. The Raspberry Pico can run 32-bit operations and set normalization type to Unique Scale for Each Feature

Click start training, it might take longer to train since the dataset is huge, for me, it took about ~6 hours. In the meantime, you can check out Exploratory Data Analysis generated once the data processing is complete, check the below video:

  • During training, you can monitor the real-time model performance by observing model status (“consistent” or “not consistent”) and Target metric value.

The target metric for me was: AUC 0.987415 and the trained model had the following characteristics:

Number of coefficients = 278, File Size for Embedding = 3.074 Kb. That's super cool!


Prediction and Embedding on Raspberry Pico

Снимок экрана 2022-03-25 в 12.55.45.png
Снимок экрана 2022-03-25 в 12.57.39.png
Снимок экрана 2022-03-25 в 14.02.25.png
Снимок экрана 2022-03-25 в 14.06.02.png
Снимок экрана 2022-03-25 в 14.06.14.png
Снимок экрана 2022-03-25 в 14.07.38.png

On the Neuton ai platform, click on the Prediction tab, and click on the Download button next to Model for Embedding, this would be the model library file that we are going to use for our device.


Once you have downloaded the model files, it's time to add our custom functions and actions. I am using Arduino IDE to program Raspberry Pico

Setting up Arduino IDE for Raspberry Pico:

I used Ubuntu for this tutorial, but the same instructions should work for other Debian based distributions such as Raspberry Pi OS.


9. Go to Tools >> Board >> Boards Manager.

10. Type “pico” in the search box and then install the Raspberry Pi Pico / RP2040 board. This will trigger another large download, approximately 300MB in size.


Note: Since we are going to make classification on the test dataset, we will use the CSV utility provided by Neuton to run inference on data sent to the MCU via USB.


Checksum, parser programfiles are for generating handshake with the CSV serial utility tool and sending column data to the Raspberry Pico for inference.

[Secrettip: If you train a similar binary classification model, just replace the model.h file and modify the *.ino file accordingly to run running inference on the CSV dataset via USB serial] See pin connection below:


Understanding the code part in APS_Failure_detection.ino file, we set different callbacks for monitoring CPU, time, and memory usage used while inferencing.


void setup() {
  Serial.begin(230400);
  while (!Serial);

  pinMode(LED_RED, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  digitalWrite(LED_RED, LOW);
  digitalWrite(LED_BLUE, LOW);
  digitalWrite(LED_GREEN, LOW);
  
  callbacks.send_data = send_data;
  callbacks.on_dataset_sample = on_dataset_sample;
  callbacks.get_cpu_freq = get_cpu_freq;
  callbacks.get_time_report = get_time_report;

  init_failed = app_init(&callbacks);
}


The real magic happens here callbacks.on_dataset_sample=on_dataset_sample

Once the input variables are ready, neuton_model_run_inference(&index, &outputs) is called which runs inference and returns outputs.


Installing CSV dataset Uploading Utility (Currently works on Linux and macOS only)

  • Install dependencies,
# For Ubuntu
$ sudo apt install libuv1-dev gengetopt
# For macOS
$ brew install libuv gengetopt

  • Clone this repo,
$ git clone https://github.com/Neuton-tinyML/dataset-uploader.git
$ cd dataset-uploader

  • Run make to build the binaries,
$ make

  • Once it's done, you can try running the help command, it's should be similar to shown below
user@desktop:~/dataset-uploader$ ./uploader -h

Usage: uploader [OPTION]...
Tool for upload CSV file MCU
  -h, --help                Print help and exit
  -V, --version             Print version and exit
  -i, --interface=STRING    interface  (possible values="udp", "serial"
                              default=`serial')
  -d, --dataset=STRING      Dataset file  (default=`./dataset.csv')
  -l, --listen-port=INT     Listen port  (default=`50000')
  -p, --send-port=INT       Send port  (default=`50005')
  -s, --serial-port=STRING  Serial port device  (default=`/dev/ttyACM0')
  -b, --baud-rate=INT       Baud rate  (possible values="9600", "115200",
                              "230400" default=`230400')
      --pause=INT           Pause before start  (default=`0')


Running Inference on Raspberry Pico

Снимок экрана 2022-03-25 в 14.08.33.png
Снимок экрана 2022-03-25 в 14.08.47.png
Снимок экрана 2022-03-25 в 14.08.59.png
Снимок экрана 2022-03-25 в 14.09.09.png
Снимок экрана 2022-03-25 в 14.09.18.png

Upload the program on the Raspberry Pico,

Once uploaded and running, open a new terminal and run this command:

$ ./uploader -s /dev/ttyACM0 -b 230400 -d /home/vil/Desktop/aps_failures_test.csv

The inference has started running, once it is completed for the whole CSV dataset it will print a full summary.

>> Request performace report
Resource report:
       CPU freq: 125000000
    Flash usage: 2785
RAM usage total: 821
      RAM usage: 821
    UART buffer: 694

Performance report:
Sample calc time, avg: 1183.0 us
Sample calc time, min: 1182.0 us
Sample calc time, max: 1346.0 us

I also did a comparison with Web Prediction on the Neuton TinyML platform and the results were similar. Plus, I tried to build the same model with TensorFlow and TensorFlow Lite. My model built with Neuton TinyML turned out to be 14.3% better in terms of AUC and 9.7 times smaller in terms of model size than the one built with TF Lite. Speaking of the number of coefficients, TensorFlow's model has 7, 060 coefficients, while Neuton's model has only 278 coefficients (which is 25.4 times smaller!).


Isn't it amazing that Raspberry Pico is able to perform tasks that are otherwise handled using high-performance machines on the cloud?

Conclusion

This tutorial is vivid proof that you don’t need to be a data scientist to rapidly build super compact ML models to proactively solve practical challenges. And, most importantly, the implementation of such solutions using tinyML, which saves lots of money and resources, doesn’t require high costs or efforts, but only a free no-code tool and a super cheap MCU!

Stay tuned for more exciting tutorials and don't forget to sign for a free Neuton AI account :)