【问题标题】:Cannot copy to a TensorFlowLite tensor (input_values:0) with 0 bytes from a Java Buffer with 49152 bytes无法从 49152 字节的 Java 缓冲区复制到 0 字节的 TensorFlowLite 张量 (input_values:0)
【发布时间】:2022-10-31 15:24:55
【问题描述】:

嗨,我想在 android studio 7.2.1 中使用以下代码向我的 .tflite 模型提供输入:

        val catBitmap = getBitmapFromAsset("bwr.jpg")// a 64*64 image 
        val output2 = TensorImage(DataType.FLOAT32)
        
        for (y in 0..63) {
            for (x in 0..63) {
                val px: Int = catBitmap.getPixel(x, y)

                // Get channel values from the pixel value.
                val r: Int = Color.red(px)
                val g: Int = Color.green(px)
                val b: Int = Color.blue(px)

                // Normalize channel values to [-1.0, 1.0]. This requirement depends
                // on the model. For example, some models might require values to be
                // normalized to the range [0.0, 1.0] instead.
                val rf = (r - 127) / 255.0f
                val gf = (g - 127) / 255.0f
                val bf = (b - 127) / 255.0f
                input.putFloat(rf)
                input.putFloat(gf)
                input.putFloat(bf)
            }
        }
        
        tflite.run(input, output2)

这是我的 tflite 模型属性:

它会导致此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tag22, PID: 15184
java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (input_values:0) with 0 bytes from a Java Buffer with 49152 bytes.

这是我可以成功获得预测的python解释器代码:

image_filename='img.jpeg'
input_data  = tf.compat.v1.gfile.FastGFile(image_filename, 
'rb').read()
cc=[input_data ]
input_data = np.array([input_data ])
interpreter.set_tensor(input_details[0]['index'], input_data)

你能帮我解决这个问题吗?我不知道在 android 中向我的模型提供输入。

【问题讨论】:

  • @Maxim 你能看看我的问题吗?

标签: python java android-studio tensorflow tflite


【解决方案1】:

最后,我找到了答案。

我的 .tflite 模型有问题。我无法向我的模型提供输入。当我从我的图表中删除一些节点并向我的图表添加一些预处理步骤时。问题已解决。

【讨论】:

    猜你喜欢
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 2014-07-12
    相关资源
    最近更新 更多