首页 > 解决方案 > 不同的 .tflite 文件和 .txt 文件用于在 App 中实现 tensorflow

问题描述

我从 MLKit 创建了 .tflite 文件并在张量应用程序中使用,但应用程序崩溃并出现以下错误。

  java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite buffer with 150528 bytes and a ByteBuffer with 786432 bytes.
            at org.tensorflow.lite.Tensor.throwIfShapeIsIncompatible(Tensor.java:281)
            at org.tensorflow.lite.Tensor.throwIfDataIsIncompatible(Tensor.java:258)
            at org.tensorflow.lite.Tensor.setTo(Tensor.java:119)
            at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:167)
            at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:275)
            at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.recognizeImage(TFLiteObjectDetectionAPIModel.java:193)
            at org.tensorflow.lite.examples.detection.DetectorActivity$2.run(DetectorActivity.java:181)
            at android.os.Handler.handleCallback(Handler.java:790)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:164)
            at android.os.HandlerThread.run(HandlerThread.java:65)

以下是 tensorFlow App 的代码。

 if (isQuantized) {
      numBytesPerChannel = 1; // Quantized
    } else {
      numBytesPerChannel = 4; // Floating point
    }
    d.imgData = ByteBuffer.allocateDirect(1 * d.inputSize * d.inputSize * 3 * numBytesPerChannel);
    d.imgData.order(ByteOrder.nativeOrder());

标签: androidandroid-studiotensorflow-lite

解决方案


这是因为您的 INPUT_SHAPE/INPUT_DATA 与模型的 INPUT_SHAPE/INPUT DATA 不同。以本机顺序将输入数据转换为 ByteBuffer,并具有确切的形状和大小,模型正在接受输入。


推荐阅读