Search code examples
kotlinonnxruntime

Using an OnnxTensor value in Kotlin


I'm using onnxruntime-android within a Kotlin app, but am having trouble with using the result from an inference.

I can create arrays in Kotlin to construct the model and run inference, but when I get the result back as in

val result = ortSession.run(inputs)

the model outputs the result as a 2D tensor with one element

val probArray = result[0].value -> (this is a float[][])

To get the float I have to index into the array

val probability = probArray[0][0]

however this generates a compile time error. I need a way to cast probability into something Kotlin can use such as an Array<Array<Float>> but when I try to run

val probArray = result[0].value as Array<Array<Float>>

I get a runtime error for the cast

java.lang.ClassCastException: float[][] cannot be cast to java.lang.Float[][]

I understand the error, but cannot figure out a way to simply access the value inside probArray.


Solution

  • Was able to get this working with

    val probArray = result[0].value as Array<FloatArray>
    val probability = probArray[0][0]