I am in the process of incorporating TensorFlow Lite Micro (TFLM) as an independent library into an existing C project. This project utilizes a neural network for real-time inferences, and my goal is to employ TFLM for inference on an embedded system equipped with an ARM Cortex-M4 processor. However, whenever it gets to the interpreter->Invoke()
section of my code, it produces a hard fault in my processor and restarts. Below are the pertinent details and the steps I have undertaken thus far:
-
Project Setup:
- Target Architecture: ARM Cortex-M4 with single-precision floating-point (sfp) support.
- Generated a static library (
tflm-libmicrolite.a
) using the command:make -f tensorflow/lite/micro/tools/make/Makefile TARGET=cortex_m_generic OPTIMIZED_KERNEL_DIR=cmsis_nn TARGET_ARCH=cortex-m4+sfp microlite
- Linked this static library in my CMakeLists.txt file
- Toolchain: Arm GNU Toolchain (arm-none-eabi) Version 11.2
- IDE: VS Code
- OS: Windows 10
-
Model Information:
- Model: TensorFlow model converted to TFLite format:
model = tf.keras.models.Sequential([ tf.keras.layers.Dense(16, activation='relu', input_shape=(X_train_scaled.shape[1], )), tf.keras.layers.Dense(16, activation='relu'), tf.keras.layers.Dense(1) # Target variable ]) model.compile(optimizer='adam', loss='mse', metrics=['mae']) history = model.fit(X_train_scaled, y_train_scaled, epochs= 400, batch_size=32, validation_split=0.2)
- Input: 3 features (RE, SLOPE, ADC).
- Output: 1 prediction value (ppt)
-
Environment:
- Platform: Custom embedded hardware with an ARM Cortex-M4 processor.
- Software: Using gcc/g++ for compilation and SEGGER J-Link for debugging.
-
Code Snippets:
- main.c:
…
// I’ve excluded other code which pertains to initializing the system and interrupts non-related to NN
initialize_model(); // Initialize NN model for inferencing- **cal_error.c (Relevant Function Call):** ```c /* ********************************************************************** * * Declaring constants for the model * ********************************************************************** */ // Define the mean and standard deviation of the input features const float input_mean[3] = {-166044.833, -396.269, 2652.257}; const float input_std_dev[3] = {94516.194, 10.971, 18.635}; // Define the Min and Max values for the output target (0,1 range) const float output_min_value = 0.0; const float output_max_value = 1.0; const float output_X_min = -907750.0001; const float output_X_max = -782625.0; void pps_count_predict_error(void) { ... float input_data[3] = {ErrorCal.running_error, ErrorCal.RE_this_slope, ErrorCal.average_temp}; scale_inputs(input_data, 3, input_mean, input_std_dev); run_inference(input_data, output_data); ... }
-
tflm_wrapper.cpp:
#include "tflm_wrapper.h" #include "tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h" #include "tensorflow/lite/micro/micro_interpreter.h" #include "tensorflow/lite/micro/micro_mutable_op_resolver.h" #include "tensorflow/lite/schema/schema_generated.h" #include "model_data.h" constexpr int tensor_arena_size = 10 * 1024; uint8_t tensor_arena[tensor_arena_size]; tflite::MicroErrorReporter micro_error_reporter; tflite::ErrorReporter* error_reporter = µ_error_reporter; tflite::MicroInterpreter* interpreter; TfLiteTensor* input1; TfLiteTensor* output; extern "C" void initialize_model() { printf("Initializing model...n"); static tflite::MicroMutableOpResolver<4> micro_op_resolver; micro_op_resolver.AddFullyConnected(); micro_op_resolver.AddRelu(); micro_op_resolver.AddReshape(); micro_op_resolver.AddSoftmax(); const tflite::Model* model = tflite::GetModel(model_data); if (model->version() != TFLITE_SCHEMA_VERSION) { TF_LITE_REPORT_ERROR(error_reporter, "Model provided is schema version %d not equal to supported version %d.", model->version(), TFLITE_SCHEMA_VERSION); return; } static tflite::MicroInterpreter static_interpreter(model, micro_op_resolver, tensor_arena, tensor_arena_size); interpreter = &static_interpreter; if (interpreter->AllocateTensors() != kTfLiteOk) { TF_LITE_REPORT_ERROR(error_reporter, "AllocateTensors() failedn"); return; } input1 = interpreter->input(0); output = interpreter->output(0); printf("Model initialized successfully.n"); } extern "C" void run_inference(float* input_data, float* output_data) { if (input1 == nullptr || output == nullptr) { printf("Input or output tensors are not initialized.n"); return; } for (int i = 0; i < 3; ++i) { input1->data.f[i] = input_data[i]; } if (interpreter->Invoke() != kTfLiteOk) { TF_LITE_REPORT_ERROR(error_reporter, "Invoke() failed"); return; } output_data[0] = output->data.f[0]; }
-
scaler.c (Scaling inputs/ inverse scale output for inference):
void scale_inputs(float* input_data, int size, const float* mean, const float* std_dev) { for (int i = 0; i < size; ++i) { input_data[i] = (input_data[i] - mean[i]) / std_dev[i]; } } float inverse_scale_output(float scaled_value, float min_value, float max_value, float X_min, float X_max) { return scaled_value * (X_max - X_min) + X_min; }
- main.c:
-
Current Issue:
When invokinginterpreter->Invoke()
, the program encounters a hard fault indicateing that the interpreter fails to execute the model. The problem may be related to memory allocation, tensor initialization, or other factors specific to TFLM integration. My code properly outputs:
Initializing model...
Model version is correct.
Model initialized successfully.
Running inference...
Input data[0]: 1.7502
Input data[1]: 36.0374
Input data[2]: -133.0732
But once it gets to this line:
if (interpreter->Invoke() != kTfLiteOk) {
TF_LITE_REPORT_ERROR(error_reporter, "Invoke() failed");
return;
}
It hard faults and restarts the unit; this the error from Debugger: WARNING: Failed to read memory @ address 0xDEADBEEE
Question:
Has anyone experienced similar issues with TFLM integration, particularly related to interpreter->Invoke()
causing hard faults? Is my setup correct to integrate TensorFlow Lite Micro? If there are any necessary details I missed, I will happily provide them.
Magnus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.