theastronomist
theastronomist

Reputation: 1056

Keras CNN Incompatible with Convolution2D

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

Upvotes: 0

Views: 486

Answers (2)

Anil  Gupta
Anil Gupta

Reputation: 145

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.

Upvotes: 1

Dr. Snoopy
Dr. Snoopy

Reputation: 56367

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.

Upvotes: 0

Related Questions