繁体   English   中英

Keras 图像分类网络总是预测一个 class,并保持在 50% 的准确率

[英]Keras image classification network always predicting one class, and stays at 50% accuracy

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

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

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

这是我正在使用的代码:

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 个示例上运行训练 session,但此时我无法让它与 4 一起使用。

粗略点:

  1. 您似乎认为密集层中的单元数应该等于您的数据维度(13869); 事实并非如此。 将它们都更改为更小的值(在 100-200 范围内)——它们甚至不必相等。 model 对于您相对较少的数据样本(图像)不建议使用。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM