2017-10-19 7 views
1

私は私がしたすべては彼女を教えた手書き数字エラーチェックするとき:2次元を持つことが期待dense_3_inputを、しかし形状を持つ配列を得た(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)、グレースケール=真) "

+0

どのラインでこのエラーが発生しましたか? –

+0

行'img = image.load_img(img_path、target_size =(28,28)、グレースケール= True) ' – StarLord

+0

イメージファイルを提供できますか? –

答えて

0

ですあなたの責任はこの行にあると思います。

np.expand_dims(x, axis=0) 

それは次のようになります。

x = np.expand_dims(x, axis=0) 
関連する問題