小编alt*_*udo的帖子

尽管神经网络在原始数据集上表现良好,但仍然对输入图像进行错误分类

链接到相关数据集

在开始之前,先介绍一些可能相关的事情:

  • 输入文件格式为 JPEG。我使用's将它们转换为numpy数组matplotlibimread
  • tensorflow然后分别使用的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”,因为某些像素不是完全黑/白的。

但它为什么要这么做呢?将来我该如何避免这个问题?

这是笔记本。

python neural-network tensorflow

3
推荐指数
1
解决办法
628
查看次数

标签 统计

neural-network ×1

python ×1

tensorflow ×1