神经网络无法识别手写数字28*28像素MNIST数据集训练

问题描述 投票:0回答:1

我已经完成了 YT 教程 https://www.youtube.com/watch?v=bte8Er0QhDg&t=1159s,关于创建识别手写数字的神经元网络。它使用 mnist 数据集进行训练,并且我的网络可以正确识别它们。但是当我向这个网络展示我自己的手写数字时,它无法识别它们。我做了所有事情,从询问 chatgpt 到重写重新更改参数等。我的图像具有完全相同的形状并且看起来相同,但它只是无法识别我的数字......看看我的代码缺少什么,为什么我的数字不被识别,如果网络运作良好吗?

import cv2
import numpy as np
import tensorflow as tf

# My own data
model = tf.keras.models.load_model("handwritten.model")

img = cv2.imread("Digits/digit11.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_flat = gray.reshape(28, 28)

prediction = model.predict(np.expand_dims(img_flat, axis=0))
print(f"This digit is probably a {np.argmax(prediction)}")

# MNIST Data
prediction = model.predict(np.expand_dims(x_train[27], axis=0))
print(f"This digit is probably a {np.argmax(prediction)}")
python keras mnist
1个回答
0
投票

实际上问题出在读取图像文件上。 Cv2 采用像素的倒数编号。我的意思是,零中是 255,而 255 是图像中的零。所以你必须颠倒它们。

import cv2
import numpy as np
import tensorflow as tf
from keras.layers import Flatten, Dense

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()


x_train = x_train.astype(float)
x_test = x_test.astype(float)

x_train /= 255.0
x_test /= 255.0

model = tf.keras.models.Sequential()
model.add(Flatten(input_shape=(28,28)))
model.add(Dense(128, activation="relu"))
model.add(Dense(128, activation="relu"))
model.add(Dense(10, activation="softmax"))
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", 
metrics=["accuracy"])

model.fit(x_train, y_train, epochs=3)

# My own data
# In[]

img = cv2.imread("Digits/digit11.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_flat = 255 - gray.reshape(28, 28).astype(float) # Here, you invert the values

img_flat /= 255.

prediction = model.predict(np.expand_dims(img_flat, axis=0))
print(f"This digit is probably a {np.argmax(prediction)}")
© www.soinside.com 2019 - 2024. All rights reserved.