简体   繁体   English

为什么我的神经网络预测在应用于 MNIST 手绘图像时是正确的,但在应用于我自己的手绘图像时却不正确?

[英]Why are my neural network predictions correct when applied to MNIST hand-drawn images, but incorrect when applied to my own hand-drawn images?

Background:背景:

I am trying to create a basic neural network to recognize hand-drawn images using the MNIST dataset.我正在尝试创建一个基本的神经网络来识别使用 MNIST 数据集的手绘图像。 I have things working when training/predicting against the MNIST data.在针对 MNIST 数据进行训练/预测时,我的工作正常。

Goal:目标:

I would like to start applying the model to non-MNIST images (ie hand-drawn images that I create myself).我想开始将 model 应用于非 MNIST 图像(即我自己创建的手绘图像)。

Problem:问题:

Every prediction of hand-drawn images that I create has ended up being incorrect (which is odd because predictions against MNIST images are 95% accurate).我创建的对手绘图像的每一个预测最终都是不正确的(这很奇怪,因为针对 MNIST 图像的预测准确率为 95%)。

Code:代码:

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import cv2

mnist = tf.keras.datasets.mnist # 28x28 images of handwritten digits (0-9)

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

x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_test, axis=1)

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))

model.compile(optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'])

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

val_loss, val_acc = model.evaluate(x_test, y_test)
print(val_loss, val_acc)

# prediction from MNIST dataset
index_of_mnist_img = 0
predictionsA = model.predict([x_test])
print(np.argmax(predictionsA[index_of_mnist_img]))
plt.imshow(x_test[index_of_mnist_img], cmap = plt.cm.binary)
plt.show()

# prediction from my own hand-drawn image (THIS IS WHERE THINGS START GOING WRONG)
img = cv2.imread('4.png')
img = cv2.resize(img, (28,28))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = np.reshape(img, [1,28,28])
predictionsB = model.predict(img)
print(np.argmax(predictionsB[0]))
plt.imshow(predictionsB[0])
plt.show()

Any ideas?有任何想法吗?

I believe that you need to invert the colormap for your new (hand-drawn) image.我相信您需要为新(手绘)图像反转颜色图。

When I look at MNIST example images, I see something like this:当我查看 MNIST 示例图像时,我看到如下内容:

# show mnist image
index_of_mnist_img = 0
plt.imshow(x_test[index_of_mnist_img], cmap = plt.cm.binary)
plt.show()

7

However, if I make an example hand-written digit, and read it in as you have, I see an inverted image.但是,如果我制作一个手写数字示例,并按照您的方式读入,我会看到一个倒置的图像。

img = cv2.imread("4.png")
img = cv2.resize(img, (28,28))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(img, cmap = plt.cm.binary)

4

You can invert the image with OpenCV by adding one line, cv2.bitwise_not() .您可以通过添加一行cv2.bitwise_not()来使用 OpenCV 反转图像。

img = cv2.imread(r"4.png")
img = cv2.resize(img, (28,28))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img= cv2.bitwise_not(img) # invert image
plt.imshow(img, cmap = plt.cm.binary)

4_倒置

When I invert the image, then I get correct predictions from the neural network you have trained above.当我反转图像时,我会从您在上面训练的神经网络中得到正确的预测。

predictionsB = model.predict(img)
print(np.argmax(predictionsB[0]))
4

Do you need consider how to the train/test was created.您是否需要考虑如何创建训练/测试。 This traning can be overrfited, with this, your give a good acurracity in train, but this is not trully whith test data.这种训练可能会被过度训练,这样,你就可以在训练中给出一个很好的准确性,但这并不是真正的测试数据。

Is also possible uses image augmentation to increase your dataset, because MNIST is a relative small dataset, and the numbers is centralized, the image does not have noise and etc.也可以使用图像增强来增加你的数据集,因为 MNIST 是一个相对较小的数据集,并且数字是集中的,图像没有噪声等。

And another concepts can be used, like Dropouts: The idea behind Dropouts is that they remove a random number of neurons in your neural network.还可以使用另一个概念,例如 Dropouts:Dropouts 背后的想法是它们会删除神经网络中随机数量的神经元。 This works very well for two reasons: The first is that neighboring neurons often end up with similar weights, which can lead to overfitting, so dropping some out at random can remove this.这有两个原因非常有效:首先是相邻神经元通常以相似的权重结束,这可能导致过度拟合,因此随机删除一些可以消除这种情况。

Another thing is use Pooling Layers to reduce size of representation, with this it will detect more features.另一件事是使用池化层来减少表示的大小,这样它将检测到更多的特征。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 TensorFlow 号码识别中使用自己的手绘图像 - How do I use my own Hand-Drawn Image in TensorFlow Number Recognition 寻找马虎手绘矩形的属性 - Finding properties of sloppy hand-drawn rectangles 如何从手绘电路的扫描图像中检测逻辑门? - How to detect logic gates from scanned images of hand drawn circuits? Tensorflow-用我自己的图像测试mnist神经网络 - Tensorflow - Testing a mnist neural net with my own images 错误的预测与自己的类似 mnist 的图像 - Wrong predictions with own mnist-like images 如何将 Keras MNIST 数据集与我自己的 MNIST 图像相结合? - How to combine Keras MNIST dataset with my own MNIST images? 尽管给出了正确的预测,为什么我的神经网络成本函数会增加? - Why is my neural network cost function increasing despite giving correct predictions? 为什么我的 neural.network 预测不正确的 class label 对于属于一个 class 的测试图像,尽管具有很高的验证准确性? - Why does my neural network predict the incorrect class label for test images belonging to one class, despite having a high validation accuracy? 为什么我的神经网络在训练后会做出如此不准确的预测? - Why does my neural network make such inaccurate predictions after training? 当我用 keras 训练网络时,为什么我的预测形状不准确? - When i train a network with keras, why are the shape of my predictions not accurate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM