Hello,
Im trying to fetch accelometer data from a sensor and want to put it into a neural network. The network runs on an EPS32 and im using the Arduino IDE 2.0.2.
The tensor has the shape (1,6,128). I quantized the inputs accordingly. But when i try to input the matrix into the model tensors i get this error.
Does somebody know what this means and how to fix it?
// Tensorflow imports
#include <TensorFlowLite_ESP32.h>
#include "tensorflow/lite/micro/all_ops_resolver.h"
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/system_setup.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "ESP32_Model.h"
// MPU-6050 imports
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
namespace {
tflite::ErrorReporter* error_reporter = nullptr;
const tflite::Model* model = nullptr;
tflite::MicroInterpreter* interpreter = nullptr;
TfLiteTensor* input = nullptr;
TfLiteTensor* output = nullptr;
// Create Memory arena for calculations
constexpr int kTensorArenaSize = 8192;
uint8_t tensor_arena[kTensorArenaSize];
}
void setup(){
// Set up the MPU-6050 --------------------------------------------
Serial.begin(9600);
while (!Serial){
delay(10);
}
if (!mpu.begin()) {
while (1) {
delay(10);
}
}
mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_260_HZ);
// Set up Tensorflow ----------------------------------------------
static tflite::MicroErrorReporter micro_error_reporter;
error_reporter = µ_error_reporter;
// Map the model into a usable data structure. This doesn't involve any
// copying or parsing, it's a very lightweight operation.
model = tflite::GetModel(ESP32_Model);
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;
}
// This pulls in all the operation implementations we need.
// NOLINTNEXTLINE(runtime-global-variables)
static tflite::AllOpsResolver resolver;
// Build an interpreter to run the model with.
static tflite::MicroInterpreter static_interpreter(
model, resolver, tensor_arena, kTensorArenaSize, error_reporter);
interpreter = &static_interpreter;
// Allocate memory from the tensor_arena for the model's tensors.
TfLiteStatus allocate_status = interpreter->AllocateTensors();
if (allocate_status != kTfLiteOk) {
TF_LITE_REPORT_ERROR(error_reporter, "AllocateTensors() failed");
return;
}
// Obtain pointers to the model's input and output tensors.
input = interpreter->input(0);
output = interpreter->output(0);
}
float Accx = 0.0;
float Accy = 0.0;
float Accz = 0.0;
float Gyrx = 0.0;
float Gyry = 0.0;
float Gyrz = 0.0;
float Temp = 0.0;
sensors_event_t Acc, Gyr, Temps;
int8_t Input_Matrix_float[1][6][128];
float Output_Matrix_float[1][6][128];
float Input_Matrix_norm[1][6][128];
int8_t Input_Matrix_quant[1][6][128];
int8_t Output_Matrix_quant[1][6][128];
int Index = 0;
float Max = 0.0;
float Min = 0.0;
int counter = 0;
int Pos_Min = 0;
int Pos_Max = 0;
int8_t Storage = 0;
float Abs_Sum = 0.0;
float MAE = 0.0;
void loop(){
mpu.getEvent(&Acc,&Gyr,&Temps);
Input_Matrix_float[0][0][Index] = Acc.acceleration.x / input->params.scale + input->params.zero_point;
Input_Matrix_float[0][1][Index] = Acc.acceleration.y / input->params.scale + input->params.zero_point;
Input_Matrix_float[0][2][Index] = Acc.acceleration.z / input->params.scale + input->params.zero_point;
Input_Matrix_float[0][3][Index] = Gyr.acceleration.x / input->params.scale + input->params.zero_point;
Input_Matrix_float[0][4][Index] = Gyr.acceleration.y / input->params.scale + input->params.zero_point;
Input_Matrix_float[0][5][Index] = Gyr.acceleration.z / input->params.scale + input->params.zero_point;
Index += 1;
if (Index==128){
Serial.print("\n");
Serial.print("\n");
Serial.print("\n");
Serial.print("Input Matrix float\n");
counter = 0;
for (int i=0; i<128; i++){
for (int k=0; k<6; k++){
Serial.print(Input_Matrix_float[0][k][i]);
Serial.print(" ");
}
Serial.print(counter);
Serial.print("\n");
counter += 1;
}
for (int i=0; i<128; i++){
for (int k=0; k<6; k++){
if (Max < Input_Matrix_float[0][k][i]){
Max = Input_Matrix_float[0][k][i];
Pos_Max = i;
}
if (Min > Input_Matrix_float[0][k][i]){
Min = Input_Matrix_float[0][k][i];
Pos_Min = i;
}
}
}
Serial.print("Min Value\n");
Serial.print(Min);
Serial.print(" ");
Serial.print(Pos_Min);
Serial.print("\nMax Value\n");
Serial.print(Max);
Serial.print(" ");
Serial.print(Pos_Max);
Serial.print("\n");
counter = 0;
Serial.print("Input Matrix norm\n");
for (int i=0; i<128; i++){
for (int k=0; k<6; k++){
Input_Matrix_norm[0][k][i] = (2*(Input_Matrix_float[0][k][i]-Min)/(Max-Min))-1;
Serial.print(Input_Matrix_norm[0][k][i]);
Serial.print(" ");
}
Serial.print(counter);
Serial.print("\n");
counter += 1;
}
counter = 0;
Serial.print("Input Matrix quant\n");
for (int i=0; i<128; i++){
for (int k=0; k<6; k++){
Input_Matrix_quant[0][k][i] = Input_Matrix_float[0][k][i] / input->params.scale + input->params.zero_point;
Serial.print(Input_Matrix_quant[0][k][i]);
Serial.print(" ");
}
Serial.print(counter);
Serial.print("\n");
counter += 1;
}
//????
input->data.int8[0] = Input_Matrix_quant;
TfLiteStatus invoke_status = interpreter->Invoke();
if (invoke_status != kTfLiteOk) {
TF_LITE_REPORT_ERROR(error_reporter, "Invoke failed on x: %f\n");
return;
}
Output_Matrix_quant = output->data.int8[0];
counter = 0;
Serial.print("Output Matrix float\n");
for (int i=0; i<128; i++){
for (int k=0; k<6; k++){
Output_Matrix_float[0][k][i] = (Output_Matrix_quant[0][k][i]-input->params.zero_point)*input->params.scale;
Serial.print(Output_Matrix_float[0][k][i]);
Serial.print(" ");
}
Serial.print(counter);
Serial.print("\n");
}
for (int i=0; i<128; i++){
for (int k=0; k<6; k++){
Abs_Sum += sqrt(sq(Output_Matrix_float[0][k][i]));
}
}
MAE = Abs_Sum/(6*128);
Serial.print("MAE is: ");
Serial.print(MAE);
Serial.print("\n");
// Set all Arrays and Vars to 0
for (int i=0; i<128; i++){
for (int k=0; k<6; k++){
Input_Matrix_float[0][k][i] = 0.0;
}
}
for (int i=0; i<128; i++){
for (int k=0; k<6; k++){
Input_Matrix_norm[0][k][i] = 0.0;
}
}
for (int i=0; i<128; i++){
for (int k=0; k<6; k++){
Input_Matrix_quant[0][k][i] = 0;
}
}
for (int i=0; i<128; i++){
for (int k=0; k<6; k++){
Output_Matrix_quant[0][k][i] = 0;
}
}
for (int i=0; i<128; i++){
for (int k=0; k<6; k++){
Output_Matrix_float[0][k][i] = 0.0;
}
}
Min = 0.0;
Max = 0.0;
Pos_Min = 0;
Pos_Max = 0;
counter = 0;
Abs_Sum = 0.0;
MAE = 0.0;
Index = 0;
}
delay(1000);
}