我正在尝试使用 tensorflow 的 keras 神经网络来识别手写数字。但是我知道为什么当我调用时predict()
,它为所有输入图像返回相同的结果。
这是代码:
### Train dataset ###
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train/255
x_test = x_test/255
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(units=128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units=10,activation=tf.nn.softmax))
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5)
结果如下所示:
Epoch 1/5
1875/1875 [==============================] - 2s 672us/step - loss: 0.2620 - accuracy: 0.9248
Epoch 2/5
1875/1875 [==============================] - 1s 567us/step - loss: 0.1148 - accuracy: 0.9658
Epoch 3/5
1875/1875 [==============================] - 1s 559us/step - loss: 0.0784 - accuracy: 0.9764
Epoch 4/5
1875/1875 [==============================] - 1s 564us/step - loss: 0.0596 - accuracy: 0.9817
Epoch 5/5
1875/1875 [==============================] - 1s 567us/step - loss: 0.0462 - accuracy: 0.9859
然后使用图像进行测试的代码如下:
img = cv.imread('path/to/1.png')
img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
img = cv.resize(img,(28,28))
img = np.array([img])
if cv.countNonZero((255-image)) == 0:
print('')
img = np.invert(img)
plt.imshow(img[0])
plt.show()
prediction = model.predict(img)
result = np.argmax(prediction)
print(prediction)
print(f'Result: {result}')
结果是:
[[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]]
Result: 3
[[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]]
Result: 3
与您在训练集上所做的一样,在推理时间内标准化您的数据
img = np.array([img]) / 255
查看此答案(推理)以获取更多详细信息。
根据您的第三条评论,这里有一些细节。
def input_prepare(img):
img = cv2.resize(img, (28, 28))
img = cv2.bitwise_not(img)
img = tf.cast(tf.divide(img, 255) , tf.float64)
img = tf.expand_dims(img, axis=0)
return img
img = cv2.imread('/content/1.png')
orig = img.copy() # save for plotting later on
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # gray scaling
img = input_prepare(img)
plt.imshow(tf.reshape(img, shape=[28, 28]))
plt.imshow(cv2.cvtColor(orig, cv2.COLOR_BGR2RGB))
plt.title(np.argmax(model.predict(img)))
plt.show()
它按预期工作。但是由于调整图像大小,数字会损坏并丢失其空间信息。这对于模型来说似乎没问题,但如果它变得更糟,那么模型将预测错误。一个案例
并且模型对此预测是错误的。
plt.imshow(cv2.cvtColor(orig, cv2.COLOR_BGR2RGB))
plt.title(np.argmax(model.predict(img)))
plt.show()
为了解决这个问题,我们可以cv2.erode
在调整大小后申请添加一些像素,例如
def input_prepare(img):
img = cv2.resize(img, (28, 28))
img = cv2.erode(img, np.ones((2, 2)))
img = cv2.bitwise_not(img)
img = tf.cast(tf.divide(img, 255) , tf.float64)
img = tf.expand_dims(img, axis=0)
return img
也许不是最好的方法,但现在模型会更好地理解。
plt.imshow(cv2.cvtColor(orig, cv2.COLOR_BGR2RGB))
plt.title(np.argmax(model.predict(img)))
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句