[英]Error when checking : expected dense_3_input to have 2 dimensions, but got array with shape (28, 28, 1)
I am writing a neuron for determining the handwritten figures 我正在写一个神经元来确定手写图形
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)
All I did was teach her to use it, but then the problem got out: 1.The result is a graph and an error ValueError: Error when checking : expected dense_3_input to have 2 dimensions, but got array with shape (28, 28, 1)
in Line ´img = image.load_img(img_path, target_size=(28,28), grayscale=True)´ 我所做的只是教她使用它,但是问题出了:1.结果是一个图形,并显示一个错误
ValueError: Error when checking : expected dense_3_input to have 2 dimensions, but got array with shape (28, 28, 1)
在“ img = image.load_img(img_path,target_size =(28,28),grayscale = True)”行中
I think your fault is in this line 我认为你的错在于
np.expand_dims(x, axis=0)
it should be: 它应该是:
x = np.expand_dims(x, axis=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.