![](/img/trans.png)
[英]ValueError: Input 0 of layer "sequential_32" is incompatible with the layer: expected shape=(None, 3, 1), found shape=(32, 0, 1)
[英]ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)
我在另一個代碼中制作了一個模型,我想使用圖像對其進行預測。
import numpy as np
from keras.preprocessing.image import img_to_array, load_img
from tensorflow import keras
model = keras.models.load_model('Faces/model/alexnet')
# load the image
img = load_img('Faces/text.jpg', target_size=(32, 32))
# prepare the image
x = img_to_array(img)
# perform prediction
preds = model.predict(x)
print('Predicted:', preds)
我在期望 shape=(None, 32, 32, 3) 的地方收到錯誤,但發現了一個相似但缺少 None 參數的形狀,shape=(32, 32, 3)。 對我來說, load_img()
似乎沒有加載正確的形狀。 我可以應用哪些解決方案?
Traceback (most recent call last):
File "d:\Projects\Experiment\findresult.py", line 12, in <module>
preds = model.predict(x)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\func_graph.py", line 1129, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py", line 1621, in predict_function
*
return step_function(self, iterator)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py", line 1611, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py", line 1604, in run_step **
outputs = model.predict_step(data)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py", line 1572, in predict_step
return self(x, training=False)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\input_spec.py", line 263, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)
我嘗試將target_size=(32,32)
更改為target_size=(224,244)
並相應地調整模型。 但是報錯改成expected shape=(None, 224, 224, 3), found shape=(32, 224, 3)
。
在代碼x = img_to_array(img)
之后添加代碼
x=np.expand_dims(x, axis=0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.