用于对象检测的 TensorFlowLite 模型在加载时抛出 ValueError

TensorFlowLite model for Object Detection throws ValueError on Loading

我对 tensorflow 没有经验。 我正在使用 tensorflow 2.3.0 创建带有自定义数据集的对象检测模型。我使用的模型是“ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8”。 加载经过训练的检查点,我可以用它来检测图像中的对象,效果很好。

现在我使用以下命令使用 SavedModel 格式保存模型: python export_tflite_graph_tf2.py --trained_checkpoint_dir training --output_directory inference_graph_tflite --pipeline_config_path training/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8.config

然后我使用以下代码将模型转换为 TFLite 格式:

converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8,
                                       tf.lite.OpsSet.TFLITE_BUILTINS]
tflite_model = converter.convert()

with open(path + 'final_detector.tflite', 'wb') as f:
  f.write(tflite_model)

此代码来自 tensorflow 文档。 (https://www.tensorflow.org/api_docs/python/tf/lite/TFLiteConverter)

如果我尝试解释这个模型,我会得到这个错误:

  File "...\python\interpreter.py", line 197, in __init__
    _interpreter_wrapper.CreateWrapperFromFile(
ValueError: Did not get operators, tensors, or buffers in subgraph 0.

我做错了什么?

能否使用netron等工具检查图表是否正确转换?

我终于找到了所有问题的根源。虽然我只是遵循了 tensorflow 文档,但在使用 tensorflow 2.3.0 时需要将 tflite 降级到 2.2.0 版本。重建 tflite 模型有效。