【问题标题】:Finetuning VGG model with VGGFace weights使用 VGGFace 权重微调 VGG 模型
【发布时间】:2019-06-16 15:56:25
【问题描述】:

我正在使用经过预训练的“VGGFace”权重的微调 VGG16 模型来处理野外标记的面孔(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。来自分类层的随机权重可以带来大的梯度更新,这基本上会破坏卷积基中的预训练权重。
  • 谢谢。该模型现在工作得更好了。
  • 我会发布作为答案。

标签: keras vgg-net


【解决方案1】:

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

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

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

【讨论】:

  • 小疑问:我们冻结了卷积基权的权重,那么这是如何破坏卷积基预训练权重的呢?
  • 你是如何冷冻它们的?我在你的代码中没有找到
  • vgg_x = VGGFace(model = 'vgg16', weights = 'vggface', input_shape = (224,224,3), include_top = False) for layer in vgg_x.layers[:-4]: layer. trainable = False last_layer = vgg_x.get_layer('pool5').output
  • 我后来解冻了,希望它能提供更好的结果。当我使用 Adam 的默认学习率时,我得到 0.027% 的准确率
  • @AmruthLakkavaram 不要使用 Adam,先使用学习率非常小的 SGD。 Adam 在 GAN 上工作得很好,除此之外,我自己运气很差。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-24
  • 2015-12-19
  • 1970-01-01
  • 2018-08-03
  • 2017-04-30
  • 1970-01-01
相关资源
最近更新 更多