[英]Error in Keras Digit Dataset: expected conv2d_input to have 4 dimensions, but got array with shape (60000, 28, 28)
Error in Keras Digit Dataset: ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (60000, 28, 28). Keras 数字数据集出错:ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到的数组形状为 (60000, 28, 28)。 I'm not sure what's going on, why do
我不知道发生了什么,为什么
I need 4 dimensions?我需要 4 个维度? I am positive they are 28x28 images.
我很肯定它们是 28x28 图像。 What shall I do?
我该怎么办?
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images/255
test_images = test_images/255
classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
model = keras.Sequential([
tf.keras.layers.Conv2D(56, (3, 3), activation='relu', input_shape=(28, 28)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(56, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(784, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(train_images, train_labels, epochs=5)
test_loss, test_acc = model.evaluate(test_images, test_labels)
Add the fourth dimension which is channel dimension to your input data using np.expand_dims
, before you pass it into the network.使用
np.expand_dims
将第四个维度(通道维度)添加到您的输入数据中,然后再将其传递到网络中。 Using this way, you will be able to use Conv2D
.使用这种方式,您将能够使用
Conv2D
。
train_images = np.expand_dims(train_images, axis=-1)
test_images = np.expand_dims(test_images, axis=-1)
print(train_images.shape()) # (60000, 28, 28, 1)
print(test_images.shape()) # (10000, 28, 28, 1)
Thanks, @bit01 for pointing my mistake I have updated my answer accordingly -谢谢,@bit01 指出我的错误我已经相应地更新了我的答案 -
you cannot use 2D methods on a no depth or channel images.您不能在没有深度或通道的图像上使用 2D 方法。 Your image doesn't contain any channel, having 28 X 28 shape, Conv2D is defined for images with channels like grey_scale(256,256,1) or RGB(256,256,3) or png images with alpha channel (256,256,4).
您的图像不包含任何通道,具有 28 X 28 形状,Conv2D 是为具有灰度(256,256,1)或 RGB(256,256,3)等通道的图像或具有 alpha 通道(256,256,4)的 png 图像定义的。 It won't work for the 2D matrix without any depth.
它不适用于没有任何深度的 2D 矩阵。
so you have two ways to solve this problem:所以你有两种方法来解决这个问题:
Like this -像这样 -
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images/255
test_images = test_images/255
classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
model = keras.Sequential([
tf.keras.layers.Conv1D(56, 3, activation='relu', input_shape=(28, 28)),
tf.keras.layers.MaxPooling1D(2, 2),
tf.keras.layers.Conv1D(56, 3, activation='relu'),
tf.keras.layers.MaxPooling1D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(784, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(train_images, train_labels, epochs=5)
test_loss, test_acc = model.evaluate(test_images, test_labels)
Like this -像这样 -
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = np.expand_dims(train_images, axis=-1) # (60000, 28, 28, 1)
test_images = np.expand_dims(test_images, axis=-1) # (10000, 28, 28, 1)
train_images = train_images/255
test_images = test_images/255
#train_images.shape, test_images.shape
classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
input_layer = keras.layers.Input(shape=(28, 28, 1), name='image_input')
conv1 = keras.layers.Conv2D(56, (3,3), activation='relu')(input_layer)
pool1 = keras.layers.MaxPooling2D(2, 2)(conv1)
conv2 = keras.layers.Conv2D(56, (3,3), activation='relu')(pool1)
pool2 = keras.layers.MaxPooling2D(2, 2)(conv2)
flatten = keras.layers.Flatten()(pool2)
dense1 = keras.layers.Dense(784, activation='relu')(flatten)
output_layer = keras.layers.Dense(10, activation='softmax')(dense1)
model = keras.models.Model(inputs=input_layer, outputs=output_layer, name='my_model')
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(train_images, train_labels, epochs=5)
test_loss, test_acc = model.evaluate(test_images, test_labels)
You need to make two tiny changes.你需要做两个微小的改变。 Firstly, you need to convert your image to a 4D tensor with a format of NHWC (batch size, height, width, channel), which is expected by Conv2D.
首先,您需要将图像转换为格式为 NHWC(批量大小、高度、宽度、通道)的 4D 张量,这是 Conv2D 所期望的。 However, you don't have channel dimension in current image dataset.
但是,您在当前图像数据集中没有通道维度。 The channel, 4th, dimension can be added like follows
通道,第 4 维可以添加如下
train_images = np.expand_dims(train_images, axis=-1) # (60000, 28, 28, 1)
test_images = np.expand_dims(test_images, axis=-1) # (10000, 28, 28, 1)
Secondly, you need to change the input_shape
to the model specifying the no.其次,您需要将
input_shape
更改为 model 指定编号。 of channel of your input image.输入图像的通道。 Since your image have single channel input image shape should be
(28, 28, 1)
由于您的图像具有单通道输入图像形状应该是
(28, 28, 1)
tf.keras.layers.Conv2D(56, (3, 3), activation='relu', input_shape=(28, 28, 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.