【问题标题】:Convert Custom Tensorflow 2 SSD mobilenet model to Tensorflow Lite Flatbuffer将 Custom Tensorflow 2 SSD mobilenet 模型转换为 Tensorflow Lite Flatbuffer
【发布时间】:2021-08-29 23:31:12
【问题描述】:

我正在尝试将我经过自定义训练的 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

然后我运行了以下脚本:

TFLite_detection_image.py

但它显示此错误:

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()

哪里错了?

提前致谢

【问题讨论】:

    标签: python numpy tensorflow object-detection tensorflow-lite


    【解决方案1】:

    我在 SSD MobileNet v2 320x320 和 SSD MobileNet V2 FPNLite 640x640 上都遇到了同样的问题。所以我发现它不应该与模型本身有关。 今天早上我刚刚通过再次生成所有需要的文件来修复这个错误:您将数据拆分为训练/测试(和验证); Tensorflow 的那些像 train.record, test.record ... 我还检查了我使用的标签图,以确保它包含我所有的类。

    经过预处理后,我使用 export_tflite_graph_tf2.py 导出了我的模型,然后使用了以下 TF Lite 转换器:

    import tensorflow as tf
    import argparse
    # Define model and output directory arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--model', help='Folder that the saved model is located in',
                        default='exported-models/my_tflite_model/saved_model')
    parser.add_argument('--output', help='Folder that the tflite model will be written to',
                        default='exported-models/my_tflite_model')
    args = parser.parse_args()
    
    converter = tf.lite.TFLiteConverter.from_saved_model(args.model)
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
    converter.experimental_new_converter = True
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
    
    tflite_model = converter.convert()
    
    output = args.output + '/model.tflite'
    with tf.io.gfile.GFile(output, 'wb') as f:
      f.write(tflite_model)
    

    注意:当我使用 TF Lite Nighty 时,我也遇到了错误,所以我只是将此脚本与 TF2 一起使用。

    之后,我可以确认这两个模型都在 Raspberry Pi 4B+ 上运行,其精度/召回分数与我在 GPU 上获得的分数相同。

    【讨论】:

    • 您能否提供用于在 Raspberry pi 4 上运行 tflite 模型的教程或代码?我怀疑我的代码是问题
    • 我已经在上面加入了转换成TF Lite的代码!
    • 如果你能给我你用来运行模型而不是转换的代码
    • 我遵循了相同的教程,所以这不是问题。另外,我在这里找到了另一个教程:github.com/armaanpriyadarshan/… 我可以确认它们非常相似,在你遇到错误的地方都是相同的 for 循环
    【解决方案2】:

    我遇到了同样的错误TypeError: object of type 'numpy.float32' has no len(),解决方案是在 python 代码中更改分数、框和类的索引,因为这意味着分数是一个标量值而不是一个数组。请参考这个answer

    顺便说一句,这个错误发生在我在 Jetson Nano 上使用 tflite-runtime 时,但是当我在 Raspberry PI 上的 TF2.5 上运行 code 时,它运行时没有改变任何东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-03
      • 2019-05-14
      • 1970-01-01
      • 1970-01-01
      • 2022-12-07
      • 2019-04-14
      相关资源
      最近更新 更多