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

D.L*_*mer 5 neural-network python-2.7 keras pre-trained-model

我不明白为什么我必须调用fit()/ fit_generator()function两次才能在Keras(版本2.0.0)中微调InceptionV3(或任何其他预训练模型).该文件提出以下建议:

在一组新类上微调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(...)
Run Code Online (Sandbox Code Playgroud)

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

编辑:

Nassim Ben和David de la Iglesia给出的以下答案都非常好.我想强烈推荐David de la Iglesia给出的链接:转学习

Nas*_*Ben 4

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

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

因此,他们移除了最顶层,并添加了一些新的、未经训练的层。他们希望使用 172 个第一层的特征提取来训练这个大型模型来完成他们的任务,并学习最后一层以适应您的任务。

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

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

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