如何处理org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs()的结果

时间:2019-02-18 13:20:49

标签: java android arrays tensorflow tensorflow-lite

我正在使用tflite在android上运行posenet(这是一个CNN)。 该模型具有以下尺寸的多个输出数组: 1x14x14x17, 1x14x14x34, 1x14x14x32, 1x14x14x32

因此使用以下命令运行Java tflite解释器

import org.tensorflow.lite.Interpreter;
Interpreter tflite;
...
tflite.runForMultipleInputsOutputs(inputs,outputs)

i可以使用tflite.getOutputTensor(i)outputs.get(i)(使用i [0,3])访问四个输出张量,因为outputsHashMap,其中{ {1}}个对象。

如何将这些输出或tflite张量转换为Java多维数组(类似于java.nio.HeapByteBuffer之类),以便能够对它们执行数学计算?

3 个答案:

答案 0 :(得分:3)

// 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 them 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());

示例输出:

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.

并以这种方式使用它:

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

答案 1 :(得分:0)

按如下所示定义输出,使您可以使用本机Java数组:

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);

答案 2 :(得分:0)

输出:https://www.tensorflow.org/lite/models/object_detection/overview#output

val locations = outputs.getValue(0).asFlowArray(),
val classes = outputs.getValue(1).asFlowArray(),
val scores = outputs.getValue(2).asFlowArray(),
val detections = outputs.getValue(3).asFlowArray()