shaqian / flutter_realtime_detection

Real-time detection demo for Flutter tflite plugin
843 stars 302 forks source link

Using an own model #15

Closed bvlbvl closed 4 years ago

bvlbvl commented 4 years ago

Hi,

I'm trying to integrate an own model based on mobileNetV2 with the image size 224. The base is the google example

'https://colab.research.google.com/github/tensorflow/examples/blob/master/community/en/flowers_tf_lite.ipynb'

As mobileNet with the same image size is used also for "MobileNet", I reused the button and only changed the path to the custom assets in loadModel():

 default:
/*        res = await Tflite.loadModel(
            model: "assets/ssd_mobilenet.tflite",
            labels: "assets/ssd_mobilenet.txt");*/

        res = await Tflite.loadModel(
            model: "assets/model_flowers.tflite",
            labels: "assets/labels_flowers.txt");

unfotunately it doesn't work:


E/AndroidRuntime(28217): FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime(28217): Process: com.example.examplerealtime, PID: 28217
E/AndroidRuntime(28217): java.lang.RuntimeException: An error occurred while executing doInBackground()
E/AndroidRuntime(28217):    at android.os.AsyncTask$3.done(AsyncTask.java:354)
E/AndroidRuntime(28217):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
E/AndroidRuntime(28217):    at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
E/AndroidRuntime(28217):    at java.util.concurrent.FutureTask.run(FutureTask.java:271)
E/AndroidRuntime(28217):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
E/AndroidRuntime(28217):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/AndroidRuntime(28217):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/AndroidRuntime(28217):    at java.lang.Thread.run(Thread.java:764)
E/AndroidRuntime(28217): Caused by: java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [1, 5] and a Java object with shape [1, 5, 4].
E/AndroidRuntime(28217):    at org.tensorflow.lite.Tensor.throwIfShapeIsIncompatible(Tensor.java:291)
E/AndroidRuntime(28217):    at org.tensorflow.lite.Tensor.throwIfDataIsIncompatible(Tensor.java:258)
E/AndroidRuntime(28217):    at org.tensorflow.lite.Tensor.copyTo(Tensor.java:150)
E/AndroidRuntime(28217):    at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:183)
E/AndroidRuntime(28217):    at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:275)
E/AndroidRuntime(28217):    at sq.flutter.tflite.TflitePlugin$RunSSDMobileNet.runTflite(TflitePlugin.java:622)
E/AndroidRuntime(28217):    at sq.flutter.tflite.TflitePlugin$TfliteTask.doInBackground(TflitePlugin.java:419)
E/AndroidRuntime(28217):    at sq.flutter.tflite.TflitePlugin$TfliteTask.doInBackground(TflitePlugin.java:393)
E/AndroidRuntime(28217):    at android.os.AsyncTask$2.call(AsyncTask.java:333)
E/AndroidRuntime(28217):    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
E/AndroidRuntime(28217):    ... 4 more

As far I understand it correctly, the tensor shape passed to the model doesn't correspond to the required tensor shape : Cannot copy between a TensorFlowLite tensor with shape [1, 5] and a Java object with shape [1, 5, 4]

Unfortunately I am not able to debug sq.flutter.tflite.TflitePlugin.java

What is the correct way to integrate a custom model?

Thank you!

bvlbvl commented 4 years ago

solved it: it should be placed under mobilenet and not ssdmoblienet