[英]How to specify input_shape in Conv2D layer in Tensor Flow 2.0, keras
I'm building an image classifier model which classifies Handwritten digits MNIST 28x28 grayscale images using CNN Here is my layer defination我正在构建一个图像分类器模型,该模型使用 CNN 对手写数字 MNIST 28x28 灰度图像进行分类 这是我的层定义
model = keras.Sequential()
model.add(keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(28,28,1)))
model.add(keras.layers.MaxPool2D((2,2)))
model.add(keras.layers.Conv2D(64,(3,3),activation='relu'))
model.add(keras.layers.MaxPool2D((2,2)))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(200,activation='relu'))
model.add(keras.layers.Dense(10,activation='softmax'))
But i get this error when i fit the model但是当我拟合模型时出现此错误
ValueError: Input 0 of layer sequential_6 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [32, 28, 28]
And also i want to know why we should mention 1 in input_shape in Conv2D layer.The image shape is 28x28 but we should mention 1 there.我还想知道为什么我们应该在 Conv2D 层的 input_shape 中提到 1。图像形状是 28x28 但我们应该在那里提到 1。
The minimal change that should work is to change the line:应该起作用的最小更改是更改行:
model.add(keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(28,28,1)))
to this, dropping the 1
:对此,删除
1
:
model.add(keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(28,28)))
The reason you have the error is that your input image is 28x28 and the batch size you feed into the network has 32 images, thus an array of dimension [32, 28, 28].您出现错误的原因是您的输入图像是 28x28,并且您输入网络的批量大小有 32 张图像,因此是一个维度为 [32, 28, 28] 的数组。 Unfortunately I don't see how you feed the input to the network.
不幸的是,我没有看到您如何将输入提供给网络。 But what your current code expect is an array of dimension [32, 28, 28, 1].
但是您当前的代码期望的是一个维度为 [32, 28, 28, 1] 的数组。 If that's a numpy array that you can manipulate, just
reshape()
it to such dimension will solve the problem.如果这是一个可以操作的 numpy 数组,只需将它
reshape()
到这样的维度就可以解决问题。
What I suggested above is to do the other way round, ask the network to expect each image of 2D array of dimension [28,28] instead of 3D array of dimension [28,28,1]我上面的建议是反过来,要求网络期望每个图像的维度为 [28,28] 的二维数组,而不是维度为 [28,28,1] 的 3D 数组
Update:更新:
You provided the following code change that made it work:您提供了以下代码更改使其工作:
train_image=train_image.reshape(60000, 28, 28, 1)
train_image=train_image / 255.0
test_image = test_image.reshape(10000, 28, 28, 1)
test_image=test_image/255.0
What this does is that your input images are in a single huge numpy array and you fit your model with it directly.这样做的作用是您的输入图像位于一个巨大的 numpy 数组中,您可以直接用它来拟合模型。 The model fit function will select "tensors" from this array from its first dimension and create a batch for each training step.
模型拟合函数将从该数组的第一维中选择“张量”,并为每个训练步骤创建一个批次。 The batch size is 32, so it will implicitly create an array of shape (32, 28, 28, 1) and pass it down the layers.
批量大小为 32,因此它将隐式创建一个形状为 (32, 28, 28, 1) 的数组并将其向下传递到层。 The 2nd to 4th dimension is merely copied from the original array.
第 2 到第 4 维只是从原始数组复制而来。
The reshape()
command is to change the dimension of the array. reshape()
命令是改变数组的维度。 Your original array before reshape was (60000, 28, 28) and if you lay it out as a single sequence of numbers, there will be 6000x28x28 floats.重塑之前的原始数组是 (60000, 28, 28) 并且如果将其布置为单个数字序列,则会有 6000x28x28 浮点数。 What
reshape()
does is to pick up these numbers and fill them into a (60000, 28, 28, 1) array, which expects 60000x28x28x1 numbers, so it can be filled exactly. reshape()
作用是将这些数字取出并填入一个 (60000, 28, 28, 1) 数组中,该数组预期为 60000x28x28x1 个数字,因此可以精确填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.