【问题标题】:Keras error: Expected to see 1 array(s), but instead got 1816Keras 错误:预计会看到 1 个数组,但得到了 1816
【发布时间】:2019-03-28 22:44:33
【问题描述】:

我正在尝试使用 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)

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

【问题讨论】:

  • 期望 'y_train' 有 4 个维度,但它有 2 个。您只是在附加零和一。

标签: python numpy keras


【解决方案1】:

它期望 x_train 有 4 个维度,它们是(图像数量、宽度、高度、nchannels)

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)

【讨论】:

    猜你喜欢
    • 2017-07-24
    • 2020-05-20
    • 2020-09-13
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多