abdelrahman-gaber / tf2-object-detection-api-tutorial

Tensorflow 2 Object Detection API Tutorial. This tutorial will take you from installation, to running pre-trained detection model, and training your model with a custom dataset, then exporting it for inference.
MIT License
114 stars 52 forks source link

Error while converting model to tensorflow lite #1

Closed hahmad2008 closed 4 years ago

hahmad2008 commented 4 years ago

Thank you, Abdelrahman, so much for the well-organized tutorial, I really spent time searching for good tutorials for object detection and up to date.

I had successfully train and test the model as you explained, but when I tried to convert it to tensorflow lite, I got the following error: Could you please help me?

tflite_convert \ --saved_model_dir=models/ssd_mobilenet_v2_raccoon/exported_model/saved_model \ --output_file=models/ssd_mobilenet_v2_raccoon/ssd_mobilenet_v2_raccoon.tflite

I got this error:

2020-08-05 10:19:30.217661: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:799] constant_folding: Graph size after: 1394 nodes (0), 2116 edges (0), time = 77.785ms. Traceback (most recent call last): File "/opt/anaconda3/envs/v_python3.6/bin/tflite_convert", line 8, in sys.exit(main()) File "/opt/anaconda3/envs/v_python3.6/lib/python3.6/site-packages/tensorflow/lite/python/tflite_convert.py", line 638, in main app.run(main=run_main, argv=sys.argv[:1]) File "/opt/anaconda3/envs/v_python3.6/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 40, in run _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef) File "/opt/anaconda3/envs/v_python3.6/lib/python3.6/site-packages/absl/app.py", line 299, in run _run_main(main, args) File "/opt/anaconda3/envs/v_python3.6/lib/python3.6/site-packages/absl/app.py", line 250, in _run_main sys.exit(main(argv)) File "/opt/anaconda3/envs/v_python3.6/lib/python3.6/site-packages/tensorflow/lite/python/tflite_convert.py", line 621, in run_main _convert_tf2_model(tflite_flags) File "/opt/anaconda3/envs/v_python3.6/lib/python3.6/site-packages/tensorflow/lite/python/tflite_convert.py", line 237, in _convert_tf2_model tflite_model = converter.convert() File "/opt/anaconda3/envs/v_python3.6/lib/python3.6/site-packages/tensorflow/lite/python/lite.py", line 483, in convert _get_tensor_name(tensor), shape_list)) ValueError: None is only supported in the 1st dimension. Tensor 'input_tensor' has invalid shape '[1, None, None, 3]'.

abdelrahman-gaber commented 4 years ago

Hi @hahmad2008 .. I did not try tf_lite with TF2 before, but will look at it this weekend. Meanwhile, can you check this solution: https://github.com/tensorflow/tensorflow/issues/22564#issuecomment-425515540

hahmad2008 commented 4 years ago

@abdelrahman-gaber Thank you! I tried to convert the checkpoint (saved_model) using this solution, it is successfully converted but I think it is incorrect since I checked the input for the model I got it as [1,1,1,3] instead of [1,300,300,3]

I use this directory image

hahmad2008 commented 4 years ago

what type of model you generated in the exported_model/saved_model is it a saved or frozen model?

abdelrahman-gaber commented 4 years ago

In the exported_model/saved_model, it is saved models. It is the same as all models in the model zoo. I think we should use the saved model in TF2 instead of frozen graphs. refer to this thread for more information. But there are some ways to freeze the model as mentioned in the thread, and also here.

hahmad2008 commented 4 years ago

Thank you @abdelrahman-gaber have you had a chance to convert the model to TensorFlow lite?

abdelrahman-gaber commented 4 years ago

@hahmad2008 Can you please try this. This solution is working on colab with the newest tensorflow nightly version, but not working on my machine with Tensoflow 2.2.0 and giving me errors. Please confirm if the output model is working with you, and then we can check the TF version which works best.

hahmad2008 commented 4 years ago

Thank you @abdelrahman-gaber I also tried this solution it works as well.

But I am trying to test the tflite model. could u please check that?

abdelrahman-gaber commented 4 years ago

@hahmad2008 Great. It is basically the same solution I tried in the colab, but it will need tensorlfow version higher than 2.2.0 Anyways, I will check exactly what is the minimum version required and let you know about that.

To test the tflie model, you should use the tflite interpreter .. I will try to make some instructions for tflite conversion and inference in the tutorial, I think it maybe helpful.

hahmad2008 commented 4 years ago

Thanks, @abdelrahman-gaber . No need. I tried the following and it works :D

image

abdelrahman-gaber commented 4 years ago

Great! So maybe you can share your complete solution here for anyone who will face this issue in the future 🙏

hahmad2008 commented 4 years ago

@abdelrahman-gaber Ya sure!

hahmad2008 commented 4 years ago

For testing TFLITE model on images check this code: LINK