我正在尝试使用Keras神经网络来识别画在画布上的数字图像,并输出相应的数字。我已经保存了神经网络,并使用Django来运行Web界面。但每次运行时,我都会遇到内部服务器错误,并且在服务器端代码中出现错误。错误信息显示异常:检查时出错:期望dense_input_1的形状为(None, 784),但得到的数组形状为(784, 1)。我的主要视图是
from django.shortcuts import renderfrom django.http import HttpResponseimport StringIOfrom PIL import Imageimport numpy as npimport refrom keras.models import model_from_jsondef home(request): if request.method=="POST": vari=request.POST.get("imgBase64","") imgstr=re.search(r'base64,(.*)', vari).group(1) tempimg = StringIO.StringIO(imgstr.decode('base64')) im=Image.open(tempimg).convert("L") im.thumbnail((28,28), Image.ANTIALIAS) img_np= np.asarray(im) img_np=img_np.flatten() img_np.astype("float32") img_np=img_np/255 json_file = open('model.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights("model.h5") # evaluate loaded model on test data loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) output=loaded_model.predict(img_np) score=output.tolist() return HttpResponse(score) else: return render(request, "digit/index.html")
我查看了以下链接:
编辑根据Rohan的建议,这是我的堆栈跟踪
内部服务器错误:/home/Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 149, in get_response response = self.process_exception_by_middleware(e, request) File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 147, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/vivek/keras/neural/digit/views.py", line 27, in homeoutput=loaded_model.predict(img_np) File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 671, in predictreturn self.model.predict(x, batch_size=batch_size, verbose=verbose) File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1161, in predictcheck_batch_dim=False) File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 108, in standardize_input_datastr(array.shape))Exception: Error when checking : expected dense_input_1 to have shape (None, 784) but got array with shape (784, 1)
另外,我有最初用来训练网络的模型。
import numpyfrom keras.datasets import mnistfrom keras.models import Sequentialfrom keras.layers import Densefrom keras.layers import Dropoutfrom keras.utils import np_utils# fix random seed for reproducibilityseed = 7numpy.random.seed(seed)(X_train, y_train), (X_test, y_test) = mnist.load_data()for item in y_train.shape: print itemnum_pixels = X_train.shape[1] * X_train.shape[2]X_train = X_train.reshape(X_train.shape[0], num_pixels).astype('float32')X_test = X_test.reshape(X_test.shape[0], num_pixels).astype('float32')# normalize inputs from 0-255 to 0-1X_train = X_train / 255X_test = X_test / 255print X_train.shape# one hot encode outputsy_train = np_utils.to_categorical(y_train)y_test = np_utils.to_categorical(y_test)num_classes = y_test.shape[1]# define baseline modeldef baseline_model(): # create model model = Sequential() model.add(Dense(num_pixels, input_dim=num_pixels, init='normal', activation='relu')) model.add(Dense(num_classes, init='normal', activation='softmax')) # Compile model model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model# build the modelmodel = baseline_model()# Fit the modelmodel.fit(X_train, y_train, validation_data=(X_test, y_test), nb_epoch=20, batch_size=200, verbose=1)# Final evaluation of the modelscores = model.evaluate(X_test, y_test, verbose=0)print("Baseline Error: %.2f%%" % (100-scores[1]*100))# serialize model to JSONmodel_json = model.to_json()with open("model.json", "w") as json_file: json_file.write(model_json)# serialize weights to HDF5model.save_weights("model.h5")print("Saved model to disk")
编辑我尝试将图像重塑为(1,784),但也失败了,出现了与此问题标题相同的信息
感谢帮助,请在评论中告诉我应该如何补充这个问题。
回答:
你要求神经网络评估784个案例,每个案例有一个输入,而不是一个案例有784个输入。我也遇到过同样的问题,我的解决方法是使用一个包含单个元素的数组,而这个元素本身是一个输入数组。请看下面的例子,第一个例子可以工作,而第二个例子会出现你遇到的问题。
model.predict(np.array([[0.5, 0.0, 0.1, 0.0, 0.0, 0.4, 0.0, 0.0, 0.1, 0.0, 0.0]]))model.predict(np.array([0.5, 0.0, 0.1, 0.0, 0.0, 0.4, 0.0, 0.0, 0.1, 0.0, 0.0]))
希望这也能解决你的问题 🙂