简体   繁体   English

Keras 数字数据集错误:预期 conv2d_input 有 4 个维度,但得到的数组形状为 (60000, 28, 28)

[英]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:所以你有两种方法来解决这个问题:

  1. switch from 2D method to 1D methods for Convolution and max-pooling layers.卷积层和最大池化层从 2D 方法切换到 1D 方法。

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)
  1. add one extra dimention to your dataset (make it 28 X 28 -> 28 X 28 X1).为您的数据集添加一个额外的维度(使其为 28 X 28 -> 28 X 28 X1)。

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.

相关问题 Keras 输入形状抛出预期 4d 的值错误,但得到了一个形状为 (60000, 28,28) 的数组 - Keras input shape throws value error expected 4d but got an array with shape (60000, 28,28) tensorflow js:未捕获的错误:检查时出错:预期 conv2d_input 有 4 个维度,但得到了形状为 [28,28,1] 的数组 - tensorflow js: Uncaught Error: Error when checking : expected conv2d_input to have 4 dimension(s), but got array with shape [28,28,1] ValueError:检查时出错:预期conv2d_1_input具有形状(28,28,1),但数组的形状为(58000,28,28) - ValueError: Error when checking : expected conv2d_1_input to have shape (28, 28, 1) but got array with shape (58000, 28, 28) 检查输入时出错:预期 conv2d_17_input 有 4 个维度,但得到形状为 (28, 28, 1) 的数组 - Error when checking input: expected conv2d_17_input to have 4 dimensions, but got array with shape (28, 28, 1) Keras model.predict检查输入时出错:预期conv2d_input具有4维,但数组的形状为(128,56) - Keras model.predict Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (128, 56) 检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了形状为 (28708, 1) 的数组 - Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (28708, 1) ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了具有形状的数组 - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到的数组具有形状(无,1) - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (None, 1) ValueError:检查输入时出错:预期density_16_input具有2维,但数组的形状为(60000,28,28) - ValueError: Error when checking input: expected dense_16_input to have 2 dimensions, but got array with shape (60000, 28, 28) ValueError:检查输入时出错:预期density_1_input具有2维,但数组的形状为(60000,28,28) - ValueError: Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (60000, 28, 28)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM