为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

扬帆大鱼 2021-05-30 14:16:02
使用预训练的Keras图像分类器试用TensorFlow Lite,将H5转换为tflite格式后,我得到的预测更糟。这是预期的行为(例如,权重量化),错误还是在使用解释器时忘记了某些东西?例子from imagesoup import ImageSoupfrom tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictionsfrom 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_filetflite_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))凯拉斯:[[('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','菠萝',0.94529104),('n03379051','football_helmet',0.033994876),('n03891332','parking_meter',0.011431991),('n04522168','花瓶', 0.0029440755),('n02094114','Norfolk_terrier',0.0022089847)]]
查看完整描述

2 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

from_keras_model_fileTensorFlow 1.10 中存在一个错误。它在 8 月 9 日每晚发布的此提交中得到修复。

nightly 可以通过pip install tf-nightly. 此外,它将在 TensorFlow 1.11 中修复。


查看完整回答
反对 回复 2021-06-01
  • 2 回答
  • 0 关注
  • 277 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号