【问题标题】:How to add metadata to a tensorflow lite file converted from a tensorflow file如何将元数据添加到从张量流文件转换的张量流精简版文件中
【发布时间】:2021-04-09 10:24:24
【问题描述】:

我正在尝试将 tensorflow lite 文件添加到 Android Studio,但是当我添加它时,我收到一条错误消息,提示找不到元数据。我的 tensorflow 模型在我的笔记本电脑上远程工作,准确率达到 70%。我使用以下代码从 tensorflow 转换为 tensorflow lite。

import tensorflow as tf

model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(units=1, input_shape=[1]),
    tf.keras.layers.Dense(units=16, activation='relu'),
    tf.keras.layers.Dense(units=1)
])
model.compile(optimizer='sgd', loss='mean_squared_error') # compile the model
model.fit(x=[-1, 0, 1], y=[-3, -1, 1], epochs=5) # train the model

converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()


with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

Android Studio 上的错误消息 - “未找到元数据” 它提供了以下链接以添加元数据 https://www.tensorflow.org/lite/convert/metadata 我无法让给定的脚本工作

谁能向我解释我将如何调整此脚本以适应我的模型或任何替代解决方案?我花了数周时间寻找解决方案,但没有找到任何帮助,我们将不胜感激。

【问题讨论】:

    标签: android-studio tensorflow tensorflow-lite


    【解决方案1】:

    当需要使用 tflite 支持库时需要元数据。由于您已经有了转换后的 tflite 模型,您可以直接使用 tflite 解释器 API 来运行转换后的模型。请参考 tflite 安卓页面。 https://www.tensorflow.org/lite/guide/android

    【讨论】:

    • 您好,Jae,感谢您的回复。我刚刚查看了您提供的链接,并添加了链接中所示的 tflite 文件,但我收到错误消息说没有元数据。我应该提到 tensorflow 模型是用 python 训练的,所以我不确定如何调用 API?我只能看到 C 代码的示例,任何建议将不胜感激
    • 消息“未显示元数据”不是错误。它只是说它没有元数据。当您不依赖 TFLite 支持库时,您可以忽略它。有一个适用于 android 的 Java API:tensorflow.org/lite/guide/…
    • 您好 Jae,感谢您的回复。我不确定我是否需要 TFLite 支持库?我正在实现一个狗品种识别功能,该功能从用户相机胶卷中获取图像并将其添加到 firebase,然后应用程序将在该图像上运行 tflite 文件并给出结果。这是我第一次使用神经网络,所以如果你能就如何实现它提供任何建议,我将不胜感激。谢谢
    • 嗨,Jae,谢谢您的建议。我已经在笔记本电脑上本地训练和测试了我的 TensorFlow 模型,准确率达到 70%,因此第一个链接没有提供任何进一步的指导。在第二个链接中,我发现很难使其适应我自己的模型。我根本没有使用相机,用户从他们的画廊上传,还有很多我不知道如何实现的空白方法。您能解释一下我如何确定我的模型是否需要元数据吗?我不确定 TFLite 支持库的用途是什么
    猜你喜欢
    • 2019-10-02
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2022-01-06
    • 2018-07-19
    • 2017-11-05
    • 2020-12-21
    • 1970-01-01
    相关资源
    最近更新 更多