ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28]
I keep on getting this error related to input shape. Any help would be highly appreciated. Thanks!
import tensorflow as tf
(xtrain, ytrain), (xtest, ytest) = tf.keras.datasets.mnist.load_data()
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(16, kernel_size=3, activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=2),
tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics='accuracy')
history = model.fit(xtrain, ytrain,
validation_data=(xtest, ytest),
epochs=10, batch_size=8)
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28]
The input layers of the model you created needs a 4 dimension tensor to work with but the x_train tensor you are passing to it has only 3 dimensions
This means that you have to reshape your training set with .reshape(n_images, 286, 384, 1). Now you have added an extra dimension without changing the data and your model is ready to run.
you need to reshape your x_train tensor to a 4 dimension before training your model. for example:
x_train = x_train.reshape(-1, 28, 28, 1)
for more info on keras inputs Check this answer
You need to add a channel dimension. Keras expects this data format:
(n_samples, height, width, channels)
For instance this, if your images are greyscale, they have 1 channel, and so they need to be given to Keras in this format:
(60000, 28, 28, 1)
Unfortunately, grayscale pictures will often be given/downloaded without a channel dimension, for instance in tf.keras.datasets.mnist.load_data
, which will be (60000, 28, 28)
, which is problematic.
Solution:
You can use tf.expand_dims
to add a dimension
xtrain = tf.expand_dims(xtrain, axis=-1)
Now your input shape will be:
(60000, 28, 28, 1)
There are other alternatives that do the same:
xtrain = xtrain[..., np.newaxis]
xtrain = xtrain[..., None]
xtrain = xtrain.reshape(-1, 28, 28, 1)
xtrain = tf.reshape(xtrain, (-1, 28, 28, 1))
xtrain = np.expand_dims(xtrain, axis=-1)