带有keras的CNN,准确性没有提高 [英] CNN with keras, accuracy not improving
问题描述
我最近开始学习机器学习,正在学习CNN,我计划借助 github存储库.
I have started with Machine Learning recently, I am learning CNN, I planned to write an application for Car Damage severity detection, with the help of this Keras blog and this github repo.
这是汽车数据集的外观:
This is how car data-set looks like:
F:\WORKSPACE\ML\CAR_DAMAGE_DETECTOR\DATASET\DATA3A
├───training (979 Images for all 3 categories of training set)
│ ├───01-minor
│ ├───02-moderate
│ └───03-severe
└───validation (171 Images for all 3 categories of validation set)
├───01-minor
├───02-moderate
└───03-severe
以下代码仅给我32%的准确性.
Following code gives me only 32% of accuracy.
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
# dimensions of our images.
img_width, img_height = 150, 150
train_data_dir = 'dataset/data3a/training'
validation_data_dir = 'dataset/data3a/validation'
nb_train_samples = 979
nb_validation_samples = 171
epochs = 10
batch_size = 16
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
model.save_weights('first_try.h5')
我尝试过:
- 通过将时代增加到10、20、50.
- 通过增加数据集中的图像(将所有验证图像添加到训练集中).
- 通过更新
Conv2D
层中的过滤器大小 - 试图添加
Conv2D
层和MaxPooling
层 - 还尝试使用其他优化器,例如
adam
,Sgd
等 - 还尝试通过将过滤器跨距更新为
(1,1) and (5,5)
而不是(3,3)
- 还尝试通过将更改的图像尺寸从
(150, 150)
更新为(256, 256)
,(64, 64)
- By increasing the epochs to 10, 20,50.
- By increasing images in the dataset (all validation images added to training set).
- By updating the filter size in the
Conv2D
layer - Tried to add couple of
Conv2D
layer,MaxPooling
layers - Also tried with different optimizers such as
adam
,Sgd
, etc - Also Tried by updating the filter strides to
(1,1) and (5,5)
instead of(3,3)
- Also tried by updating the changing image dimensions to
(256, 256)
,(64, 64)
from(150, 150)
但是没有运气,每次我获得的准确率都达到或低于32%,但不超过32%. 知道我想念的是什么.
But no luck, every-time I'm getting accuracy up to 32% or less than that but not more. Any idea what I'm missing.
与 github回购一样,可以看到,对于同一数据集,它给出了72%的准确度(训练-979,验证-171).为什么它对我不起作用.
As in the github repo we can see, it gives 72% accuracy for the same dataset (Training -979, Validation -171). Why its not working for me.
我从机器上的github链接尝试了他的代码,但是在训练数据集时挂断了(我等了8个小时以上),所以改变了方法,但是到目前为止仍然没有运气.
I tried his code from the github link on my machine but it hanged up while training the dataset(I waited for more than 8 hours), so changed the approach, but still no luck so far.
这是 Pastebin ,其中包含我训练时期的输出.
Here's the Pastebin containing output of my training epochs.
推荐答案
此问题是由于输出类的数量(三个)与您选择的最终层激活(S型)和损失函数(二元交叉熵).
The issue is caused by a mis-match between the number of output classes (three) and your choice of final layer activation (sigmoid) and loss-function (binary cross entropy).
sigmoid函数将实际值压榨"为[0,1]之间的值,但它仅适用于二进制(两类)问题.对于多个类,您需要使用诸如softmax函数之类的东西. Softmax是Sigmoid的通用版本(当您有两个类时,两个应等效).
The sigmoid function 'squashes' real values into a value between [0, 1] but it is designed for binary (two class) problems only. For multiple classes you need to use something like the softmax function. Softmax is a generalised version of sigmoid (the two should be equivalent when you have two classes).
损耗值也需要更新为可以处理多个类别的值-在这种情况下,分类交叉熵将起作用.
The loss value also needs to be updated to one that can handle multiple classes - categorical cross entropy will work in this case.
就代码而言,如果您将模型定义和编译代码修改为下面的版本,则它应该可以工作.
In terms of code, if you modify the model definition and compilation code to the version below it should work.
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
最后,您需要在数据生成器中指定class_mode='categorical'
.这将确保输出目标的格式设置为3列分类矩阵,该列在列中的一个对应于正确的值,在其他位置为零. categorical_cross_entropy
损失函数需要这种响应格式.
Finally you need to specify class_mode='categorical'
in your data generators. That will ensure that the output targets are formatted as a categorical 3-column matrix that has a one in the column corresponding to the correct value and zeroes elsewhere. This response format is needed by the categorical_cross_entropy
loss function.
这篇关于带有keras的CNN,准确性没有提高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!