2017-10-19 59 views
1

我寫一個神經元用於確定手寫數字錯誤時檢查:預計dense_3_input有2個維度,但得到陣列形狀(28,28,1)

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.結果是一個曲線圖,並在線路'img = image.load_img錯誤ValueError: Error when checking : expected dense_3_input to have 2 dimensions, but got array with shape (28, 28, 1)(img_path,target_size =(28,28),灰度= TRUE)'

+0

在哪條線路會出現這種錯誤發生的呢? –

+0

Line'img = image.load_img(img_path,target_size =(28,28),grayscale = True)' – StarLord

+0

您可以提供圖像文件嗎? –

回答

0

我認爲你的錯在這一行

np.expand_dims(x, axis=0) 

它應該是:

x = np.expand_dims(x, axis=0) 
相關問題