mvoelk / ssd_detectors

SSD-based object and text detection with Keras, SSD, DSOD, TextBoxes, SegLink, TextBoxes++, CRNN
MIT License
302 stars 85 forks source link

How to convert to tflite? #51

Open weian312 opened 4 years ago

weian312 commented 4 years ago

Hello, I'm trying to convert your model in TBPP_end2end_predict_GPUonly.ipynb to tflite format for mobile application. This is my environment

Python           3.6.9
Notebook         5.3.1
NumPy            1.18.5
Pandas           1.0.5
Matplotlib       3.2.2
OpenCV           4.1.2
TensorFlow       2.3.0
Keras            2.4.0
tqdm             4.41.1
imageio          2.4.1

Here's my code after the concatenate end2end model.

import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

open("convert.tflite", "wb").write(tflite_model)

And I got this error:

Exception                                 Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/convert.py in toco_convert_protos(model_flags_str, toco_flags_str, input_data_str, debug_info_str, enable_mlir_converter)
    198                                                  debug_info_str,
--> 199                                                  enable_mlir_converter)
    200       return model_str

5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/wrap_toco.py in wrapped_toco_convert(model_flags_str, toco_flags_str, input_data_str, debug_info_str, enable_mlir_converter)
     37       debug_info_str,
---> 38       enable_mlir_converter)
     39 

Exception: /usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py:507:0: error: requires element_shape to be 1D tensor during TF Lite transformation pass
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py:574:0: note: called from
/content/drive/.shortcut-targets-by-id/1U-eNJ9b4Rq8kRh8RQ-t88VQTH1HUMEmA/ssd_detectors/tbpp_layers.py:209:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py:302:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:508:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:386:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saving_utils.py:134:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py:600:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py:507:0: note: see current operation: %1382 = "tf.TensorListReserve"(%249, %1381) {device = ""} : (tensor<i32>, tensor<i32>) -> tensor<!tf.variant<tensor<*xf32>>>
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py:507:0: error: failed to legalize operation 'tf.TensorListReserve' that was explicitly marked illegal
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py:574:0: note: called from
/content/drive/.shortcut-targets-by-id/1U-eNJ9b4Rq8kRh8RQ-t88VQTH1HUMEmA/ssd_detectors/tbpp_layers.py:209:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py:302:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:508:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:386:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saving_utils.py:134:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py:600:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py:507:0: note: see current operation: %1382 = "tf.TensorListReserve"(%249, %1381) {device = ""} : (tensor<i32>, tensor<i32>) -> tensor<!tf.variant<tensor<*xf32>>>

During handling of the above exception, another exception occurred:

ConverterError                            Traceback (most recent call last)
<ipython-input-9-ba886ac57a78> in <module>()
      4 # converter.experimental_new_converter = True
      5 # converter.target_spec.supported_ops =[tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
----> 6 tflite_model = converter.convert()
      7 
      8 open("convert.tflite", "wb").write(tflite_model)

/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in convert(self)
    829 
    830     return super(TFLiteKerasModelConverterV2,
--> 831                  self).convert(graph_def, input_tensors, output_tensors)
    832 
    833 

/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in convert(self, graph_def, input_tensors, output_tensors)
    631         input_tensors=input_tensors,
    632         output_tensors=output_tensors,
--> 633         **converter_kwargs)
    634 
    635     calibrate_and_quantize, flags = quant_mode.quantizer_flags(

/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/convert.py in toco_convert_impl(input_data, input_tensors, output_tensors, enable_mlir_converter, *args, **kwargs)
    572       input_data.SerializeToString(),
    573       debug_info_str=debug_info_str,
--> 574       enable_mlir_converter=enable_mlir_converter)
    575   return data
    576 

/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/convert.py in toco_convert_protos(model_flags_str, toco_flags_str, input_data_str, debug_info_str, enable_mlir_converter)
    200       return model_str
    201     except Exception as e:
--> 202       raise ConverterError(str(e))
    203 
    204   if distutils.spawn.find_executable(_toco_from_proto_bin) is None:

ConverterError: /usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py:507:0: error: requires element_shape to be 1D tensor during TF Lite transformation pass
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py:574:0: note: called from
/content/drive/.shortcut-targets-by-id/1U-eNJ9b4Rq8kRh8RQ-t88VQTH1HUMEmA/ssd_detectors/tbpp_layers.py:209:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py:302:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:508:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:386:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saving_utils.py:134:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py:600:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py:507:0: note: see current operation: %1382 = "tf.TensorListReserve"(%249, %1381) {device = ""} : (tensor<i32>, tensor<i32>) -> tensor<!tf.variant<tensor<*xf32>>>
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py:507:0: error: failed to legalize operation 'tf.TensorListReserve' that was explicitly marked illegal
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py:574:0: note: called from
/content/drive/.shortcut-targets-by-id/1U-eNJ9b4Rq8kRh8RQ-t88VQTH1HUMEmA/ssd_detectors/tbpp_layers.py:209:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py:302:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:508:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:386:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saving_utils.py:134:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py:600:0: note: called from
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py:507:0: note: see current operation: %1382 = "tf.TensorListReserve"(%249, %1381) {device = ""} : (tensor<i32>, tensor<i32>) -> tensor<!tf.variant<tensor<*xf32>>>

Hope you can help me solve this problem, thanks a lot!