TFLite - Object Detection - Custom Model - Cannot copy to a TensorFlowLite tensorwith * bytes from a Java Buffer with * bytes

Viewed 1529

I'm trying to create a model to recognise drawed shapes but I'm pretty new in Tensorflow and I can't make it work.

I was able to create my model following these steps https://medium.com/over-engineering/building-a-custom-machine-learning-model-on-android-with-tensorflow-lite-26447e53abf2 (it's pretty much the same than the CodeLab tensorflow-for-poets-2) and I got my custom_model.tflite

I tested my model into the Image Classification example ( https://github.com/googlecodelabs/tensorflow-for-poets-2 ) and wohoo! It worked perfectly.

enter image description here

But now I want to use my model in the Object Detection example, so I can know the position of the shapes and boxing them. I downloaded the example from here https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android and pasted my custom_model.tflite into the assets folders.
Also, I changed the attributte TF_OD_API_IS_QUANTIZED to false into 'DetectorActivity' class.

When I run the code I get this error

2020-06-25 22:42:46.594 11515-11591/org.tensorflow.lite.examples.detection E/AndroidRuntime: FATAL EXCEPTION: inference
    Process: org.tensorflow.lite.examples.detection, PID: 11515
    java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (normalized_input_image_tensor) with 270000 bytes from a Java Buffer with 1080000 bytes.
        at org.tensorflow.lite.Tensor.throwIfSrcShapeIsIncompatible(Tensor.java:423)
        at org.tensorflow.lite.Tensor.setTo(Tensor.java:189)
        at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:154)
        at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:343)
        at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.recognizeImage(TFLiteObjectDetectionAPIModel.java:196)
        at org.tensorflow.lite.examples.detection.DetectorActivity$2.run(DetectorActivity.java:181)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.os.HandlerThread.run(HandlerThread.java:67)

I thought maybe the model was the problem so I created a new one from Google Cloud Plataform - Vision API, but I got the same result.

enter image description here

Let me know if you need more info.

1 Answers

This worked for me. Hope it will work for you too.

Try changing below line FROM:

d.imgData = ByteBuffer.allocateDirect(1 * d.inputSize * d.inputSize * 3 * numBytesPerChannel);

TO:

d.imgData = ByteBuffer.allocateDirect(4 * d.inputSize * d.inputSize * 3 * numBytesPerChannel);

Demo app of TFLite-Object-Detection did same.

Related