CNN keras手写识别具有较高的准确性,但预测效果较差 [英] CNN keras hand written recognition has high accuracy but poor predictions
问题描述
我基本上是在为一个学校项目做这件事,并遵循一些指南来使用CNN建立神经元网络.我正在使用的库是cv2,NumPy,TensorFlow和matplotlib.我目前面临的问题是我的网络具有很高的准确性,但预测却很糟糕.我确保图片是反向的和28x28.我还将可预测的图像数量从5个扩展到10个.我也尝试添加更多的图层,但均无济于事.如果有人可以帮助我,那就太好了!我对此也很陌生,所以请尽你所能!
I am basically doing this for a school project and followed some guides to make a neuron network using CNN. Libraries I am using are cv2, NumPy, TensorFlow, and matplotlib. The problem currently I am facing is that my network has high accuracy but very bad predictions. I made sure the pictures are inverted and 28x28. I also expand the number of images to predict from 5 to 10. I also tried adding more layers but didn't help either. If anyone can help me out would be awesome! I am also very new to this so please explain the best you can!
输出示例:如您所见,手写效果不错或任何东西,但仍然无法预测它是6而是1.
这是代码:
import cv2
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(a_train, b_train), (a_test, b_test) = mnist.load_data()
a_train = tf.keras.utils.normalize(a_train, axis=1)
a_test = tf.keras.utils.normalize(a_test, axis=1)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(units=255, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units=255, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units=20, activation=tf.nn.softmax))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(a_train, b_train, epochs=50)
lost, accuracy = model.evaluate(a_train, b_train)
print(lost)
print(accuracy)
model.save('test.model')
for x in range(1,11):
img = cv2.imread(fr'C:\Users\Eric\PycharmProjects\pythonProject2\test.model\{x}.png')[:,:,0]
img = np.invert(np.array([img]))
prediction = model.predict(img)
print(f'My Guess is: {np.argmax(prediction)}')
plt.imshow(img[0], cmap=plt.cm.binary)
plt.show()
我尝试做的事情:我尝试添加更多的层,假设它将进行训练并具有更好的预测.我添加了更多样本数,以查看是否可以有更高的预测.我从5上升到10,但仍然是正确预测的20%.我已经尝试过更改Epoch,并尝试了更多的批处理大小,但还是没有用.
Somethings I tried doing: I tried adding more layers assuming that it will train and have a better prediction. I added more samples numbers to see if I can have a higher prediction. I went from 5 to 10 but still a 20% right prediction. I have tried changing about of Epoch and tried more batch size but also didn't work.
在这一点上,我几乎陷入了困境,尽我所能来理解它,但根本无法改进它.如果有人有任何提示,请告诉我!
I am pretty much stuck at this point trying my best to understand it but not able to improve it at all. If anyone has any tips, please let me know!
推荐答案
预测时需要对图像进行归一化. cv2.imread
创建一个从0到255的数组.您可以通过将 img
除以 255进行规范化.
You need to normalize your images when predicting. cv2.imread
creates an array from 0 to 255. You can normalize it by dividing img
by 255.
您用来预测的图片在黑色背景上也应该有白色文字.
Your image you use to predict should also have white text on a black background.
最后,您不需要 np.invert
.
所以您的代码应该是
for x in range(1, 11):
img = np.expand_dims(cv2.imread(f'C:\Users\Eric\PycharmProjects\pythonProject2\test.model\{x}.png')[:, :, 0], 0) / 255.
prediction = model.predict(img)
print(f'My Guess is: {np.argmax(prediction)}')
plt.imshow(img[0], cmap=plt.cm.binary)
plt.show()
这篇关于CNN keras手写识别具有较高的准确性,但预测效果较差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!