forked from Kotlin/kotlindl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SSD model for android (Kotlin#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 (Kotlin#434) * Add support for zero indexed COCO labels
- Loading branch information
1 parent
6ad3ebc
commit b21b786
Showing
12 changed files
with
161 additions
and
86 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
61 changes: 61 additions & 0 deletions
61
...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,61 @@ | ||
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.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), | ||
InferenceModel by internalModel { | ||
|
||
override val classLabels: Map<Int, String> = Coco(V2017, zeroIndexed = true).labels | ||
|
||
private var targetRotation = 0f | ||
|
||
public constructor (modelBytes: ByteArray) : this(OnnxInferenceModel(modelBytes)) { | ||
internalModel.initializeWith(CPU()) | ||
preprocessing = buildPreprocessingPipeline() | ||
} | ||
|
||
override lateinit var preprocessing: Operation<Bitmap, Pair<FloatArray, TensorShape>> | ||
private set | ||
|
||
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 = inputDimensions[0].toInt() | ||
outputWidth = inputDimensions[1].toInt() | ||
} | ||
.rotate { degrees = targetRotation } | ||
.toFloatArray { layout = TensorLayout.NHWC } | ||
} | ||
|
||
override fun copy( | ||
copiedModelName: String?, | ||
saveOptimizerState: Boolean, | ||
copyWeights: Boolean | ||
): SSDMobileNetObjectDetectionModel { | ||
return SSDMobileNetObjectDetectionModel(internalModel.copy(copiedModelName, saveOptimizerState, copyWeights)) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...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,8 @@ | ||
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 | ||
|
||
public interface ExecutionProviderCompatible { | ||
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.