简体   繁体   English

如何从 tflite model 中提取元数据

[英]How to extract metadata from tflite model

I'm loading this object detection model in python.我在 python 中加载这个object 检测 model。 I can load it with the following lines of code:我可以使用以下代码行加载它:

import tflite_runtime.interpreter as tflite

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

I'm able to perform inferences on this without any problem.我可以毫无问题地对此进行推断。 However, labels are suposed to be included in the metadata, according to model's documentation , but I can't extract it.但是,根据模型的文档,标签应该包含在元数据中,但我无法提取它。

The closest I was, it was when following 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)

but the very first line of code, fails with this error: {AtributeError}'int' object has no attribute 'tobytes' .但是第一行代码失败并出现以下错误: {AtributeError}'int' object has no attribute 'tobytes'

How to extract it?如何提取它?

If you only care about the label file, you can simply run command like unzip model_path on Linux or Mac.如果您只关心 label 文件,您可以简单地在 Linux 或 Mac 上运行类似unzip model_path的命令。 TFLite model with metadata is essentially a zip file.带有元数据的 TFLite model 本质上是一个 zip 文件。 See the public introduction for more details.更多详情见公开介绍

You code snippet to extract metadata works on my end.您提取元数据的代码片段对我有用。 Make sure to double check model_path .确保仔细检查model_path It should be a string, such as "lite-model_ssd_mobilenet_v1_1_metadata_2.tflite" .它应该是一个字符串,例如"lite-model_ssd_mobilenet_v1_1_metadata_2.tflite"

If you'd like to read label files in an Android app, here is the sample code to do so.如果您想在 Android 应用程序中阅读 label 文件, 这里是示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM