Attention!
This tutorial is only for AIfES version 2.0.0. You can find the new tutorial here.
AIfES is compatible with nearly every ML framework, like TensorFlow, Keras or PyTorch. But we were also frequently asked if it is possible to use AIfES on the PC to develop artificial neural networks (ANN) more easily or to pre-train larger networks to run them later on your Arduino board.
The answer is: Yes you can 😃
In this tutorial we use the free and open-source C/C++ Code::Blocks IDE.
How to train an ANN directly on an Arduino board with AIfES we have already shown in the last tutorial.
In this tutorial we show you:
- How to install AIfES in the Arduino IDE
- How to import AIfES into a Code::Blocks project
- How to train an ANN on a PC with AIfES
- How to extract the weights
- How to get the ANN on your Arduino board
Since AIfES was programmed in pure C, the source code of the AIfES for Arduino library can be used on almost any hardware. For this reason AIfES should also run on almost any Arduino or Arduino compatible board. Hardware specific accelerators like Arm CMSIS must be enabled in aifes.h. An example of this can be found here.
The AIfES for Arduino library source code is also executable on the PC. The compiler / hardware must be GCC compatible.
This has the advantage that large networks can be pre-trained on the PC and the source code corresponds to the Arduino implementation. A porting is therefore possible without any problems. In addition, the ANN structure can also be developed on the PC to insert it later in the Arduino IDE. This is useful for self-learning systems.
For more information about AIfES, visit www.aifes.ai.
Install AIfES in the Arduino IDETo follow up the examples you have to download and install AIfES (search for aifes) with the Arduino library manager.
The used exampleFor this tutorial we will use examples that are already available in the AIfES library. An XOR gate is replicated and trained with an ANN. The XOR truth table is shown in the image below.
The used ANN structure for the replication is shown in the picture below. The sigmoid function is used as the activation function in the entire ANN.
We have included several XOR examples in the AIfES library, which should also run on any board.
This is how you can find the examples with this ANN structure in your Arduino IDE:
File
-> Examples
-> AIfES for Arduino
-> 0_Universal
-> 0_XOR
- Here only the inference is performed and the weights are already trained
- Here the training is performed directly on the board
- Here the training is done in Keras and the weights are transferred to AIfES afterwards
In this tutorial the ANN will be trained on the PC with AIfES and the trained weights will be printed. On the Arduino board the same ANN will be implemented with AIfES and the weights will be inserted. The inference can then be done directly on the board.
A suitable Sketch for this comes from the 4_XOR_Inference_keras
example. Here the training is done in Keras, the weights are printed and inserted in a suitable sketch. The sketch from this example can be used.
For the training in AIfES there is also a suitable example with 1_XOR_training
. We have put the example into the classic C structure with a main function. The known Arduino Sktech functions like setup()
or loop()
are omitted here. Also the Serial.print()
functions were replaced by classic printf()
. The biggest difference is the final output of the weights. Normally this example is used to train the ANN directly on the board and finally execute it. Now the training is done on the PC and the program prints the weights afterwards via printf()
. The weights are copied and pasted into the Arduino sketch. The source code for the training is provided in this project as main.c.
The next step is to create a Code::Blocks project and integrate AIfES.
Preparation:
- Download the Code::Blocks IDE and install it (Note: Download the "mingw" version, it includes the GCC/G++/GFortran compiler)
- Download AIfES_for_Arduino from GitHub an unzip it
- Download the main.c file from this project
- Put the main.c file into the folder:
AIfES_for_Arduino\src
Create a new projectinCode::Blocks:
- Create a new Project:
File
->new
->Project...
- Select "
Console application
"
- Select "
C
" as language
- Think of a project name. We have chosen "AIfES_XOR_training"
- Compiler:
GNU GCC Compiler
- Create the project
Integrate AIfES in Code::Blocks:
- Delete the automatically generated main.c file from the project:
right-click
on main.c ->remove file from project
- Navigate to:
Project
->Add files recursively...
- Choose the
AIfES_for_Arduino\src
folder (The downloaded main.c should also be located there)
- Confirm everything
- After that your project should look like this:
- Navigate to:
Project
->Build options
->Search directories
->Compiler
- Press the Button
Add
and navigate to theAIfES_for_Arduino\src
folder
- Confirm Keep this as a relative path? with
yes
- Save the project
- Now AIfES is integrated into your project
Code::Blocks separates the header and source code files by itself.
Training in Code::BlocksFor training the weights are randomly generated in a value range from -2 to 2.
The ADAM optimizer is used for training, with full batch over 100 epochs.
The next step is training:
- Build the project
- Run it
Troubleshooting:
Depending on your Code::Blocks configuration, the following files may fail to compile:
- ailayer_dense_cmsis.c
- aimath_f32_cmsis.c
In these files is the following include: #include "../../../aifes.h"
Please change it in both files to: #include "aifes.h"
Since we imported AIfES with relative paths, there may be some problems here. After the change, the project should compile successfully.
After the training, the learning success should be controlled. For this purpose, the loss is observed and the expected output is compared with the calculated output. (See red box)
Of course, learning success is not guaranteed since the weights are randomly generated. We have included a warning that appears when the loss is above 0.3.
At the end of the output are the trained weights, already formatted as C arrays. These have to be inserted into the Arduino Sketch.
The last step is the inference on the Arduino board. Open the compatible sketch from the AIfES examples:
File
-> Examples
-> AIfES for Arduino
-> 0_Universal
-> 0_XOR
-> 4_XOR_Inference_keras
Copy the trained weights to the places provided for them.There are two places, one for the hidden and the second for the output layer. Overwrite the weights from the example.
- Upload the sketch to your board
- Start the
Serial Monitor
(115200 baud)
- Type "inference" and press
return
The sketch feeds the trained ANN with 0 and 1 as shown in the following table. So the result should be as close to 1 as possible.
Compare the result from the Serial Monitor with the result from training.
Done 😉
Comments
Please log in or sign up to comment.