在开始之前,先介绍一些可能相关的事情:
numpy数组matplotlibimreadtensorflow然后分别使用的image.resize方法和方法将 RGB 图像重新整形并转换为灰度图像image.rgb_to_grayscale。这是我的模型:
model = Sequential(
[
tf.keras.Input(shape=(784,),),
Dense(200, activation= "relu"),
Dense(150, activation= "relu"),
Dense(100, activation= "relu"),
Dense(50, activation= "relu"),
Dense(26, activation= "linear")
]
)
Run Code Online (Sandbox Code Playgroud)
神经网络在数据集上的准确率达到 98.9%。但是,当我尝试使用自己的图像时,它总是将输入分类为“A”。
我什至达到了反转图像颜色的程度(黑到白,反之亦然;原始灰度图像的字母为黑色,其余部分为白色)。
img = plt.imread("20220922_194823.jpg")
img = tf.image.rgb_to_grayscale(img)
plt.imshow(img, cmap="gray")
Run Code Online (Sandbox Code Playgroud)
显示此图像。
img.shape回报TensorShape([675, 637, 1])
img = 1 - img
img = tf.image.resize(img, [28,28]).numpy()
plt.imshow(img, cmap="gray")
Run Code Online (Sandbox Code Playgroud)
这是结果img = 1-img
我怀疑神经网络不断将输入图像分类为“A”,因为某些像素不是完全黑/白的。
但它为什么要这么做呢?将来我该如何避免这个问题?