Python 检查时出错:预期密集_3_输入有2维,但得到了形状为(28,28,1)的数组
我正在写一个神经元来确定手写数字Python 检查时出错:预期密集_3_输入有2维,但得到了形状为(28,28,1)的数组,python,neural-network,keras,anaconda,Python,Neural Network,Keras,Anaconda,我正在写一个神经元来确定手写数字 import numpy as np from keras.utils import np_utils from keras.models import model_from_json from keras.preprocessing import image import matplotlib.pyplot as plt json_file = open("mnist_model.json", "r") loaded_model_json = json_fi
import numpy as np
from keras.utils import np_utils
from keras.models import model_from_json
from keras.preprocessing import image
import matplotlib.pyplot as plt
json_file = open("mnist_model.json", "r")
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("mnist_model.h5")
loaded_model.compile(loss= "categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
img_path ="5.png"
img = image.load_img(img_path, target_size=(28,28), grayscale=True)
plt.imshow(img, cmap='gray')
plt.show
x =image.img_to_array(img)
x = 255 - x
x/= 255
np.expand_dims(x, axis=0)
prediction = loaded_model.predict(x)
prediction = np_utils.categorical_pobabs_to_classes(prediction)
print(prediction)
我所做的只是教她如何使用它,但问题就出来了:
1.结果是一个图形和一个错误
ValueError:检查时出错:预期密集的\u 3\u输入有两个维度,但得到的数组的形状为(28,28,1)
在第行'img=image.load\u img(img\u路径,target\u size=(28,28),grayscale=True)'我认为您的错误在这一行
np.expand_dims(x, axis=0)
应该是:
x = np.expand_dims(x, axis=0)
我认为这是你的错
np.expand_dims(x, axis=0)
应该是:
x = np.expand_dims(x, axis=0)
此错误发生在哪一行?行'img=image.load_img(img_路径,目标大小=(28,28),灰度=True)')您能提供图像文件吗?@KenWei[link]()。。。。。。我确信错误在加载的模型中。预测(x)。这是一个非常典型的错误。您的模型需要形状
(28,28)
,但您正在使用形状(28,28,1)
传递输入图像。只需在模型中相应地调整input_shape
。此错误发生在哪一行?line'img=image.load_img(img_path,target_size=(28,28),grayscale=True)')您能提供图像文件吗?@KenWei[link]()。。。。。。我确信错误在加载的模型中。预测(x)。这是一个非常典型的错误。您的模型需要形状(28,28)
,但您正在使用形状(28,28,1)
传递输入图像。只需在模型中相应地调整输入形状
。