Reputation: 11
I am trying to make use of NN for 28x28 grey scale images. My training data is shaped as follows:
Reshape data
out:
x_train.shape
(24000, 28, 28, 1)
y_train.shape
(24000, 1)
Define the keras model
model = Sequential()
model.add(layers.Conv2D(28, (1, 1), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(56, (1, 1), activation='relu'))
model.add(layers.Conv2D(56, (1, 1), activation='relu'))
#model.add(layers.Flatten())
#model.add(layers.Dense(56, activation='relu'))
#model.add(layers.Dense(10))
model.summary()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(input_x, input_y, epochs=20, batch_size=28)
_, accuracy = model.evaluate(X_validation, y_validation)
print('Accuracy: %.2f' % (accuracy*100))
Model Summary
conv2d_86 (Conv2D) (None, 28, 28, 28) 56
max_pooling2d_52 (MaxPooling (None, 14, 14, 28) 0
conv2d_87 (Conv2D) (None, 14, 14, 56) 1624
conv2d_88 (Conv2D) (None, 14, 14, 56) 3192
Total params: 4,872
Trainable params: 4,872
Non-trainable params: 0
Error InvalidArgumentError: Incompatible shapes: [28,14,14] vs. [28,1] I've applied a simple NN structure and gives out this error, ill next add flatten and dense layers once I am able to run it with these ones.
Upvotes: 1
Views: 158
Reputation: 349
The last Conv2D layer output [14*14] cannot be compared with the target shape [1] to calculate the loss. Hence the error. Generally, Conv2D layers need to be flattened and passed through a DNN (the part that you have commented) for the model architecture to be complete. The units(neurons) in the last Dense layer is determined by the nature of the problem that you are trying to solve. Here, you have mentioned it as 10 but then you have used the loss here as binary_crossentropy which is generally used for a binary classification problem. If you are looking to solve a multi class classification problem (where the output in your case is one of 10 classes), then you need to use the loss as 'sparse_catergorical_crossentropy' or 'categorical_crossentropy'. You would also need to use the 'softmax' activation function in the last dense layer for Multi class classification and the 'sigmoid' activation function for binary classification.
I would suggest that a basic understanding of Covolutional Neural Networks would be beneficial before proceeding on your task.
One great source that I can suggest from which I personally benefited is : http://introtodeeplearning.com/. Lecture 3 deals with CNNs but it would be good to take a look at lecture 1 before you get to lecture 3. All the best.
Upvotes: 2