Thursday, 20 June 2019

java.nio.BufferOverflowException running tensorflow model

I get this fatal error while I run a tensorflow model in my android app:

Caused by: java.nio.BufferOverflowException
                                                                 at java.nio.HeapFloatBuffer.put(HeapFloatBuffer.java:179)
                                                                 at org.tensorflow.Tensor.writeTo(Tensor.java:488)
                                                                 at org.tensorflow.contrib.android.TensorFlowInferenceInterface.fetch(TensorFlowInferenceInterface.java:488)
                                                                 at org.tensorflow.contrib.android.TensorFlowInferenceInterface.fetch(TensorFlowInferenceInterface.java:442)

Code is as follows:

    //sample values: WANTED_WIDTH = 714, WANTED_HEIGHT = 438
    int[] intValues = new int[WANTED_WIDTH * WANTED_HEIGHT];
    float[] floatValues = new float[WANTED_WIDTH * WANTED_HEIGHT * 3];
    float[] outputValues = new float[WANTED_WIDTH * WANTED_HEIGHT * 3];

    Bitmap bitmap = activity.mainBitmap;

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, WANTED_WIDTH, WANTED_HEIGHT, true);
    scaledBitmap.getPixels(intValues, 0, scaledBitmap.getWidth(), 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight());

    for (int i = 0; i < intValues.length; i++) {
        final int val = intValues[i];
        floatValues[i*3] = ((val >> 16) & 0xFF);
        floatValues[i*3+1] = ((val >> 8) & 0xFF);
        floatValues[i*3+2] = (val & 0xFF);
    }

    AssetManager assetManager = getResources().getAssets();
    mInferenceInterface = new TensorFlowInferenceInterface(assetManager, MODEL_FILE);


    final float[] styleVals = new float[NUM_STYLES];
    for (int i = 0; i < NUM_STYLES; ++i) {
        styleVals[i] = 0.0f / NUM_STYLES;
    }
    styleVals[params[0]] = 1.5f;

    mInferenceInterface.feed(INPUT_NODE, floatValues, 1, WANTED_HEIGHT, WANTED_WIDTH, 3);
    mInferenceInterface.feed("style_num", styleVals, NUM_STYLES);
    mInferenceInterface.run(new String[] {OUTPUT_NODE}, false);
    mInferenceInterface.fetch(OUTPUT_NODE, outputValues);

Error is shown to be on the last line of code I have provided above, i.e. "mInferenceInterface.fetch(OUTPUT_NODE, outputValues);"

Any idea as of how to resolve this issue, I have already gone through google search, but nothing touches on this particular issue.

Thanks in advance!



from java.nio.BufferOverflowException running tensorflow model

No comments:

Post a Comment