Showing results for 
Search instead for 
Did you mean: 

How do I send my input data to a neural network on an STM32F4 to make predictions?

Associate II

Hello thank you very much. I did a course on neural networks with keras in EDX, and I can now train, save and load my networks on an STM32F4VE, with compression 8, using STM32Cube AI. The verification with my data set works well. And now how do I continue? How do I send my data to the STM32 to make predictions?

For example, I used Keras in Anaconda 3, and I used the MNIST data set, in Keras I can make predictions based on images that I enter with my numbers and my calligraphy, and it predicts very well. That same set of images that I wrote myself, I normalize them, and export them in a comma separated file, the images are floating numbers. I use that file to do the verification with STM32Cube AI and it works very well. That is to say that until now everything is going very well, my question now is, How do I continue ?, How do I modify the code generated by STM32Cube AI, to receive my image and that the microcontroller, process it and give me the output?

Best Regards,

ST Employee

​Hi Fernando,

For the next step, you have different way. You can generate a project (aiTemplate) and use it as skeleton. You need to update with our own application code to use the embedded reference API.  Application is in charge to collect/acquire and pre-process the inputs, to call the ai_network_run() fct and after to post-process the prediction.

see doc : C:\Users\<user_name>\STM32Cube\Repository\Packs\STMicroelectronics\X-CUBE-AI\5.0.0\Documentation\index.html ("Embedded inference client API" entry)

#include <stdio.h>
#include "network.h"
/* Handle to reference the instantiated NN */
static ai_handle network = AI_HANDLE_NULL;
/* Allocate the input buffer */
static ai_u8 in_data[AI_NETWORK_IN_1_SIZE_BYTES];
/* Allocate the output buffer */
static ai_u8 out_data[AI_NETWORK_OUT_1_SIZE_BYTES];
int aiRun(const void *in_data, void *out_data)
  ai_i32 n_batch;
  ai_error err;
  /* 1 - Create the AI buffer IO handlers */
  ai_buffer ai_input[AI_NETWORK_IN_NUM] = AI_NETWORK_IN ;
  ai_buffer ai_output[AI_NETWORK_OUT_NUM] = AI_NETWORK_OUT ;
  /* 2 - Initialize input/output buffer handlers */
  ai_input[0].n_batches = 1;
  ai_input[0].data = AI_HANDLE_PTR(in_data);
  ai_output[0].n_batches = 1;
  ai_output[0].data = AI_HANDLE_PTR(out_data);
  /* 3 - Perform the inference */
  n_batch = ai_network_run(network, &ai_input[0], &ai_output[0]);
  if (n_batch != 1) {
      err = ai_network_get_error(network);
      // ...
      return err.code;
void main_loop()
  while (1) {
    /* 1 - Acquire, pre-process and fill the input buffers */
    /* 2 - Call inference engine */
    aiRun(in_data, out_data);
    /* 3 - Post-process the predictions */

You can use also, according the tagetted UC,  a Functional Pack as starting point: