user9942241
user9942241

Reputation:

Error when checking input: expected conv2d_1_input to have shape (28, 28, 1) but got array with shape (3, 224, 224)

How do I solve it? codes used by me are displayed below

this is for converting image to vector

import cv2
import numpy as np

file = cv2.imread('17316.png')
file = cv2.resize(file, (224, 224))
file = cv2.cvtColor(file, cv2.COLOR_BGR2RGB)
file = np.array(file).reshape((1, 3, 224, 224))
print(file.shape[0])

this is part of convolution neural network applied by me whoich lead to that error what do I do,how do I solve it please suggest me changes to code so that I can get proper predictions for my dataset?

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

Upvotes: 0

Views: 4192

Answers (2)

mgross
mgross

Reputation: 652

Could please post your full code? What is the value of input_shape? I think you should set it to (3, 224, 224). Apparently, your data_format is channels_first and according to Keras conv2d documentation the default is channels_last. So, I would suggest to you to use for your first convolutional layer

model.add(Conv2D(32, kernel_size = (3, 3),
                 activation = 'relu',
                 input_shape = (3, 224, 224), 
                 data_format = "channels_first")

UPDATE: Based on your code, the following should work, but might not produce the results that you want. You are training on the mnist dataset which expects images that are in 28x28x1 format, so you have to resize like shown in the answer by Mitiku. I hope this helps.

import keras
import cv2 
import numpy as np 
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten 
from keras.layers import Conv2D, MaxPooling2D 
from keras import backend as K 

batch_size = 128 
num_classes = 10 
epochs = 1 
img_rows, img_cols = 28, 28
(x_train, y_train), (x_test, y_test) = mnist.load_data() 

if K.image_data_format() == 'channels_first': 
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 
    input_shape = (1, img_rows, img_cols) 
else: 
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 
    input_shape = (img_rows, img_cols, 1) 
    x_train = x_train.astype('float32')

x_test = x_test.astype('float32') 
x_train /= 255 
x_test /= 255 
print('x_train shape:', x_train.shape) 
print(x_train.shape[0], 'train samples') 
print(x_test.shape[0], 'test samples') 
y_train = keras.utils.to_categorical(y_train, num_classes) 
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential() 
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu',\
                 input_shape = input_shape))
model.add(Conv2D(64, (3, 3), activation='relu')) 
model.add(MaxPooling2D(pool_size=(2, 2))) 
model.add(Dropout(0.25)) 
model.add(Flatten()) 
model.add(Dense(128, activation='relu')) 
model.add(Dropout(0.5)) 
model.add(Dense(num_classes, activation='softmax')) 
model.compile(loss=keras.losses.categorical_crossentropy, \
              optimizer=keras.optimizers.Adadelta(), \
              metrics=['accuracy'])

model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, \
          verbose=1, validation_data=(x_test, y_test)) 
score = model.evaluate(x_test, y_test, verbose=0) 

print('Test loss:', score[0]) 
print('Test accuracy:', score[1]) 

file = cv2.imread('17316.png') 
file = cv2.resize(file, (28, 28))
file = cv2.cvtColor(file, cv2.COLOR_BGR2GRAY)
file = file.reshape((28, 28,1))

model.predict(np.expand_dims(file, axis = 0))

UPDATE 2: You have 10 classes for the mnist dataset. You have a binary classification problem. Your output classifies your image as class 8, which corresponds to the number 7, because the mnist dataset classes are numbers from 0 to 9. We have to know how the classes are encoded - this is problem specific. In this case, to return the number you could do:

prediction = model.predict(np.expand_dims(file, axis = 0))
prediction = np.squeeze(prediction)
index = np.where(prediction == 1)[0]
number = (index - 1).item()
print("predicted number for my image: ", number)

The last line returns the index of where prediction contains a 1 and as indexing starts at 1, you can subtract one from the index to get the number corresponding to your image.

Upvotes: 0

Mitiku
Mitiku

Reputation: 5412

From the error message it is obvious that the model is expecting image shape with (28,28,1). So try to resize image before feeding it to the model.

file = cv2.imread('17316.png')
file = cv2.resize(file, (28, 28))
file = cv2.cvtColor(file, cv2.COLOR_BGR2GRAY)
file = file.reshape((-1, 28, 28,1))

this will solve the problem.

Upvotes: 3

Related Questions