0

I'm trying to build an ai model which recognizes skin cancer images in tensorflow. Unfortunately I found an error when trying to predict an image it says that shape doesn't match, but I checked it earlier and the shape is the same. It looks like it reshape it when providing it to predict.

Here is the code

test = tf.keras.utils.load_img('./thumbnails/malicious/ISIC_0029615.jpg')
test = tf.keras.utils.img_to_array(test)
test = tf.image.resize(test, (256, 256))
test /= 255
plt.imshow(test)
print(test.shape)
model.predict(test)

And here is the output

Traceback (most recent call last):
  at cell 21, line 7
  at /opt/python/envs/default/lib/python3.8/site-packages/keras/utils/traceback_utils.py, line 67, in error_handler(*args, **kwargs)
  at /opt/python/envs/default/lib/python3.8/site-packages/keras/engine/training.py, line 15, in tf__predict_function(iterator)
ValueError: in user code: File "/opt/python/envs/default/lib/python3.8/site-packages/keras/engine/training.py", line 1845, in predict_function * return step_function(self, iterator) File "/opt/python/envs/default/lib/python3.8/site-packages/keras/engine/training.py", line 1834, in step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) File "/opt/python/envs/default/lib/python3.8/site-packages/keras/engine/training.py", line 1823, in run_step ** outputs = model.predict_step(data) File "/opt/python/envs/default/lib/python3.8/site-packages/keras/engine/training.py", line 1791, in predict_step return self(x, training=False) File "/opt/python/envs/default/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler raise e.with_traceback(filtered_tb) from None File "/opt/python/envs/default/lib/python3.8/site-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility raise ValueError(f'Input {input_index} of layer "{layer_name}" is ' ValueError: Input 0 of layer "sequential_6" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(32, 256, 3) 

If you want to see a full code here is a link to notebook view: notebook preview

I'm trying to reshape it and also transpose this array because I found this in similar problem but nothing helps.

1 Answer 1

1

check this code

import numpy as np

test = tf.keras.utils.load_img('./thumbnails/malicious/ISIC_0029615.jpg')
test = tf.keras.utils.img_to_array(test)
test = tf.image.resize(test, (256, 256))
test /= 255

# Add a new dimension to make it a batch of size 1
test = np.expand_dims(test, axis=0)

plt.imshow(test[0])
print(test.shape)

model.predict(test)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.