我正在尝试将我的自定义训练 SSD mobilenet TF2 对象检测模型转换为 .tflite 格式(flatbuffer),它将与 Raspberry pi 一起使用,我遵循了将我的模型转换为 tflite 模型的官方 tensorflow 教程:
注意:我使用 Colab 和 Tensorflow 2.5-gpu 进行训练,使用 Tensorflow 2.7-nightly 进行转换(提到一些与 SSD 到 tflite 模型转换相关的 Github 问题,使用 nightly 版本)
1-我首先尝试使用export_tflite_ssd_graph.py这些参数导出 tflite 图:
!python object_detection/export_tflite_ssd_graph.py \
--pipeline_config_path models/myssd_mobile/pipeline.config \
--trained_checkpoint_prefix models/myssd_mobile/ckpt-9.index \
--output_directory exported_models/tflite_model
但它显示以下错误:
RuntimeError: tf.placeholder() is not compatible with eager execution.
即使在我通过添加tf.disable_eager_execution()它禁用它之后显示以下错误:
NameError: name 'graph_matcher' is not defined
所以我意识到它可能不是为 tf2 创建的,所以我export_tflite_graph_tf2.py使用下面的代码转换了模型,我得到了保存的模型:
!python object_detection/export_tflite_graph_tf2.py \
--pipeline_config_path models/myssd_mobile/pipeline.config \
--trained_checkpoint_dir models/myssd_mobile \
--output_directory exported_models/tflite_model
2-我使用以下代码将 tflite savedmodel 转换为 .tflite 模型,该代码取自 tensorflow 文档:
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model('exported_models/tflite_model/saved_model')
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
]
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
之后,我手动创建了 tflite labels.txt,它是:
t1
t2
t3
t4
t5
t6
t7
t8
t9
t10
t11
然后我运行了以下脚本:
但它显示了这个错误:
Traceback (most recent call last):
File "TFLite_detection_image.py", line 157, in <module>
for i in range(len(scores)):
TypeError: object of type 'numpy.float32' has no len()
哪里错了?
提前致谢