user 007
user 007

Reputation: 911

java.lang.IllegalArgumentException: Output error: Shape of output target [1, 1917, 4] does not match with the shape of the Tensor [1, 1917, 1, 4]

I've trained my own model for object detection with tensorflow and I got it working with Tensorflow mobile for android. Now since Tensorflow Lite is released and is going to replace mobile in the future I wanted to start working with it. The Tensorflow team provided a demo for TFLite for object detection (you can find it here). So I tried to get it working with my model but I got the error in the title. Here's the logcat :

05-17 11:18:50.624 25688-25688/? I/tensorflow: DetectorActivity: Camera orientation relative to screen canvas: 90
05-17 11:18:50.624 25688-25688/? I/tensorflow: DetectorActivity: Initializing at size 640x480
05-17 11:18:50.628 25688-25688/? I/tensorflow: MultiBoxTracker: Initializing ObjectTracker: 640x480
05-17 11:18:50.637 25688-25688/? I/tensorflow: DetectorActivity: Preparing image 1 for detection in bg thread.
05-17 11:18:50.689 25688-25707/? I/tensorflow: DetectorActivity: Running detection on image 1
05-17 11:18:52.496 25688-25707/? E/AndroidRuntime: FATAL EXCEPTION: inference
                                                   Process: org.tensorflow.lite.demo, PID: 25688
                                                   java.lang.IllegalArgumentException: Output error: Shape of output target [1, 1917, 4] does not match with the shape of the Tensor [1, 1917, 1, 4].
                                                       at org.tensorflow.lite.Tensor.copyTo(Tensor.java:44)
                                                       at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:154)
                                                       at org.tensorflow.demo.TFLiteObjectDetectionAPIModel.recognizeImage(TFLiteObjectDetectionAPIModel.java:222)
                                                       at org.tensorflow.demo.DetectorActivity$3.run(DetectorActivity.java:242)
                                                       at android.os.Handler.handleCallback(Handler.java:761)
                                                       at android.os.Handler.dispatchMessage(Handler.java:98)
                                                       at android.os.Looper.loop(Looper.java:156)
                                                       at android.os.HandlerThread.run(HandlerThread.java:61)

Note : as a checkpoint to train the model I used ssd_mobilenet_v1_coco_2017_11_17 and the only thing I changed in the code is this (TFLiteObjectDetectionAPIModel.java):

  private static final int NUM_CLASSES = 3;

because I only have two objects to detect. Any help or information would much appreciated.

Upvotes: 1

Views: 1317

Answers (1)

Milorenus Lomaliza
Milorenus Lomaliza

Reputation: 221

I also had the same problem but here is how i solved it with a small hack:
in the "TFLiteObjectDetectionAPIModel.java" file create a new variable array:

float [][][][] temp1 = new float[1][NUM_RESULTS][1][4];

and then for your "outputMap" object replace:

outputMap.put(0, outputLocations);

by:

outputMap.put(0, temp1);

This will solve the shape miss-match problem. and also be sure to put the correct number of classes. For example i had only one class but in the .txt file, the first class is listed as "???" and then the second one is my actual class. Hence i had:

private static final int NUM_CLASSES = 2;

even if i only have one class. But those two hacks seem to solve the problem.

P.S: The TFLite version of the fronzen model seems to run even slower than the .pb extension(on my Samsung Galaxy S8 android API 26).

Upvotes: 1

Related Questions