0
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)

4

2 回答 2

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)
于 2019-09-30T09:53:58.043 回答
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)
于 2019-09-30T09:50:24.973 回答