0
votes

I have a pre-trained Keras model that I need to use to classify a 512x 512 image that is originally in grayscale format. The input to the Keras model should be in the shape (None, 512, 512, 1). enter image description here.

I executed the following code:

model=load_model('model.h5')
img = Image.open('img.jpg')
img_array = np.array (img)
img_array = img_array/255
model.predict (img_array)

However, I get the following error

Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (512, 512)

I know that I need to reshape my grayscale image into 4D to match the desired input shape, however, I am not sure how to do this so that the image keeps its original features. How can I make the grayscale image into 4D properly?

Thanks.

1

1 Answers

0
votes

try reshaping the array

img_array = img_array.reshape((1, 512, 512, 1)) 

here 1st and last dimension are batch size and channels respectively