Search code examples
arduinotensorflow-liteesp32

copy input into buffer for CNN with tensorflow lite micro ESP32


When using tensorflow lite one a esp32, I use

loat input_data[3] = {value1, value2, value3};

// Copy value to input buffer (tensor)
for (size_t i = 0; i < 3; i++){
    model_input->data.f[i] = input_data[i];
    }

to copy the "feature values" to the input buffer. For applications with only 3 features per sample(like the setosa example), this is pretty straight forward.

How do pass the values for a CNN? My model input is

[1,80,3,1]

with

[noOfTimeSeries, noOfMeasurementsPerSensorPerTimeSeries, noOfSensors, anotherDimensionForTensorflow]

My features are so far stored in a struct, but I could put them into an array if necessary.

How would I pass values for CNN with images?


Solution

  • Although the tflite model has multidimensional input, you can fill your input buffer as if it were a 1D dimensional array in tflite for microcontrollers.

    In your case,

    size_t input_size = 1*80*3*1;
    
    for (size_t i = 0; i < input_size; i++){
        model_input->data.f[i] = input_data[i];
    }
    

    In your case, the flattened 1D array would look like

    [Measurement1Sensor1, Measurement1Sensor2, Measurement1Sensor3, Measurement2Sensor1, ...]
    

    Hopefully, you get the idea.

    As side advice and for other readers, consider quantizing the model to int8 parameters or even int8 input and outputs. It will reduce the size and improve inference time considerably.