Build and run a letter recognition NN model on an STM32L4 Discovery board
Introduction
Prepare development environment
Collect training data
Train the model
Feature extraction
Run the model on development board
Next Steps
Build and run a letter recognition NN model on an STM32L4 Discovery board
In this section, you will deploy the model directly on the STM32 board.
Setup STM32CubeMX project
STM32CubeMX
is a graphical tool for configuring STM32 microcontrollers.
STM32Cube.AI
is an extension of STM32CubeMX to import ML models.
Download and install STM32CubeMX from the STM32CubeMX product page .
Open STM32CubeMX IDE.
Click
Access to Board Selector. Find yourB-L475E-IOT01Aboard and clickStart Project.Go to
Project Manager. Enter a project name and select the project location where the project will be saved.Under
Pinout & Configuration, expandPinoutmenu, and clickClear pinouts.Expand
Software Packsmenu, and clickSelect Components.Locate
X-CUBE-AI, and click itsInstallbutton.Expand the menu and enable
X-CUBE-AI>Core. For device application, chooseValidation. If the status shows a yellow warning, clickResolveto install any necessary dependencies.Click
OKwhen done.
Validate the NN model
You will now validate the NN model built in the previous section on the desktop and the development board.
Validate on desktop
On the STM32CubeAI menu, locate
X-CUBE-AIconfiguration from the list ofCategories.click
Add network. Browse to the generated model (.h5file). You can also specify other values, such as validation input, and validation output. Leave as default for now.Click
Analyze, then it will generate a detailed report on the model. You can check the number of parameters, the size of the weights, and the amount of memory to be used.Click
Show graphto visualize your model.Click
Validate on Desktopto validate the model with the provided input and output on your desktop.
Validate on development board
To validate the model on the target, you need to first generate code and install the code on the target.
- Click
Validate on target, and enableAutomatic compilation and download. Select the appropriate toolchain (STM32CubeIDE), and clickOK.
Demo with the sample application
A sample application is provided. This uses a feature-based model, trained with the letters O and S written in a horizontal plane.
There is a bug in the example as supplied. You must edit /Core/Src/main.c before building.
Locate the function call in main() to
MX_USART1_UART_Init()
and move to before the call to
MX_X_CUBE_AI_Init()
Using a terminal application (such as
PuTTY), set up the serial connection to the board.COMnumber will be as before. The baud rate should be 115200.Import the application. Click the File tab and click
Import. SelectExisting project into workspace. Go totf_stm32folder and selectMCU_Activity_Recognition.Open
X-CUBE-AI/App/app_x-cube-ai.c. Observe that functions are provided for data acquisition from the accelerometer sensor and feature extraction.Build application and flash to target with
Run As.Observe output on the terminal.
Press the blue button on the board, draw a letter, then press the button again.
Create your own application
You can create your own application from the model you have trained.
Open the list of Software Packages. Change the device application as
Application.Click
Generate Codethen the project is updated.Open
STM32CubeIDE. Since you re-generated the code again, you need to delete the existing project and import the project again.Open
X-CUBE-AI/App/app_x-cube-ai.c. This is the auto-generated code bySTM32CubeMX.Between
USER_CODE_BEGINandUSER_CODE_END, you can write code for your application.
There are two main functions that you should implement:
acquire_and_process_data()is for getting data from sensors and processing the data, for example, extracting features.postprocess()is for postprocessing the output