【问题标题】:Adding metdata to tensorflow lite file将元数据添加到 tensorflow lite 文件
【发布时间】:2020-11-28 08:10:51
【问题描述】:

我重新训练了一个 MobileNet 模型,对模型进行了调整,并在 TensorFlow 中自定义了输出。我必须使用 Google ML KIT 在 Android 上运行模型,但问题是它需要元数据。但是每当我完成他的过程时,它都会给我一个错误:

ValueError:文件“/content/drive/My Drive/labels.txt”记录在元数据中,但尚未加载到填充器中。

这是我添加元数据的代码:

model_metadata=_metadata_fb.ModelMetadataT()
model_metadata.name="MobileNet_with_Metadata"
model_metadata.description="This model is trained on plant village leaf disease dataset so that it can be used for detectiong crop diseases"
model_metadata.version="v1.0.0.0"
model_metadata.author="open-source"
model_metadata.license=("Apache License. Version 2.0 "
                      "http://www.apache.org/licenses/LICENSE-2.0.")
input_metadata=_metadata_fb.TensorMetadataT()
output_metadata=_metadata_fb.TensorMetadataT()
input_metadata.name="image"
input_metadata.description="input_meta.description = (
    "Input image to be classified. The expected image is {0} x {1}, with "
    "three channels (red, blue, and green) per pixel. Each value in the "
    "tensor is a single byte between 0 and 1.".format(256, 256))"
input_normalization = _metadata_fb.ProcessUnitT()
input_normalization.optionsType = (
    _metadata_fb.ProcessUnitOptions.NormalizationOptions)
input_normalization.options = _metadata_fb.NormalizationOptionsT()
input_normalization.options.mean = [127.5]
input_normalization.options.std = [127.5]

input_metadata.processUnits = [input_normalization]
input_stats = _metadata_fb.StatsT()
input_stats.max = [255]
input_stats.min = [0]
input_metadata.stats = input_stats
output_metadata.name="Probability"
output_metadata.description="Probabbility of 50 classes"
output_stats = _metadata_fb.StatsT()
output_stats.max = [1.0]
output_stats.min = [0.0]
output_metadata.stats = output_stats
label_file = _metadata_fb.AssociatedFileT()
label_file.name = "/content/drive/My Drive/labels.txt"
label_file.description = "Labels for objects that the model can recognize."
label_file.type = _metadata_fb.AssociatedFileType.TENSOR_AXIS_LABELS
output_metadata.associatedFiles = [label_file]
subgraph = _metadata_fb.SubGraphMetadataT()
subgraph.inputTensorMetadata = [input_metadata]
subgraph.outputTensorMetadata = [output_metadata]
model_metadata.subgraphMetadata = [subgraph]

b = flatbuffers.Builder(0)
b.Finish(
    model_metadata.Pack(b),
    _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER)
metadata_buf = b.Output()
populator = _metadata.MetadataPopulator.with_model_file("/content/drive/My Drive/MobileNet_Model_latest.tflite")
populator.load_metadata_buffer(metadata_buf)
populator.load_associated_files(["/content/drive/My Drive/labels.txt"])
populator.populate()

我是第一次这样做,我无法获得任何适当的帮助或文档。另外,谁能正确告诉我如何将元数据添加到 tflite 模型?

我已经参考了这个链接:Add Metadata to TensorFlow

【问题讨论】:

    标签: tensorflow keras metadata tensorflow-lite


    【解决方案1】:

    我之前在尝试将 TF2 对象检测模型部署到 mlkit 视觉样本时遇到过这个问题。如果我没记错的话,它试图从 tflite 元数据中读取标签,而不是资产文件夹中的标签映射。

    这个元数据问题在 github 上存在问题。正如其他人指出的那样,它似乎尚未修复。有几种方法可以解决它。看看这些问题,

    https://github.com/tensorflow/models/issues/9341

    https://github.com/tensorflow/tensorflow/issues/43583

    编辑:下面显示的方式对于添加元数据输出不正确。正确的方法应该在上面的链接中。

    要向对象检测模型添加元数据,请查看这些,

    https://nbviewer.jupyter.org/github/quickgrid/CodeLab/blob/master/tensorflow/TFlite_Object_Detection_Custom_Model_Export_With_Metadata_TF1.ipynb

    https://nbviewer.jupyter.org/github/quickgrid/CodeLab/blob/master/tensorflow/TFlite_Object_Detection_Custom_Model_Export_With_Metadata_TF2.ipynb

    【讨论】:

    • 抱歉回复晚了,对我帮助很大,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多