使用 Quantized MobilenetV1 Tf-lite 预测期间不同图像的相同输出
Same output for different images during prediction with Quantized MobilenetV1 Tf-lite
中的示例进行操作
我有不同的输入图像;然而,当使用相同的 tflite model/file 进行预测时,输出总是相同的。我的代码示例和输出如下。
我错过了什么?
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]]
我认为问题出在我训练中的 epoch 数量少。我用 200000 个纪元重新训练了网络。所有图像都按预期生成不同的概率值。
我有不同的输入图像;然而,当使用相同的 tflite model/file 进行预测时,输出总是相同的。我的代码示例和输出如下。
我错过了什么?
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]]
我认为问题出在我训练中的 epoch 数量少。我用 200000 个纪元重新训练了网络。所有图像都按预期生成不同的概率值。