cancel
Showing results for 
Search instead for 
Did you mean: 

Please help me with stm32 AI code once

limjongmin
Associate

 

When designing stm32 AI, the values come out, but the learned values don't seem to come out. This is training data using 12 variables. Please help me if there is a problem with the code.

 

/* System headers */

#include <stdint.h>

#include <stdlib.h>

#include <stdio.h>

#include <inttypes.h>

#include <string.h>

 

#include "app_x-cube-ai.h"

#include "main.h"

#include "ai_datatypes_defines.h"

#include "network.h"

#include "network_data.h"

 

/* USER CODE BEGIN includes */

/* USER CODE END includes */

 

/* IO buffers ----------------------------------------------------------------*/

#if !defined(AI_NETWORK_INPUTS_IN_ACTIVATIONS)

AI_ALIGNED(4) ai_i8 data_in_1[AI_NETWORK_IN_1_SIZE_BYTES];

ai_i8* data_ins[AI_NETWORK_IN_NUM] = {

data_in_1

};

#else

ai_float* data_ins[AI_NETWORK_IN_1_SIZE_BYTES] ;

#endif

 

#if !defined(AI_NETWORK_OUTPUTS_IN_ACTIVATIONS)

AI_ALIGNED(4) ai_i8 data_out_1[AI_NETWORK_OUT_1_SIZE_BYTES];

ai_i8* data_outs[AI_NETWORK_OUT_NUM] = {

data_out_1

};

#else

ai_float* data_outs[AI_NETWORK_OUT_1_SIZE_BYTES];

#endif

 

/* Activations buffers -------------------------------------------------------*/

AI_ALIGNED(32)

static uint8_t POOL_0_RAM[AI_NETWORK_DATA_ACTIVATION_1_SIZE];

 

ai_handle data_activations0[] = {POOL_0_RAM};

 

/* AI objects ----------------------------------------------------------------*/

 

static ai_handle network = AI_HANDLE_NULL;

 

static ai_buffer* ai_input;

static ai_buffer* ai_output;

 

static void ai_log_err(const ai_error err, const char *fct)

{

/* USER CODE BEGIN log */

if (fct)

printf("TEMPLATE - Error (%s) - type=0x%02x code=0x%02x\r\n", fct,

err.type, err.code);

else

printf("TEMPLATE - Error - type=0x%02x code=0x%02x\r\n", err.type, err.code);

 

do {} while (1);

/* USER CODE END log */

}

 

static int ai_boostrap(ai_handle *act_addr)

{

ai_error err;

 

/* Create and initialize an instance of the model */

err = ai_network_create_and_init(&network, act_addr, NULL);

if (err.type != AI_ERROR_NONE) {

ai_log_err(err, "ai_network_create_and_init");

return -1;

}

 

ai_input = ai_network_inputs_get(network, NULL);

ai_output = ai_network_outputs_get(network, NULL);

 

#if defined(AI_NETWORK_INPUTS_IN_ACTIVATIONS)

/* In the case where "--allocate-inputs" option is used, memory buffer can be

* used from the activations buffer. This is not mandatory.

*/

for (int idx=0; idx < AI_NETWORK_IN_NUM; idx++) {

data_ins[idx] = ai_input[idx].data;

 

printf("1");

}

#else

for (int idx=0; idx < AI_NETWORK_IN_NUM; idx++) {

ai_input[idx].data = data_ins[idx];

printf("2");

}

#endif

 

#if defined(AI_NETWORK_OUTPUTS_IN_ACTIVATIONS)

/* In the case where "--allocate-outputs" option is used, memory buffer can be

* used from the activations buffer. This is no mandatory.

*/

for (int idx=0; idx < AI_NETWORK_OUT_NUM; idx++) {

data_outs[idx] = ai_output[idx].data;

}

#else

for (int idx=0; idx < AI_NETWORK_OUT_NUM; idx++) {

ai_output[idx].data = data_outs[idx];

}

#endif

 

return 0;

}

 

static int ai_run(ai_float *in_data, ai_float *out_data)

{

ai_i32 batch;

 

ai_input[0].data = AI_HANDLE_PTR(in_data);

ai_output[0].data = AI_HANDLE_PTR(out_data);

 

 

batch = ai_network_run(network, &ai_input[0], &ai_output[0]);

if (batch != 1) {

ai_log_err(ai_network_get_error(network),

"ai_network_run");

return -1;

}

 

// out_data[0] = ((ai_float *) (ai_output[0].data))[0];

 

return 0;

}

 

/* USER CODE BEGIN 2 */

int acquire_and_process_data(ai_i8* data[])

{

/* fill the inputs of the c-model

for (int idx=0; idx < AI_NETWORK_IN_NUM; idx++ )

{

data[idx] = ....

}

 

*/

return 0;

}

 

int post_process(ai_float* data[])

{

/* process the predictions

for (int idx=0; idx < AI_NETWORK_OUT_NUM; idx++ )

{

data[idx] = ....

}

 

*/

 

((ai_float *)data)[0] = (ai_float)1.0f;

((ai_float *)data)[1] = (ai_float)5.0f;

((ai_float *)data)[2] = (ai_float)1.0f;

((ai_float *)data)[3] = (ai_float)1.0f;

((ai_float *)data)[4] = (ai_float)1.0f;

((ai_float *)data)[5] = (ai_float)1.0f;

((ai_float *)data)[6] = (ai_float)1.0f;

((ai_float *)data)[7] = (ai_float)1.0f;

((ai_float *)data)[8] = (ai_float)1.0f;

((ai_float *)data)[9] = (ai_float)1.0f;

((ai_float *)data)[10] = (ai_float)1.0f;

((ai_float *)data)[11] = (ai_float)1.0f;

 

return 0;

}

/* USER CODE END 2 */

 

/* Entry points --------------------------------------------------------------*/

 

void MX_X_CUBE_AI_Init(void)

{

/* USER CODE BEGIN 5 */

ai_boostrap(data_activations0);

/* USER CODE END 5 */

}

 

void MX_X_CUBE_AI_Process(void)

{

float y_val;

/* USER CODE BEGIN 6 */

post_process(data_ins);

ai_run(data_ins,data_outs);

for(int i=0;i<12;i++)

{

printf("%f\r\n",((float *)data_ins)[i]);

}

y_val = ((float *)data_outs)[0];

printf("output: %f\r\n",y_val);

HAL_Delay(1000);

/* USER CODE END 6 */

}

#ifdef __cplusplus

}

#endif

0 REPLIES 0