Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot copy between a TensorFlowLite tensor with shape [1, 2] and a Java object with shape [1, 4]., null) #10

Closed
emailsubjekt opened this issue Jan 14, 2019 · 20 comments
Labels
bug Something isn't working

Comments

@emailsubjekt
Copy link

Hi, I’m using a graph.lite trained with 2 results and a labels.txt, I have managed to get the example working, however it works wonderfully while I’m using it and I can analyze and take as many pictures as I want, yet when I leave to another screen in the app and come back I get an error like the one below stating that somehow the Java object which had correctly worked and gave me a result on top of the taken image, has now changed from [1,2] to [1,4], what’s more interesting is that this discrepancy keeps growing as I leave and come back, as the next time it’s [1,6], the next [1,8], next [1,10] you get the picture, and it constantly keeps giving me this error and doesn’t go on to analyze the image therefore I get no results anymore. I have to close the app and start it again for tensorflow to work, then again if I leave to another screen and come back it starts the same cycle. Is this a bug or should I change something else? I have used either 224 or 299 for image size when preparing tflite, imageMean was 128 not 127.5 but I’ve tried changing that too without any results. I have tried modifying the variables numResults, imageMean, imageStd, image sizes in main and tflite.dart however I still get an error on second run. The only thing I didn’t try changing was threshold. Can you please help. This is the error I get below. Best regards

E/flutter (23306): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
E/flutter (23306): PlatformException(error, Unsupported value: java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [1, 2] and a Java object with shape [1, 4]., null)
E/flutter (23306): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:551:7)
E/flutter (23306): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:292:18)
E/flutter (23306):
E/flutter (23306): #2 Tflite.runModelOnImage (package:tflite/tflite.dart:33:27)
E/flutter (23306):
E/flutter (23306): #3 _MyAppState.recognizeImage (package:someApp_tabbed_activity/tflite/tflitemain.dart:74:37)
E/flutter (23306):
E/flutter (23306): #4 _MyAppState.getImage (package:someApp_tabbed_activity/tflite/tflitemain.dart:25:5)
E/flutter (23306):
E/flutter (23306): #5 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
E/flutter (23306): #6 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:562:30)
E/flutter (23306): #7 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter (23306): #8 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
E/flutter (23306): #9 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:204:7)
E/flutter (23306): #10 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (23306): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:184:20)
E/flutter (23306): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:158:22)
E/flutter (23306): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:138:7)
E/flutter (23306): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
E/flutter (23306): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
E/flutter (23306): #16 _invoke1 (dart:ui/hooks.dart:168:13)
E/flutter (23306): #17 _dispatchPointerDataPacket (dart:ui/hooks.dart:122:5)

@shaqian
Copy link
Owner

shaqian commented Jan 15, 2019

Thank you for the feedback.
Would it be convenient for you to provide a sample project that reproduces the issue?

@emailsubjekt
Copy link
Author

Hi, I used your example with no modifications, then modified it with the files I have and the above mentioned modifications I did made no difference to how the app reacted. It works when I first use it even with no modifications to your example code, but when I leave the screen to another screen in the app and come back and try to use it I get the error I wrote out. What I don't understand is how can it work if I have a .lite with 2 possible answers and the example has in one class a definition of numResults = 6 and in another numResults = 5, yet it still provides me a correct answer when using the app.

I could remake the .lite for the project but you could remake it really easy too. I can't unfortunately send you the original as it's proprietary and from a client I'm making this for, but it's a .lite with 2 possible answers as is the accompanying txt file which has the names of the options written out. As I said the example works great, I get a result meaning the file I'm using works and has worked on an Android version perfectly. However, here I keep getting this error after using it going to another screen in the app and then coming back.

Contact me if you need help with making a .lite file, I'll be happy to help you solve this...

@shaqian
Copy link
Owner

shaqian commented Jan 17, 2019

Thank you for the reply.
When you go back from the other screen, do you reload the model? I suspect loadLabels() is called again so label size is multiplied.
Can you please try clearing labels array? Add labels = new Vector<>(); before

while ((line = br.readLine()) != null) {
labels.add(line);
}
labelProb = new float[1][labels.size()];

@emailsubjekt
Copy link
Author

Thank you for the reply.
When you go back from the other screen, do you reload the model? I suspect loadLabels() is called again so label size is multiplied.
Can you please try clearing labels array? Add labels = new Vector<>(); before
flutter_tflite/android/src/main/java/sq/flutter/tflite/TflitePlugin.java

Lines 107 to 110 in 624c46d

while ((line = br.readLine()) != null) {
labels.add(line);
}
labelProb = new float[1][labels.size()];

Thanks! This works. =)
For ios in TflitePlugin.mm there's labels.clear(); in the close() method so I'm guessing that one should be ok then for the ios side?

Just one more thing irrelevant to this discussion, when running the project I keep getting this notification

