简体   繁体   English

在TensorFlow Lite中运行Keras模型时的不同预测

[英]Different predictions when running Keras model in TensorFlow Lite

Trying out TensorFlow Lite with a pretrained Keras image classifier, I'm getting worse predictions after converting the H5 to the tflite format. 使用预训练的Keras图像分类器尝试TensorFlow Lite,将H5转换为tflite格式后,我得到的预测更糟。 Is this intended behaviour (eg weight quantization), a bug or am I forgetting something when using the interpreter? 这是预期的行为(例如权重量化),错误还是在使用解释器时忘记了某些东西?

Example

from imagesoup import ImageSoup
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
from tensorflow.keras.preprocessing.image import load_img, img_to_array

# Load an example image.
ImageSoup().search('terrier', n_images=1)[0].to_file('image.jpg')
i = load_img('image.jpg', target_size=(224, 224))
x = img_to_array(i)
x = x[None, ...]
x = preprocess_input(x)

# Classify image with Keras.
model = ResNet50()
y = model.predict(x)
print("Keras:", decode_predictions(y))

# Convert Keras model to TensorFlow Lite.
model.save(f'{model.name}.h5')
converter = tf.contrib.lite.TocoConverter.from_keras_model_file
tflite_model = converter(f'{model.name}.h5').convert()
with open(f'{model.name}.tflite', 'wb') as f:
    f.write(tflite_model)

# Classify image with TensorFlow Lite.
f = tf.contrib.lite.Interpreter(f'{model.name}.tflite')
f.allocate_tensors()
i = f.get_input_details()[0]
o = f.get_output_details()[0]
f.set_tensor(i['index'], x)
f.invoke()
y = f.get_tensor(o['index'])
print("TensorFlow Lite:", decode_predictions(y))

Keras: [[('n02098105', 'soft-coated_wheaten_terrier', 0.70274395), ('n02091635', 'otterhound', 0.0885325), ('n02090721', 'Irish_wolfhound', 0.06422518), ('n02093991', 'Irish_terrier', 0.040120784), ('n02111500', 'Great_Pyrenees', 0.03408164)]] 凯拉斯:[[('n02098105','soft-coated_wheaten_terrier',0.70274395),('n02091635','otterhound',0.0885325),('n02090721','Irish_wolfhound',0.06422518),('n02093991','Irish_terrier' ,0.040120784),('n02111500','Great_Pyrenees',0.03408164)]]

TensorFlow Lite: [[('n07753275', 'pineapple', 0.94529104), ('n03379051', 'football_helmet', 0.033994876), ('n03891332', 'parking_meter', 0.011431991), ('n04522168', 'vase', 0.0029440755), ('n02094114', 'Norfolk_terrier', 0.0022089847)]] TensorFlow Lite:[[('n07753275','菠萝',0.94529104),('n03379051','football_helmet',0.033994876),('n03891332','parking_meter',0.011431991),('n04522168','花瓶', 0.0029440755),('n02094114','Norfolk_terrier',0.0022089847)]]

Did you make sure to set the learning phase to 0 in order to avoid Dropout and other non-deterministic layers during the prediction phase? 您是否确定将学习阶段设置为0,以便在预测阶段避免出现Dropout和其他不确定性层?

https://www.tensorflow.org/api_docs/python/tf/keras/backend/learning_phase https://www.tensorflow.org/api_docs/python/tf/keras/backend/learning_phase

There was a bug in from_keras_model_file in TensorFlow 1.10. TensorFlow 1.10中的from_keras_model_file中存在一个错误。 It was fixed in the August 9th nightly release in this commit . 它已在8月9日每晚发布的此提交中修复。

The nightly can be installed via pip install tf-nightly . 每晚可以通过pip install tf-nightly Additionally, it will be fixed in TensorFlow 1.11. 此外,它将在TensorFlow 1.11中修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 tensorflow/keras 模型转换为 tensorflow lite 模型时出现的问题 - Issues when converting tensorflow/keras model to tensorflow lite model tensorflow 无法将 keras 模型转换为 tensorflow lite - tensorflow can not convert keras model to tensorflow lite 使用Keras Functional API为Tensorflow LITE建立模型 - Build a model with Keras Functional API for Tensorflow LITE keras 和 tensorflow lite model 之间的预测差异 - Prediction differences between keras and tensorflow lite model 具有Tensorflow后端和Theano后端的Keras使用相同的模型和相同的输入进行不同的预测 - Keras with Tensorflow backend and Theano backend make different predictions with same model and same input Keras Model 在评估时返回预测 - Keras Model return predictions when evaluating 将 Keras 模型权重和架构转换为 TensorFlow Lite 模型 - Converting Keras Model Weights and Architecture to TensorFlow Lite Model 在 TF2 中将 Keras RNN 模型转换为 TensorFlow Lite 模型 - Converting Keras RNN model to TensorFlow Lite model in TF2 在运行toco时尝试将TensorFlow模型转换为TensorFlow lite --help给我一个错误 - Trying to convert TensorFlow model to TensorFlow lite, when running toco --help gives me an error 使用keras模型中的张量流图进行预测 - Make predictions using a tensorflow graph from a keras model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM