为什么我必须执行两个训练步骤来微调 Keras 中的 InceptionV3?

Why do I have to do two train steps for fine-tuning InceptionV3 in Keras?

我不明白为什么我必须调用 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 给出的 link:Transfer Learning

如果您在已经调优的卷积网络之上附加 2 个具有随机初始化的层,并且您尝试在没有 "warming up" 新层的情况下微调一些卷积层,这个新层的高梯度会炸毁 (有用的)那些卷积层学到的东西。

这就是为什么您的第一个 fit 只训练这 2 个新层,使用像某种 "fixed" 特征提取器一样的预训练卷积网络。

在那之后,你的 2 个密集层没有高梯度,你可以微调一些预训练的卷积层。这就是你在第二个 fit.

上所做的

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

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

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

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

  1. 通过将整个 inceptionV3 设置为不可训练来学习 "good enough" 最后一层,这将产生良好的结果。
  2. 新训练的层会很好,如果你"unfreeze"一些顶层,它们不会受到太多干扰,它们只会被微调,就像你想要的那样。

总而言之,当你想训练 "already learnt" 层与新层的混合时,你可以更新新层,然后对所有内容进行训练以微调它们。