Skip to content

Commit

Permalink
Fix ssdLightAPITest (Kotlin#440)
Browse files Browse the repository at this point in the history
* Turn Coco from class to enum
  • Loading branch information
ermolenkodev committed Sep 8, 2022
1 parent b21b786 commit e4907ea
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@

package org.jetbrains.kotlinx.dl.dataset

public class Coco(public val version: CocoVersion, zeroIndexed: Boolean = false) {
public val labels: Map<Int, String> = when (version) {
CocoVersion.V2014 -> if (zeroIndexed) toZeroIndexed(cocoCategories2014) else cocoCategories2014
CocoVersion.V2017 -> if (zeroIndexed) toZeroIndexed(cocoCategories2017) else cocoCategories2017

public enum class Coco {
V2014,
V2017;

public fun labels(zeroIndexed: Boolean = false) : Map<Int, String> {
return when (this) {
V2014 -> if (zeroIndexed) toZeroIndexed(cocoCategories2014) else cocoCategories2014
V2017 -> if (zeroIndexed) toZeroIndexed(cocoCategories2017) else cocoCategories2017
}
}

private fun toZeroIndexed(labels: Map<Int, String>) : Map<Int, String> {
Expand All @@ -20,7 +26,6 @@ public class Coco(public val version: CocoVersion, zeroIndexed: Boolean = false)
}
}


/**
* 80 object categories in COCO dataset.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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
Expand All @@ -22,18 +23,18 @@ public class SSDMobileNetObjectDetectionModel(override val internalModel: OnnxIn
SSDObjectDetectionModelBase<Bitmap>(SSD_MOBILENET_METADATA),
InferenceModel by internalModel {

override val classLabels: Map<Int, String> = Coco(V2017, zeroIndexed = true).labels
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()
}

override lateinit var preprocessing: Operation<Bitmap, Pair<FloatArray, TensorShape>>
private set

public fun setTargetRotation(targetRotation: Float) {
if (this.targetRotation == targetRotation) return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public abstract class EfficientDetObjectDetectionModelBase<I> : ObjectDetectionM
/**
* Base class for object detection model based on SSD architecture.
*/
public abstract class SSDObjectDetectionModelBase<I>(private val metadata: SSDModelMetadata) : ObjectDetectionModelBase<I>() {

public abstract class SSDObjectDetectionModelBase<I>(protected val metadata: SSDModelMetadata) : ObjectDetectionModelBase<I>() {
override fun convert(output: Map<String, Any>): List<DetectedObject> {
val boxes = (output[metadata.outputBoxesName] as Array<Array<FloatArray>>)[0]
val classIndices = (output[metadata.outputClassesName] as Array<FloatArray>)[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class EfficientDetObjectDetectionModel(override val internalModel: OnnxIn
// model is quite sensitive for this
.convert { colorMode = ColorMode.RGB }
.toFloatArray { }
override val classLabels: Map<Int, String> = Coco(V2017).labels
override val classLabels: Map<Int, String> = Coco.V2017.labels(zeroIndexed = false)

/**
* Constructs the object detection model from a given path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class SSDMobileNetV1ObjectDetectionModel(override val internalModel: Onnx
.toFloatArray { }
.call(ONNXModels.ObjectDetection.SSDMobileNetV1.preprocessor)

override val classLabels: Map<Int, String> = Coco(V2017).labels
override val classLabels: Map<Int, String> = Coco.V2017.labels()

/**
* Constructs the object detection model from a given path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class SSDObjectDetectionModel(override val internalModel: OnnxInferenceMo
.toFloatArray { }
.call(ONNXModels.ObjectDetection.SSD.preprocessor)

override val classLabels: Map<Int, String> = Coco(CocoVersion.V2014).labels
override val classLabels: Map<Int, String> = Coco.V2014.labels()

/**
* Constructs the object detection model from a given path.
Expand All @@ -80,6 +80,30 @@ public class SSDObjectDetectionModel(override val internalModel: OnnxInferenceMo
return detectObjects(ImageConverter.toBufferedImage(imageFile), topK)
}

// TODO remove code duplication due to different type of class labels array
override fun convert(output: Map<String, Any>): List<DetectedObject> {
val boxes = (output[metadata.outputBoxesName] as Array<Array<FloatArray>>)[0]
val classIndices = (output[metadata.outputClassesName] as Array<LongArray>)[0]
val probabilities = (output[metadata.outputScoresName] as Array<FloatArray>)[0]
val numberOfFoundObjects = boxes.size

val foundObjects = mutableListOf<DetectedObject>()
for (i in 0 until numberOfFoundObjects) {
val detectedObject = DetectedObject(
classLabel = if (classIndices[i].toInt() in classLabels.keys) classLabels[classIndices[i].toInt()]!! else "Unknown",
probability = probabilities[i],
// left, bot, right, top
xMin = boxes[i][metadata.xMinIdx],
yMin = boxes[i][metadata.yMinIdx],
xMax = boxes[i][metadata.xMinIdx + 2],
yMax = boxes[i][metadata.yMinIdx + 2]
)
foundObjects.add(detectedObject)
}
return foundObjects
}


override fun copy(
copiedModelName: String?,
saveOptimizerState: Boolean,
Expand Down

0 comments on commit e4907ea

Please sign in to comment.