[英]How can I use pre-trained weights from an existing CNN model for transfer learning in Keras?
I am working on a 2D RGB pixel-based image classification problem via convolution neural networks (CNN) in Keras. 我正在通过Keras中的卷积神经网络(CNN)处理基于2D RGB像素的图像分类问题。 My full CNN model can be found here .
我的完整CNN模型可以在这里找到。
I do the following to train/fit the CNN model: 我执行以下操作来训练/拟合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))
How can I change my code, so that I use pre-trained weights (ie, transfer learning) from well-known CNN architectures such as VGG
and Inception
如何更改代码,以便使用来自
VGG
和Inception
等著名CNN架构的预训练权重(即,转移学习)
As people have mentioned in the comments, keras.applications
provides a way for you to access pretrained models. 正如人们在评论中提到的那样,
keras.applications
为您提供了一种访问预训练模型的方法。 As an example: 举个例子:
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
You can train this model in the same way you trained your previous CNN. 您可以按照训练以前的CNN的相同方式训练模型。 Keras applications provides access to many models such as Inception, VGG16, VGG19, ResNet, and more-- you can access them all in a similar way.
Keras应用程序提供对许多模型的访问,例如Inception,VGG16,VGG19,ResNet等-您可以通过类似的方式访问它们。 I wrote a blog post walking through how to use transfer learning in Keras to build an image classifier here: http://innolitics.com/10x/pretrained-models-with-keras/ .
我写了一篇博客文章,其中介绍了如何在Keras中使用转移学习来构建图像分类器: http : //innolitics.com/10x/pretrained-models-with-keras/ 。 It's got a working code example that you can look at as well.
您也可以查看一个工作代码示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.