python - 运行从 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,这是错误的。
解决方案
推荐阅读
- jvm - 当 Xmx 高达 64G 时,Ignite 启动非常慢,并且无法形成集群超时
- javascript - Javascript:转置 json 数据
- reactjs - 如何在 React 15 中使用带有 React.Fragments 的 3rd 方反应库
- apache-spark - 计算火花中大矩阵的逆时的内存错误
- c++ - 仅将 Linux system() 调用命令的输出重定向到变量
- bash - 用于拆分和移动文件的 Bash 脚本。拆分完成之前移动的文件?
- c# - 我如何为 c# 方法实现父级
- java - 如何缩短此代码并减少重复性?
- ruby-on-rails - Update to array in ActiveRecord column not saving
- javascript - Why don't all web workers send back data with a postMessage?