import tensorflow as tf
from tensorflow import keras
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
import cv2
import matplotlib.pyplot as plt
model=tf.keras.models.load_model('model_ex-024_acc-0.996875.h5')
img_array = cv2.imread('30.jpg') # convert to array
img_rgb = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
img_rgb = cv2.resize(img_rgb,(224,224),3)
plt.imshow(img_rgb) # graph it
plt.show()
model.predict(img_rgb)
ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (224, 224, 3)
您应该按照模型的预期扩展输入图像的尺寸。你可以使用np.expand_dims
。此外,您可能想要缩放图像。
img_rgb = cv2.resize(img_rgb,(224,224),3) # resize
img_rgb = np.array(img_rgb).astype(np.float32)/255.0 # scaling
img_rgb = np.expand_dims(img_rgb, axis=0) # expand dimension
y_pred = model.predict(img_rgb) # prediction
y_pred_class = y_pred.argmax(axis=1)[0]
希望它会有所帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)