简体   繁体   English

如何使用现有CNN模型中的预训练权重在Keras中进行迁移学习?

[英]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 如何更改代码,以便使用来自VGGInception等著名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.

相关问题 如何使用来自一个预先训练的MLP的最后一个隐藏层权重作为Keras的新MLP(转移学习)的输入? - How to use the last hidden layer weights from one pre-trained MLP as input to a new MLP (transfer learning) with Keras? 如何使用预训练的 model 进行双输入迁移学习 - how to use pre-trained model for dual input transfer learning 如何在 Keras 中更改预训练 CNN model 中层的 output? - How to change output of a layer in a pre-trained CNN model in Keras? 如何使用预先训练的权重为 keras 中的特定通道设置权重? - How to set weights for specific channels in keras with pre-trained weights? 在Keras的Alexnet模型中使用预先训练的权重 - Using pre-trained weights in Alexnet model in Keras 如何从 keras 中预训练的 model 预测图像 - How to predict image from a pre-trained model in keras 在迁移学习预训练模型上训练新数据集 - Train new dataset on transfer learning pre-trained model 使用预先训练的ImageNet模型进行PyTorch传输学习 - PyTorch transfer learning with pre-trained ImageNet model Tensorflow:如何从预先训练的CNN的特定层提取图像特征? - Tensorflow: How can I extract image features from a specific layer of a pre-trained CNN? 从预训练的 model 中移除顶层,迁移学习,tensorflow (load_model) - Remove top layer from pre-trained model, transfer learning, tensorflow (load_model)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM