Keras错误:预期会看到1个数组,但得到1816个

时间:2019-03-28 22:44:34

标签: python numpy keras

我试图使用python和Keras进行图像分类,但是遇到以下错误:

ValueError: Error when checking model input: the list of Numpy arrays that 
you are passing to your model is not the size the model expected. Expected 
to see 1 array(s), but instead got the following list of 1816 arrays:

我尝试将x_train更改为一个numpy数组,但仍然出现错误:

ValueError: Error when checking input: expected conv2d_13_input to have 4 
dimensions, but got array with shape (1816, 1)

这是我的代码的一部分:

def read_and_process_image(imagesTrain):

    x_train = []
    y_train = []

    for trImage in imagesTrain:
        x_train.append(cv2.imread(trImage, cv2.IMREAD_COLOR))

        if 'A' in trImage:
            y_train.append(0)
        elif 'B' in trImage:
            y_train.append(1)


    return x_train, y_train


x_train, y_train = read_and_process_image(train_imgs)


modelo.fit(x_train,y_train,batch_size=50,epochs=7,verbose=1)

我没有显示完整的代码,以至于它不会填满整个窗口,但是有人知道如何解决此问题吗?

1 个答案:

答案 0 :(得分:0)

期望x_train具有4个尺寸,即(图像数量,宽度,高度,n个通道)

for trImage in imagesTrain:
    x_train.append(np.array(cv2.imread(trImage, cv2.IMREAD_COLOR)))

    if 'A' in trImage:
        y_train.append(0)
    elif 'B' in trImage:
        y_train.append(1)


return np.array(x_train), np.array(y_train)