summary:Why can you run a neural network on STM32? In short, use the X-Cube-AI extension package in STM32CubeMX to convert the current popular AI framework to C code to support use on embedded devices. Currently, using X-Cube-AI requires STM32CubeMX version 5.0 Above, the models that support conversion include Keras, TFlite, ONNX, Lasagne, Caffe, and ConvNetJS. Cube-AI converts the model into a bunch of arrays, and then parses the contents of these arrays into models, which is the same as the model in Tensorflow after converting to arrays.

1. Environment installation and configuration



F4/H7/MP157 development board

2. Construction of AI neural network model

Here we use the officially provided model for testing and train with the keras framework:

Model introduction

Human Activity Recognition Using CNNs in Keras: This repository contains code for small projects. The purpose of this project is to create a simple Convolutional Neural Network (CNN) based Human Activity Recognition (HAR) system. The system uses sensor data from a 3D accelerometer and recognizes the user's movements, such as forward or backward. HAR means Human Activity Recognition (HAR) system, that is, human behavior recognition. This model is based on the 3D acceleration data of a person for a period of time to judge the current behavior of a person, such as walking, running, going upstairs, going downstairs, etc., which is very suitable for the application scenario of Cortex-M series MCU. The data used is shown in the figure below.

Raw data used by HAR

The repository contains the following files, a Python script file containing a Keras implementation of a CNN-based Human Activity Recognition (HAR) model,

actitracker_raw.txt, a text file containing the dataset used in this experiment,

model.h5, a pre-trained model, trained on the training data,, a Python script file that contains the evaluation script. This script evaluates the performance of pretrained netowrk on the provided testData,

testData.npy, a Python data file containing test data for evaluating available pretrained models,

groundTruth.npy, a Python data file containing the corresponding output ground truth and

Don't panic with so many files, the model.h5 model obtained after model training is what we need.

3. New construction

1. By default, everyone has already installed the STM32CubeMX software.

To verify the neural network model (HAR human activity recognition) on STM32, STM32F3/F4/L4/F7/L7 series high-performance microcontrollers are generally required. Running the network model generally requires more than 3MB of flash memory space, and ordinary microcontrollers do not support such a large space. , CUBEMX provides a compression rate option, you can choose an appropriate compression rate, which is actually to compress the weight coefficient of the neural network model, so that the network model can run on a single-chip computer, the compression rate is 8, so that the model is reduced to 366KB, and the verification can pass ;

Then follow the steps below to install the CUBE.AI extension package

I have installed three of these, just install one of the latest versions.

The next step is to get familiar with the new construction.

Because the AI ​​package is installed, the artificial intelligence option will appear on this interface. Click Enable to see which chips support AI.

The next step is to configure the download interface and external crystal oscillator.

Then remember to select a serial port as the debug information printout.

Select Software Packs, click on the two AI-related packages after entering, tick the first one, and select Validation for the first one.

System Performance Project: The entire application project runs on STM32MCU, which can accurately measure NN inference results, CP∪U load and memory usage. Monitor results using a serial terminal (egTera Term)

Validation Project: A complete application that incrementally validates the results returned by NN with random or user test data in desktop PC and STM32 Arm Cortex-m based MCU embedded environments. Use with the X-CUBE-A verification tool.

Application Template Project: An empty template project that allows building applications, including multi-network support.

After that, click on Software Packs in the left column, select X-CUBE-AI, and tick both checkboxes in the pop-up Mode window. In the Configuration window, click on the network tab.

Select the serial port just configured for debugging.

Click add network, select the downloaded model point h5 model, and select the compression factor of 8;

Click Analyze to see the parameter comparison before and after model compression

Click validate on desktop to perform model validation on the PC, including the comparison between the original model and the converted model, and the result of the current validation will be shown below.

To this end, the model verification is completed, and the model deployment is started.

4. Model conversion and deployment

Clock configuration, the system will automatically configure the clock. You can configure the clock according to the actual selection of your microcontroller.

Finally, click GENERATE CODE to generate the project.

Then compile and link in MDK.

After selecting the downloader, you can download the code.

Then open the serial debugging assistant to see a series of printing information.

After the code is programmed into the chip, go back to the position shown in the figure below in CubeMX, we click Validate on target, and run the verification program on the board, the effect is as shown in the figure below, it can work, and the model is successfully deployed in the MCU.

This time, I will run the official routine first, and then I will study it later and run my own model.

Reviewing Editor: Li Qian

Leave a Reply

Your email address will not be published.