Danny Julian
Danny Julian

Reputation: 77

keras reshape input image to work with CNN

There are other post with similar questions but none of the answers are helping me. I´m new to this CNN world.

I followed this tutorial for training a CNN with Keras using theano as BackEnd with the MNIST dataset. Now I want to pass to the CNN my own jpg image but I dont know how to reshape it. Can you help me please? Im super new at this.

So far, I tried this to reshape

image = np.expand_dims(image, axis=0) image = preprocess_input(image)

but get the following error when predicting:

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

As you can see, my CNN uses width = 28, height = 28 and depth =1.

Upvotes: 5

Views: 15066

Answers (3)

Giri Madhav Chowdhury
Giri Madhav Chowdhury

Reputation: 33

simply use '''image = np.reshape(len(image), (28,28, 1))'''

Upvotes: 0

VictorLi
VictorLi

Reputation: 211

The error message shows the network expects the image shape is 1*28*28, but your input is in 3*28*28. I guess the image you input is a color image, 3 channels(RGB), while the network expects a gray image, one channel.

When you call opencv to read image, please use code below. img = cv2.imread(imgfile, cv2.IMREAD_GRAYSCALE)

Upvotes: 2

Saurav--
Saurav--

Reputation: 1589

Try using Numpy for reshaping. Since, you have been using a 2D-Convolutional model:

 image = np.reshape(image, (28, 1, 28, 1))

Upvotes: 4

Related Questions