尝试使用keras mnist和python将手写数字转换为整数(错误)



所以我目前正试图将手写数字转换为python中的整数,但我只是得到了很多numpy数组等的错误。我跟随了很多教程,并试图从他们编码一些东西,但它只是不想工作。

我有这个代码来训练我的模型:

from keras import Input, Model
from keras.layers import Activation, Dense
from keras.utils import to_categorical
from tensorflow import keras
import os
from PIL import Image
import numpy as np
def main():
mnist = keras.datasets.mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
num_train = X_train.shape[0]
num_test = X_test.shape[0]
img_height = X_train.shape[1]
img_width = X_train.shape[2]
X_train = X_train.reshape((num_train, img_width * img_height))
X_test = X_test.reshape((num_test, img_width * img_height))
y_train = to_categorical(y_train, num_classes=10)
y_test = to_categorical(y_test, num_classes=10)
num_classes = 10
xi = Input(shape=(img_height*img_width,))
xo = Dense(num_classes)(xi)
yo = Activation('softmax')(xo)
model = Model(inputs=[xi], outputs=[yo])
model.summary()
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, y_train,
batch_size=128,
epochs=20,
verbose=1,
validation_split=0.1)
score = model.evaluate(X_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
model.save("mnist_model.h5")

if __name__ == "__main__":
main()

我认为这段代码是有效的,因为它给了我一个有效的文件,但是当我尝试用下面的代码用这个模型预测一个数字时,无论我尝试什么,它都会给我带来大量的错误…

主代码(有更多的例子,例如从一个整数中获得单个数字,但我不认为这是问题):

from keras.saving.save import load_model
from tensorflow import keras
import os
from PIL import Image, ImageOps
import numpy as np
model = load_model("mnist_model.h5")
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

def predict_digit(img):
# # resize image to 28x28 pixels
img = img.resize((28, 28))
# convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
# reshaping to support our model input and normalizing
img = img / 255.0
img = img.reshape((1,784))
# predicting the class
res = model.predict(img)
# print(res)
return np.argmax(res), max(res)
digit, acc = predict_digit(digit_image)
print(str(digit) + ', ' + str(int(acc * 100)) + '%')

当我运行它给了我一个错误的代码行:

打印(str(数字 ) + ', ' + str (int (acc * 100 )) + '%')

错误是:

TypeError:只有size-1的数组可以转换为Python标量

但是我的代码可能有更多的问题:/所以我希望有人能帮助我!

该模型期望28x28=784像素的输入,但用img = img.reshape(1, 28)重塑图像会降低维数,使其不兼容。试着注释掉这个层,看看会发生什么!

最新更新