MNIST 模型错误地预测了测试图像,即使它具有很高的训练和测试准确性
MNIST model predicting test images incorrectly even though it had high training and testing accuracy
我正在尝试创建一个可以预测手写数字的模型。我已经使用以下架构训练了一个 Keras 模型,并在测试期间看到了接近 99% 的准确率。但是,我决定从互联网上截取一些黑白数字的照片,并使用 OpenCV 将它们的大小最小化为 28x28,以尝试使用我训练的模型来预测它们。在测试了所有 10 个数字后,我的模型仅正确预测了 4/10,这意味着它有 40% 的分数。
为什么会发生这种情况,我该如何解决?
这是我正在使用的 MNIST 数据集:
https://www.kaggle.com/oddrationale/mnist-in-csv
以下是我正在测试的图像:
https://drive.google.com/open?id=1G0CMK0ZPI1JIX2cYXNRt3Qsq_YpjlMjX
#architecture
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
#Code for minmizing
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img,(28,28))
#turning the 28x28 array into a (1, 28, 28, 1) numpy array for the model to process
#dividing each value by 255. During the training, each grayscale pixel value was treated the same way
for i in range(28):
temp = []
for x in range(28):
temp.append([img[i][x]/255])
pendo.append(temp)
img = np.array([pendo]).astype("float32")
#predicting the image
model = load_model('MNIST.h5')
classes = model.predict(img)
MNIST 不是一个旨在学习完全通用数字识别模型的数据集,它只是一个学术基准,一个非常古老的基准,因此获得接近 99% 的任何一种测试准确率都非常容易,但这并不意味着该模型将完全推广。
归根结底这不是程序的问题,这一点应该清楚,不是代码的问题,是数据的问题。您的图像与 MNIST 训练集中的图像相差太大。
我正在尝试创建一个可以预测手写数字的模型。我已经使用以下架构训练了一个 Keras 模型,并在测试期间看到了接近 99% 的准确率。但是,我决定从互联网上截取一些黑白数字的照片,并使用 OpenCV 将它们的大小最小化为 28x28,以尝试使用我训练的模型来预测它们。在测试了所有 10 个数字后,我的模型仅正确预测了 4/10,这意味着它有 40% 的分数。
为什么会发生这种情况,我该如何解决?
这是我正在使用的 MNIST 数据集: https://www.kaggle.com/oddrationale/mnist-in-csv
以下是我正在测试的图像: https://drive.google.com/open?id=1G0CMK0ZPI1JIX2cYXNRt3Qsq_YpjlMjX
#architecture
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
#Code for minmizing
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img,(28,28))
#turning the 28x28 array into a (1, 28, 28, 1) numpy array for the model to process
#dividing each value by 255. During the training, each grayscale pixel value was treated the same way
for i in range(28):
temp = []
for x in range(28):
temp.append([img[i][x]/255])
pendo.append(temp)
img = np.array([pendo]).astype("float32")
#predicting the image
model = load_model('MNIST.h5')
classes = model.predict(img)
MNIST 不是一个旨在学习完全通用数字识别模型的数据集,它只是一个学术基准,一个非常古老的基准,因此获得接近 99% 的任何一种测试准确率都非常容易,但这并不意味着该模型将完全推广。
归根结底这不是程序的问题,这一点应该清楚,不是代码的问题,是数据的问题。您的图像与 MNIST 训练集中的图像相差太大。