![](/img/trans.png)
[英]How to use the last hidden layer weights from one pre-trained MLP as input to a new MLP (transfer learning) with Keras?
[英]How can I use pre-trained weights from an existing CNN model for transfer learning in Keras?
我正在通过Keras中的卷积神经网络(CNN)处理基于2D RGB像素的图像分类问题。 我的完整CNN模型可以在这里找到。
我执行以下操作来训练/拟合CNN模型:
model = my_CNN_unet()
model_checkpoint = ModelCheckpoint('testweights_{epoch:02d}.hdf5')
model.fit(x_trn, y_trn, batch_size=50, epochs=3, verbose=1, shuffle=True,
callbacks=[model_checkpoint], validation_data=(x_val, y_val))
如何更改代码,以便使用来自VGG
和Inception
等著名CNN架构的预训练权重(即,转移学习)
正如人们在评论中提到的那样, keras.applications
为您提供了一种访问预训练模型的方法。 举个例子:
import keras
from keras.models import Model
model_base = keras.applications.vgg16.VGG16(include_top=False, input_shape=(*IMG_SIZE, 3), weights='imagenet')
output = model_base.output
# Add any other layers you want to `output` here...
output = Dense(len(categories), activation='softmax')(output)
model = Model(model_base.input, output)
for layer in model_base.layers:
layer.trainable = False
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
您可以按照训练以前的CNN的相同方式训练模型。 Keras应用程序提供对许多模型的访问,例如Inception,VGG16,VGG19,ResNet等-您可以通过类似的方式访问它们。 我写了一篇博客文章,其中介绍了如何在Keras中使用转移学习来构建图像分类器: http : //innolitics.com/10x/pretrained-models-with-keras/ 。 您也可以查看一个工作代码示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.