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)
答案 0 :(得分:1)
首先,您需要了解除64x64图像(而不是32x32图像)之外的网络,请更改
IMG_SIZE
变量为64
而不是32
第二,网络将输入图像着色为非灰度,因此该通道的数量应为3,而不是1。
此行的 cv2.IMREAD_GRAYSCALE
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
总而言之,这是您的新prepare_image函数
def prepare(filepath):
IMG_SIZE = 64
img_array = cv2.imread(filepath)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 3)
答案 1 :(得分:0)
更改此:
IMG_SIZE = 32
new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
为此:
IMG_SIZE = 64
new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 3)