
[英]How to load and predict a pre-trained tensorflow model into Java code?
[英]how to predict with pre-trained model in tensorflow?
我是 TensorFlow 的新手。 我正在尝试从 github -https://github.com/Curt-Park/handwritten_digit_recognition运行预训练的 NN 用于数字识别“wide_resnet_28_10”。 当我尝试预测图像时,它说预期输入具有 4D。 这是我试过的-
from tensorflow.keras.models import load_model
import tensorflow as tf
import cv2
import numpy
model = load_model(r'C:\Users\sesha\Desktop\python\Deep learning NN\handwritten_digit_recognition-master\models\WideResNet28_10.h5')
image = cv2.imread(r'C:\Users\sesha\Desktop\python\Deep learning NN\test_org01.png')
img = tf.convert_to_tensor(image)
predictions = model.predict([img])
print(np.argmax(predictions))
大多数教程都含糊不清,我确实尝试过 np.reshape(1,X,X,-1) 没有用。
对于 4D 输入,它需要成批的数据。 您可以通过执行以下操作使其成为 4D 张量:
predictions = model.predict(tf.expand_dims(img, 0))
如果这不起作用,请尝试 predict_on_batch 而不是 predict。
另外:我认为您的图像阅读不正确。 它可能会给你一个字节字符串的张量。
这应该工作
path = tf.constant(img_path)
image = tf.io.read_file(path)
image = tf.io.decode_image(image)
image = tf.image.resize(image, (X, Y)) # if necessary
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.