【问题标题】:How to fine-tune ResNet50 in Keras?如何在 Keras 中微调 ResNet50?
【发布时间】:2017-10-07 14:24:31
【问题描述】:

我正在尝试微调 Keras 中的现有模型以对我自己的数据集进行分类。到目前为止,我已经尝试了以下代码(取自 Keras 文档:https://keras.io/applications/),其中 Inception V3 在一组新的类上进行了微调。

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(inputs=base_model.input, outputs=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(...)

谁能指导我对上述代码进行哪些更改,以便微调 Keras 中存在的 ResNet50 模型。

提前致谢。

【问题讨论】:

  • 请回复某人

标签: python-2.7 keras resnet


【解决方案1】:

我想我遇到了同样的问题。这似乎是一个复杂的问题,在 github(https://github.com/keras-team/keras/issues/9214) 上有一个不错的线程。问题在于网络未冻结块的批量标准化。您有两种解决方案:

  1. 只改变顶层(保持原样)
  2. 从上面的 github 线程添加一个补丁。

【讨论】:

    【解决方案2】:

    除了上述 ResNet50 答案中提到的要点之外(!如果您的图像的格式与原始 Keras 代码 (224,224) 中的格式相似 - 不是矩形),您可以替换:

    # add a global spatial average pooling layer
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    

    通过

    x = base_model.output
    x = Flatten(x)
    

    编辑:请阅读下面的@Yu-Yang 评论

    【讨论】:

    • 除非您 100% 确定您不会对矩形图像进行分类,否则您不应在此处使用 Flatten()。原始 Keras 代码使用Flatten(),因为假设输入图像被重新整形为(224, 224)。但是,是否重塑取决于数据集和您拥有的准确性。使用GlobalAveragePooling2D() 会更灵活(实际上相当于Flatten() 用于方形图像)。
    • 非常感谢您的评论,我不知道,并会尊重地纠正答案
    【解决方案3】:

    很难找出具体的question,除了复制代码而不做任何更改之外,您是否尝试过其他方法?

    也就是说,代码中存在大量问题:它是从 keras.io 进行的简单复制/粘贴,不能正常运行,需要在工作之前进行一些调整(无论使用 ResNet50 还是 InceptionV3):

    1):加载InceptionV3时需要定义input_shape,具体将base_model = InceptionV3(weights='imagenet', include_top=False)替换为base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299,299,3))

    2):此外,您需要调整最后添加的层中的类数,例如如果你只有 2 个类:predictions = Dense(2, activation='softmax')(x)

    3):将模型编译时的损失函数从 categorical_crossentropy 更改为 sparse_categorical_crossentropy

    4):最重要的是,您需要在调用model.fit_generator() 之前定义fit_generator 并添加steps_per_epoch。如果您的训练图像在 ./data/train 中,并且每个类别都在不同的子文件夹中,则可以这样做,例如像这样:

    from keras.preprocessing.image import ImageDataGenerator
    train_datagen = ImageDataGenerator()
    train_generator = train_datagen.flow_from_directory(
         "./data/train",
        target_size=(299, 299),
        batch_size=50,
        class_mode='binary')
    model.fit_generator(train_generator, steps_per_epoch=100)
    

    这当然只进行基本训练,例如,您需要定义保存调用以保持训练的权重。仅当您通过上述更改使 InceptionV3 的代码工作时,我建议继续为 ResNet50 实现此功能:首先,您可以将 InceptionV3() 替换为 ResNet50()(当然仅在 from keras.applications.resnet50 import ResNet50 之后),然后更改input_shape(224,224,3)target_size(224,244)

    上述代码更改应该适用于 Python 3.5.3 / Keras 2.0 / Tensorflow 后端。

    【讨论】:

      猜你喜欢
      • 2017-12-30
      • 1970-01-01
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 2019-05-26
      相关资源
      最近更新 更多