【问题标题】:Keras image classification network always predicting one class, and stays at 50% accuracyKeras 图像分类网络总是预测一个类别,并且保持在 50% 的准确率
【发布时间】:2020-12-10 06:45:56
【问题描述】:

我一直在使用 Keras 网络对图像进行分类,以确定它们是否包含交通信号灯,但到目前为止,我的成功率为 0。我有一个包含 11000 多张图像的数据集,在我的第一次测试中,我使用了 240 张图像(或者更确切地说,每个图像的文本文件都带有灰度像素值)。只有一个输出 - 0 或 1 表示图像是否包含交通信号灯。

但是,当我运行测试时,它只预测了一个类。鉴于 53/240 幅图像有交通信号灯,它达到了大约 79% 的准确率,因为它一直都在预测 0。我读到这可能是由于数据不平衡,所以我缩小到只有 4 张图像 - 2 张有红绿灯,2 张没有。

即使进行了这个测试,它在 5 个 epoch 后仍然停留在 50% 的准确率;它只是预测一个类!已经回答了类似的问题,但我没有找到任何对我有用的东西:(

这是我正在使用的代码:

from keras.datasets import mnist
from keras import models
from keras import layers
from keras.utils import to_categorical
import numpy as np
import os

train_images = []
train_labels = []

#The following is just admin tasks - extracting the grayscale pixel values
#from the text files, adding them to the input array. Same with the labels, which
#are extracted from text files and added to output array. Not important to performance. 

for fileName in os.listdir('pixels1/'):
    newRead = open(os.path.join('pixels1/', fileName))

    currentList = []
    for pixel in newRead:
        rePixel = int(pixel.replace('\n', ''))/255
        currentList.append(rePixel)
    train_images.append(currentList)

        
for fileName in os.listdir('labels1/'):
    newRead = open(os.path.join('labels1/', fileName))
    line = newRead.readline()
    train_labels.append(int(line))

train_images = np.array(train_images)
train_labels = np.array(train_labels)

train_images = train_images.reshape((4,13689))

#model

model = models.Sequential()

model.add(layers.Dense(13689, input_dim=13689, activation='relu'))
model.add(layers.Dense(13689, activation='relu'))
model.add(layers.Dense(1, activation='softmax'))

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

model.fit(train_images, train_labels, epochs=5, batch_size=1)

我希望它至少能够在最后识别图像。我真的很想继续对我的全部 11,000 个示例进行培训,但目前我无法使用 4 个示例。

【问题讨论】:

    标签: python machine-learning keras neural-network


    【解决方案1】:

    粗略点:

    1. 您似乎认为密集层中的单元数应该等于您的数据维度 (13869);这不是的情况。将它们都更改为更小的值(在 100-200 范围内)——它们甚至不必相等。对于数据样本(图像)相对较少的情况,不建议使用太大的模型。

    2. 由于您在最后一层具有单个节点的二进制分类设置,因此您应该为此(最后)层使用activation=sigmoid,并使用loss='binary_crossentropy' 编译您的模型。

    3. 在成像应用中,通常前几层是卷积层。

    【讨论】:

    • 有趣 - 感谢您的回答。我对模型进行了更改 1 和 2,但没有成功。然而,当我运行它 20 多个 epoch 时,它最终变为 1。我认为这是一场胜利,并再次将其扩展到 240 个图像,但它做同样的事情 - 保持稳定 79%。有什么想法吗?
    • @JoshuaPotts 好吧,与您最初报告的 50%(即随机猜测)相比,79% 的准确率听起来像是一个很大的进步 - 所以它是一回事。我不确定你的意思是“放大”你的模型,但无论如何 240 张图像听起来并不多。下一步是尝试合并卷积层;但这里的想法不是提供完整的教程(这超出了 SO 的范围),只是为了查明您的方法中的一些经典错误。
    • 好的,知道了。实际上它停留在 79%,如果只是猜测的话,它会得到它(240 张图像中有 137 张没有红绿灯)。但是,当我将 epoch 提高到 400 并更改批量大小时,它最终达到 1,所以我认为我已经成功了。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 2020-10-11
    • 2017-05-20
    • 2019-04-18
    • 1970-01-01
    • 2017-11-20
    • 2021-08-03
    相关资源
    最近更新 更多