Skip to content

Replace TensorFlow low-level tensor exception on early asserts in Kotlin for input shapes #289

@zaleslaw

Description

@zaleslaw

We have ugly low-level exceptions from the TF Java API internals related to the appropriate tensor shape calculation. It makes the users crazy for not knowing what to do with it. I suggest adding early contracts via require/check in Kotlin to avoid TF exception and make the message to the user easy to read and understand and share some tips on how to fix the problem.

Exception in thread "main" java.lang.IllegalArgumentException: buffer with 10125000 elements is not compatible with a Tensor with shape [1, 224, 224, 3]
	at org.tensorflow.Tensor.incompatibleBuffer(Tensor.java:580)
	at org.tensorflow.Tensor.allocateForBuffer(Tensor.java:295)
	at org.tensorflow.Tensor.create(Tensor.java:188)
	at org.jetbrains.kotlinx.dl.api.core.GraphTrainableModel.internalPredict(GraphTrainableModel.kt:645)
	at org.jetbrains.kotlinx.dl.api.core.GraphTrainableModel.predictSoftly(GraphTrainableModel.kt:624)
	at org.jetbrains.kotlinx.dl.api.inference.InferenceModel.predictSoftly$default(InferenceModel.kt:36)
	at org.jetbrains.kotlinx.dl.api.core.GraphTrainableModel.predict(GraphTrainableModel.kt:565)
	at org.jetbrains.kotlinx.dl.api.inference.imagerecognition.ImageRecognitionModel.predictObject(ImageRecognitionModel.kt:92)
	at resnet.Resnet50easyPrediction2Kt.resnet50easyPrediction2(resnet50easyPrediction2.kt:30)
	at resnet.Resnet50easyPrediction2Kt.main(resnet50easyPrediction2.kt:40)
	at resnet.Resnet50easyPrediction2Kt.main(resnet50easyPrediction2.kt)

Process finished with exit code 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions