matplotlib Tensorflow数组大小不匹配,但应该匹配

vyu0f0g1  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(89)

我正在尝试建立一个人工智能模型来识别tensorflow 中的皮肤癌图像,不幸的是,我在尝试预测图像时发现了一个错误,它说形状不匹配,但我之前检查过了,形状是一样的,它看起来像是在提供预测时重塑了它。
下面是代码

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)

这是输出

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)

如果你想看到完整的代码,这里有一个笔记本视图的链接:notebook preview
我试着改变它的形状,也转置这个数组,因为我在类似的问题中发现了这个,但是没有任何帮助。

eqqqjvef

eqqqjvef1#

检查此代码

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)

相关问题