mnist CNN ValueError expected min_ndim=4, found ndim=3. Full shape received: [32, 28, 28]

Question:

I define the model definition as follows.

tf.keras.datasets.mnist 
model = keras.models.Sequential([  
    tf.keras.layers.Conv2D(28, (3,3), activation='relu', input_shape=(28, 28, 1)),  
    tf.keras.layers.MaxPooling2D((2, 2)),  
    tf.keras.layers.Conv2D(56, (3,3), activation='relu'),  
    tf.keras.layers.Flatten(),  
    tf.keras.layers.Dense(64, activation='relu'),  
    tf.keras.layers.Dense(10, activation='softmax'),  
])  
model.fit(x_train, y_train, epochs=3)  <----error

When I try to run with my data set, it gives the following error.

 ValueError: Input 0 of layer sequential_3 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [32, 28, 28]
Asked By: user15217679

||

Answers:

By seeing your error, I think you probably didn’t add the channel axis in the training set i.e [batch, w, h, channel]. Here is the working code

DataSet

import tensorflow as tf 
import numpy as np 
from sklearn.model_selection import train_test_split

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

x_train = np.expand_dims(x_train, axis=-1) # <--- add channel axis
x_train = x_train.astype('float32') / 255
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)

print(x_train.shape, y_train.shape)
# (60000, 28, 28, 1) (60000, 10)

Training

model = tf.keras.models.Sequential([  
    tf.keras.layers.Conv2D(28, (3,3), activation='relu', input_shape=(28, 28, 1)),  
    tf.keras.layers.MaxPooling2D((2, 2)),  
    tf.keras.layers.Conv2D(56, (3,3), activation='relu'),  
    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'])
model.fit(x_train, y_train, epochs=3)
Epoch 1/3
1875/1875 [==============================] - 11s 2ms/step - loss: 0.2803 - accuracy: 0.9160
Epoch 2/3
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0434 - accuracy: 0.9869
Epoch 3/3
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0273 - accuracy: 0.9917
Answered By: M.Innat

another way to solve it:
x_train, x_test = x_train.reshape(-1,28,28,1), x_test.reshape(-1,28,28,1)

Answered By: user15217679