问题
I've trained my own model for image classification model in keras and i converted it into tflite then i want to use that model in android through tensorflow lite. for this i used a github project to get my hands directly on the app link to the project is here:
https://github.com/amitshekhariitbhu/Android-TensorFlow-Lite-Example/tree/master/app/src/main/java/com/amitshekhar/tflite
But i have got this error in the logcat:
2020-03-30 14:50:48.747 27421-27421/com.amitshekhar.tflite E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.amitshekhar.tflite, PID: 27421
java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [2] and a Java object with shape [1, 2].
at org.tensorflow.lite.Tensor.throwIfShapeIsIncompatible(Tensor.java:342)
at org.tensorflow.lite.Tensor.throwIfDataIsIncompatible(Tensor.java:305)
at org.tensorflow.lite.Tensor.copyTo(Tensor.java:183)
at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:166)
at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:311)
at org.tensorflow.lite.Interpreter.run(Interpreter.java:272)
at com.amitshekhar.tflite.TensorFlowImageClassifier.recognizeImage(TensorFlowImageClassifier.java:70)
at com.amitshekhar.tflite.MainActivity$1.onImage(MainActivity.java:75)
at com.wonderkiln.camerakit.EventDispatcher$1.run(EventDispatcher.java:42)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7094)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:536)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:928)
Any help regarding this will be appreciated and furthermore i have not make any changes in the code except input size of my model and my own tflite model and labels.txt files in assets folder.
回答1:
I have solved my problem. It was an issue with the conversion process of my .h5 keras model into tflite. So, that resulted in wrong output tensor. So anyone who face this should assure that in
Your sndroid application input sizes should be like specified in the model.
Always check your model before and after conversion with this wonderful tool here is the link to the tool below.
[Netron visualizer tool for all types of models]
https://www.electronjs.org/apps/netron
- Use this notebook for conversion either to .pb or .tflite. I have explained almost every step in it for newers too.
[Google Colab Notebook]
https://colab.research.google.com/drive/1wgy0vqS6IRO0rHju7l_InCUiatCo85y6#scrollTo=0tZma5ebnzZN
Thanks..
来源:https://stackoverflow.com/questions/60929312/java-lang-illegalargumentexception-cannot-copy-between-a-tensorflowlite-tensor