[英]Fine tuning resnet unfrozen layers in keras
i am working with resnet to train my data.我正在与 resnet 合作训练我的数据。 I have frozen most of the layers and only working training with the last 4 layers.我已经冻结了大部分层,只对最后 4 层进行了训练。 I want to change these last four layer dimension so that it matches my input dimension and channels.我想更改最后四层的维度,使其与我的输入维度和通道相匹配。 As i am new to this i dont know how to do it.因为我是新手,所以我不知道该怎么做。 I tried googling it but cannot find the solution我尝试谷歌搜索但找不到解决方案
base_model = tf.keras.applications.ResNet50(
include_top=False,
weights="imagenet",
input_tensor=3,
input_shape=(150,150),
pooling=None,
)
for layer in base_model.layers[:46]:
layer.trainable = False
If you want to change last layers architecture, you should get output of the desired intermediate layer and connect it to yours.如果您想更改最后一层架构,您应该获取所需中间层的 output 并将其连接到您的中间层。
I assume that you want to change the architecture after the 46th layer.我假设您想在第 46 层之后更改架构。
First define pre-trained model:首先定义预训练的model:
base_model = tf.keras.applications.ResNet50(
include_top=False,
weights="imagenet",
input_shape=(150,150,3),
)
for layer in base_model.layers[:46]:
layer.trainable = False
Then, get the name of intermediate layer you want (in this case 46th layer):然后,获取您想要的中间层的名称(在本例中为第 46 层):
print(base_model.layers[46].name)
For me, The output is conv3_block1_3_conv
对我来说,output 是conv3_block1_3_conv
Then get the output of this layer and connect to your own layers:然后得到这一层的output,连接到自己的层:
last_layer = base_model.get_layer('conv3_block1_3_conv') #get the layer
last_output = last_layer.output #get the layer output
x = tf.keras.layers.Flatten()(last_output) #flatten the output
x = tf.keras.layers.Dense(1024, activation='relu')(x) #add your own layers
x = tf.keras.layers.Dense(1, activation='sigmoid')(x) #add your own output
model = tf.keras.Model(base_model.input, x) #create the new model
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.