How to implement Artificial Intelligence in an Arduino Project

This example aims to design a classification system deployed in Arduino Nano (https://store.arduino.cc/usa/arduino-nano) that can classify all 3 different IRIS flowers..

Tuan Nghia Nguyen · 3/11/2019 1:11:52 PM

How to implement Artificial Intelligence in an Arduino Project

This example aims to design a classification system deployed in Arduino Nano (https://store.arduino.cc/usa/arduino-nano) that can classify all 3 different IRIS flowers.

Isis1.png

The Arduino source code can be downloaded in here

1. Data preparation and feature extraction

In order to classify three IRIS flowers, common features such as petal length, petal width, sepal length, and sepal width are used. The IRIS flowers' dataset can be obtained via Wikipedia (https://en.wikipedia.org/wiki/Iris_flower_data_set). The dataset contains a set of 150 records under five attributes as follows:

IRIS_Flower_dataset.png

Figure 1: IRIS flowers' dataset under 5 attributes: Sepal Length, Sepal Width, Petal Length, Petal Width, and Species.

 

 

 

Since 3 types of IRIS flowers being classified, the outputs will be decoded into binary. For example, [1,0,0] for Setosa, [0,1,0] for Versicolor and [0,0,1] for Virginica. The IRIS dataset will be separated into a training set (70%) and test set (30%). These datasets are prepared in Excel "csv" files as follows:

IRISCSVFile.png

Figure 2: IRIS flower training set.

 

2. Design Bayesian Neural Network Classifier using ANNHub

The system diagram for IRIS flower classification is shown in Figure 3.  The neural network classifier will be able to give a prediction of IRIS flower type based on its features (sepal length, sepal width, petal length, and petal width)

Versiolor.png

 

NeuralNetworkClassifier.png

 

Figure 3: Neural Network Classifier for IRIS flowers.

 

There are four steps to designing a Neural Network Classifier. Since IRIS dataset is small, Bayesian Neural Network with Regularization is used in this case. 

 

Step 1:  Load IRIS training set 

 LoadIRISDatasetToANNHUB.png

 

 Figure 4: Load IRIS training dataset into ANNHUB.

Step 2: Configure the Neural Network

ConfigureBayesianNeuralNetworkForIRIS.png

 Figure 5: Configure Neural Network on ANNHUB.

 

Based on dataset format, ANNHUB will suggest the neural network structure such as training algorithm, number of hidden nodes, training data ratio, preprocessing method, post-processing method, cost function and activation functions for different layers. Advanced users can tweak to achieve the optimal neural network model' performance.

 

Step 3: Train Neural Network

 

TrainBayesianNeuralNetworkForIRIS.png

 Figure 6: Train Neural Network on ANNHUB.

ANNHUB will suggest training parameters and stopping criteria for according training algorithm configured in Step 2. Early stopping technique and Bayesian Regularization will be handled automatically to prevent overfitting issue during the training process. That will ease the burden of the designer task. In this example, 70% of the training dataset is used to train the neural network classifier.

 

Step 4: Evaluate the Neural Network

After the neural network being trained, it then needs to be evaluated to analyze if it performs well on validation and test set. In this example, since Bayesian Regularization is used, validation set is not required. Only 30% of the training dataset is used to evaluate the neural network performance. Both confusion matrix and ROC curve are supported to give a designer an insight view on the classifier's performance and stability.

 

EvaluateBaysianNeuralNetworkIRIS.png

 Figure 7: Evaluate Neural Network on ANNHUB.

 

 

4. Test Neural Network performance with a new dataset in ANNHUB

ANNHUB allows the design to import completely new dataset to verify the trained model. In this example, 44 data samples, which are not included in the training dataset, are used to test the trained Neural Network Classifier.

TestBayesianNeuralNetworkIRIS.png

ROCCuves.png

 Figure 8: Test Neural Network on a new dataset in ANNHUB. 

ANNHUB supports breakdown details to aid inspection of the classifier's performance and robustness.

5. Deploy trained Neural Network classifier into Arduino device.

ANNHUB supports exporting trained neural network model directly to Arduino source code (*.ino) as shown in Figure 9.

ExportToArduinoSourceCode.png

 Figure 9: Export trained Neural Network Classifier into Arduino Sketch format

This source code can then be opened in Arduino IDE directly.  The user can modify the source code to meet his/her requirement for any application. No library or dependency is required for compiling and uploading the compiled code into the Arduino device. The source code in this example can be downloaded in here.

 

CustomiseArduinoSourceCode.png

UploadCompiedToArduino.png

5. Test Arduino IRIS flower classifier. 

Once the trained model is uploaded into the Arduino device, this device will be able to classify an IRIS flower type based on its input's features. To demonstrate it, the simple hardware setup is shown in Figure 10 as follows:

ArduinoClassifierSetup.png

Figure 10: Communication between Laptop PC and Arduino device (Arduino Nano) via USB port.

In this setup, a user can input IRIS flower's features (sepal length, sepal width, petal length, petal width) to Arduino Nano via USB (serial communication), the Arduino Nano then interprets these inputs to give a prediction based on trained Neural Network in its firmware. This prediction result will be sent back to PC via USB (serial communication).

The Serial Monitor Window in Arduino IDE can be used as user interface:

SerialPortCommucation.png

 

Figure 11: Serial communication interface between Laptop PC and Arduino device (Arduino Nano) via USB port.

 

Test dataset will be used to verify Arduino prediction results:

Prediction result of Setosa

SetosaTesting.png

SetosaPredictionResults.png

Prediction result of Versicolor

 

 

VesicolorData.png

VesicolorResult.png

Prediction result of Virginica

Virfinicadata.png

VirginicaResults.png

5. Conclusion. 

In this example, the Bayesian Neural Network is used to classify three types of IRIS flowers. This Bayesian Neural Network then is deployed directly to Arduino Nano (with limited RAM memory - 2kB only). The Arduino Nano based AI device can handle classification task that can successfully recognise different types of IRIS flowers based on theirs sepal length, sepal width, petal length and petal width.

 

This example also demonstrates the capability of ANNHub that allows designers deploying a Neural Network on the embedded system easily.  

Related Blogs

Keep in touch with the latest blogs & news.

Be the first to post a comment.

Please login to add your comment.

ANS Center Logo

ANS Center provides elegent solutions to simplify machine learning and deep learning design and deployment.

We support mutiple hardware platforms and programming languages, including LabVIEW, LabVIEW NXG, LabWindow CVI, C/C++/C#, and Arduino.