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/ 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( at at org.tensorflow.lite.InterpreterImpl.runForMultipleInputsOutputs( at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs( at at at com.example.appleleafdiseasedetection.DiseaseDetector$2.onClick( at android.view.View.performClick( at android.view.View.performClickInternal( at android.view.View.access$3700( at android.view.View$ at android.os.Handler.handleCallback( at android.os.Handler.dispatchMessage( at android.os.Looper.loopOnce( at android.os.Looper.loop( at at java.lang.reflect.Method.invoke(Native Method) at$ at
How can I solve it?

What I have tried:

this is code related to preprocessing in 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;
Updated 12-Jun-23 22:11pm
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