-1

I was building a model and I wanted to test its performance, thus I imported a local file and load it and try to predict its label with the following code:

from tensorflow.preprocessing import image
# Other imports for tensorlfow etc.

#...

# Sample image
img_path = "./Model/data/brain/train/Glioma/images/gg (2).jpg"
img = image.load_img(img_path,target_size=(256,256))
arr = image.img_to_array(img)
t_img = tf.convert_to_tensor(arr)
print(t_img.shape) # Returns (256,256,3)
# Client testing
client = Client("brain") # Custom Class. Contains model: Sequential (compiled and trained)
client.predict(img=t_img) # Calls self.model.predict(t_img)

However I get the following error:

Invalid input shape for input Tensor("data:0", shape=(32, 256, 3), dtype=float32). Expected shape (None, 256, 256, 3), but input has incompatible shape (32, 256, 3)

I have an input layer in the trained model which has input_shape=[256,256,3] (comes from image width, height, and rgb values)

Can you help me understand the issue and solve it?

6
  • Are you sure that your custom class is correct? For some reasen, it appears the the first dimension of the input is divided by 8, but it is difficult to give further help without knowing what happens in client. Commented Oct 27, 2024 at 13:27
  • 3
    You are missing the batch dimension, that's why it does not work. Commented Oct 27, 2024 at 15:49
  • @Dr.Snoopy Please don't answer in the comments. Commented Oct 29, 2024 at 16:00
  • my usual trick is I formulate questions like "where's your batch dimension?" if I think the issue is that minor ;) Commented Oct 29, 2024 at 16:17
  • 1
    @TylerH This has been asked and answered so many times, it should be marked as a duplicate. Commented Oct 29, 2024 at 17:00

1 Answer 1

1

Dr. Snoopy already gave the answer in the comments, but for the sake of completeness a short solution copied from the TF load_image page:

image = keras.utils.load_img(image_path)
input_arr = keras.utils.img_to_array(image)
input_arr = np.array([input_arr])  # Convert single image to a batch.
predictions = model.predict(input_arr)

model.predict() expects batches of images. This solultion would transform your (256, 256, 3) shape to (1, 256, 256, 3). There are also other solutions, e.g. with tf.expand_dims(image, 0) if you rather want to work with tensors directly instead of arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

I solved this issue few days back, but totally forgot to come back to the platform to share it. I don't know why I'm disabled from answering my own question, but I used Image.open, resize, then Image.convert to 'RGB'. After I turned it into a np.array, then to tensor. At the end I used tensor.expand_dims to add an outer dimension of length 1. It worked as usual. The above solution is also valid and I wanted to highlight that here as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.