6

在将转换后的 TFLite 模型加载到 android 应用程序时遇到问题。

模型:Deeplabv3 Mobilenetv2(在 Pascal VOC 上训练)TFLite 版本:1.10

使用 tflite_convert 将 pb 文件转换为 tflite。(张量流版本:1.11.0)

代码:

private MappedByteBuffer loadModelFile(AssetManager assets, String modelFilename)
      throws IOException {
    AssetFileDescriptor fileDescriptor = assets.openFd(modelFilename);
    FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
    FileChannel fileChannel = inputStream.getChannel();
    long startOffset = fileDescriptor.getStartOffset();
    long declaredLength = fileDescriptor.getDeclaredLength();
    return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
  }

  /**
   * Initializes a native TensorFlow session for classifying images.
   *
   * @param assetManager The asset manager to be used to load assets.
   * @param modelFilename The filepath of the model GraphDef protocol buffer.
   */
  public static Segmentor create(
      final AssetManager assetManager,
      final String modelFilename,
      final String labelFilename,
      final int inputWidth,
      final int inputHeight,
      final int numClass, final int numOutput) throws IOException {
    final TFLiteObjectSegmentationAPIModel d = new TFLiteObjectSegmentationAPIModel();

    d.inputWidth = inputWidth;
    d.inputHeight = inputHeight;
    d.numClass = numClass;

    try {
      d.tfLite = new Interpreter(d.loadModelFile(assetManager, modelFilename));
      d.tfLite.setNumThreads(4);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

错误:

E/tensorflow: CameraActivity: Exception!
    java.lang.RuntimeException: java.lang.IllegalArgumentException: ByteBuffer is not a valid flatbuffer model
        at org.tensorflow.demo.segmentation.TFLiteObjectSegmentationAPIModel.create(TFLiteObjectSegmentationAPIModel.java:93)
        at org.tensorflow.demo.SegmentorActivity.onPreviewSizeChosen(SegmentorActivity.java:112)
        at org.tensorflow.demo.CameraActivity.onPreviewFrame(CameraActivity.java:122)
        at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1266)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6251)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

在build.gradle中将tensorflow-lite版本改为'org.tensorflow:tensorflow-lite:0.0.0-nightly',得到如下错误

E/AndroidRuntime: FATAL EXCEPTION: front
    Process: me.tantara.real_time_segmentation, PID: 17205
    java.lang.IllegalArgumentException: Internal error: Failed to run on the given Interpreter: tensorflow/lite/kernels/depthwise_conv.cc:99 params->depth_multiplier * SizeOfDimension(input, 3) != SizeOfDimension(filter, 3) (0 != 32)Node number 30 (DEPTHWISE_CONV_2D) failed to prepare.

        at org.tensorflow.lite.NativeInterpreterWrapper.run(Native Method)
        at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:145)
        at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:229)
        at org.tensorflow.lite.Interpreter.run(Interpreter.java:208)
        at org.tensorflow.demo.segmentation.TFLiteObjectSegmentationAPIModel.segmentImage(TFLiteObjectSegmentationAPIModel.java:146)
        at org.tensorflow.demo.SegmentorActivity$3.run(SegmentorActivity.java:221)
        at android.os.Handler.handleCallback(Handler.java:836)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:203)
        at android.os.HandlerThread.run(HandlerThread.java:61)
4

0 回答 0