无法在TensorFlow Lite对象检测Android应用中使用自定义模型

时间:2019-09-17 04:42:09

标签: android tensorflow tensorflow-lite

我使用Google AutoMl训练了模型,然后生成了tensorflow lite模型来检测塑料瓶等。我想在tensorflowlite对象检测android示例中使用

这是我引用的github:https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android

我用我自己的项目替换了tflite文件和txt文件,在android studio上的安装效果很好,但是应用崩溃了并且没用

public class DetectorActivity extends CameraActivity implements OnImageAvailableListener {
  private static final Logger LOGGER = new Logger();

  // Configuration values for the prepackaged SSD model.
  private static final int TF_OD_API_INPUT_SIZE = 300;
  private static final boolean TF_OD_API_IS_QUANTIZED = true;
  private static final String TF_OD_API_MODEL_FILE = "swai.tflite";
  private static final String TF_OD_API_LABELS_FILE = "file:///android_asset/swai.txt";
  private static final DetectorMode MODE = DetectorMode.TF_OD_API;
  // Minimum detection confidence to track a detection.
  private static final float MINIMUM_CONFIDENCE_TF_OD_API = 0.5f;
  private static final boolean MAINTAIN_ASPECT = false;
  private static final Size DESIRED_PREVIEW_SIZE = new Size(640, 480);
  private static final boolean SAVE_PREVIEW_BITMAP = false;
  private static final float TEXT_SIZE_DIP = 10;
  OverlayView trackingOverlay;

这是我在虚拟设备中部署时发现的错误

09-17 13:32:09.283 1599-1856/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 462848
09-17 13:32:09.325 9980-10000/org.tensorflow.lite.examples.detection E/AndroidRuntime: FATAL EXCEPTION: inference
    Process: org.tensorflow.lite.examples.detection, PID: 9980
    java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite buffer with 786432 bytes and a ByteBuffer with 270000 bytes.
        at org.tensorflow.lite.Tensor.throwIfShapeIsIncompatible(Tensor.java:272)
        at org.tensorflow.lite.Tensor.throwIfDataIsIncompatible(Tensor.java:249)
        at org.tensorflow.lite.Tensor.setTo(Tensor.java:110)
        at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:151)
        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:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.os.HandlerThread.run(HandlerThread.java:65)


我认为没有设置可以实现此模型工作,请问Sugesstion吗?谢谢!

1 个答案:

答案 0 :(得分:0)

关闭案例,通过将TF_OD_API_INPUT_SIZE从300更改为512来解决。786432代表512x512x3,但我的输入仅为270000(300x3003)。