Created
September 13, 2022 06:28
-
-
Save zhuker/85187b8a90aa5bf9dd36c9b47336d014 to your computer and use it in GitHub Desktop.
Onnx runtime example in kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 | |
} | |
} | |
} |
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
gradle: