android - 运行 tensorflow 模型的 java.nio.BufferOverflowException

标签 android tensorflow

我在我的 android 应用程序中运行 tensorflow 模型时遇到这个 fatal error :

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

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

提前致谢!

最佳答案

您需要验证 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

关于android - 运行 tensorflow 模型的 java.nio.BufferOverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51458477/

相关文章:

java - java.io.File 的 jar 在哪里?

android - 如何在ActionBar的navigation tabs中设置自定义View,并让tabs自适应高度?

java - 当我在 ListView 中添加标题时,我需要执行位置 - 1 来访问后端列表

python - 选择可训练变量来计算梯度 "No variables to optimize"

python - 使用 CNN 模型中的 channel 均值和标准差对训练数据进行归一化

python - 不支持 hdf5(请安装/重新安装 h5py)不支持 Scipy!什么时候导入 TFLearn?

android - Android 12 keystore 用户 0 丢失文件夹

java - Android 编程 - 使用警告对话框以编程方式更改按钮文本

pandas - tf.estimator.inputs.pandas_input_fn 标签张量

python - 使用 Tensorflow-2.0 tf.optimizers 时如何修复 'The given object is not an Optimizer instance'?