TensorflowLite C API input buffer layout for multidimensional tensor

Viewed 339

I'm having a TensorFlow model that takes inputs of shape [1, 128, 1024, 2]. I'd like to run inference on this model with the TensorflowLite C API.

The sample code from the tensorflow/lite/c/c_api.h shows how to feed input into the model:

std::vector<float> inputBuffer(1 * 128 * 1024 * 2);

// populate input buffer
// ...

TfLiteTensor *inputTensor = TfLiteInterpreterGetInputTensor(interpreter, 0);
TfLiteStatus status = TfLiteTensorCopyFromBuffer(inputTensor, inputBuffer.data(), inputBuffer.size() * sizeof(float));

The question is now how to populate the input buffer correctly.

Assuming I am given an array float inputArray[1][128][1024][2], how do I correctly flatten this array to a one-dimensional vector that TensorflowLite can understand?

1 Answers

You don't need to flatten that array, just changed inputBuffer.data() to the pointer of your data. The input tensors in TFLite model has the shape information (1, 128, 1024, 2). So It will be able to understand your array correctly.

Related