如何使用来自 keras.applications 的模型进行迁移学习? [英] How to use models from keras.applications for transfer learnig?

查看:16
本文介绍了如何使用来自 keras.applications 的模型进行迁移学习?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