0

I am getting into Convolutional Neural Networks and want to create one for MNIST data. Whenever I add a convolutional Layer to my CNN, I get an error:

Input 0 is incompatible with layer conv2d_4: expected ndim=4, found ndim=5

I have attemped to reshape X_Train data set but was not successful I tried to add a flatten layer first but that returns this error:

Input 0 is incompatible with layer conv2d_5: expected ndim=4, found ndim=2

import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import Flatten, Dense, Dropout
img_width, img_height = 28, 28
mnist = keras.datasets.mnist
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train = keras.utils.normalize(X_train, axis=1) #Normalizes from 0-1 (originally each pixel is valued 0-255)
X_test = keras.utils.normalize(X_test, axis=1) #Normalizes from 0-1 (originally each pixel is valued 0-255)
Y_train = keras.utils.to_categorical(Y_train) #Reshapes to allow ytrain to work with x train
Y_test = keras.utils.to_categorical(Y_test)

from sklearn import preprocessing
lb = preprocessing.LabelBinarizer()
Y_train = lb.fit_transform(Y_train)
Y_test = lb.fit_transform(Y_test)

#Model
model = Sequential()
model.add(Flatten())
model.add(Convolution2D(16, 5, 5, activation='relu', input_shape=(1,img_width, img_height, 1)))
model.add(Dense(128, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dropout(.2))
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))

model.compile(optimizer = 'adam',
            loss='categorical_crossentropy',
            metrics=['accuracy'])
model.fit(X_train, Y_train, epochs=3, verbose=2)

val_loss, val_acc = model.evaluate(X_test, Y_test) #Check to see if model fits test
print(val_loss, val_acc)

If I comment out the Convolutional layer, it works very well (accuracy>95%), but I am planning on making a more complex neural network that requires Convolution in the future and this is my starting point

theastronomist
  • 955
  • 2
  • 13
  • 33

2 Answers2

1

Keras is looking for a tensor of dimension 4 but it's getting ndim as number of dimension as 2. first make sure you kernel size in Conv2D layer is in parenthesis model.add(Convolution2D(32, (3, 3), activation='relu', input_shape=(img_height, img_height, 1)))

Second you need to reshape the X_train, X_test variable as Conv2D layer is expecting a tensor input.

X_train = X_train.reshape(-1,28, 28, 1) #Reshape for CNN - should work!! X_test = X_test.reshape(-1,28, 28, 1) model.fit(X_train, Y_train, epochs=3, verbose=2)

For more information about Conv2D you can look into Keras Documentation here

Hope this helps.

Anil Gupta
  • 147
  • 7
  • I missed few points in my answer as it has been correctly mentioned by Matias Valdenegro so give it a read to below answer. – Anil Gupta Jul 01 '19 at 22:59
0

There are two issues in your code.

  1. You are encoding your labels two times, once using to_categorical, and another time using LabelBinarizer. The latter is no needed here, so just encode your labels into categorical once, using to_categorical.

2.- Your input shape is incorrect, it should be (28, 28, 1).

Also you should add a Flatten layer after the convolutional layers so the Dense layer works properly.

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
  • Even after reshaping, I still get the same error: Input 0 is incompatible with layer conv2d_9: expected ndim=4, found ndim=5 – theastronomist Jul 02 '19 at 13:57
  • @rgbarishian Why is your input 5-dimensional? – Dr. Snoopy Jul 02 '19 at 14:00
  • I don't know why. I added a check to see the dimensions and X_test/X_train are 3 dimension and Y_test/Y_train are 1 dimension. Maybe I'm not understanding what its finding the dimension of – theastronomist Jul 02 '19 at 14:10
  • I figured it out. I was changing the shape of my input arrays instead of changing the shape in the convolutional layer. This article helped: https://stackoverflow.com/questions/47665391/keras-valueerror-input-0-is-incompatible-with-layer-conv2d-1-expected-ndim-4?rq=1 – theastronomist Jul 02 '19 at 14:15