简体   繁体   English

如何使用Tensorflow Keras API从预训练的模型复制特定的图层权重?

[英]How do I copy specific layer weights from pretrained models using Tensorflow Keras api?

I'm trying to train a conv net which takes a 4 channel input, and want to use a pretrained model like VGG16. 我正在尝试训练需要4通道输入的转换网络,并希望使用像VGG16这样的预训练模型。 It makes sense that I should not use initial conv blocks from VGG16 since they're trained for 3 channel inputs, and redefine the initial conv blocks. 有意义的是,我不应该使用VGG16中的初始转换块,因为它们经过了3通道输入的训练,并重新定义了初始转换块。

However, I want to use block3 onwards from VGG16. 但是,我想从VGG16开始使用block3。 How do I achieve this using Tensorflow Keras api? 如何使用Tensorflow Keras API实现此目标?

In short, how do I copy weights from specific layers from pretrained models. 简而言之,如何从预训练模型的特定图层复制权重。 I'm using tensorflow 2.0 alpha version. 我正在使用tensorflow 2.0 alpha版本。

A quick way to do this is to make a new model that combines your custom input and the last layers of VGG16. 一种快速的方法是创建一个新模型,该模型将您的自定义输入和VGG16的最后一层结合在一起。 Find the index of the first VGG16 layer you'd like to keep, and connect it to your newly created input. 找到您要保留的第一个VGG16层的索引,并将其连接到新创建的输入。 Then connect each following VGG16 layer manually to recreate the VGG16 segment. 然后,手动连接下面的每个VGG16层,以重新创建VGG16段。 You can freeze the VGG16 layers along the way. 您可以一路冻结VGG16层。

from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D

vgg16 = VGG16()

# Find the index of the first block3 layer
for index in range(len(vgg16.layers)):
    if 'block3' in vgg16.layers[index].name:
        break

# Add your own input
model_input = Input(shape=(224,224,4), name='new_input')
x = Conv2D(...)(model_input)
...

# Connect your last layer to the VGG16 model, starting at the "block3" layer
# Then, you need to connect every layer manually in a for-loop, freezing each layer along the way

for i in range(index, len(vgg16.layers)):
  # freeze the VGG16 layer
  vgg16.layers[i].trainable = False  

  # connect the layer
  x = vgg16.layers[i](x)

model_output = x
newModel = Model(model_input, model_output)

Also make sure that the output of your custom layers matches the shape that the block3 layers are expecting as input. 还要确保自定义图层的输出与block3图层期望作为输入的形状匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Tensorflow中加载预训练的LSTM模型权重 - how to load pretrained LSTM models weights in Tensorflow 如何在 Keras 中获取图层的权重? - How do I get the weights of a layer in Keras? 升级到 Tensorflow 2.5 现在使用预训练的 Keras 应用程序模型时会出现 Lambda 层错误 - Upgraded to Tensorflow 2.5 now get a Lambda Layer error when using pretrained Keras Applications Models 如何修剪 tensorflow 层中的最高权重? tfmot.sparsity.keras.prune_low_magnitude - How do I prune over the highest weights in tensorflow layer? tfmot.sparsity.keras.prune_low_magnitude 如何在Tensorflow中将预训练网络用作图层? - How do I use a pretrained network as a layer in Tensorflow? 使用`tensorflow.python.keras.estimator.model_to_estimator`将Keras模型转换为Estimator API时如何通知类权重? - How to inform class weights when using `tensorflow.python.keras.estimator.model_to_estimator` to convert Keras Models to Estimator API? Keras:在theano和tensorflow之间转换预训练的权重 - Keras: convert pretrained weights between theano and tensorflow 在TF2中,不使用tf.keras API时如何保存模型/权重? - in TF2, how do you save models/weights when not using the tf.keras API? tensorflow keras 负载型号重量 - tensorflow keras load models weights 如何在 Tensorflow 中手动设置 LSTM 层的权重 - How do I manually set the weights of an LSTM layer in Tensorflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM