用于在应用程序中实现张量流的不同的.tflite文件和.txt文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用于在应用程序中实现张量流的不同的.tflite文件和.txt文件相关的知识,希望对你有一定的参考价值。

我已经从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());
答案
的TensorFlowLite缓冲区之间进行转换

这是因为您的INPUT_SHAPE / INPUT_DATA与模型的INPUT_SHAPE / INPUT DATA不同。以本机顺序将输入数据转换为ByteBuffer,其形状和大小与输入模型的方式完全相同。

以上是关于用于在应用程序中实现张量流的不同的.tflite文件和.txt文件的主要内容,如果未能解决你的问题,请参考以下文章

如何在张量流中实现提前停止

量化如何以及何时在 TFLite 图中起作用?

如何使用 sparse_softmax_cross_entropy_with_logits 在张量流中实现加权交叉熵损失

TensorFlow Lite:toco_convert 用于任意大小的输入张量

为啥我会得到不同形状的张量错误?

如何在张量流的自定义损失中获取张量的形状