在Android中报错:
Input tensor has type kTfLiteFloat32: it requires specifying NormalizationOptions metadata to prepro
1.首先安装tflite_support:
pip install tflite_support
2.然后将自己的tflite文件重新转换一下:
from tflite_support.metadata_writers import object_detector
from tflite_support.metadata_writers import writer_utils
from tflite_support import metadata
ObjectDetectorWriter = object_detector.MetadataWriter
# _MODEL_PATH = "yolo_without_metadata.tflite"
_MODEL_PATH = "/home/gyx/QR/qr_detect_model/convert_model/tflite/ssd_mobilenet_v2_quantized_300x300_uint8.tflite"
# Task Library expects label files that are in the same format as the one below.
_LABEL_FILE = "/home/gyx/QR/qr_detect_model/data_all/label_map.pbtxt"
# _SAVE_TO_PATH = "yolo_with_metadata.tflite"
_SAVE_TO_PATH = "/home/gyx/QR/qr_detect_model/convert_model/tflite/ssd_mobilenet_v2_quantized_300x300_uint8_metadata.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)
将转换好的文件放入Android中就不会报错了!