Help reshaping image arrays please

Hello,

I am having a little trouble with the shape of arrays when predicting a keras model.

I have trained my model using image size 75,75 and array shape of 1,75,75,1

However when normalizing and feeding it to the prediction it cannot reshape.

I do not fully understand whats going on under the hood with array reshaping and a lot of what I find on the net doesn’t cover what I am looking for.

I open the image, then resize it 75,75 and put it in an np.asarray and when printing the shape of said array it is (75,75,3)

image = Image.open(i)
image1 = image.resize((75, 75))
            
image_array = np.asarray(image1)
print(image_array.shape)
Result: (75, 75, 3)

Then I feed it to predict:

prediction = model.predict_classes((image_array/255).reshape((1,75,75,1)))

This is the error I get back:

predicting..
Error predicting model.predict(data): cannot reshape array of size 16875 into shape (1,75,75,1)

The prediction line including reshape works for other people yet it does not work for me but I am not sure of their setup and I don’t get why in the error message it it refers to the shape as a 16875 instead 75,75,3?

Any help even a point to the right info is greatly appreciated!

Thank You

Hi,

Reshape is to changed the way to arrange data.
So you can update the dimension but the total item need to be the same.

To solve this issue, you can try to convert the image from color to gray first.
Then the item will change from [75,75,3] to [75,75,1] directly.

Thanks.

1 Like