hello i would like to run this example of motion sensing h5 transform to .tflite and run using tflite micro runtime instead of stmcube runtime ...
https://wiki.st.com/stm32mcu/wiki/How_to_perform_motion_sensing_on_STM32L4_IoTnode
Can anyone help?
hello i would like to run this example of motion sensing h5 transform to .tflite and run using tflite micro runtime instead of stmcube runtime ...
https://wiki.st.com/stm32mcu/wiki/How_to_perform_motion_sensing_on_STM32L4_IoTnode
Can anyone help?
In order to use the TFLm runtime, the Keras .h5 needs to be converted in to a .tflite file. Details on how to do this can be found in the X-CUBE-AI html documentation:
def representative_dataset_gen():
data = tload(...)
for _ in range(num_calibration_steps):
# Get sample input data as a numpy array in a method of your choosing.
input = get_sample(data)
yield [input]
converter = tf.lite.TFLiteConverter.from_keras_model_file(<keras_model_path>)
converter.representative_dataset = representative_dataset_gen
# This enables quantization
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# This ensures that if any ops can't be quantized, the converter throws an error
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# For full integer quantization, though supported types defaults to int8 only
converter.target_spec.supported_types = [tf.int8]
# These set the input and output tensors to uint8 (added in r2.3)
converter.inference_input_type = tf.uint8 # or tf.int8/tf.float32
converter.inference_output_type = tf.uint8 # or tf.int8/tf.float32
quant_model = converter.convert()
# Save the quantized file
with open(<tflite_quant_model_path>, "wb") as f:
f.write(quant_model)
...
Once you have your .tflite file, you can use CubeMX & X-CUBE-AI to generate a project using the TFLite Micro runtime as show in your screenshot. I would recommend you generate an X-CUBE-AI "Application Template" first as the AI bootstraping functions will be different than the ones given in the wiki for Cube.AI (6.5 Add AI bootstrapping functions). The idea is to adapt your code with the following snippet (extracted from the Application Template)
void AI_Init(void)
{
int res;
res = ai_boostrap(BIN_ADDRESS, tensor_arena, ARENA_SIZE);
if (res) {
printf("E: unable to instantiate the embedded image of the TFLite model/file..\n\r");
Error_Handler();
}
}
void AI_Run(void)
{
int res = -1;
uint8_t *in_data = NULL;
uint8_t *out_data = NULL;
if (model_hdl) {
/* 1 - Retrieve the addresses of the IO buffers (index=0) */
struct tflm_c_tensor_info info;
tflm_c_input(model_hdl, 0, &info);
in_data = (uint8_t *)info.data;
tflm_c_output(model_hdl, 0, &info);
out_data = (uint8_t *)info.data;
/* 2 - main loop */
do {
/* 1 - acquire and pre-process input data */
res = acquire_and_process_data(in_data);
/* 2 - process the data - call inference engine */
if (res == 0) {
if (tflm_c_invoke(model_hdl) != kTfLiteOk) {
res = -1;
}
}
/* 3- post-process the predictions */
if (res == 0)
res = post_process(out_data);
} while (res==0);
}
if (res) {
printf("E: unable to use the TFLite model..\n\r");
}
}
Perhaps you could try running a X-CUBE-AI Application ->Validation with the tflite model you created. The generated application will show you how to feed the input tensors and read the output tensors over UART.
In your CubeMX project, make sure to enable the X-CUBE-AI Application -> Validation component (UM2526 rev7 Figure 21, page 16). You generated code should contain a file named aiValidation_TFLM.c and look for the aiPbCmdNNRun() function.