首页 > 解决方案 > 运行从 Keras 转换的 tf-lite 模型对 Raspberry pi 的错误预测

问题描述

该问题类似于现有问题:“在 TensorFlow Lite 中运行 Keras 模型时的不同预测”(在 TensorFlow Lite 中运行 Keras 模型时的不同预测)。

在 tf 版本为 1.12.0 或更高版本的 docker 容器中,运行以下 python 脚本。

import numpy as np
import tensorflow as tf

IMAGE_WIDTH = 128
IMAGE_HEIGHT = 128
IMAGE_CHANNELS = 3

import pathlib
import os
import cv2

# read an image to predict. 
img = cv2.imread("daisy.jpg")
images = []
img = cv2.resize(img, (IMAGE_WIDTH, IMAGE_HEIGHT))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image_data = np.array(img, dtype=np.float)
images.append(image_data)
images = np.asarray(images)
image = images.reshape(1, IMAGE_WIDTH, IMAGE_HEIGHT,
        IMAGE_CHANNELS)

# input a pretrained model. 
keras_file = "keras_model.h5"
model = tf.keras.models.load_model(keras_file)

# predict the image. 
predictions = model.predict(image)
print("prediction: ", np.argmax(predictions[0]))

# Convert to TensorFlow Lite model.
converter=tf.contrib.lite.TocoConverter.from_keras_model_file
        (keras_file)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

结果是:('prediction: ', 0),这是预期的。

converted_model.tflite在 Raspberry Pi 上运行相同,prediction总是不是 0,这是错误的。

标签: pythontensorflowraspberry-pi3tensorflow-lite

解决方案


推荐阅读