【问题标题】:Why do I get a BufferOverflowException when running a TensorFlowLite Model?为什么在运行 TensorFlowLite 模型时会出现 BufferOverflowException?
【发布时间】:2020-09-04 13:37:34
【问题描述】:

我想使用 TensorFlowLite(并使用 Kotlin)在 Android 上运行自定义 tflite 模型。 尽管使用 TFLite 支持库创建了一个假定形状正确的输入和输出缓冲区,但每次调用 run() 方法时都会收到以下错误消息。

这是我的课:

class Inference(context: Context) {
    private val tag = "Inference"
    private var interpreter: Interpreter
    private var inputBuffer: TensorBuffer
    private var outputBuffer: TensorBuffer

    init {
        val mappedByteBuffer= FileUtil.loadMappedFile(context, "CNN_ReLU.tflite")
        interpreter = Interpreter(mappedByteBuffer as ByteBuffer)
        interpreter.allocateTensors()

        val inputShape = interpreter.getInputTensor(0).shape()
        val outputShape = interpreter.getOutputTensor(0).shape()

        inputBuffer = TensorBuffer.createFixedSize(inputShape, DataType.FLOAT32)
        outputBuffer = TensorBuffer.createFixedSize(outputShape, DataType.FLOAT32)
    }

    fun run() {
        interpreter.run(inputBuffer.buffer, outputBuffer.buffer) // XXX: generates error message
    }
}

这是错误信息:

W/System.err: java.nio.BufferOverflowException
W/System.err:     at java.nio.ByteBuffer.put(ByteBuffer.java:615)
W/System.err:     at org.tensorflow.lite.Tensor.copyTo(Tensor.java:264)
W/System.err:     at org.tensorflow.lite.Tensor.copyTo(Tensor.java:254)
W/System.err:     at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:170)
W/System.err:     at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:347)
W/System.err:     at org.tensorflow.lite.Interpreter.run(Interpreter.java:306)

我只初始化了输入和输出缓冲区,还没有写入任何数据。

我正在使用这些 gradle 依赖项:

implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
implementation 'org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly'
implementation 'org.tensorflow:tensorflow-lite-support:0.0.0-nightly'

.tflite 模型是使用这些 TensorFlow 版本构建的:

tensorflow                        2.3.0
tensorflow-cpu                    2.2.0
tensorflow-datasets               3.1.0
tensorflow-estimator              2.3.0
tensorflow-gan                    2.0.0
tensorflow-hub                    0.7.0
tensorflow-metadata               0.22.0
tensorflow-probability            0.7.0
tensorflowjs                      1.7.4.post1

非常感谢任何想法或提示,谢谢。

【问题讨论】:

  • 如果将数据类型更改为小于 float32 会发生什么?
  • @mangusta 当使用UINT8 作为数据类型时,我得到java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (input_1) with 3520 bytes from a Java Buffer with 880 bytes.,我猜这是有道理的。
  • 你试过其他型号吗?

标签: android kotlin buffer-overflow tensorflow-lite


【解决方案1】:

是否将 .rewind() 添加到您的输入和输出缓冲区使其工作? 如果不是,我想知道你的输入或输出张量是否是动态张量?在这种情况下,返回形状不能以这种方式使用。

【讨论】:

  • interpreter.run() 之前调用outputBuffer.buffer.rewind() 确实摆脱了BufferOverflowException!似乎调用clear() 而不是rewind() 也具有相同的效果。你能给出更多的背景信息吗?我对 ByteBuffer 类一点也不熟悉。
猜你喜欢
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多