我想从以下层开始训练一个深度网络:
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)))
?
您定义的输入形状是单个样本的形状。模型本身需要一些样本数组作为输入(即使它是长度为 1 的数组)。
你的输出真的应该是 4-d,用第一个维度来枚举样本。即对于单个图像,您应该返回 (1, 32, 32, 3) 的形状。
您可以在“Convolution2D”/“输入形状”下找到更多信息 here
编辑:根据下面丹尼的评论,如果您想要批量大小为 1,您可以使用以下方法添加缺少的维度:
image = np.expand_dims(image, axis=0)
就像添加一维一样简单,所以我正在阅读 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)
如果这对您有用,请在此处分享反馈。
可能非常微不足道,但我只是通过将输入转换为 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)]
https://i.stack.imgur.com/USoqV.png
但是当我把它改成
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)])
它解决了这个问题。
我在使用 mnist 数据集时遇到了同样的错误,看起来像 X_train 的尺寸问题。我添加了另一个维度,它解决了这个目的。
X_train, X_test, \ y_train, y_test = train_test_split(X_reshape, 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)
您应该简单地将以下转换应用于您的输入数据数组。
input_data = input_data.reshape((-1, image_side1, image_side2, channels))
这取决于您实际如何订购数据,如果它是基于通道的,那么您应该重塑您的数据: x_train=x_train.reshape(x_train.shape[0],channel,width,height)
如果它的频道最后: x_train=s_train.reshape(x_train.shape[0],width,height,channel)
是的,它接受四个参数的元组,如果你有训练图像数(或其他)=6000,图像大小=28x28 和灰度图像,你将有参数为 (6000,28,28,1)
最后一个参数是 1 表示灰度,3 表示彩色图像。
遇到同样的问题,没有一个答案对我有用。经过大量调试,我发现一张图像的大小小于32
。这会导致损坏的数组具有错误的尺寸和上述错误。
要解决此问题,请确保所有图像都具有正确的尺寸。
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 个样本 Epoch 1/5 60000/60000 [==============================] - 157s 3ms/step - loss: 0.0981 - acc: 0.9692 - val_loss: 0.0468 - val_acc: 0.9861 Epoch 2/5 60000/60000 [======================= =======] - 157s 3ms/步 - 损失:0.0352 - acc: 0.9892 - val_loss: 0.0408 - val_acc: 0.9879 Epoch 3/5 60000/60000 [============= =================] - 159s 3ms/步 - 损失:0.0242 - acc:0.9924 - val_loss:0.0291 - val_acc:0.9913 Epoch 4/5 60000/60000 [=== ===========================] - 165s 3ms/step - loss: 0.0181 - acc: 0.9945 - val_loss: 0.0361 - val_acc: 0.9888 Epoch 5/5 60000/60000 [===============================] - 168s 3ms/步 - 损失:0.0142 - acc: 0.9958 - val_loss:0.0354 - val_acc:0.9906
不定期副业成功案例分享
image = np.expand_dims(image, axis=0))
添加额外维度