因此,我得到了一个循序渐进的模型,主要是跟随this视频一起观看。我还用JS完成了一个绘图应用程序,并使用JQuery将画布的内容发送到烧瓶服务器。所有这些似乎都可以正常工作,但是当我从画布上给模型提供图像并调用预测时,它总是会给我相同的准确响应。每个索引的值始终相同。
我想念一些简单的东西吗?我是在服务器中称预测为错误,还是与模型本身有关?它可以在笔记本中获得正确的预测。
感谢您的时间!
烧瓶服务器:
model = load_model('MyModel.h5')
imageWidth = 28
imageHeight = 28
dim = (imageWidth, imageHeight)
# Create the flask web application
app = fl.Flask(__name__)
@app.route('/uploadimage', methods = ['GET', 'POST'])
def uploadimage():
# Get the image from the request
theImage = fl.request.values.get("theImage", "")
#Decode the string to an image
decodedimg = base64.b64decode(theImage[22:])
# Save the image
with open ("theImage.png", "wb") as f:
f.write(decodedimg)
# Open the image from the request as originalImage
originalImage = Image.open("theImage.png")
# Resize it
resizedImage = ImageOps.fit(originalImage, dim, Image.ANTIALIAS)
# Confirm the dimensions of the resized image
w1, h1 = resizedImage.size
print(w1, h1)
# Save it locally
resizedImage.save("resizedImage.png", quality=100, optimize=True)
# Convert to grayscale and then convert that to an array
grayscaleImage = ImageOps.grayscale(resizedImage)
grayscaleArray = np.array(grayscaleImage)
grayscaleArray.shape # Gives (20, 20)
grayscaleArray = grayscaleArray.reshape(1, 28, 28)
setPrediction = model.predict(grayscaleArray)
print(setPrediction) # Always gives back same values
getPrediction = np.array(setPrediction[0])
predictedNumber = str(np.argmax(getPrediction))
print(predictedNumber) # Always '5'
return predictedNumber
型号:
model = kr.models.Sequential() # Create a new sequential neural network
model.add(kr.layers.Flatten()) # Input layer
model.add(kr.layers.Dense(128, activation="relu")) # 128 neurons and the 'basic' activation function.
model.add(kr.layers.Dense(128, activation="relu"))
model.add(kr.layers.Dense(10, activation="softmax"))
model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=["accuracy"]) # Played around with 'sgd' and 'rmsporp' optimizer also.
model.fit(X_train, y_train, epochs=3)
val_loss, val_acc = model.evaluate(X_test, y_test)
print(val_loss, val_acc)