tensorflow: mobile_ssd_v2_float_coco.tflite model :Cannot copy between a TensorFlowLite tensor with shape [1, 2034, 4] and a Java object with shape [1, 10, 4].
When using mobile_ssd_v2_float_coco.tflite model with Tensorflow Object Detection example code, I am facing this error:
Process: org.tensorflow.lite.examples.detection, PID: 30765 java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [1, 2034, 4] and a Java object with shape [1, 10, 4]. at org.tensorflow.lite.Tensor.throwIfShapeIsIncompatible(Tensor.java:282) at org.tensorflow.lite.Tensor.throwIfDataIsIncompatible(Tensor.java:249) at org.tensorflow.lite.Tensor.copyTo(Tensor.java:141) at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:161) at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:275) at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.recognizeImage(TFLiteObjectDetectionAPIModel.java:220) at org.tensorflow.lite.examples.detection.DetectorActivity$2.run(DetectorActivity.java:197) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:214) at android.os.HandlerThread.run(HandlerThread.java:65)
- I did change the
private static final int TF_OD_API_INPUT_SIZE = 320;`` - Followed this StackOverflow solution https://stackoverflow.com/questions/54423649/tensorflow-lite-gpu-support-on-object-detector This gives only a partial soluton the problem. Can anyone suggest a proper solution.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 36 (10 by maintainers)
@jdduke One more query, The official documentation suggests
mobile_ssd_v2_float_coco.tflitemodel for enabling GPU delegate, but after analyzing the Model with Netron,mobile_ssd_v2_float_coco.tfliteOUTPUT: raw_outputs/box_encodings id: raw_outputs/box_encodings type: float32[1,2034,4] raw_outputs/class_predictions id: raw_outputs/class_predictions type: float32[1,2034,91]Whereas the default model
detect.tfliteused in the demo has 4 different parameters. OUTPUT: TFLite_Detection_PostProcess id: TFLite_Detection_PostProcess type: float32 TFLite_Detection_PostProcess:1 id: TFLite_Detection_PostProcess:1 type: float32 TFLite_Detection_PostProcess:2 id: TFLite_Detection_PostProcess:2 type: float32 TFLite_Detection_PostProcess:3 id: TFLite_Detection_PostProcess:3 type: float32 ie for Location, Classes, Scores, Number and detectionsHow can we use
mobile_ssd_v2_float_coco.tfliteor any model to get enable GPU with Object Detection, Please suggest?So, these models aren’t directly compatible. That is, the model included with the detection sample creates the following output tensors: locations, classes, scores, detections. The
mobile_ssd_v2_float_coco.tflitemodel produces a 1x2034x4 encodings output tensor and a 1x2034x91 class predictions tensor. You’ll need to modify the logic in TFLiteObjectDetectionAPIModel to handle this altered output format. You can use the Netron visualization tool to help identify how the output tensors differ.So if you look at the
output_details, specifically the shape of each output tensor, you will see that the order of outputs is actually[classes, boxes, num_detections, scores]or[scores, boxes, num_detections, classes](because the shape of output at index 3 is same as shape of output 0, so one of them is scores and other is classes - I am not sure which). Right now, its likely that your Java code assumes[boxes, classes, scores, num_detections]- which is why the error says that you are trying to copy a wrong-shaped tensor into another one.You can run the model with some Python code with example images (as in the Colab I sent you) to debug.
Hey @srjoglekar246
Thank you so much. You made my day!
For anyone who looks answer for future.
as advised from @srjoglekar246
// outputLocations: array of shape [Batchsize, NUM_DETECTIONS,4] // contains the location of detected boxes private float[][][] outputLocations; // outputClasses: array of shape [Batchsize, NUM_DETECTIONS] // contains the classes of detected boxes private float[][] outputClasses; // outputScores: array of shape [Batchsize, NUM_DETECTIONS] // contains the scores of detected boxes private float[][] outputScores; // numDetections: array of shape [Batchsize] // contains the number of detected boxes private float[] numDetections;The answer lies here - [numDetections:2, outputScores:0/3, outputLocations:1 outputClasses:3/0]The changes are in indexing of outputMap.put and it works all fine! Object[] inputArray = {imgData}; Map<Integer, Object> outputMap = new HashMap<>(); outputMap.put(1, outputLocations); outputMap.put(3, outputClasses); outputMap.put(0, outputScores); outputMap.put(2, numDetections);
Thank you once again, for swift replies. Will try to communicate this in all stack overflow to help others 😃
God speed.
Feel free to just follow up here, so that its better for me to track 😃
I got the same issue, I set num_classes as 5 to train my custom dataset. but I got the error: java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [1, 10, 4] and a Java object with shape [1, 5, 4]. The 5 is the number of classes, Why is the output tensor 10?
label_map.pbtxt
ssdlite_mobilenet_v3_small_320x320_coco.config
Convert pb to tflite.