简体   繁体   English

带有 keras 的 CNN,准确率没有提高

[英]CNN with keras, accuracy not improving

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 .我最近开始学习机器学习,我正在学习 CNN,我计划在这个Keras 博客和这个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

Following code gives me only 32% of accuracy.下面的代码只给了我 32% 的准确率。

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')

I tried:我试过:

  • By increasing the epochs to 10, 20,50.通过将时代增加到 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通过更新Conv2D层中的过滤器大小
  • Tried to add couple of Conv2D layer, MaxPooling layers尝试添加几个Conv2D层、 MaxPooling
  • Also tried with different optimizers such as adam , Sgd , etc还尝试了不同的优化器,如adamSgd
  • Also Tried by updating the filter strides to (1,1) and (5,5) instead of (3,3)还尝试通过将过滤器步幅更新为(1,1) and (5,5)而不是(3,3)
  • Also tried by updating the changing image dimensions to (256, 256) , (64, 64) from (150, 150)还尝试通过将更改的图像尺寸从(150, 150)更新为(256, 256) , (64, 64) (150, 150)

But no luck, every-time I'm getting accuracy up to 32% or less than that but not more.但没有运气,每次我的准确率都达到 32% 或更低,但不会更高。 Any idea what I'm missing.知道我错过了什么。

As in the github repo we can see, it gives 72% accuracy for the same dataset (Training -979, Validation -171).正如我们在github 存储库中看到的那样,它为相同的数据集提供了 72% 的准确率(训练 -979,验证 -171)。 Why its not working for me.为什么它不适合我。

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.我在我的机器上从 github 链接尝试了他的代码,但在训练数据集时挂断了(我等了 8 个多小时),所以改变了方法,但到目前为止仍然没有运气。

Here's the Pastebin containing output of my training epochs.下面是引擎收录我的训练时期的含输出。

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)和损失函数(二元交叉熵)之间的不匹配引起的。

The sigmoid function 'squashes' real values into a value between [0, 1] but it is designed for binary (two class) problems only. sigmoid 函数将实际值“压缩”为 [0, 1] 之间的值,但它仅适用于二元(两类)问题。 For multiple classes you need to use something like the softmax function.对于多个类,您需要使用诸如 softmax 函数之类的东西。 Softmax is a generalised version of sigmoid (the two should be equivalent when you have two classes). Softmax 是 sigmoid 的广义版本(当你有两个类时,两者应该是等价的)。

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(3))
model.add(Activation('softmax'))

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

Finally you need to specify class_mode='categorical' in your data generators.最后,您需要在数据生成器中指定class_mode='categorical' 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.这将确保输出目标被格式化为一个分类的 3 列矩阵,该矩阵在对应于正确值的列中有一个 1,在其他地方有 0。 This response format is needed by the categorical_cross_entropy loss function. categorical_cross_entropy损失函数需要这种响应格式。

Minor correction:小修正:

model.add(Dense(1))

Should be:应该:

model.add(Dense(3))

It has to comply with number of classes in the output.它必须符合输出中的类数。

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

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