如何在 Keras 中使用带有灰度图像的中性网络



我正在尝试在灰色图像上进行训练。batch_size = 32, image size = (48*48) .我定义我的网络input_shape = (48,48,1)。当我训练网络时,我收到如下错误。

错误:

值错误:检查输入时出错:预期conv2d_17_input有 4 个维度,但得到形状为 (32、48、48( 的数组

model.add(Conv2D(32, kernel_size=(5, 5),
                 activation='relu',
                 input_shape=(48,48,1)
                )
         )

假设您有1000训练图像,其中每个图像都是灰度48x48。将图像加载到 numpy 数组后,最终将得到形状 : (1000, 48, 48) .

这实质上意味着数组中有1000个元素,每个元素都是一个48x48矩阵。

现在,为了提供这些数据来训练CNN,您必须重塑此列表以(1000, 48, 48, 1) 1代表通道维度的位置。由于您有灰度图像,因此您必须使用 1 .如果是RGB,它将是3.

考虑下面给出的玩具示例,

x_train = np.random.rand(1000, 48, 48) #images
y_train = np.array([np.random.randint(0, 2) for x in range(1000)]) # labels
# simple model
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5),
                 activation='relu',
                 input_shape=(48,48,1)
                )
         )
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')
# fitting model
model.fit(x_train, y_train, epochs=10, batch_size=32)

这将抛出一个错误,

检查输入时出错:预期conv2d_3_input有 4 个维度,但得到的数组形状(1000、48、48(

为了修复它,像这样重塑x_train

x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)

现在拟合模型,

model.fit(x_train, y_train, epochs=10, batch_size=32)
Epoch 1/10
1000/1000 [==============================] - 1s 1ms/step - loss: 0.7177
Epoch 2/10
1000/1000 [==============================] - 1s 882us/step - loss: 0.6762
Epoch 3/10
1000/1000 [==============================] - 1s 870us/step - loss: 0.5882
Epoch 4/10
1000/1000 [==============================] - 1s 888us/step - loss: 0.4588
Epoch 5/10
1000/1000 [==============================] - 1s 906us/step - loss: 0.3272
Epoch 6/10
1000/1000 [==============================] - 1s 910us/step - loss: 0.2228
Epoch 7/10
1000/1000 [==============================] - 1s 895us/step - loss: 0.1607
Epoch 8/10
1000/1000 [==============================] - 1s 879us/step - loss: 0.1172
Epoch 9/10
1000/1000 [==============================] - 1s 886us/step - loss: 0.0935
Epoch 10/10
1000/1000 [==============================] - 1s 888us/step - loss: 0.0638

即使它是灰度,您的输入也应该有 4 个数字。因此,您可以使用 np.reshape(input,(32,48,48,1))np.expand_dims(input,axis=3) .

您的图像应该重新塑造为(sample_length, 48, 48, 1)和您的input_shape = (48, 48, 1)

    x_train = x_train.reshape(x_train.shape[0], 48, 48, 1)
    x_test = x_test.reshape(x_test.shape[0], 48, 48, 1)
    input_shape = (48, 48, 1)

你可以在这里查看MNIST的例子,它与你的情况类似。

最新更新