android .tflite模型报错 No metadata found in this model

Posted 行码阁119

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android .tflite模型报错 No metadata found in this model相关的知识,希望对你有一定的参考价值。

 参考官方历程即可:TensorFlow Lite Metadata Writer API

可能需要vpn打开。

官方例程分为了很多部分:

每部分解决方式不一样,找到自己对应的版本即可,我是Object detectors:

首先在环境下安装:

(base) C:\\Users\\Administrator>pip install tflite-support-nightly

 然后利用下面代码:

from tflite_support.metadata_writers import image_classifier
from tflite_support.metadata_writers import writer_utils

from tflite_support.metadata_writers import object_detector
from tflite_support.metadata_writers import writer_utils

ObjectDetectorWriter = object_detector.MetadataWriter
_MODEL_PATH = "E:/aa/2/android/app\\src/main/assets/model_tflite.tflite"
# Task Library expects label files that are in the same format as the one below.
_LABEL_FILE = "E:/aa/2/android/app\\src/main/assets/label.txt"
_SAVE_TO_PATH = "E:/aa/2/android/app\\src/main/assets/model_tflite_meta.tflite"
# Normalization parameters is required when reprocessing the image. It is
# optional if the image pixel values are in range of [0, 255] and the input
# tensor is quantized to uint8. See the introduction for normalization and
# quantization parameters below for more details.
# https://www.tensorflow.org/lite/convert/metadata#normalization_and_quantization_parameters)
_INPUT_NORM_MEAN = 127.5
_INPUT_NORM_STD = 127.5

# Create the metadata writer.
writer = ObjectDetectorWriter.create_for_inference(
    writer_utils.load_file(_MODEL_PATH), [_INPUT_NORM_MEAN], [_INPUT_NORM_STD],
    [_LABEL_FILE])

# Verify the metadata generated by metadata writer.
print(writer.get_metadata_json())

# Populate the metadata into the model.
writer_utils.save_file(writer.populate(), _SAVE_TO_PATH)

只需要将下述代码代码修改自己文件对应(我是自己模型的.tflite):

结果如下,即成功:

 回到

android打开文件: 

 

以上是关于android .tflite模型报错 No metadata found in this model的主要内容,如果未能解决你的问题,请参考以下文章

在android中运行TFLite模型[字节缓冲区的大小和形状不匹配]

无法在 android studio 的 Interpreter 上运行 tflite 模型

如何从 tflite 模型输出形状 [1, 28, 28,1] 的数组作为 android 中的图像

带有来自 tflite Model-maker 的模型的错误 Android 对象检测应用程序

tflite 模型的准确度、精确度和召回率

Tf Lite 模型图像分类打印标签