cancel
Showing results for 
Search instead for 
Did you mean: 

Cube.AI: Hard fault in ai_platform_network_process() function

löns
Associate II

Hello,

I want to run my own neural network on a NUCLEO-F722ZE board for classifying audio samples. Since there is an older existing example project by STM (Acoustic Scene Classification) so I kept the whole data structure quite similar to this project, using the same network topology and same input vector size.

I loaded my keras .h5 file into X-CUBE-AI and was able to successfully analyze the model and generate code. After this I followed the instructions in the arcticle "How to perform motion sensing on STM32L4 IoTnode" and implemented two functions identical to the AI_Init() and AI_Run() and the respective variables. 

I am able to successfully initialize and create the AI model, with the ai_network_1_create_and_init() function returning 0 and the network handle being not null (my given network name is "network_1") . After this I fill the input array with data and call the AI_Run() function, which then again calls my ai_network_1_run() function, which again calls the ai_platform_network_process(). This is how far I can trace the error back. When I step through the lines of code the point of the call of the ai_platform_network_process() function is always where the hard fault interrupt occures. Behind this everything what happens is a black box to me, which leaves me with no other option but asking for help.

After finding this forum post with the same issue, I tried downgrading from X-CUBE-AI v9.0.0 to v8.1.0 and re-generating the code. This doesn't change anything in the behaviour though. Unfortunately I cannot try the suggested v7.x since I run an ARM based Mac.

To me it's really quite odd, since I assume the model is recognized by Cube.AI correctly, because the analyze + code generation is successful. Also the model itself is - apart from the weight values and the output vector size (5 instead of 3) - identical to the Acoustic Scene Classification example project. CRC is also enabled and the init function is called, this was also one thing frequently pointed out in my research.

I tried feeding random float numbers, but also actual spectrogram data into the function (verified with live expressions)  with no success. I always get the hard fault.

Here are my functions for the nn handling derived from the knowledgebase article:

 

ai_handle network;
ai_u8 activations[AI_NETWORK_1_DATA_ACTIVATIONS_SIZE];
ai_buffer * ai_input;
ai_buffer * ai_output;

int AI_Init() {
	ai_error err;

	/* Create a local array with the addresses of the activations buffers */
	const ai_handle act_addr[] = { activations };
	/* Create an instance of the model */
	err = ai_network_1_create_and_init(&network, act_addr, NULL);
	if (err.type != AI_ERROR_NONE) {
		printf("ai_network_create error - type=%d code=%d\r\n", err.type, err.code);
		return -1;
	}
	ai_input = ai_network_1_inputs_get(network, NULL);
	ai_output = ai_network_1_outputs_get(network, NULL);

	return 0;
}

int AI_Run(float* spectrogram, float* classification_result) {
    ai_i32 batch;
    ai_error err;

    /* Update IO handlers with the data payload */
    ai_input[0].data = AI_HANDLE_PTR(spectrogram);
    ai_output[0].data = AI_HANDLE_PTR(classification_result);

    if(network == AI_HANDLE_NULL)
      {
          printf("E: network handle is NULL\r\n");
          return -1;
      }
    // Run the network
    batch = ai_network_1_run(network, ai_input, ai_output);
    if (batch != 1) {
        err = ai_network_1_get_error(network);
        printf("AI ai_network_run error - type=%d code=%d\r\n", err.type, err.code);
        return -1;
    }
    return 0; // Success
}

 

And here is my main.c:

 

/* USER CODE BEGIN PV */
float spectrogram[AI_NETWORK_1_IN_1_SIZE];
float aiOutData[AI_NETWORK_1_OUT_1_SIZE];
/* USER CODE END PV */
int main(void)
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* Enable the CPU Cache */

  /* Enable I-Cache---------------------------------------------------------*/
  SCB_EnableICache();

  /* Enable D-Cache---------------------------------------------------------*/
  SCB_EnableDCache();

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  MX_USB_OTG_FS_PCD_Init();
  MX_CRC_Init();
  /* USER CODE BEGIN 2 */
  __HAL_RCC_CRC_CLK_ENABLE();
  if (AI_Init() != 0) {
      printf("Failed to initialize neural network\n");
      return -1;
  }
  for (uint32_t i = 0; i < AI_NETWORK_1_IN_1_SIZE; i++) {
      spectrogram[i] = (i+1) / 1.1f; // fill array with example data
  }
  if (AI_Run(spectrogram, aiOutData) == 0) {
      for (uint32_t i = 0; i < AI_NETWORK_1_OUT_1_SIZE; i++) {
          printf("%8.6f ", aiOutData[i]);
      }
   }
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 

I also attached my model.h5 file. Any help getting this to work is very much appreciated.

Thanks,

Leon

1 REPLY 1
löns
Associate II

In the meantime I tried the following:

  • lower Keras/Tensorflow version (2.10 instead of 2.15) with which the model is generated
  • .tflite model file format
  • the model file from the ASC processing example (inside the same Cube MX project as with my model)
  • different X-CUBE-AI versions on different Operating Systems (7.3 on Windows and 8.1.0 on Mac)
  • increase minimum stack size in CubeMX to 128 kbyte (although I am a bit irritated by the "minimum", because the way I understand it, it shouldn't really have an impact since it's not a maximum and should just take what it needs)

All with no success. The hard fault always occures always in the same position as before.

Does anyone have any idea what the cause could be? 

Thank you very much.

Best regards,

Leon