如何从 tflite 模型中提取元数据

How to extract metadata from tflite model

我正在 python 中加载 this 对象检测模型。我可以使用以下代码行加载它:

import tflite_runtime.interpreter as tflite

model_path = 'path_to_model_file.tf'
interpreter = tflite.Interpreter(model_path)

我可以毫无问题地对此进行推理。但是,根据 model's documentation,标签应该包含在元数据中,但我无法提取它。

离我最近的是在关注this:

的时候
from tflite_support import metadata as _metadata

displayer = _metadata.MetadataDisplayer.with_model_file(model_path)
export_json_file = "extracted_metadata.json")
json_file = displayer.get_metadata_json()

# Optional: write out the metadata as a json file
with open(export_json_file, "w") as f:
  f.write(json_file)

但是第一行代码失败并出现此错误:{AtributeError}'int' object has no attribute 'tobytes'.

如何提取?

如果您只关心标签文件,您可以在Linux 或Mac 上简单地运行 命令,如unzip model_path。带有元数据的 TFLite 模型本质上是一个 zip 文件。有关详细信息,请参阅 public introduction

你提取元数据的代码片段对我有效。确保仔细检查 model_path。应该是字符串,比如"lite-model_ssd_mobilenet_v1_1_metadata_2.tflite".

如果您想在 Android 应用程序中读取标签文件,here 是执行此操作的示例代码。