[Python] Why is every prediction for my MNIST digit project wrong?

Discussão em 'Python' iniciado por Stack, Agosto 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have done this basic project with Anaconda notebook. Everything runs fine, but every prediction with my own digit picture is wrong. I am using the MNIST Set for digital numbers and I am trying to paint my own digit with black background and white painting. But every prediction is wrong. Could one see what´s missing in the code?

    [​IMG]

    # Install TensorFlow
    import tensorflow as tf

    # Import matplotlib library
    import matplotlib.pyplot as plt

    #Import numpy
    import numpy as np

    #import cv
    import cv2

    #Dataset
    mnist = tf.keras.datasets.mnist

    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train, x_test = x_train / 255.0, x_test / 255.0
    model = tf.keras.models.Sequential([

    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation='softmax')])

    model.compile(optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'])
    model.fit(x_train, y_train, epochs=5)
    print("Evaluierung");
    model.evaluate(x_test, y_test)

    plt.imshow(x_train[1], cmap="gray") # Import the image
    plt.show() # Plot the image
    predictions = model.predict([x_train]) # Make prediction, works perfect
    print(np.argmax(predictions[1])) # Print out the number, works perfect

    # my own picture, black background, white color, 28 px * 28 px in size
    img = cv2.imread("bild1.png", cv2.IMREAD_GRAYSCALE)
    plt.imshow(img) # Import the image
    plt.show() # Plot the image

    cv2.imshow("image",img)
    cv2.waitKey(2000)
    img = img/255.0
    img = img.reshape(1,28,28)
    pred = model.predict_classes(img)
    print("Prediction: ", pred)


    Every prediction from the training and test data are correct, my own pictures are ALL wrong with NO error code! It would be great if you could help me

    Continue reading...

Compartilhe esta Página