Skip to content

Instantly share code, notes, and snippets.

@zhuker
Created September 13, 2022 06:28
Show Gist options
  • Save zhuker/85187b8a90aa5bf9dd36c9b47336d014 to your computer and use it in GitHub Desktop.
Save zhuker/85187b8a90aa5bf9dd36c9b47336d014 to your computer and use it in GitHub Desktop.
Onnx runtime example in kotlin
@Test
fun onnxRuntime() {
val env = OrtEnvironment.getEnvironment()
OrtSession.SessionOptions().use { opts ->
opts.setOptimizationLevel(OrtSession.SessionOptions.OptLevel.BASIC_OPT)
env.createSession("mymodel.onnx", opts).use { session ->
for (i in session.inputInfo.values) {
println(i)
}
for (o in session.outputInfo.values) {
println(o)
}
val data = ByteBuffer.allocateDirect(1 * 3 * 64 * 64 * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer()
val tensor = OnnxTensor.createTensor(env, data, longArrayOf(1, 3, 64, 64))
val output = session.run(Collections.singletonMap("images", tensor))
println(output)
val ot = output.get(0) as OnnxTensor
val floatBuffer = ot.getDirectBuffer().asFloatBuffer()
val toFloatArray = floatBuffer.toFloatArray()
toFloatArray
}
}
}
@zhuker
Copy link
Author

zhuker commented Sep 13, 2022

gradle:

    implementation("com.microsoft.onnxruntime:onnxruntime:1.12.1")

@zhuker
Copy link
Author

zhuker commented Sep 14, 2022

on android

implementation("com.microsoft.onnxruntime:onnxruntime-android:1.12.1")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment