首页 > 解决方案 > 使用 Quantized MobilenetV1 Tf-lite 预测期间不同图像的相同输出

问题描述

我正在关注https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/micro/examples/person_detection/training_a_model.md上的示例

我有不同的输入图像;但是,当使用相同的 tflite 模型/文件进行预测时,输出总是相同的。我的代码示例和输出如下。

我错过了什么?

Tflite 文件量化为:

converter = tf.lite.TFLiteConverter.from_frozen_graph('vww_96_grayscale_frozen.pb', ['input'], ['MobilenetV1/Predictions/Reshape_1'])
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset_gen
tflite_quant_model = converter.convert()

推理代码:

interpreter = tf.lite.Interpreter(model_path=TFLITE_MODEL)
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.allocate_tensors()

for cnt in range(5):
    image_path = "/data/0-val_img-"+str(cnt)+".png"
    image_data = PIL.Image.open(image_path).resize((96, 96)).convert('L')    

    #plt.imshow(image_data)  # shows different images, as expected
    #plt.show()

    array = np.array(image_data).astype(np.uint8)
    array = ((array / 127.5) - 1.0).astype(np.float32)
    array = np.expand_dims(array, axis=2)
    array = np.expand_dims(array, axis=0)

 
    # set input data
    interpreter.set_tensor(input_details[0]['index'], array)
    interpreter.invoke()
    # get output
    output_data = interpreter.get_tensor(output_details[0]['index'])
    print(output_data)

输出

[[0.91796875 0.08203125]]
[[0.91796875 0.08203125]]
[[0.91796875 0.08203125]]
[[0.91796875 0.08203125]]
[[0.91796875 0.08203125]]

标签: pythontensorflowtensorflow-lite

解决方案


我认为问题是我训练中的时期数少。我用 200000 个 epoch 重新训练了网络。所有图像都按预期生成不同的概率值。


推荐阅读