keras reshape input image to work with CNN

11,820

Solution 1

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

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

Solution 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)

Share:
11,820
Danny Julian
Author by

Danny Julian

Updated on June 24, 2022

Comments

  • Danny Julian
    Danny Julian almost 2 years

    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.