检查模型输入时出错:期望convolution2d_input_1具有4个维度,但得到的数组形状为(32, 32, 3)。

73

我想训练一个深度网络,从以下层开始:

model = Sequential()
model.add(Conv2D(32, 3, 3, input_shape=(32, 32, 3)))

使用

history = model.fit_generator(get_training_data(),
                samples_per_epoch=1, nb_epoch=1,nb_val_samples=5,
                verbose=1,validation_data=get_validation_data()

使用以下生成器:
def get_training_data(self):
     while 1:
        for i in range(1,5):
            image = self.X_train[i]
            label = self.Y_train[i]
            yield (image,label)

我将会翻译以下内容:

(验证码生成器看起来非常相似)。

在训练过程中,我遇到了错误:

Error when checking model input: expected convolution2d_input_1 to have 4 
dimensions, but got array with shape (32, 32, 3)

如何才能实现第一层的操作呢?

 model.add(Conv2D(32, 3, 3, input_shape=(32, 32, 3)))

?


9
你是如何修复它的? - Abhishek Bhatia
只需在图像数据列表周围添加np.asarray()即可。这将调整您提供的列表大小以符合预期。即使您正在对单个图像数据进行预测,也请将其放入列表中并使用np.asarray()。 - Mousam Singh
9个回答

71
您定义的输入形状是单个样本的形状。模型本身期望作为输入的是一些样本数组(即使是长度为1的数组)。
您的输出真正应该是4-D的,其中第一个维度用于枚举样本。例如,对于单个图像,您应该返回形状为(1,32,32,3)。
您可以在此处找到更多信息,在“Convolution2D”/“Input shape”下方。 编辑:根据Danny的下面的评论,如果您想要批次大小为1,则可以使用以下内容添加缺少的维度:
image = np.expand_dims(image, axis=0)

@AbhishekBhatia,你应该以相同的方式更改 x_ip_shape。 - ginge
21
改变输入尺寸会导致错误变为“输入0与层conv2d_1不兼容:期望ndim=4,发现ndim=5”。 有人可以提供帮助吗? - Stormsson
29
使用image = np.expand_dims(image, axis=0)添加额外的维度。 - Danny Wang

6

只需添加一个维度即可,我正在学习Siraj Rawal有关CNN代码部署教程的教程,他在他的终端上运行正常,但是同样的代码在我的终端上无法工作。所以我进行了一些研究并解决了问题,不知道对你们是否有效。这里是我的解决方案;

下面是未解决的代码行,它导致了问题:

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    print(x_train.shape)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols)
    input_shape = (img_rows, img_cols, 1)

已解决代码:

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    print(x_train.shape)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

如果这对你有用,请在此分享反馈。

4

可能非常简单,但我通过将输入转换为 numpy数组来解决了它。

对于神经网络架构,

    model = Sequential()
    model.add(Conv2D(32, (5, 5), activation="relu", input_shape=(32, 32, 3)))

当输入为时,
    n_train = len(train_y_raw)
    train_X = [train_X_raw[:,:,:,i] for i in range(n_train)]
    train_y = [train_y_raw[i][0] for i in range(n_train)]

我遇到了错误,enter image description here

但是当我改成下面这样时,

   n_train = len(train_y_raw)
   train_X = np.asarray([train_X_raw[:,:,:,i] for i in range(n_train)])
   train_y = np.asarray([train_y_raw[i][0] for i in range(n_train)])

问题已被修复。


3

在使用mnist数据集时,我遇到了相同的错误,看起来是X_train的维度出现了问题。我增加了另一个维度,问题得到了解决。

X_train、X_test、y_train和y_test = train_test_split(X_reshaped, y_labels, train_size=0.8, random_state=42)

X_train = X_train.reshape(-1,28, 28, 1)

X_test = X_test.reshape(-1,28, 28, 1)


2
您只需要将以下转换应用于您的输入数据数组即可。
input_data = input_data.reshape((-1, image_side1, image_side2, channels))

1
这取决于您如何实际订购数据,如果是以通道为基础的话,则应重新塑造数据: x_train=x_train.reshape(x_train.shape[0],channel,width,height)
如果是通道最后: x_train=s_train.reshape(x_train.shape[0],width,height,channel)

0

是的,它接受四个参数的元组, 如果您有6000张训练图像(或其他), 图像大小=28x28 和灰度图像 您的参数将为(6000,28,28,1)

最后一个参数是1表示灰度图像,3表示彩色图像。


0

遇到了同样的问题,没有一个答案对我有用。经过大量调试,我发现其中一张图片的大小小于32。这导致了一个错误的数组维度和上述错误。

为了解决这个问题,请确保所有图片都具有正确的尺寸。


-1
x_train = x_train.reshape(-1,28, 28, 1)   #Reshape for CNN -  should work!!
x_test = x_test.reshape(-1,28, 28, 1)
history_cnn = cnn.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))

输出:

训练样本为60000个,验证样本为10000个。 第1个Epoch 60000/60000 [==============================] - 157秒3毫秒/步 - 损失: 0.0981 - 准确率: 0.9692 - 验证损失: 0.0468 - 验证准确率: 0.9861 第2个Epoch 60000/60000 [==============================] - 157秒3毫秒/步 - 损失: 0.0352 - 准确率: 0.9892 - 验证损失: 0.0408 - 验证准确率: 0.9879 第3个Epoch 60000/60000 [==============================] - 159秒3毫秒/步 - 损失: 0.0242 - 准确率: 0.9924 - 验证损失: 0.0291 - 验证准确率: 0.9913 第4个Epoch 60000/60000 [==============================] - 165秒3毫秒/步 - 损失: 0.0181 - 准确率: 0.9945 - 验证损失: 0.0361 - 验证准确率: 0.9888 第5个Epoch 60000/60000 [==============================] - 168秒3毫秒/步 - 损失: 0.0142 - 准确率: 0.9958 - 验证损失: 0.0354 - 验证准确率: 0.9906

9
请描述您所做的事情和原因。 - Oliort UA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接