【问题标题】:Why do I have to do two train steps for fine-tuning InceptionV3 in Keras?为什么我必须做两个训练步骤才能在 Keras 中微调 InceptionV3?
【发布时间】:2017-03-17 16:53:01
【问题描述】:

我不明白为什么我必须调用fit()/fit_generator() 函数两次才能在 Keras(版本 2.0.0)中微调 InceptionV3(或任何其他预训练模型)。 documentation 建议如下:

在一组新的类上微调 InceptionV3

from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K

# create the base pre-trained model
base_model = InceptionV3(weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 200 classes
predictions = Dense(200, activation='softmax')(x)

# this is the model we will train
model = Model(input=base_model.input, output=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in base_model.layers:
    layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

# train the model on the new data for a few epochs
model.fit_generator(...)

# at this point, the top layers are well trained and we can start fine-tuning
# convolutional layers from inception V3. We will freeze the bottom N layers
# and train the remaining top layers.

# let's visualize layer names and layer indices to see how many layers
# we should freeze:
for i, layer in enumerate(base_model.layers):
   print(i, layer.name)

# we chose to train the top 2 inception blocks, i.e. we will freeze
# the first 172 layers and unfreeze the rest:
for layer in model.layers[:172]:
   layer.trainable = False
for layer in model.layers[172:]:
   layer.trainable = True

# we need to recompile the model for these modifications to take effect
# we use SGD with a low learning rate
from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')

# we train our model again (this time fine-tuning the top 2 inception blocks
# alongside the top Dense layers
model.fit_generator(...)

我们为什么不只调用一次fit()/fit_generator()? 一如既往,感谢您的帮助!

编辑:

以下 Nassim Ben 和 David de la Iglesia 给出的答案都非常好。我强烈推荐 David de la Iglesia 提供的链接:Transfer Learning

【问题讨论】:

    标签: python-2.7 neural-network keras pre-trained-model


    【解决方案1】:

    InceptionV3 是一个非常深和复杂的网络,它已经被训练来识别一些东西,但你正在将它用于另一个分类任务。这意味着当您使用它时,它并不能完全适应您的工作。

    所以他们在这里想要实现的目标是使用训练网络已经学习的一些特征,并对网络的顶部进行一些修改(最高级别的特征,最接近你的任务)。

    所以他们移除了最顶层并添加了更多新的和未经训练的层。他们想为他们的任务训练那个大模型,使用 172 个第一层进行的特征提取并学习最后一层以适应您的任务。

    在他们想要训练的部分中,有一个子部分具有已学习的参数,另一个具有新的、随机初始化的参数。问题是已经学习的层,你只想微调它们而不是从头开始重新学习......模型无法区分它应该只是微调的层和应该完全学习的层。如果你只对模型的 [172:] 层进行一次拟合,你将失去在 imagnet 的庞大数据集上学到的有趣特征。你不想那样,所以你要做的是:

    1. 通过将整个 inceptionV3 设置为不可训练来学习“足够好”的最后一层,这将产生良好的结果。
    2. 新训练的层会很好,如果您“解冻”一些顶层,它们不会受到太多干扰,只会根据您的需要进行微调。

    总而言之,当您想要训练“已经学习”层和新层的混合时,您需要更新新层,然后对所有内容进行训练以对其进行微调。

    【讨论】:

    • 感谢您的详细回答:) 我认为主要句子是:“模型无法区分应该只是微调的层和应该完全学习的层”。是否有任何经验法则可以“足够好”地训练最后一层?
    • 没有经验法则,他们的做法是好的。冻结预训练的层,只在那些楦上训练,这将使它们达到足够好的性能,然后一起微调以获得最佳性能。 :)
    • 是的!但是,为了在微调之前只训练最后一层,多少个 epoch 才足够好?它是我必须在试验中找到的另一个超参数吗?
    • 不,让它训练直到验证损失最小......只是常规训练......如果你想使用earlystopping
    • 哦,很简单 :D 谢谢!
    【解决方案2】:

    如果你在一个已经调整好的卷积网络之上附加 2 个随机初始化的层,并且你尝试在不“预热”新层的情况下微调一些卷积层,那么这个新层的高梯度会炸毁(有用的)东西由这些卷积层学习。

    这就是为什么你的第一个 fit 只训练这 2 个新层,使用预训练的卷积网络,比如某种“固定”特征提取器。

    之后,您的 2 个 Dense 层没有高梯度,您可以微调一些预训练的卷积层。这就是你在第二个fit 上所做的事情。

    【讨论】:

    • 感谢您的精彩回答。
    • 为了澄清:首先我只训练新层,其次我可以微调预训练的层。这意味着,在有限的范围内,我可以微调整个模型。但这是不明智的,因为我不想失去已经学过的功能。是否有任何指导方针来选择我应该微调的“正确”数量的层?
    • 这取决于您的新数据集。检查这个: .cs231n.github.io/transfer-learning 。阅读何时以及如何微调?部分
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2017-06-05
    相关资源
    最近更新 更多