Issues with TFLM “Interpreter->Invoke()” causing hard fault

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:

  1. 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
  2. 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)
  3. Environment:

    • Platform: Custom embedded hardware with an ARM Cortex-M4 processor.
    • Software: Using gcc/g++ for compilation and SEGGER J-Link for debugging.
  4. 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 = &micro_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;
      }
      
  5. Current Issue:
    When invoking interpreter->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.

New contributor

Magnus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật