0

我正在尝试训练一个模型来识别面部表情,所以基本上是一个有 7 个类的分类问题:

img_size=48
batch_size=64
datagen_train=ImageDataGenerator(       rotation_range=15,
                                        width_shift_range=0.15,
                                        height_shift_range=0.15,
                                        shear_range=0.15,
                                        zoom_range=0.15,
                                        horizontal_flip=True,
                                        preprocessing_function=preprocess_input
                                    )
train_generator=datagen_train.flow_from_directory(
   train_path,
    target_size=(img_size,img_size),
    # color_mode='grayscale',
    batch_size=batch_size,
    class_mode='categorical',   
    shuffle=True
)

datagen_validation=ImageDataGenerator( horizontal_flip=True,  preprocessing_function=preprocess_input)
validation_generator=datagen_train.flow_from_directory(
   valid_path,
    target_size=(img_size,img_size),
    # color_mode='grayscale',
    batch_size=batch_size,
    class_mode='categorical',   
    shuffle=True,
)

我正在使用 ImageDataGenerator 并且我用 VGG16 做了我的模型,没有像这样的头部迁移学习:

ptm = PretrainedModel(
    input_shape=[48,48,3],
    weights='imagenet',
    include_top=False)

x = Flatten()(ptm.output)

x = Dropout(0.5)(x)
x = Dense(512, activation='relu')(x)
x = BatchNormalization()(x)

x = Dropout(0.5)(x)
x = Dense(512, activation='relu')(x)
x = BatchNormalization()(x)


x = Dense(7, activation='softmax',kernel_initializer='random_uniform', bias_initializer='random_uniform', bias_regularizer=regularizers.l2(0.01), name='predictions')(x)

opt=optimizers.RMSprop(learning_rate=0.0001)
model = Model(inputs=ptm.input, outputs=x)
model.compile(
  loss='categorical_crossentropy',
  optimizer=opt,
  metrics=['accuracy']
)
model.summary()

我使用了优化器和提前停止并运行了 100 个 epoch:

early_stopping = EarlyStopping(
    monitor='val_accuracy',
    min_delta=0.00005,
    patience=11,
    verbose=1,
    restore_best_weights=True,
)

lr_scheduler = ReduceLROnPlateau(
    monitor='val_accuracy',
    factor=0.5,
    patience=7,
    min_lr=1e-7,
    verbose=1,
)

callbacks = [
    early_stopping,
    lr_scheduler,
]

在 61 个 epoch 之后,我提前停止了,我得到了不错的准确度,但 val_accuracy 与之相比非常低:

loss: 0.6081 - accuracy: 0.7910 - val_loss: 1.4658 - val_accuracy: 0.5608

关于如何解决这种过度拟合的任何建议?谢谢!

4

1 回答 1

1

在您的验证生成器中删除horizontal_flip=True并设置shuffle=False. 另外,你有代码

validation_generator=datagen_train.flow_from_directory( etc

你想把它改成

validation_generator=datagen_validation.flow_from_directory(etc
于 2021-03-21T00:17:01.037 回答