使用 VGGFace 权重微调 VGG 模型

Finetuning VGG model with VGGFace weights

我正在使用微调的 VGG16 模型,使用预训练的 'VGGFace' 权重来处理野外标记的人脸(LFW 数据集)。问题是,在训练一个时期(大约 0.0037%)之后,我的准确度非常低,即模型根本没有学习。

我认为这与我的架构有关。我的架构是这样的:

vgg_x = VGGFace(model = 'vgg16', weights = 'vggface', input_shape = (224,224,3), include_top = False)
last_layer = vgg_x.get_layer('pool5').output
x = Flatten(name='flatten')(last_layer)
x = Dense(4096, activation='relu', name='fc6')(x)

out = Dense(311, activation='softmax', name='fc8')(x)
custom_vgg_model = Model(vgg_x.input, out)

custom_vgg_model.compile(optimizer = keras.optimizers.Adam(), loss = 
keras.losses.categorical_crossentropy, metrics = ['accuracy'])

kfold = KFold(n_splits = 15,random_state = 42)
kf = kfold.get_n_splits(X_train)

for train_index,test_index in kfold.split(X_train):
    X_cross_train = X_train[train_index]
    X_cross_test = X_train[test_index]
    Y_cross_train = y_train[train_index]
    Y_cross_test = y_train[test_index]
    custom_vgg_model.fit(x = X_cross_train,y = Y_cross_train, batch_size = 32, epochs = 10,verbose = 2, validation_data = (X_cross_test,Y_cross_test))

我希望该模型即使不能获得很高的准确性,也至少可以学习。可能是什么问题呢 ?是我的架构有问题还是其他问题?

预处理步骤应该不会出错,但以防万一:

image_set_x = keras_vggface.utils.preprocess_input(image_set_x, version=1)

尝试使用小于默认学习率(例如 1e-4)的学习率进行训练。来自分类层的随机权重可以带来较大的梯度更新。这些将导致较低层的大量权重更新,并基本上破坏卷积基中的预训练权重。

此外,您可以使用 ReduceLROnPlateau 回调在验证准确率停止增加时进一步降低学习率。

另一种避免大的破坏性梯度更新的策略是先冻结卷积基中的权重,预训练分类层,然后用小的学习率微调整个堆栈。这种方法在 Keras 关于迁移学习的博文中有详细解释:https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html