检查模型输入时出错: 预期卷积2d_input_1为4维,但得到的数组为形状(32,32,3)

我想从下面这一层开始培养一个深层次的网络:

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

165867 次浏览

您定义的输入形状是单个样本的形状。模型本身需要一些样本数组作为输入(即使它的数组长度为1)。

您的输出实际上应该是4-d,使用第一维来枚举示例。也就是说,对于一个图像,你应该返回一个(1,32,32,3)的形状。

你可在“ Convolution2D”/“ Input form”下找到更多资料 给你

编辑 : 基于 Danny 下面的评论,如果你想要一个1的批量大小,你可以使用以下方法添加缺少的维度:

image = np.expand_dims(image, axis=0)
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/5 60000/60000[ = = = = = = = = = = = = = = = = = = = = = = = = = = = ]-157s 3ms/step-loss: 0.0981-acc: 0.9692-val _ loss: 0.0468-val _ acc: 0.9861 时代2/5 60000/60000[ = = = = = = = = = = = = = = = = = = = = = = = = = ]-157s 3ms/step-loss: 0.0352-acc: 0.9892-val _ loss: 0.0408-val _ acc: 0.9879 时代3/5 60000/60000[ = = = = = = = = = = = = = = = = = = = = = = = = ]-159s 3ms/step-loss: 0.0242-acc: 0.9924-val _ loss: 0.0291-val _ acc: 0.9913 时代4/5 60000/60000[ = = = = = = = = = = = = = = = = = = = = = = = = ]-165s 3ms/step-loss: 0.0181-acc: 0.9945-val _ loss: 0.0361-val _ acc: 0.9888 时代5/5 60000/60000[ = = = = = = = = = = = = = = = = = = = = = = = = ]-168s 3ms/step-loss: 0.0142-acc: 0.9958-val _ loss: 0.0354-val _ acc: 0.9906

它就像添加一个维度一样简单,所以我看了 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)

如果这对你有用的话,请在这里分享你的反馈。

这取决于你实际上如何排序你的数据,如果它首先是一个渠道,那么你应该重塑你的数据: 形状(x _ train. 形状[0] ,通道,宽度,高度)

如果它的频道持续: 形状(x _ train. 形状[0] ,宽度,高度,通道)

可能是 非常微不足道,但我只是把输入转换成 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)])

问题解决了。

您只需将以下转换应用于输入数据数组。

input_data = input_data.reshape((-1, image_side1, image_side2, channels))

我在处理 mnist 数据集时也出现了同样的错误,看起来像是 X _ train 的尺寸出了问题。我增加了另一个维度,解决了问题。

火车 X 测试 Y _ test = train _ test _ split (X _ 标签, Train _ size = 0.8, 州 = 42)

形状(-1,28,28,1)

X _ test = X _ test. 形状(-1,28,28,1)

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

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

遇到同样的问题,没有一个答案对我有用。经过大量的调试,我发现一个图像的大小小于 32。这将导致一个具有错误尺寸和上述错误的破碎数组。

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