无法在类型为 UINT8 的 TensorFlowLite 张量和类型为 [[F 的 Java 对象(与 TensorFlowLite 类型 FLOAT32 兼容)之间转换

Cant convert between a TensorFlowLite tensor with type UINT8 and a Java object of type [[F (which is compatible with the TensorFlowLite type FLOAT32)

我正在尝试 运行 android 上的 Tflite 模型,但是我收到了这个错误-

E/AndroidRuntime(18461): Caused by: java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (serving_default_sequential_5_input:0) with 150528 bytes from a Java Buffer with 602112 bytes. E/AndroidRuntime(18461): at org.tensorflow.lite.TensorImpl.throwIfSrcShapeIsIncompatible(TensorImpl.java:418) E/AndroidRuntime(18461): at org.tensorflow.lite.TensorImpl.setTo(TensorImpl.java:139) E/AndroidRuntime(18461): at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:237) E/AndroidRuntime(18461): at org.tensorflow.lite.InterpreterImpl.runForMultipleInputsOutputs(InterpreterImpl.java:135) E/AndroidRuntime(18461): at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:80) E/AndroidRuntime(18461): at org.tensorflow.lite.InterpreterImpl.run(InterpreterImpl.java:128) E/AndroidRuntime(18461): at org.tensorflow.lite.Interpreter.run(Interpreter.java:80) E/AndroidRuntime(18461): at sq.flutter.tflite.TflitePlugin$RunModelOnBinary.runTflite(TflitePlugin.java:530) E/AndroidRuntime(18461): at sq.flutter.tflite.TflitePlugin$TfliteTask.doInBackground(TflitePlugin.java:471) E/AndroidRuntime(18461): at sq.flutter.tflite.TflitePlugin$TfliteTask.doInBackground(TflitePlugin.java:445) E/AndroidRuntime(18461): at android.os.AsyncTask.call(AsyncTask.java:378) E/AndroidRuntime(18461): at java.util.concurrent.FutureTask.run(FutureTask.java:266) E/AndroidRuntime(18461): ... 4 more I/Process (18461): Sending signal. PID: 18461 SIG: 9

这条线让我印象深刻-

Caused by: java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (serving_default_sequential_5_input:0) with 150528 bytes from a Java Buffer with 602112 bytes.

我做错了什么?这是我的代码-

Future<List<dynamic>> runModel(Uint8List image) async {
    print("Loadin gmodel");
    String? res = await Tflite.loadModel(
      model: "assets/model.tflite",
      labels: "assets/labels.txt",
    );
    print("model loaded and loading running predictin");
    img.Image? Image = img.decodeJpg(image);
    var recognitions = await Tflite.runModelOnBinary(
        binary: imageToByteListFloat32(Image!, 224),
        numResults: 2, // get this value to be the number of classes you have
        threshold: 0.05, // defaults to 0.1, or put whatever you want here
        asynch: true // defaults to true
        );
    print(recognitions);
    await Tflite.close();
    return [];
  }

  Uint8List imageToByteListFloat32(img.Image image, int inputSize) {
    var convertedBytes = Float32List(1 * inputSize * inputSize * 3);
    var buffer = Float32List.view(convertedBytes.buffer);
    int pixelIndex = 0;
    for (var i = 0; i < inputSize; i++) {
      for (var j = 0; j < inputSize; j++) {
        var pixel = image.getPixel(j, i);
        buffer[pixelIndex++] = img.getRed(pixel) / 255.0;
        buffer[pixelIndex++] = img.getGreen(pixel) / 255.0;
        buffer[pixelIndex++] = img.getBlue(pixel) / 255.0;
      }
    }
    return convertedBytes.buffer.asUint8List();
  }

该模型似乎在请求 UINT8(无符号 8 位整数)张量。

我认为您可以稍微简化一下代码:

  • 准备一个 UInt8 缓冲区而不是 Float32
  • 您不需要将该值除以 255.0

那么应该可以了。

(附带说明,使用 ByteBuffer 比 array/list 效率更高)