访问keras中微调网络中间层的输出

时间:2018-05-08 19:32:21

标签: python neural-network keras

我在Keras中使用此图层调整了vgg16:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
vgg16 (Model)                (None, 1, 1, 512)         14714688  
_________________________________________________________________
flatten_1 (Flatten)          (None, 512)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 1024)              525312    
_________________________________________________________________
dense_2 (Dense)              (None, 512)               524800    
_________________________________________________________________
dropout_1 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 10)                5130      
=================================================================
Total params: 15,769,930
Trainable params: 8,134,666
Non-trainable params: 7,635,264

但我可以通过flatten_1 , dense_1 ... , dense_3

model.layers[1].output , model.layers[1].output , ... , model.layers[5].output中提取输入图像的功能

那么如何在vgg16的中间层中提取特征?

1 个答案:

答案 0 :(得分:0)

这是获取给定输入x_test的中间层输出的常见模式:

import keras.backend as K

get_layer = K.function(
    [model.layers[0].input, K.learning_phase()],
    [model.layers[LAYER_DESIRED].output])
layer_output = get_layer([x_test, 0])[0]

其中LAYER_DESIRED是您要输出的图层的索引。