2

我试图微调 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 模型。

提前致谢。

4

3 回答 3

4

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

也就是说,代码中存在大量问题:它是来自 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):最重要的是,您需要fit_generator在调用model.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_shapeto(224,224,3)target_sizeto更改为(224,244)

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

于 2017-05-15T20:23:41.217 回答
0

除了上述 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 评论

于 2017-08-02T20:32:35.403 回答
0

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

  1. 只改变顶层(保持原样)
  2. 从上面的 github 线程中添加一个补丁。
于 2019-06-03T23:12:52.077 回答