顺序层的输入 0 与层不兼容:预期 ndim=4,发现 ndim=2。已收到完整形状:[None, 67500]
Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 67500]
我在从 cnn 模型进行预测时遇到问题
model structure
from tensorflow.keras.preprocessing import image
import numpy as np
img = image.load_img("test/apple/apple.jpg", target_size=(150,150))
x=image.img_to_array(img) / 255
x = x.reshape(1,-1)
model.predict(x)
您正在拼合图像,但您的模型 takes batch-wise image data
。
使用 np.expand_dims 向调整后的图像添加一个维度,并传递给模型进行预测。
这样试试
img = image.load_img("test/apple/apple.jpg", target_size=(150,150))
x=image.img_to_array(img) / 255
resized_img_np = np.expand_dims(x,axis=0)
prediction = model.predict(resized_img_np)
我在从 cnn 模型进行预测时遇到问题
model structure
from tensorflow.keras.preprocessing import image
import numpy as np
img = image.load_img("test/apple/apple.jpg", target_size=(150,150))
x=image.img_to_array(img) / 255
x = x.reshape(1,-1)
model.predict(x)
您正在拼合图像,但您的模型 takes batch-wise image data
。
使用 np.expand_dims 向调整后的图像添加一个维度,并传递给模型进行预测。
这样试试
img = image.load_img("test/apple/apple.jpg", target_size=(150,150))
x=image.img_to_array(img) / 255
resized_img_np = np.expand_dims(x,axis=0)
prediction = model.predict(resized_img_np)