简体   繁体   English

如何防止此模型中的过度拟合?

[英]How can I prevent Overfitting in this model?

I made this model for an image classification problem.我为图像分类问题制作了这个模型。 The problem I'm encountering is that the validation accuracy is always from 5-8% lower than the training accuracy and the validation loss is way higher than the training loss.我遇到的问题是验证准确度总是比训练准确度低 5-8%,并且验证损失远高于训练损失。 Here's an example of one of my epochs: loss: 0.2232 - acc: 0.9245 - val_loss: 0.4131 - val_acc: 0.8700这是我的一个时代的示例:损失:0.2232 - acc:0.9245 - val_loss:0.4131 - val_acc:0.8700

model = Sequential()

model.add(Conv2D(32, 3, 3, border_mode='same', input_shape=(150, 
150, 3), activation='relu'))
model.add(Conv2D(32, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(128, 3, 3, border_mode='same', 
activation='relu'))
model.add(Conv2D(128, 3, 3, border_mode='same', 
activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(256, 3, 3, border_mode='same', 
activation='relu'))
model.add(Conv2D(256, 3, 3, border_mode='same', 
activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer=RMSprop(lr=0.0001),
              metrics=['accuracy'])

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.1,
    zoom_range=0.1,
    horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)

I've tried Bayesian Hyperparameter Optimization using Hyperas, but the model hyperparameters that it's recommending aren't really working for me.我已经尝试使用 Hyperas 进行贝叶斯超参数优化,但它推荐的模型超参数对我来说并不真正有效。 What should I change in my model to prevent it from Overfitting?我应该改变我的模型以防止它过度拟合? I'm not using much data to train and validate the model because I won't have much data for what the model will be used in real-life.我没有使用太多数据来训练和验证模型,因为我没有太多关于模型将在现实生活中使用的数据。 Any recommendation will be greatly appreciated.任何建议将不胜感激。

Overfitting is one thing and training vs validation error is another.过度拟合是一回事,训练与验证错误是另一回事。

The fact that your training scores are better than your validation doesn' t mean that you are overfitting.您的训练分数优于验证的事实并不意味着您过度拟合。 You are overfitting when you validation scores reaches their best and then start to be getting worse with training.当您的验证分数达到最佳状态,然后随着训练开始变得更糟时,您就过度拟合了。

If what you are looking for is better validation score - better model generalization, what you can do is:如果您正在寻找更好的验证分数 - 更好的模型泛化,您可以做的是:

  • increase dropout (your dropout looks good enough but try increasing it and see what will happen,增加 dropout(你的 dropout 看起来不错,但尝试增加它,看看会发生什么,

  • use more data for training (not possible as you are saying above)使用更多数据进行训练(不可能如你上面所说)

  • try heavier augmentation尝试更重的增强

  • try pre-trained networks尝试预先训练的网络

  • try ensembling尝试集成

  • try tta (test time augmentation)尝试 tta(测试时间增加)

  • try any other training strategy as cosine annealing, mixup generator, or other generators (not keras) as albumentations尝试任何其他训练策略,如余弦退火、混合生成器或其他生成器(不是 keras)作为白化

Have you turned off DropOut layer during testing phase?您是否在测试阶段关闭了 DropOut 层?

Since DropOut layers are only used during training phase to prevent overfitting, they're not used in testing phase.由于 DropOut 层仅在训练阶段使用以防止过度拟合,因此它们不会在测试阶段使用。 That's why Tf.Estimator is famous nowadays, since you can turn off DropOut easier with is_training=True/False这就是 Tf.Estimator 现在出名的原因,因为您可以使用 is_training=True/False 更轻松地关闭 DropOut

You can turn off with tf.keras.backend.set_learning_phase(0).您可以使用 tf.keras.backend.set_learning_phase(0) 关闭。 Please make sure you are using tensorflow.keras from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten, there is difference between tf.keras and keras, and tf.keras is better.请确保您使用的是tensorflow.keras from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten,tf.keras 和 keras 有区别,tf.keras 更好。

If you have turned off, below are my techniques to prevent overfitting:如果您已关闭,以下是我防止过度拟合的技巧:
- Make error analysis. - 进行错误分析。 You can refer to Prof.Andrew best material https://www.coursera.org/learn/machine-learning-projects?specialization=deep-learning可以参考 Prof.Andrew 最佳材料https://www.coursera.org/learn/machine-learning-projects?specialization=deep-learning
- Check test and train set distribution, data augmentation (flip, rotate, ...) - 检查测试和训练集分布,数据增强(翻转,旋转,...)
- Increase InputShape for more features. - 增加 InputShape 以获得更多功能。 One of the best current techniques is using compounding scaling method from https://arxiv.org/pdf/1905.11946.pdf当前最好的技术之一是使用来自https://arxiv.org/pdf/1905.11946.pdf 的复合缩放方法

Hope this helps!希望这可以帮助! Happy Coding!快乐编码!

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

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