import cv2
import tensorflow as tf
CATEGORIES = ["Dog", "Cat"] # will use this to convert prediction num to string value
def prepare(filepath):
IMG_SIZE = 32 # 50 in txt-based
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE) # read in the image, convert to grayscale
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) # resize image to match model's expected sizing
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1) # return the image with shaping that TF wants.
import pickle
with open ('module','rb') as f:
model=pickle.load(f)
prediction = model.predict([prepare('dog.5000.jpg')])
print(prediction) # will be a list in a list.
print(CATEGORIES[int(prediction[0][0])])
当我执行此代码时 prediction =model.predict([prepare('dog.5000.jpg')]),出现错误 ValueError:
检查输入时出错:预期 conv2d_9_input 的形状为 (64, 64, 3) 但得到的数组的形状为 (32, 32, 1)