1

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

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

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

我应该如何删除最后一层进行迁移学习?

PS Keras 版本 = 2.2.4

4

2 回答 2

3

只是不要一开始就将最后一层添加到您的模型中。这样你甚至不需要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'))
于 2019-03-25T10:40:42.997 回答
2

作为 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。

当然,预测前的 dropout 会给你更好的结果。

于 2019-03-25T11:06:18.437 回答