【问题标题】:How to correctly run tensorflow lite inference on Android with CSV as input?如何使用 CSV 作为输入在 Android 上正确运行 tensorflow lite 推理?
【发布时间】:2021-03-18 17:57:57
【问题描述】:

我有一个期望输入形状为 (1, 1000, 12) 的 tflite 模型。只是为了测试它,我打算加载一个 CSV 文件并对其进行推理。下面是我的代码和运行它时得到的错误消息的相关部分。

我认为我在正确加载或读取 CSV 文件时出错了。我对 Android 比较陌生,如果能在这件事上提供任何帮助,我将不胜感激!

val testModel = myModel.newInstance(context)

// Creates inputs for reference.
val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 1000, 12), DataType.FLOAT32)

val openRawResource = resources.openRawResource(R.raw.inputdata).readBytes()
val byteBuffer = ByteBuffer.wrap(openRawResource)

// inputFeature0.loadBuffer(byteBuffer)
inputFeature0.loadBuffer(byteBuffer)

// Runs model inference and gets result.
val outputs = testModel.process(inputFeature0)
val outputFeature0 = outputs.outputFeature0AsTensorBuffer

// Releases model resources if no longer used.
model.close()
Caused by: java.lang.IllegalArgumentException: The size of byte buffer and the shape do not match.
        at org.tensorflow.lite.support.common.SupportPreconditions.checkArgument(SupportPreconditions.java:104)
        at org.tensorflow.lite.support.tensorbuffer.TensorBuffer.loadBuffer(TensorBuffer.java:296)
        at org.tensorflow.lite.support.tensorbuffer.TensorBuffer.loadBuffer(TensorBuffer.java:323)
        at com.example.ecgclassifier.MainActivity.analyze(MainActivity.kt:47)
        at com.example.ecgclassifier.MainActivity.onCreate(MainActivity.kt:23)

【问题讨论】:

    标签: android csv tensorflow kotlin tensorflow-lite


    【解决方案1】:

    请确保上述原始资源字节数组有一个用于张量的浮点缓冲区数组,形状为 [1, 1000, 12]。

    错误消息说来自byteBuffer 的给定字节数组与[1, 1000, 12] 形状的浮点张量的大小要求不匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-05
      • 2020-10-15
      • 2021-12-31
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多