参考官方历程即可: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打开文件: