Object Detection Android App creates error with model from tflite-model-maker (it had worked for many weeks a until a few weeks ago)

I can verify that the changes to the Object Detection with TensorFlow Model Maker do work.

  • Able to train using VOC label files
  • Able to evaluate the resultant model
  • Able to convert model to TFLite
  • Able to evaluate TFLite model

However when I tried to test the model using a URL. I got an error about cv2 unable to import…
so did a import

  • !pip install “opencv-python-headless<4.3”

ran the last 2 cells and it worked…

Thanks @Winton_Cape and @khanhlvg for replying to help clear up my confusion.

I think I’ve got it all sussed in my head now and might lay it out here for anyone that stumbles across this thread later on trying to fix the Salad Detector example Android app crashing with custom trained models. This information is relevant for anyone doing this after Nov 2021.

This is the tutorial to follow if you are looking to build an Android app using a transfer learned model - known as a the Salad Detector tutorial.

The Android example apps have been updated in Jan 2022 to work successfully with models trained on TensorFlow 2.6 and Model Maker 0.3.4.

The only problem with this tutorial is it doesn’t link to the correct Colab file for transfer learning on Step 7: Train a custom object detection model. DO NOT follow the links to the Colab on this step.

The correct Colab to train a working tflite model is the updated one located here.

They look very similar but have a few changes that make it work and not crash the app. There are also a few variations of the Android example app floating around out there as well. You want to use the on in this step of the tutorial here.

Hopefully all this information is correct as of Feb 2022.

Tutorial to follow for building Android App with Salad Detector

Step with the updated Android example app

Step with non-working Colab for training your own model

Correct Colab/Tutorial for training your own model

Its 2022 - and I get the same problem (deploying with android lead to error of

java.lang.IllegalArgumentException: Error occurred when initializing ObjectDetector: Output tensor at index 0 is expected to have 3 dimensions, found 2.

with the model that is quantized using

config = QuantizationConfig.for_int8(test_data, inference_input_type = tf.uint8, inference_output_type = tf.uint8)

here is the notebook Google Colab

I hope I should not go back to TF 2.5 !

Same here…are you able to resolve it?
float16 has no issue, the issue is only with uint8 quantization.
Thanks!

If you’re using Model Maker to train your tflite model I’d use the nightly build.

pip install -q tflite-model-maker-nightly

The android dependency for the example apps was updated to incorporate output changes from TF 2.6 onwards.

This post here clarifies it all a bit better.

Basically use nightly to build your model and update your dependancies to 0.3.1 and above (I’m using 0.4.0)

    implementation 'org.tensorflow:tensorflow-lite-task-vision:0.4.0'

Greetings, everyone. I am new to TensorFlow and I am currently developing a real-time object detection application. I have trained an object detection model on my custom dataset using tflite model maker, but when I try to use the model in my app, the following error occurred.

Caused by: java.lang.IllegalArgumentException: Cannot copy from a TensorFlowLite tensor (StatefulPartitionedCall:1) with shape [1, 25] to a Java object with shape [1, 25, 4].

May I know if is it caused by the output order? As I used the Netron to visualize my model and found that the bounding box output order of my model is different from the sample model (which works fine in my app).

The screenshot of my model’s outputs and the sample model’s outputs are in this google drive:
https://drive.google.com/drive/folders/1pUR5WVkB2G5AP9op_b09qD9wv9Y7dmS0?usp=sharing

Please help me, thank you!!! :pray:

Greetings Everyone. I am also working on object detection. I trained my model from Teachable Machine. I am getting same error. Did anyone error got resolved because I have been trying to for days but nothing is working.