Kotlin/kotlindl

Confusing error message if inference model was not initialized

StefanOltmann opened this issue · 1 comments

I use version 0.5.0-alpha-2 and try to use if from a local file.

I read the bytes I downloaded previously using the ONNXModelHub, but doing it this way there is an exception.

My code:

fun dlTest() {

//    val modelHub = ONNXModelHub(cacheDirectory = File("build/cache"))
//    val model = ONNXModels.FaceDetection.UltraFace320.pretrainedModel(modelHub)

    val bytes = File("ultraface_320.onnx").readBytes()

    val inferenceModel = OnnxInferenceModel { bytes }
    val model = FaceDetectionModel(inferenceModel, "UltraFace320")

    val folder = File("/Users/sol/Pictures/")

    for (file in folder.listFiles()) {

        if (!file.name.endsWith("jpg"))
            continue

        val image = ImageConverter.toBufferedImage(file)

        val faces: List<DetectedObject> = model.detectFaces(image)

        if (faces.isEmpty())
            continue

        for (face in faces)
            println(face)
    }
}

Stacktrace:

Exception in thread "main" kotlin.UninitializedPropertyAccessException: lateinit property inputShape has not been initialized
	at org.jetbrains.kotlinx.dl.onnx.inference.OnnxInferenceModel.getInputDimensions(OnnxInferenceModel.kt:202)
	at org.jetbrains.kotlinx.dl.onnx.inference.facealignment.FaceDetectionModel$preprocessing$1.invoke(FaceDetectionModel.kt:34)
	at org.jetbrains.kotlinx.dl.onnx.inference.facealignment.FaceDetectionModel$preprocessing$1.invoke(FaceDetectionModel.kt:33)
	at org.jetbrains.kotlinx.dl.impl.preprocessing.image.ImagePreprocessingDslKt.resize(ImagePreprocessingDsl.kt:29)
	at org.jetbrains.kotlinx.dl.onnx.inference.facealignment.FaceDetectionModel.getPreprocessing(FaceDetectionModel.kt:33)
	at org.jetbrains.kotlinx.dl.onnx.inference.OnnxHighLevelModel$DefaultImpls.predict(OnnxHighLevelModel.kt:49)
	at org.jetbrains.kotlinx.dl.onnx.inference.facealignment.FaceDetectionModelBase.predict(FaceDetectionModelBase.kt:18)
	at org.jetbrains.kotlinx.dl.onnx.inference.facealignment.FaceDetectionModelBase.predict(FaceDetectionModelBase.kt:18)
	at org.jetbrains.kotlinx.dl.onnx.inference.facealignment.FaceDetectionModelBase.detectFaces(FaceDetectionModelBase.kt:44)
	at org.jetbrains.kotlinx.dl.onnx.inference.facealignment.FaceDetectionModelBase.detectFaces$default(FaceDetectionModelBase.kt:43)
	at KotlindltestKt.dlTest(kotlindltest.kt:28)

Oh, okay, I see I missed the inferenceModel.initializeWith().

I suggest a better error message, like an UninitializedException: Plase call initializeWith() first.