Tensorflow lite 模型请求的缓冲区大于必要的缓冲区

Tensorflow lite model request a buffer bigger than the neccesary

我在 tensorflow 中使用 keras 创建了一个自定义模型。我使用的版本是 tensorflow nightly 1.13.1。我用的是官方工具搭建的tensorflow lite模型(方法tf.lite.TFLiteConverter.from_keras_model_file)。

创建模型后,我查看了输入形状,似乎没有什么问题。

tensorflow lite 模型中的输入和输出形状为:

[{'name': 'input_1', 'index': 59, 'shape': array([  1, 240, 240,   3], dtype=int32), 'dtype': , 'quantization': (0.0, 0)}]

[{'name': 'dense/Softmax', 'index': 57, 'shape': array([1, 6], dtype=int32), 'dtype': , 'quantization': (0.0, 0)}]

您可以注意到输入形状是 1 * 240 * 240 * 3 所以我预计缓冲区的大小为 172800 个单位。

但是,当我尝试在 android 设备中 运行 模型时,我收到了下一个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.megacode, PID: 15067
    java.lang.RuntimeException: Unable to create application com.megacode.base.ApplicationBase: java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite buffer with 691200 bytes and a ByteBuffer with 172800 bytes.
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5771)
        at android.app.ActivityThread.-wrap2(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1648)

我不明白模型要求输入形状为 691200 个单位的原因。

如果有人有建议,我将不胜感激

你是对的,输入的形状包含1 * 240 * 240 * 3 个元素.

但是每个元素都是int32类型,每个占4个字节

因此,ByteBuffer的总大小应该是1 * 240 * 240 * 3 * 4 = 691200。