keras reshape input image to work with CNN
Asked Answered
H

3

5

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.

Homy answered 7/8, 2017 at 0:10 Comment(1)
hi there, did you manage to find an answer to this? I too have my own set of images to be shaped / resized to fit into keras 28, 28, 1 model. How do I get all images of various sizes to fit in 28,28,1? I am getting the below error when feeding in unprocessed image: ValueError: cannot reshape array of size 21600 into shape (28,28,1)Wauters
P
4

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

 image = np.reshape(image, (28, 1, 28, 1))
Psychogenic answered 7/8, 2017 at 0:55 Comment(0)
F
2

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)

Firstfoot answered 7/8, 2017 at 6:39 Comment(0)
M
0

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

Mertiemerton answered 9/2, 2023 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.