我一直在Keras网络上对图像是否包含交通信号灯进行分类,但到目前为止,我的成功为0。我有一个超过11000张图像的数据集,在我的第一次测试中,我使用了240张图像(或者说,每张图像的文本文件都带有灰度像素值)。只有一个输出-0或1表示图像是否包含交通灯。
但是,当我进行测试时,它只能预测一堂课。考虑到53/240张图像带有交通信号灯,它的准确率一直达到79%,因为它一直都在预测0。我读到这可能是由于数据不均衡所致,因此我将图像缩小为仅4张图像-2张带有交通信号灯,2张没有图像。
即使进行了此测试,在经过5个周期后仍保持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一起使用。
粗略点:
您似乎认为密集层中的单位数应等于数据维度(13869);事实并非如此。将它们都更改为较小的值(在100-200范围内)-它们甚至不必相等。不建议您使用相对较小的数据样本(图像)那么大的模型。
由于您处于二进制分类设置中,因此在最后一层中只有一个节点,因此应activation=sigmoid
在该(最后)层中使用,并使用编译模型loss='binary_crossentropy'
。
在成像应用中,通常第一对层是卷积层。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句