简体   繁体   English

ValueError:层“顺序”的输入 0 与层不兼容:预期形状=(无,32,32,3),找到形状=(32,32,3)

[英]ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)

I made a model in another code and I would like to make prediction with it using an image.我在另一个代码中制作了一个模型,我想使用图像对其进行预测。

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)

I am getting an error where it is expecting shape=(None, 32, 32, 3) but found a shape that is similar but missing the None parameter, shape=(32, 32, 3).我在期望 shape=(None, 32, 32, 3) 的地方收到错误,但发现了一个相似但缺少 None 参数的形状,shape=(32, 32, 3)。 To me it looks like the load_img() is not loading the correct shape.对我来说, load_img()似乎没有加载正确的形状。 What solutions can I apply?我可以应用哪些解决方案?

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)

I have tried changing the target_size=(32,32) to target_size=(224,244) and adjusted the model accordingly.我尝试将target_size=(32,32)更改为target_size=(224,244)并相应地调整模型。 But the error changed to expected shape=(None, 224, 224, 3), found shape=(32, 224, 3) instead.但是报错改成expected shape=(None, 224, 224, 3), found shape=(32, 224, 3)

After the code x = img_to_array(img) add code在代码x = img_to_array(img)之后添加代码

x=np.expand_dims(x, axis=0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError: 层“sequential_32”的输入 0 与层不兼容:预期形状=(None, 3, 1),发现形状=(32, 0, 1) - ValueError: Input 0 of layer "sequential_32" is incompatible with the layer: expected shape=(None, 3, 1), found shape=(32, 0, 1) TensorFlow ValueError: Input 0 is in compatible with layer model_1: expected shape=(None, 32, 32, 1), found shape=(1, 1, 32, 32) - TensorFlow ValueError: Input 0 is incompatible with layer model_1: expected shape=(None, 32, 32, 1), found shape=(1, 1, 32, 32) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状=(无,128,128,3),找到形状=(32,128,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(32, 128, 3) ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[None, 32, 32] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 32, 32] ValueError:layersequential_32 的输入 0 与 layer 不兼容::预期 min_ndim=3,发现 ndim=2。 收到的完整形状:[无,256] - ValueError: Input 0 of layer sequential_32 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 256] ValueError:层顺序的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。 收到的完整形状:[32, 64, 3] - ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [32, 64, 3] 层“sequential_14”的输入 0 与层不兼容:预期形状=(None, 256, 256, 3),找到的形状=(None, 32, 32, 3) - nput 0 of layer "sequential_14" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 32, 32, 3) 层“lstm”的输入 0 与层不兼容:预期形状=(1, None, 1),找到的形状=(32, 9, 1) - Input 0 of layer "lstm" is incompatible with the layer: expected shape=(1, None, 1), found shape=(32, 9, 1) ValueError:layersequence_4 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(32、224、3) - ValueError: Input 0 of layer sequential_4 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (32, 224, 3) ValueError: 层 max_pooling1d 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:(无、128、1、32) - ValueError: Input 0 of layer max_pooling1d is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 128, 1, 32)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM