2019-8-14 resnet50过拟合处理
训练自己的训练集的时候,发现训练集的acc远大于测试集的acc,很可能是过拟合的原因。
有很多过拟合处理方法:
1、Dropout层
2、正则化处理
3、增大数据集(数据增强)
下面的方法我们可以有效的处理过拟合:
import keras.backend as K
from keras.layers import Flatten, Dense, BatchNormalization
from keras.models import Model
from keras import Input, regularizers
K.set_learning_phase(0)
Inp = Input((224, 224, 3))
base_model = ResNet50(weights='imagenet', include_top=False,
input_shape=(image_size, image_size, 3), )
x = base_model(Inp)
K.set_learning_phase(1)
x = Flatten(name='flatten')(x)
x = Dense(2048, activation='relu', kernel_regularizer=regularizers.l2(0.01), )(x)
x = BatchNormalization()(x)
x = Dense(1024, activation='relu', kernel_regularizer=regularizers.l2(0.01))(x)
x = BatchNormalization(name='bn_fc_01')(x)
predictions = Dense(num_classes, activation='softmax')(x)
model = Model(inputs=Inp, outputs=predictions)