Python 序列模型总是预测5

Python 序列模型总是预测5,python,tensorflow,flask,keras,neural-network,Python,Tensorflow,Flask,Keras,Neural Network,所以我有一个序列模型,主要是跟视频一起。我还用JS完成了一个绘图应用程序,并使用JQuery将画布的内容发送到flask服务器。所有这些看起来都很好,但当我给我的模型画布上的图像并调用predict时,它总是给我相同的准确响应。每个索引的值总是相同的 我错过了一些简单的东西吗?我在服务器中调用predict是错误的还是与模型本身有关?它可以在笔记本上正确地得到预测 谢谢你的时间 烧瓶服务器: model = load_model('MyModel.h5') imageWidth = 28 ima

所以我有一个序列模型,主要是跟视频一起。我还用JS完成了一个绘图应用程序,并使用JQuery将画布的内容发送到flask服务器。所有这些看起来都很好,但当我给我的模型画布上的图像并调用predict时,它总是给我相同的准确响应。每个索引的值总是相同的

我错过了一些简单的东西吗?我在服务器中调用predict是错误的还是与模型本身有关?它可以在笔记本上正确地得到预测

谢谢你的时间

烧瓶服务器:

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)

型号:

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)


你为什么不检查一下你的样本是什么样的,以确保你正确地创建了你的样本(训练和预测)。默认情况下,NN只会猜测训练输出的平均值,也许这就是发生的情况?还有一个原因是你为什么要使用稀疏猫交叉而不是非稀疏猫交叉?嘿,谢谢你的回复。是的,我已经检查了培训和测试数据,以验证其正确性。我只是想,我需要得到帮助的唯一相关部分是实际的模型。另外,我使用稀疏,因为这是它在视频中完成的方式!在处理了其他损失后,他们没有一个人让模型编译,并给出一个错误,说密集层应该有一个形状为10的数组,但是gt有一个形状为1的数组!根据你的模型,我不明白你为什么不能使用“分类交叉熵”。以后我可以试着自己运行代码。无论哪种方式,发生这种情况的一个原因是,您的输入和输出之间根本没有相关性。您的输入和输出是什么?此外,我不太熟悉的图像任务,但我会确保您创建您的像素阵列和缩放您的像素正确。。。再次尝试只是为了确保仍然得到相同的错误。输入为28x28 png图像。但是我把它改成了灰色的数组来调用它的predict。我试着打电话给你。在阵列上展平,但是模型说它需要一个三维阵列。如果没有模型中的展平层,我会更好吗?我看到这里是ImageOps.grayscale(resizedImage),您正在将图像转换为灰度。我假设您事先已经看过该图像,并且它是有效的。之后你将从(20,20)重塑为(1,28,28),你的理由是什么?我看不到你在哪里扩展数据?