gwoonryan
gwoonryan

Reputation: 46

Trying to take handwritten digits and turning them into an integer with keras mnist and python (error)

so i am currently trying to take handwritten digits and turning them into an integer in python, but i just get a lot of errors with numpy arrays and such. i followed a lot of tutorials and tried to code something from them but it just doesn't want to work.

i have this code for training my model:

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()

And i think this code just works because it gives me a valid file, but when i try to predict a digit with this model with the code below, it gives me a ton of errors whatever i try...

main code (there is more to for example get the individual digits out of a whole number, but i don't think that is te problem):

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)) + '%')

and when i run the code it gives me an error on the line:

print(str(digit) + ', ' + str(int(acc * 100)) + '%')

the error is:

TypeError: only size-1 arrays can be converted to Python scalars

But there are probably some more things wrong with my code :/ so i hope someone can help me !

Upvotes: 0

Views: 148

Answers (1)

pikruse
pikruse

Reputation: 11

The model expects an input of 28x28=784 pixels, but reshaping the image with img = img.reshape(1, 28) reduces the dimension, making it incompatible. Try commenting out this layer and see what happens!

Upvotes: 1

Related Questions