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. Also, of course, the question of whether you can use other microcontroller IDEs.
The answer is: Yes you can 😃
We have already put the first guides for various IDEs in our GitHub repository which you can find here. You can help us by downloading the *.docx
template and write a manual for your favourite IDE.
Send it as a PDF to aifes(at)ims.fraunhofer.de
In this tutorial we use the free and open-source C/C++ Code::Blocks IDE.
In this tutorial we show you:
- How to prepare AIfES to be universally usable
- How to install AIfES in the Arduino IDE
- How to use AIfES in Wokwi simulation
- How to import AIfES into a Code::Blocks project
- How to train an ANN on a PC with AIfES and AIfES-Express
- 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 Arduinolibrary can be used on almost any hardware. For this reason it also runs on almost any Arduino or Arduino compatible board. Hardware specific accelerators like Arm CMSIS must be enabled.
We have different configuration files for AIfES® to make it compatible with different IDEs. In the default configuration it is of course optimized for the Arduino IDE. Arduino specific functions like serial.print()
are used. By changing the configuration files, you can make it universal. Then the classic printf()
functions are used. You can then use AIfES® almost everywhere, on the PC, in a microcontroller IDE of your choice or even in a hardware simulator.
For more information about AIfES®, visit www.aifes.ai.
Download and prepare the AIfES® library- Download AIfES_for_Arduino from GitHub an unzip it
- Navigateto the “src/” folder
- Replace the files “aifes_config.cpp” and “aifes_config.h” in the “src/”directory by the ones found in “etc/aifes_configurations/pc/”
- Note that the new config file has the extension *.c and not *.cpp
- That's it 😃
Of course, you can also use CMSIS in the universal version if you use e.g. another microcontroller IDE. For the installation please follow this guide.
The only difference is that you don't make the changes in the file structure of the Arduino IDE, but in your just downloaded copy.
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.
You don't have hardware? Use WokwiIf you don't have an Arduino board, you can also try AIfES® in the online hardware simulator Wokwi.
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.
We have included an example for AIfES® and for the simplified API AIfES-Express. Each example is stored in a separate function.
This is how you can find the examples with this ANN structure in your Arduino IDE:
AIfES®:
File
-> Examples
-> AIfES for Arduino
-> 0_Universal
-> 0_XOR_F32
- 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_F32_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_F32_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.
AIfES-Express:
For the AIfES-Express example you can use the following sketch:
0_AIfES_Express_XOR_F32_Inference
Just replace the existing weights with the newly trained ones.
Integrate AIfES® in Code::BlocksThe next step is to create a Code::Blocks project and integrate AIfES®.
Preparation:
- Note that you have prepared the AIfES library as described above
- Download the main.c file from this project
- Put the main.c file into the folder:
\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-main\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-main\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::BlocksAs already explained, there are two examples. One for AIfES® AIfES_demo()
and one for AIfES-Express AIfES_Express_demo()
. In the main
function you can select which example you want to test.
AIfES_demo()
For 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
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.
AIfES_Express_demo()
The AIfES-Express demo is based on this example:
2_AIfES_Express_XOR_F32_training
The weight initialization is identical here, but there is an early stopping function. You can specify a target loss up to which you want to train and the training will stop automatically when it is reached. In this case it is 0.004.
Here the weights are output in an array as FlatWeights.
The last step is the inference on the Arduino board.
AIfES_demo()
Open the compatible sketch from the AIfES examples:
File -> Examples -> AIfES for Arduino -> 0_Universal -> 0_XOR_F32 -> 4_XOR_F32_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.
AIfES_Express_demo()
Open the compatible sketch from the AIfES examples:
File -> Examples -> AIfES for Arduino -> 0_Universal -> 2_AIfES_Express_XOR_F32 -> 0_AIfES_Express_XOR_F32_Inference
Copy the trained weights from the PC into the sketch and replace the existing weights. In this example all 4 XOR combinations are calculated.
Results:
Done 😉
Comments
Please log in or sign up to comment.