我正在尝试对Keras中的现有模型进行微调,以对我自己的数据集进行分类。到目前为止,我已经尝试了以下代码(摘自Keras文档:https://keras.io/applications/),其中的初始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模型进行微调。
提前谢谢。
发布于 2017-05-16 04:23:41
很难弄清楚一个特定的question,你有没有尝试过不做任何修改就直接复制代码呢?
也就是说,代码中有很多问题:它是一个简单的从keras.io复制/粘贴的代码,而不是函数式,并且在工作之前需要进行一些调整(无论使用ResNet50还是InceptionV3,都是如此):
1):加载InceptionV3时需要定义input_shape,特别是用base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299,299,3))替换base_model = InceptionV3(weights='imagenet', include_top=False)
2):此外,您需要调整最后添加的层中的类的数量,例如,如果您只有2个类:predictions = Dense(2, activation='softmax')(x)
3):将模型从categorical_crossentropy编译为sparse_categorical_crossentropy时,更改损失函数
4):最重要的是,在调用model.fit_generator()并添加steps_per_epoch之前,需要先定义fit_generator。如果你的训练图像在./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实现这个:作为开始,你可以用ResNet50()替换InceptionV3() (当然只在from keras.applications.resnet50 import ResNet50之后),并将input_shape改为(224,224,3),将target_size改为(224,244)。
上面提到的代码更改应该适用于Python 3.5.3 / Keras 2.0 / Tensorflow后端。
发布于 2017-08-03 04:32:35
除了上面关于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评论
发布于 2019-06-04 07:12:52
我想我也遇到过同样的问题。这似乎是一个复杂的问题,在github(https://github.com/keras-team/keras/issues/9214)上有一个很好的线索。问题在于对网络中未冻结的块进行批量标准化。您有两种解决方案:
https://stackoverflow.com/questions/43867032
复制相似问题