-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for SSD model on android (#446)
* Add SSD model for android (#440) * Add onnxruntime-mobile dependency for androidMain * Reduc code duplication for SSD models code on JVM and Android platforms * Add support for .inferUsing API for OnnxHighLevelModel (#434) * Add support for zero indexed COCO labels * Fix ssdLightAPITest (#440) * Turn Coco from class to enum * Fix version of onnxruntime dependency for an Android (#440) * Cleanup (#440) * Add docs for ExecutionProviderCompatible interface * Make ExecutionProviderCompatible extends AutoCloseable and simplify inferUsing functions * Remove unnecessary implementation of InferenceModel interface from SSD model on Android
- Loading branch information
1 parent
55c38d8
commit 9ff47d7
Showing
12 changed files
with
185 additions
and
87 deletions.
There are no files selected for viewing
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
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
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
57 changes: 57 additions & 0 deletions
57
...tbrains/kotlinx/dl/api/inference/onnx/objectdetection/SSDMobileNetObjectDetectionModel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.jetbrains.kotlinx.dl.api.inference.onnx.objectdetection | ||
|
||
import android.graphics.Bitmap | ||
import org.jetbrains.kotlinx.dl.api.inference.InferenceModel | ||
import org.jetbrains.kotlinx.dl.api.inference.objectdetection.DetectedObject | ||
import org.jetbrains.kotlinx.dl.api.inference.onnx.OnnxInferenceModel | ||
import org.jetbrains.kotlinx.dl.api.inference.onnx.executionproviders.ExecutionProvider.CPU | ||
import org.jetbrains.kotlinx.dl.dataset.Coco | ||
import org.jetbrains.kotlinx.dl.dataset.CocoVersion.V2017 | ||
import org.jetbrains.kotlinx.dl.dataset.preprocessing.* | ||
import org.jetbrains.kotlinx.dl.dataset.shape.TensorShape | ||
|
||
|
||
private val SSD_MOBILENET_METADATA = SSDModelMetadata( | ||
"TFLite_Detection_PostProcess", | ||
"TFLite_Detection_PostProcess:1", | ||
"TFLite_Detection_PostProcess:2", | ||
0, 1 | ||
) | ||
|
||
|
||
public class SSDMobileNetObjectDetectionModel(override val internalModel: OnnxInferenceModel) : | ||
SSDObjectDetectionModelBase<Bitmap>(SSD_MOBILENET_METADATA) { | ||
|
||
override val classLabels: Map<Int, String> = Coco.V2017.labels(zeroIndexed = true) | ||
|
||
private var targetRotation = 0f | ||
|
||
override lateinit var preprocessing: Operation<Bitmap, Pair<FloatArray, TensorShape>> | ||
private set | ||
|
||
public constructor (modelBytes: ByteArray) : this(OnnxInferenceModel(modelBytes)) { | ||
internalModel.initializeWith(CPU()) | ||
preprocessing = buildPreprocessingPipeline() | ||
} | ||
|
||
public fun setTargetRotation(targetRotation: Float) { | ||
if (this.targetRotation == targetRotation) return | ||
|
||
this.targetRotation = targetRotation | ||
preprocessing = buildPreprocessingPipeline() | ||
} | ||
|
||
private fun buildPreprocessingPipeline(): Operation<Bitmap, Pair<FloatArray, TensorShape>> { | ||
return pipeline<Bitmap>() | ||
.resize { | ||
outputHeight = internalModel.inputDimensions[0].toInt() | ||
outputWidth = internalModel.inputDimensions[1].toInt() | ||
} | ||
.rotate { degrees = targetRotation } | ||
.toFloatArray { layout = TensorLayout.NHWC } | ||
} | ||
|
||
override fun close() { | ||
internalModel.close() | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...monMain/kotlin/org/jetbrains/kotlinx/dl/api/inference/onnx/ExecutionProviderCompatible.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.jetbrains.kotlinx.dl.api.inference.onnx | ||
|
||
import org.jetbrains.kotlinx.dl.api.inference.onnx.executionproviders.ExecutionProvider | ||
import org.jetbrains.kotlinx.dl.api.inference.onnx.executionproviders.ExecutionProvider.CPU | ||
|
||
/** | ||
* Interface for a different kinds of ONNX models which support different execution providers. | ||
*/ | ||
public interface ExecutionProviderCompatible : AutoCloseable { | ||
/** | ||
* Initialize the model with the specified executions providers. | ||
*/ | ||
public fun initializeWith(vararg executionProviders: ExecutionProvider = arrayOf(CPU(true))) | ||
} |
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
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
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
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
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
Oops, something went wrong.