[英]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.