如何从预先训练的模型中删除最后一层。我已经尝试过model.layers.pop()但它无法正常工作

问题描述 投票:0回答:2

我试图删除最后一层,以便我可以使用转移学习。

vgg16_model = keras.applications.vgg16.VGG16()
model = Sequential()

for layer in vgg16_model.layers:
    model.add(layer)

model.layers.pop()


# Freeze the layers 
for layer in model.layers:
    layer.trainable = False


# Add 'softmax' instead of earlier 'prediction' layer.
model.add(Dense(2, activation='softmax'))


# Check the summary, and yes new layer has been added. 
model.summary()

但我得到的输出并不是我的预期。它仍然显示最后一层vgg16模型。

这是输出

    _________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928       

**THE HIDDEN LAYERS** 
_________________________________________________________________
fc1 (Dense)                  (None, 4096)              102764544 
_________________________________________________________________
fc2 (Dense)                  (None, 4096)              16781312  
_________________________________________________________________
predictions (Dense)          (None, 1000)              4097000   
_________________________________________________________________
dense_10 (Dense)             (None, 2)                 2002      
=================================================================
Total params: 138,359,546
Trainable params: 2,002
Non-trainable params: 138,357,544

注意 - 在输出中我没有显示整个模型,只显示了前几层和最后一层。

我应该如何删除最后一层做转移学习?

P.S硬版本= 2.2.4

python keras keras-layer tf.keras
2个回答
1
投票

首先,不要将最后一层添加到模型中。这样你甚至不需要pop

vgg16_model = keras.applications.vgg16.VGG16()
model = Sequential()

for layer in vgg16_model.layers[:-1]: # this is where I changed your code
    model.add(layer)    

# Freeze the layers 
for layer in model.layers:
    layer.trainable = False

# Add 'softmax' instead of earlier 'prediction' layer.
model.add(Dense(2, activation='softmax'))

0
投票

作为markuscosinus答案的替代方案,您可以在预测图层之前获取输出并将其传递给您自己的预测图层。你可以这样做:

for layer in vgg16_model.layers: 
    layer.trainable = False
last_layer = vgg16_model.get_layer('fc2').output
out = Flatten()(last_layer)
out = Dense(128, activation='relu', name='fc3')(out)
out = Dropout(0.5)(out)
out = Dense(n_classes, activation='softmax', name='prediction')(out)
vgg16_custom_model = Model(input=vgg16_model.input, output=out)

我建议你在softmax之前添加一个Flatten和另一个Dense图层,因为最后一个“fc2”有4096个节点,很难将它改为2。

当然,在预测之前退出会给你更好的结果。

© www.soinside.com 2019 - 2024. All rights reserved.