【问题标题】:java.nio.BufferOverflowException running tensorflow modeljava.nio.BufferOverflowException 运行张量流模型
【发布时间】:2018-07-21 17:22:16
【问题描述】:

我在我的 android 应用中运行 tensorflow 模型时遇到这个致命错误:

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)

代码如下:

    //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);

错误显示在我上面提供的最后一行代码上,即 "mInferenceInterface.fetch(OUTPUT_NODE, outputValues);"

关于如何解决这个问题的任何想法,我已经通过谷歌搜索,但没有涉及到这个特定问题。

提前致谢!

【问题讨论】:

    标签: android tensorflow


    【解决方案1】:

    您需要验证 OUTPUT_NODE 的维度是否等于 outputValues 的维度?

    另外可以参考我的项目代码,url:https://github.com/tz28/Chinese-number-gestures-recognition/blob/master/DigitalGestureRecognition/app/src/main/java/com/example/hc/digitalgesturerecognition/Classifier.java

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 2018-11-25
      • 2019-06-18
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多