Simple inference example tflite + kotlin

I’m trying to adapt a simple example of tflite and android.

val inputArray = arrayOf(initInputArray(bitmap))
val outputMap = initOutputMap(getInterpreter())
getInterpreter().runForMultipleInputsOutputs(inputArray, outputMap)

Since my model only has one output. I change the outputMap into an outputArray

val inputArray = arrayOf(initInputArray(bitmap))
val outputArray = Array(1){ Array(height) { Array(width) { FloatArray( channels }}}
getInterpreter().run(inputArray, outputArray)

However when feeding it to the inference method I get:

DataType error: cannot resolve DataType of [Ljava.nio.ByteBuffer;

Any idea what’s going wrong?


Found the error:

val inputArray = arrayOf(initInputArray(bitmap))

was the culprit. When using run instead of runForMultipleInputsOutputs this cannot be an array! So changing the line to

val inputArray = initInputArray(bitmap)

fixes the issue.

A good documentation of the required data formats would have helped…

  • run -> requires “DirectByteBuffer” as input
  • runForMultipleInputsOutputs -> requires “ByteBuffer” as input