0

I am trying to classify the MNIST database of handwritten digits in a convolution network but have been getting this error: ValueError: Error when checking input: expected conv2d_40_input to have 4 dimensions, but got array with shape (28, 28, 1)

My task is to use cross sampling, that is why the data is being split into 5 groups.

def train_conv_subsample():

#splitting data into chunks
chunks = []
chunk_labels = []
num_chunks = 5
chunk_size = int(train_data.shape[0]/num_chunks)
for i in range(num_chunks):
    chunks.append(train_data[(i*chunk_size):(i+1)*chunk_size])
    chunk_labels.append(train_labels[(i*chunk_size):(i+1)*chunk_size])

#Create another convolutional model to train.
for i in range(num_chunks):
    current_train_data = []
    current_train_lables = []
    for j in range(num_chunks):
        if(i == j):
            validation_data = chunks[i]
            validation_labels = chunk_labels[i]
        else:
            current_train_data.extend(chunks[j])
            current_train_lables.extend(chunks[j])

    print(np.shape(current_train_data)) #Says it has a shape of (48000,28,28, 1)

    model = models.Sequential([
        layers.Conv2D(16, kernel_size=(3, 3), activation='relu', input_shape=(28,28,1)),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Flatten(),
        layers.Dense(32, activation='relu'),
        layers.Dense(10, activation='softmax')
    ])
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.CategoricalCrossentropy(),
                  metrics=['accuracy'])

    #But when it goes to fit it raises the error: expected 4 dim, but got array with shape (28, 28, 1)
    model.fit(current_train_data, current_train_lables, epochs=1, validation_data=(validation_data, validation_labels))
    tf.keras.backend.clear_session()

That is my code, and the dataset im using can be imported from keras datasets, datasets.mnist.load_data()

Thanks for any help

10dan
  • 1
  • 3
  • Welcome to SO! Please provide a minimal example of the code that is required to reproduce your error. This greatly helps to provide on-spot answers. – Fourier Dec 17 '19 at 15:00
  • as @Fourier mentioned. However, from the error I can guess you need to create 4rd object, where 1 dimension is for images. 3dim image, 4dim -> batch of images – Martin Dec 17 '19 at 15:44
  • @Martin thanks for the reply, strange thing is it looks like there is already a 4th dimension, when i run print(np.shape(current_train_data)) it returns (48000,28,28,1). – 10dan Dec 17 '19 at 16:17
  • @DeSoul So why not `input_shape = np.shape(current_train_data)`? – Fourier Dec 17 '19 at 16:29
  • @DeSoul Before putting question on stackoverflow, look more around. Its not really a forum. Check out this: https://stackoverflow.com/questions/41563720/error-when-checking-model-input-expected-convolution2d-input-1-to-have-4-dimens – Martin Dec 17 '19 at 16:30
  • @Fourier changing the input shape to np.shape(current_train_data) now gives the error: Input 0 of layer conv2d_4 is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [None, 48000, 28, 28, 1] – 10dan Dec 17 '19 at 16:31
  • @DeSoul try to add: `data_format="channels_last"` as argument for `layers.Conv2D(...)` – Fourier Dec 17 '19 at 16:32

1 Answers1

1

I think the problem is that with the shape of the image in the mnist dataset you need to reshape them to 4 dim arrays using the reshape in the numpy array library as follows :

    import numpy as np 

    np.reshape(dataset,(-1,28,28,1) 

if this did not work try to convert them to grayscale before reshaping using OpenCV library

xxzozoxx1
  • 82
  • 8