如何使用keras.applications中的模型进行转移学习? [英] How to use models from keras.applications for transfer learnig?
问题描述
我想在Keras中获得预训练的VGG16模型,删除其输出层,然后放置一个具有适合我的问题的类数的新输出层,然后将其适合新数据.因此,我试图在此处使用该模型: https://keras.io/applications/#vgg16 ,但是由于它不是顺序的,因此我不能仅按model.pop()
键.从图层弹出并添加它也是行不通的,因为在预测中它仍然希望使用旧形状.我该怎么做?有没有办法将这种类型的模型转换为Sequential
?
I want to get pretrained VGG16 model in Keras, remove its output layer, and then put a new output layer with the number of classes suited for my problem, and then to fit it on new data. For this reason, I am trying to use the model here: https://keras.io/applications/#vgg16, but since it is not Sequential, I cannot just model.pop()
. Popping from layers and adding it also does not work, because in the predictions it still expects the old shape. How would I do that? Is there a way to convert this type of model to Sequential
?
推荐答案
您可以在model.layers
上使用pop()
,然后使用model.layers[-1].output
创建新图层.
You can use pop()
on model.layers
and then use model.layers[-1].output
to create new layers.
示例:
from keras.models import Model
from keras.layers import Dense,Flatten
from keras.applications import vgg16
from keras import backend as K
model = vgg16.VGG16(weights='imagenet', include_top=True)
model.input
model.summary(line_length=150)
model.layers.pop()
model.layers.pop()
model.summary(line_length=150)
new_layer = Dense(10, activation='softmax', name='my_dense')
inp = model.input
out = new_layer(model.layers[-1].output)
model2 = Model(inp, out)
model2.summary(line_length=150)
或者,您可以使用这些模型的include_top=False
选项.在这种情况下,如果您需要使用平坦化层,则还需要通过input_shape
.
Alternatively, you can use include_top=False
option of these models. In this case if you need to use flatten the layer then you need to pass the input_shape
also.
model3 = vgg16.VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
model3.summary(line_length=150)
flatten = Flatten()
new_layer2 = Dense(10, activation='softmax', name='my_dense_2')
inp2 = model3.input
out2 = new_layer2(flatten(model3.output))
model4 = Model(inp2, out2)
model4.summary(line_length=150)
这篇关于如何使用keras.applications中的模型进行转移学习?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!