Click here to Skip to main content
15,851,387 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have converted my DenseNet-121 model to model.tflite, and when i am loading it to android app and trying to make predictions, it's giving following errors
java.lang.IllegalStateException: Internal error: Unexpected failure when preparing tensor allocations: tensorflow/lite/kernels/pad.cc:79 SizeOfDimension(op_context->paddings, 0) != op_context->dims (4 != 1) Node number 0 (PAD) failed to prepare. at org.tensorflow.lite.NativeInterpreterWrapper.allocateTensors(Native Method) at org.tensorflow.lite.NativeInterpreterWrapper.allocateTensorsIfNeeded(NativeInterpreterWrapper.java:308) at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:248) at org.tensorflow.lite.InterpreterImpl.runForMultipleInputsOutputs(InterpreterImpl.java:101) at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:77) at org.tensorflow.lite.InterpreterImpl.run(InterpreterImpl.java:94) at org.tensorflow.lite.Interpreter.run(Interpreter.java:77) at com.example.appleleafdiseasedetection.DiseaseDetector$2.onClick(DiseaseDetector.java:72) at android.view.View.performClick(View.java:7743) at android.view.View.performClickInternal(View.java:7720) at android.view.View.access$3700(View.java:854) at android.view.View$PerformClick.run(View.java:29111) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:210) at android.os.Looper.loop(Looper.java:299) at android.app.ActivityThread.main(ActivityThread.java:8309) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038)
How can I solve it?

What I have tried:

this is code related to preprocessing in java :
Java
private float[] preprocessImage(Bitmap image) {
    // Convert the image to a float array or apply any necessary transformations
    // Assuming the model expects a specific input shape [1, 224, 224]
    int inputWidth = 224;
    int inputHeight = 224;

    Bitmap resizedImage = Bitmap.createScaledBitmap(image, inputWidth, inputHeight, true);

    // Convert the resized image to a float array
    int[] intValues = new int[inputWidth * inputHeight];
    float[] floatValues = new float[1 * inputWidth * inputHeight];
    resizedImage.getPixels(intValues, 0, inputWidth, 0, 0, inputWidth, inputHeight);

    for (int i = 0; i < intValues.length; i++) {
        final int val = intValues[i];
        floatValues[i] = ((val >> 16) & 0xFF) / 255.0f; // Red component
    }

    return floatValues;
}
Posted
Updated 12-Jun-23 22:11pm
v2
Comments
Sandeep Mewara 12-Jun-23 22:31pm    
I have seen similar issue related to the incompatible version on the Python side. Please make sure all the dependencies are updated and of correct version.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900