Note: C:\flutter.pub-cache\hosted\pub.dartlang.org\tflite-0.0.5\android\src\main\java\sq\flutter\tflite\TflitePlugin.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

I was also getting it before applying your solution. Everything works great and it compiles and runs on the phone, just thought I'd mention it so you know.

Awesome project by the way, and thanks for making it! You've got a beer from me.

@shaqian
Copy link
Owner

shaqian commented Jan 20, 2019

Glad to know it works. 😃I will add the fix in the next version.

@shaqian shaqian added the bug Something isn't working label Feb 2, 2019
@emailsubjekt
Copy link
Author

emailsubjekt commented Mar 1, 2019

I've tried version 1.0.1 What did you do, the import is red, I'm using import 'package:tflite/tflite.dart'; like your instructions say and it says 'Target of URI doesn't exist: 'package:tflite/tflite.dart'.' after fliding over it with the mouse to see why it's red.
Also, why isn't there an example what to write into pubspec, you write "Add tflite as a dependency in your pubspec.yaml file." and then it takes me to the general info about adding dependencies. I know that, how do I add yours? Like this -> tflite: ^1.0.1 ?

Also, I had a calculation of the final output and showed the result on the screen, I updated to that 1.0.1 (also tried 1.0.0) and now no screen result is showing. I'm working on another app now and don't have time to look into this but why would you change the output, now I have to do this again.

@shaqian
Copy link
Owner

shaqian commented Mar 1, 2019

There are some breaking changes since 1.0.0
https://github.com/shaqian/flutter_tflite#breaking-changes-since-100

I think you were using 0.0.2 or 0.0.3. The latest version that doesn't break your code might be 0.0.5.
https://pub.dartlang.org/packages/tflite#-changelog-tab-

You can try fixing the version like tflite: 0.0.5, then run flutter packages get. If you still get red squiggly lines, go to ios folder and run pod install.

Thanks,
Qian

@emailsubjekt
Copy link
Author

Ok, thanks for the answer I will look into it in a couple of days, keep up the great work! =)

@SergioBernal8
Copy link

I had a similar bug!
After searching and searching I realized that the ".txt" files that I dowloaded from the Git for the assets folder was incorrect. Then I fixed that and everything its working fine now!
By the way I'm using detectObjectOnImage with the model "SSDMobileNet"
tflite version: 1.0.2

Hope it helps!!

@brunokitaka
Copy link

brunokitaka commented May 16, 2019

I'm trying to use a tflite file that I trained myself and I'm getting this error:

E/flutter (24486): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Unsupported value: java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [1, 10, 4] and a Java object with shape [1, 9, 9, 30]., null)
E/flutter (24486): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
E/flutter (24486): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:302:33)
E/flutter (24486):
E/flutter (24486): #2 Tflite.detectObjectOnFrame (package:tflite/tflite.dart:152:27)
E/flutter (24486):
E/flutter (24486): #3 _CameraState.initState.. (package:flutter_realtime_detection/camera.dart:50:20)
E/flutter (24486): #4 CameraController.startImageStream. (package:camera/camera.dart:353:20)
E/flutter (24486): #5 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter (24486): #6 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (24486): #7 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (24486): #8 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
E/flutter (24486): #9 _DelayedData.perform (dart:async/stream_impl.dart:591:14)
E/flutter (24486): #10 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:707:11)
E/flutter (24486): #11 _PendingEvents.schedule. (dart:async/stream_impl.dart:667:7)
E/flutter (24486): #12 _rootRun (dart:async/zone.dart:1120:38)
E/flutter (24486): #13 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (24486): #14 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter (24486): #15 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
E/flutter (24486): #16 _rootRun (dart:async/zone.dart:1124:13)
E/flutter (24486): #17 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (24486): #18 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter (24486): #19 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
E/flutter (24486): #20 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (24486): #21 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

@emailsubjekt
Copy link
Author

I'm trying to use a tflite file that I trained myself and I'm getting this error:

E/flutter (24486): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Unsupported value: java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [1, 10, 4] and a Java object with shape [1, 9, 9, 30]., null)
E/flutter (24486): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
E/flutter (24486): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:302:33)
E/flutter (24486):
E/flutter (24486): #2 Tflite.detectObjectOnFrame (package:tflite/tflite.dart:152:27)
E/flutter (24486):
E/flutter (24486): #3 _CameraState.initState.. (package:flutter_realtime_detection/camera.dart:50:20)
E/flutter (24486): #4 CameraController.startImageStream. (package:camera/camera.dart:353:20)
E/flutter (24486): #5 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter (24486): #6 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (24486): #7 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (24486): #8 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
E/flutter (24486): #9 _DelayedData.perform (dart:async/stream_impl.dart:591:14)
E/flutter (24486): #10 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:707:11)
E/flutter (24486): #11 _PendingEvents.schedule. (dart:async/stream_impl.dart:667:7)
E/flutter (24486): #12 _rootRun (dart:async/zone.dart:1120:38)
E/flutter (24486): #13 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (24486): #14 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter (24486): #15 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
E/flutter (24486): #16 _rootRun (dart:async/zone.dart:1124:13)
E/flutter (24486): #17 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (24486): #18 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter (24486): #19 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
E/flutter (24486): #20 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (24486): #21 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

