【发布时间】: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