繁体   English   中英

ValueError:检查输入时出错:预期 conv2d_9_input 的形状为 (64, 64, 3) 但得到的数组的形状为 (32, 32, 1)

[英]ValueError: Error when checking input: expected conv2d_9_input to have shape (64, 64, 3) but got array with shape (32, 32, 1)

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)

首先,您需要了解您的网络除了 64x64 图像,而不是 32x32 图像,更改您的

IMG_SIZE变量为64而不是32

其次,网络除了输入图像要着色而不是灰度,因此通道数应该是 3,而不是 1,用于移除

来自这一行的cv2.IMREAD_GRAYSCALE img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)

总之,这是你的新 prepare_image function

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)

改变这个:

IMG_SIZE = 32
new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)

为了这:

IMG_SIZE = 64
new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM