cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to create and inject data from my STM32F4 ?

NBlan.1
Associate III

Hello all,

Is it possible to create, inject data and perform the inference with just my STM32F4-DISCO without any connection with my PC (of course I must beforhand flash my model into my board). And how would you do it, I tried to integrate the code from "How to run locally a c-model" in my file but it doesn't seems to work. I thanks you in advance !

The code :

int ai_network_***(void)
{
    ai_u8 activations[AI_NETWORK_DATA_ACTIVATIONS_SIZE];
    ai_u8 in_data[AI_NETWORK_IN_1_SIZE_BYTES];
    ai_u8 out_data[AI_NETWORK_OUT_1_SIZE_BYTES];
 
    /* AI buffer IO handlers */
    ai_buffer *ai_input;
    ai_buffer *ai_output;
    ai_handle network = AI_HANDLE_NULL;
    ai_error err;
    ai_network_report report;
 
    /** @brief Initialize network */
    const ai_handle acts[] = { activations };
    err = ai_network_create_and_init(&network, acts, NULL);
    if (err.type != AI_ERROR_NONE) {
        printf("ai init_and_create error\n");
        return -1;
    }
 
    /** @brief {optional} for debug/log purpose */
    if (ai_network_get_report(network, &report) != true) {
        printf("ai get report error\n");
        return -1;
    }
 
    printf("Model name      : %s\n", report.model_name);
    printf("Model signature : %s\n", report.model_signature);
 
    ai_input = &report.inputs[0];
    ai_output = &report.outputs[0];
 
    /** @brief Fill input buffer with random values */
    srand(1);
    for (int i = 0; i < AI_NETWORK_IN_1_SIZE; i++) {
        in_data[i] = rand() % 0xFFFF;
    }
 
    /** @brief Normalize, convert and/or quantize inputs if necessary... */
 
    /** @brief Perform inference */
    ai_i32 n_batch;
 
    /** @brief Create the AI buffer IO handlers
     *  @note  ai_inuput/ai_output are already initilaized after the
     *         ai_network_get_report() call. This is just here to illustrate
     *         the case where get_report() is not called.
     */
    ai_input = ai_network_inputs_get(network, NULL);
    ai_output = ai_network_outputs_get(network, NULL);
 
    /** @brief Set input/output buffer addresses */
    ai_input[0].data = AI_HANDLE_PTR(in_data);
    ai_output[0].data = AI_HANDLE_PTR(out_data);
 
    /** @brief 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);
        printf("ai run error %d, %d\n", err.type, err.code);
      return -1;
    }
 
    /** @brief Post-process the output results/predictions */
    printf("Inference output..\n");
    for (int i = 0; i < AI_NETWORK_OUT_1_SIZE; i++) {
        printf("%d,", out_data[i]);
    }
    printf("\n");
    return 0;
}

Regards,

Numa

13 REPLIES 13
Andrew Neil
Evangelist III

Sure.

Instead of reading from real sensors, and/or a serial port, read the data from Flash instead.

Hello,

Thank you for your response. Isn't it what I am doing with the function ai_network_*** ? :

==> Creating my data lines 35-37

==> Set buffer addresses lines 54-55

==> Perform the inference line 58

Regards,

Numa

Andrew Neil
Evangelist III

@NBlan.1​ "it doesn't seems to work"

What does that mean?

  • What, exactly, were you expecting to happen?
  • What, exactly, is actually happening?
  • What testing/investigation/debugging have you done to find where it's going wrong?

Not sure that feeding random data into your model is going to give any useful results?

You're effectively just testing its handling of noise.

What is line 43 supposed to do?

Should that be a function call?

    /** @brief Perform inference */
    ai_i32 n_batch;
 

Hello,

Feeding random data to my model is a viable solution, it is used in the examples provided by ST.

ai_i32 is the type of data and n_batch is the variable that I'm calling line 58.

Regards,

Numa

Hello,

  • I was expecting that input was send to my board, processed by my model in my board and that I receive an output on my the terminal.
  • I'm using the XCubeAI package, I'm calling a function in aiValidation.c, the function is from "How to run locally a c-model" from the XCubeAI documentation.
  • I placed breakpoint in my code and I can't call my function, it's never used.

Regards,

Numa

so it's just the comment that's wrong?

It seems like it, it was in the original example.

@NBlan.1​  "I was expecting that input was send to my board"

But you're not sending anything? You asked how to do it without sending anything?

"receive an output on my the terminal."

So what output, exactly, were you expecting?

And what did you actually get?

"I can't call my function, it's never used"

So nothing to do with creating the test data?

Have you traced your code to see where the function should get called?