java – How to deal with with result of org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs()-ThrowExceptions

Exception or error:

I am running posenet (which is a CNN) on android with tflite.
The model has multiple output arrays with the following dimensions:
1x14x14x17, 1x14x14x34, 1x14x14x32, 1x14x14x32

Therefore running the java tflite interpreter with

import org.tensorflow.lite.Interpreter;
Interpreter tflite;

i can access the four output tensors with tflite.getOutputTensor(i) or with outputs.get(i) (with i el. [0,3]) as outputs is a HashMap filled with java.nio.HeapByteBuffer objects.

How can I convert these outputs or tflite tensors to java multi-dimensional arrays (something like float[][][][];) to be able to perform mathematical computations on them?

How to solve:

Defining the outputs like the following lets you work with native java arrays, which is what i wanted:

out1 = new float[1][14][14][17];
out2 = new float[1][14][14][34];
out3 = new float[1][14][14][32];
out4 = new float[1][14][14][32];
Map<Integer, Object> outputs = new HashMap<>();
outputs.put(0, out1);
outputs.put(1, out2);
outputs.put(2, out3);
outputs.put(3, out4);


// The shape of *1* output's tensor
int[] OutputShape;
// The type of the *1* output's tensor
DataType OutputDataType;
// The multi-tensor ready storage
outputProbabilityBuffers = new HashMap<>();

ByteBuffer x;
// For each model's tensors (there are getOutputTensorCount() of the for this tflite model)
for (int i = 0; i < tflite.getOutputTensorCount(); i++) {
    OutputShape = tflite.getOutputTensor(i).shape();
    OutputDataType = tflite.getOutputTensor(i).dataType();
    x = TensorBuffer.createFixedSize(OutputShape, OutputDataType).getBuffer();
    outputProbabilityBuffers.put(i, x);
    LOGGER.d("Created a buffer of %d bytes for tensor %d.", x.limit(), i);

LOGGER.d("Created a tflite output of %d output tensors.", outputProbabilityBuffers.size());

Example output:

Classifier: Created a buffer of 11264 bytes for tensor 0.
Classifier: Created a buffer of 11264 bytes for tensor 1.
Classifier: Created a buffer of 4 bytes for tensor 2.
Classifier: Created a buffer of 11264 bytes for tensor 3.
Classifier: Created a tflite output of 4 output tensors.

And use it that way:

Object[] inputs = { your_regular_input };
tflite.runForMultipleInputsOutputs(inputs, outputProbabilityBuffers);

Leave a Reply

Your email address will not be published. Required fields are marked *