xuannianz / EfficientDet

EfficientDet (Scalable and Efficient Object Detection) implementation in Keras and Tensorflow
Apache License 2.0
1.39k stars 395 forks source link

Unable to quantize the model to tflite #135

Closed alexiniestags closed 4 years ago

alexiniestags commented 4 years ago

Hello, First of all, thank you for the amazing work made in this repository. I have tried through multiple ways to export the model to TFLite, and the one that works to me was the following:

phi=0
_, prediction_model, = efficientdet(phi=phi, weighted_bifpn=True, num_classes=90)
prediction_model.load_weights('./weights/efficientdet-d{}.h5'.format(phi), by_name=True)
converter = tf2.lite.TFLiteConverter.from_keras_model(prediction_model)
converter.allow_custom_ops = True
converter.target_spec.supported_ops = [tf2.lite.OpsSet.TFLITE_BUILTINS_INT8, tf2.lite.OpsSet.TFLITE_BUILTINS,tf2.lite.OpsSet.SELECT_TF_OPS]
converter.optimizations = [tf2.lite.Optimize.DEFAULT]
converter.inference_type = tf2.quint8
converter.inference_input_type = tf2.quint8
tflite_model = converter.convert()

The thing is that, as you can see in the code, I have enabled the flags to quantize the model and so on, but the input type is still float32. I have also tried the tflite model on a mobile phone with the TFLite Object Detection App and it works, but it takes more than a second per image (SD845), and the input that it receives is float32, where with a MobileNetV2-SSD quantized takes around 50ms. Any idea of how to quantize the model? I have also tried to export it as a frozen graph and the result is the same. Note that the converted used is the tf2 one. I am using tensorflow 2.1.0 to export it.

Thanks in advance!

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ankitchandracs commented 3 years ago

Is there any update after this? Where did you put this code? How did you use the exported model for inference?