Well, this is your problem --> Cannot copy between a TensorFlowLite tensor with shape [1, 10, 4] and a Java object with shape [1, 9, 9, 30]., null) - You've trained it wrong.

@shaqian
Copy link
Owner

shaqian commented May 30, 2019

Archiving the thread as the original issue is fixed in new version.

@brunokitaka Feel free to raise a new issue if you still get error with your model.

Thanks,
Qian

@shaqian shaqian closed this as completed May 30, 2019
@amit-dingare
Copy link

Hi I trained the Mobilenet model on certain domain specific images using transfer learning. But, when I try to run the model, I get "Cannot copy between a TensorFlowLite tensor with shape [1, 11] and a Java object with shape [1, 11, 4]". I have 11 classes in my dataset. How may I fix the issue?

@amit-dingare
Copy link

After searching and searching I realized that the ".txt" files that I dowloaded from the Git for the assets folder was incorrect. Then I fixed that and everything its working fine now!

When you say the txt files were incorrect, what do you mean? Incorrect in what sense?

@rash1994
Copy link

I'm trying to use a tflite file that I trained myself and I'm getting this error:
E/flutter (24486): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Unsupported value: java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [1, 10, 4] and a Java object with shape [1, 9, 9, 30]., null)
E/flutter (24486): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
E/flutter (24486): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:302:33)
E/flutter (24486):
E/flutter (24486): #2 Tflite.detectObjectOnFrame (package:tflite/tflite.dart:152:27)
E/flutter (24486):
E/flutter (24486): #3 _CameraState.initState.. (package:flutter_realtime_detection/camera.dart:50:20)
E/flutter (24486): #4 CameraController.startImageStream. (package:camera/camera.dart:353:20)
E/flutter (24486): #5 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter (24486): #6 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (24486): #7 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (24486): #8 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
E/flutter (24486): #9 _DelayedData.perform (dart:async/stream_impl.dart:591:14)
E/flutter (24486): #10 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:707:11)
E/flutter (24486): #11 _PendingEvents.schedule. (dart:async/stream_impl.dart:667:7)
E/flutter (24486): #12 _rootRun (dart:async/zone.dart:1120:38)
E/flutter (24486): #13 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (24486): #14 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter (24486): #15 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
E/flutter (24486): #16 _rootRun (dart:async/zone.dart:1124:13)
E/flutter (24486): #17 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (24486): #18 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter (24486): #19 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
E/flutter (24486): #20 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (24486): #21 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

Well, this is your problem --> Cannot copy between a TensorFlowLite tensor with shape [1, 10, 4] and a Java object with shape [1, 9, 9, 30]., null) - You've trained it wrong.

Can you tell trained it wrong meaning ?
what is wrong is training?

@EyeSlash1998
Copy link

Hi I trained the Mobilenet model on certain domain specific images using transfer learning. But, when I try to run the model, I get "Cannot copy between a TensorFlowLite tensor with shape [1, 11] and a Java object with shape [1, 11, 4]". I have 11 classes in my dataset. How may I fix the issue?

Were you able to solve this issue, because even I'am facing an issue were I get the error "Cannot copy between a TensorFlow Lite tensor with shape [1,2535,9] and a java object with shape [1,13,13,45]".

@supermarille
Copy link

Same issue here :
java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [1, 10, 4] and a Java object with shape [1, 1]
I have only one class, I followed this tutorial https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_on_mobile_tensorflowlite.md using bazel to convert to tflite.
I understand that the second value of the shape should be the number of classes, but what are the other values ?

@rxsh96
Copy link

rxsh96 commented May 26, 2020

I have the same problem: Cannot copy between a TensorFlowLite tensor with shape [1, 1] and a Java object with shape [1, 2]
I have MobileNetV2 as input layer, 1 flatten and 3 dense layers and then converted to tflite. I have 2 classes in my dataset. Is there some kind of limitation for tflite-flutter package does not work with custom models?

@shaharhn
Copy link

Any solution ?

@nikunj5022
Copy link

Hi I trained the Mobilenet model on certain domain specific images using transfer learning. But, when I try to run the model, I get "Cannot copy between a TensorFlowLite tensor with shape [1, 11] and a Java object with shape [1, 11, 4]". I have 11 classes in my dataset. How may I fix the issue?

Were you able to solve this issue, because even I'am facing an issue were I get the error "Cannot copy between a TensorFlow Lite tensor with shape [1,2535,9] and a java object with shape [1,13,13,45]".

Any solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests